blob: a5a922e9f4574e9253a54fedab2ba17ec8c0317c [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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +010052class NullCheckSlowPathX86 : public SlowPathCodeX86 {
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
Calin Juravled0d48522014-11-04 16:40:20 +000078class DivZeroCheckSlowPathX86 : public SlowPathCodeX86 {
79 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
Calin Juravlebacfec32014-11-14 15:54:36 +0000104class DivRemMinusOneSlowPathX86 : public SlowPathCodeX86 {
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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100126class BoundsCheckSlowPathX86 : public SlowPathCodeX86 {
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
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100164class SuspendCheckSlowPathX86 : public SlowPathCodeX86 {
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
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000204class LoadStringSlowPathX86 : public SlowPathCodeX86 {
205 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
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000236class LoadClassSlowPathX86 : public SlowPathCodeX86 {
237 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
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000288class TypeCheckSlowPathX86 : public SlowPathCodeX86 {
289 public:
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100290 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 Geoffray64acf302015-09-14 22:20:29 +0100302
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 Geoffray64acf302015-09-14 22:20:29 +0100340 if (!is_fatal_) {
341 if (instruction_->IsInstanceOf()) {
342 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
343 }
344 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100346 __ 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 Geoffray64acf302015-09-14 22:20:29 +0100351 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 Geoffray64acf302015-09-14 22:20:29 +0100355 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356
357 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
358};
359
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700360class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
361 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 Geoffraye5038322014-07-04 09:41:32 +0100383#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100384#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100385
Roland Levillain4fa13f62015-07-06 18:11:54 +0100386inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700387 switch (cond) {
388 case kCondEQ: return kEqual;
389 case kCondNE: return kNotEqual;
390 case kCondLT: return kLess;
391 case kCondLE: return kLessEqual;
392 case kCondGT: return kGreater;
393 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700394 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100395 LOG(FATAL) << "Unreachable";
396 UNREACHABLE();
397}
398
399inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
400 switch (cond) {
401 case kCondEQ: return kEqual;
402 case kCondNE: return kNotEqual;
403 case kCondLT: return kBelow;
404 case kCondLE: return kBelowEqual;
405 case kCondGT: return kAbove;
406 case kCondGE: return kAboveEqual;
407 }
408 LOG(FATAL) << "Unreachable";
409 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700410}
411
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100412void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100413 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100414}
415
416void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100417 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100418}
419
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100420size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
421 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
422 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100423}
424
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100425size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
426 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
427 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100428}
429
Mark Mendell7c8d0092015-01-26 11:21:33 -0500430size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
431 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
432 return GetFloatingPointSpillSlotSize();
433}
434
435size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
436 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
437 return GetFloatingPointSpillSlotSize();
438}
439
Calin Juravle175dc732015-08-25 15:42:32 +0100440void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
441 HInstruction* instruction,
442 uint32_t dex_pc,
443 SlowPathCode* slow_path) {
444 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
445 instruction,
446 dex_pc,
447 slow_path);
448}
449
450void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100451 HInstruction* instruction,
452 uint32_t dex_pc,
453 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100454 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100455 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100456 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100457}
458
Mark Mendellfb8d2792015-03-31 22:16:59 -0400459CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
460 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100461 const CompilerOptions& compiler_options,
462 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500463 : CodeGenerator(graph,
464 kNumberOfCpuRegisters,
465 kNumberOfXmmRegisters,
466 kNumberOfRegisterPairs,
467 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
468 arraysize(kCoreCalleeSaves))
469 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100470 0,
471 compiler_options,
472 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100473 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100474 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100475 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400476 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000477 isa_features_(isa_features),
478 method_patches_(graph->GetArena()->Adapter()),
479 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000480 // Use a fake return address register to mimic Quick.
481 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100482}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100483
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100484Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100485 switch (type) {
486 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100487 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100488 X86ManagedRegister pair =
489 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100490 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
491 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100492 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
493 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100494 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100495 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100496 }
497
498 case Primitive::kPrimByte:
499 case Primitive::kPrimBoolean:
500 case Primitive::kPrimChar:
501 case Primitive::kPrimShort:
502 case Primitive::kPrimInt:
503 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100504 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100505 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100506 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100507 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
508 X86ManagedRegister current =
509 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
510 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100511 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100512 }
513 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100514 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100515 }
516
517 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100518 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100519 return Location::FpuRegisterLocation(
520 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100521 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100522
523 case Primitive::kPrimVoid:
524 LOG(FATAL) << "Unreachable type " << type;
525 }
526
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100527 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100528}
529
Mark Mendell5f874182015-03-04 15:42:45 -0500530void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100531 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100532 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100533
534 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100535 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100536
Mark Mendell5f874182015-03-04 15:42:45 -0500537 if (is_baseline) {
538 blocked_core_registers_[EBP] = true;
539 blocked_core_registers_[ESI] = true;
540 blocked_core_registers_[EDI] = true;
541 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100542
543 UpdateBlockedPairRegisters();
544}
545
546void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
547 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
548 X86ManagedRegister current =
549 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
550 if (blocked_core_registers_[current.AsRegisterPairLow()]
551 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
552 blocked_register_pairs_[i] = true;
553 }
554 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100555}
556
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100557InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
558 : HGraphVisitor(graph),
559 assembler_(codegen->GetAssembler()),
560 codegen_(codegen) {}
561
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100562static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100563 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100564}
565
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000566void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100567 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000568 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000569 bool skip_overflow_check =
570 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000571 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000572
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000573 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100574 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100575 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100576 }
577
Mark Mendell5f874182015-03-04 15:42:45 -0500578 if (HasEmptyFrame()) {
579 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000580 }
Mark Mendell5f874182015-03-04 15:42:45 -0500581
582 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
583 Register reg = kCoreCalleeSaves[i];
584 if (allocated_registers_.ContainsCoreRegister(reg)) {
585 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100586 __ cfi().AdjustCFAOffset(kX86WordSize);
587 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500588 }
589 }
590
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100591 int adjust = GetFrameSize() - FrameEntrySpillSize();
592 __ subl(ESP, Immediate(adjust));
593 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100594 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000595}
596
597void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100598 __ cfi().RememberState();
599 if (!HasEmptyFrame()) {
600 int adjust = GetFrameSize() - FrameEntrySpillSize();
601 __ addl(ESP, Immediate(adjust));
602 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500603
David Srbeckyc34dc932015-04-12 09:27:43 +0100604 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
605 Register reg = kCoreCalleeSaves[i];
606 if (allocated_registers_.ContainsCoreRegister(reg)) {
607 __ popl(reg);
608 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
609 __ cfi().Restore(DWARFReg(reg));
610 }
Mark Mendell5f874182015-03-04 15:42:45 -0500611 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000612 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100613 __ ret();
614 __ cfi().RestoreState();
615 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000616}
617
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100618void CodeGeneratorX86::Bind(HBasicBlock* block) {
619 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000620}
621
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100622Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
623 switch (load->GetType()) {
624 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100625 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100626 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100627
628 case Primitive::kPrimInt:
629 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100630 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100631 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100632
633 case Primitive::kPrimBoolean:
634 case Primitive::kPrimByte:
635 case Primitive::kPrimChar:
636 case Primitive::kPrimShort:
637 case Primitive::kPrimVoid:
638 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700639 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100640 }
641
642 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700643 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100644}
645
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100646Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
647 switch (type) {
648 case Primitive::kPrimBoolean:
649 case Primitive::kPrimByte:
650 case Primitive::kPrimChar:
651 case Primitive::kPrimShort:
652 case Primitive::kPrimInt:
653 case Primitive::kPrimNot:
654 return Location::RegisterLocation(EAX);
655
656 case Primitive::kPrimLong:
657 return Location::RegisterPairLocation(EAX, EDX);
658
659 case Primitive::kPrimVoid:
660 return Location::NoLocation();
661
662 case Primitive::kPrimDouble:
663 case Primitive::kPrimFloat:
664 return Location::FpuRegisterLocation(XMM0);
665 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100666
667 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100668}
669
670Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
671 return Location::RegisterLocation(kMethodRegisterArgument);
672}
673
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100674Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100675 switch (type) {
676 case Primitive::kPrimBoolean:
677 case Primitive::kPrimByte:
678 case Primitive::kPrimChar:
679 case Primitive::kPrimShort:
680 case Primitive::kPrimInt:
681 case Primitive::kPrimNot: {
682 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000683 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100684 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100685 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100686 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000687 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100688 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100689 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100690
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000691 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100692 uint32_t index = gp_index_;
693 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000694 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100695 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100696 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
697 calling_convention.GetRegisterPairAt(index));
698 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100699 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000700 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
701 }
702 }
703
704 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100705 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000706 stack_index_++;
707 if (index < calling_convention.GetNumberOfFpuRegisters()) {
708 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
709 } else {
710 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
711 }
712 }
713
714 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100715 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000716 stack_index_ += 2;
717 if (index < calling_convention.GetNumberOfFpuRegisters()) {
718 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
719 } else {
720 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100721 }
722 }
723
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100724 case Primitive::kPrimVoid:
725 LOG(FATAL) << "Unexpected parameter type " << type;
726 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100727 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100728 return Location();
729}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100730
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100731void CodeGeneratorX86::Move32(Location destination, Location source) {
732 if (source.Equals(destination)) {
733 return;
734 }
735 if (destination.IsRegister()) {
736 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000737 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100738 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000739 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100740 } else {
741 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000742 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100743 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100744 } else if (destination.IsFpuRegister()) {
745 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000746 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100747 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000748 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100749 } else {
750 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000751 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100752 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100753 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000754 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100755 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000756 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100757 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000758 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500759 } else if (source.IsConstant()) {
760 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000761 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500762 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100763 } else {
764 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100765 __ pushl(Address(ESP, source.GetStackIndex()));
766 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100767 }
768 }
769}
770
771void CodeGeneratorX86::Move64(Location destination, Location source) {
772 if (source.Equals(destination)) {
773 return;
774 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100775 if (destination.IsRegisterPair()) {
776 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000777 EmitParallelMoves(
778 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
779 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100780 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000781 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100782 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
783 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100784 } else if (source.IsFpuRegister()) {
785 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100786 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000787 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100788 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100789 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
790 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100791 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
792 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100793 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500794 if (source.IsFpuRegister()) {
795 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
796 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100798 } else {
799 LOG(FATAL) << "Unimplemented";
800 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000802 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100803 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000804 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100805 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100806 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100807 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100808 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000809 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000810 } else if (source.IsConstant()) {
811 HConstant* constant = source.GetConstant();
812 int64_t value;
813 if (constant->IsLongConstant()) {
814 value = constant->AsLongConstant()->GetValue();
815 } else {
816 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000817 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000818 }
819 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
820 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100821 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000822 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000823 EmitParallelMoves(
824 Location::StackSlot(source.GetStackIndex()),
825 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100826 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000827 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100828 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
829 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100830 }
831 }
832}
833
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100834void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000835 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100836 if (instruction->IsCurrentMethod()) {
837 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
838 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000839 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100840 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000841 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000842 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
843 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000844 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000845 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000846 } else if (location.IsStackSlot()) {
847 __ movl(Address(ESP, location.GetStackIndex()), imm);
848 } else {
849 DCHECK(location.IsConstant());
850 DCHECK_EQ(location.GetConstant(), const_to_move);
851 }
852 } else if (const_to_move->IsLongConstant()) {
853 int64_t value = const_to_move->AsLongConstant()->GetValue();
854 if (location.IsRegisterPair()) {
855 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
856 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
857 } else if (location.IsDoubleStackSlot()) {
858 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000859 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
860 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000861 } else {
862 DCHECK(location.IsConstant());
863 DCHECK_EQ(location.GetConstant(), instruction);
864 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100865 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000866 } else if (instruction->IsTemporary()) {
867 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000868 if (temp_location.IsStackSlot()) {
869 Move32(location, temp_location);
870 } else {
871 DCHECK(temp_location.IsDoubleStackSlot());
872 Move64(location, temp_location);
873 }
Roland Levillain476df552014-10-09 17:51:36 +0100874 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100875 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100876 switch (instruction->GetType()) {
877 case Primitive::kPrimBoolean:
878 case Primitive::kPrimByte:
879 case Primitive::kPrimChar:
880 case Primitive::kPrimShort:
881 case Primitive::kPrimInt:
882 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100883 case Primitive::kPrimFloat:
884 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100885 break;
886
887 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100888 case Primitive::kPrimDouble:
889 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100890 break;
891
892 default:
893 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
894 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000895 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100896 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100897 switch (instruction->GetType()) {
898 case Primitive::kPrimBoolean:
899 case Primitive::kPrimByte:
900 case Primitive::kPrimChar:
901 case Primitive::kPrimShort:
902 case Primitive::kPrimInt:
903 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100904 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000905 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100906 break;
907
908 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000910 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100911 break;
912
913 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100914 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100915 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000916 }
917}
918
Calin Juravle175dc732015-08-25 15:42:32 +0100919void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
920 DCHECK(location.IsRegister());
921 __ movl(location.AsRegister<Register>(), Immediate(value));
922}
923
Calin Juravle23a8e352015-09-08 19:56:31 +0100924void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
925 if (location.IsRegister()) {
926 locations->AddTemp(location);
927 } else if (location.IsRegisterPair()) {
928 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
929 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
930 } else {
931 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
932 }
933}
934
935void CodeGeneratorX86::MoveLocationToTemp(Location source,
936 const LocationSummary& locations,
937 int temp_index,
938 Primitive::Type type) {
939 if (!Primitive::IsFloatingPointType(type)) {
940 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
941 }
942
943 DCHECK(source.IsFpuRegister()) << source;
944 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
945 if (type == Primitive::kPrimFloat) {
946 __ movd(locations.GetTemp(temp_index).AsRegister<Register>(), src_reg);
947 } else {
948 DCHECK(type == Primitive::kPrimDouble);
949 __ movd(locations.GetTemp(temp_index).AsRegister<Register>(), src_reg);
950 __ psrlq(src_reg, Immediate(32));
951 __ movd(locations.GetTemp(temp_index + 1).AsRegister<Register>(), src_reg);
952 }
953}
954
955void CodeGeneratorX86::MoveTempToLocation(const LocationSummary& locations,
956 int temp_index,
957 Location destination,
958 Primitive::Type type) {
959 if (!Primitive::IsFloatingPointType(type)) {
960 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
961 }
962
963 DCHECK(destination.IsFpuRegister()) << destination;
964 XmmRegister dst_reg = destination.AsFpuRegister<XmmRegister>();
965 if (type == Primitive::kPrimFloat) {
966 __ movd(dst_reg, locations.GetTemp(temp_index).AsRegister<Register>());
967 } else {
968 DCHECK(type == Primitive::kPrimDouble);
969 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
970 // Create stack space for 2 elements.
971 __ subl(ESP, Immediate(2 * elem_size));
972 __ movl(Address(ESP, 0), locations.GetTemp(temp_index).AsRegister<Register>());
973 __ movl(Address(ESP, elem_size), locations.GetTemp(temp_index + 1).AsRegister<Register>());
974 __ movsd(dst_reg, Address(ESP, 0));
975 // And remove the temporary stack space we allocated.
976 __ addl(ESP, Immediate(2 * elem_size));
977 }
978}
979
David Brazdilfc6a86a2015-06-26 10:33:45 +0000980void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100981 DCHECK(!successor->IsExitBlock());
982
983 HBasicBlock* block = got->GetBlock();
984 HInstruction* previous = got->GetPrevious();
985
986 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000987 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100988 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
989 return;
990 }
991
992 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
993 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
994 }
995 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000996 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000997 }
998}
999
David Brazdilfc6a86a2015-06-26 10:33:45 +00001000void LocationsBuilderX86::VisitGoto(HGoto* got) {
1001 got->SetLocations(nullptr);
1002}
1003
1004void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1005 HandleGoto(got, got->GetSuccessor());
1006}
1007
1008void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1009 try_boundary->SetLocations(nullptr);
1010}
1011
1012void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1013 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1014 if (!successor->IsExitBlock()) {
1015 HandleGoto(try_boundary, successor);
1016 }
1017}
1018
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001019void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001020 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001021}
1022
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001023void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001024 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001025}
1026
Mark Mendellc4701932015-04-10 13:18:51 -04001027void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1028 Label* true_label,
1029 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001030 if (cond->IsFPConditionTrueIfNaN()) {
1031 __ j(kUnordered, true_label);
1032 } else if (cond->IsFPConditionFalseIfNaN()) {
1033 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001034 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001035 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001036}
1037
1038void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1039 Label* true_label,
1040 Label* false_label) {
1041 LocationSummary* locations = cond->GetLocations();
1042 Location left = locations->InAt(0);
1043 Location right = locations->InAt(1);
1044 IfCondition if_cond = cond->GetCondition();
1045
Mark Mendellc4701932015-04-10 13:18:51 -04001046 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001047 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001048 IfCondition true_high_cond = if_cond;
1049 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001050 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -04001051
1052 // Set the conditions for the test, remembering that == needs to be
1053 // decided using the low words.
1054 switch (if_cond) {
1055 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001056 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001057 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001058 break;
1059 case kCondLT:
1060 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001061 break;
1062 case kCondLE:
1063 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001064 break;
1065 case kCondGT:
1066 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001067 break;
1068 case kCondGE:
1069 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001070 break;
1071 }
1072
1073 if (right.IsConstant()) {
1074 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001075 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001076 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001077
1078 if (val_high == 0) {
1079 __ testl(left_high, left_high);
1080 } else {
1081 __ cmpl(left_high, Immediate(val_high));
1082 }
1083 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001084 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001085 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001086 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001087 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001088 __ j(X86SignedCondition(true_high_cond), true_label);
1089 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001090 }
1091 // Must be equal high, so compare the lows.
1092 if (val_low == 0) {
1093 __ testl(left_low, left_low);
1094 } else {
1095 __ cmpl(left_low, Immediate(val_low));
1096 }
1097 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001098 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001099 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001100
1101 __ cmpl(left_high, right_high);
1102 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001103 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001104 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001105 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001106 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001107 __ j(X86SignedCondition(true_high_cond), true_label);
1108 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001109 }
1110 // Must be equal high, so compare the lows.
1111 __ cmpl(left_low, right_low);
1112 }
1113 // The last comparison might be unsigned.
1114 __ j(final_condition, true_label);
1115}
1116
1117void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1118 HCondition* condition,
1119 Label* true_target,
1120 Label* false_target,
1121 Label* always_true_target) {
1122 LocationSummary* locations = condition->GetLocations();
1123 Location left = locations->InAt(0);
1124 Location right = locations->InAt(1);
1125
1126 // We don't want true_target as a nullptr.
1127 if (true_target == nullptr) {
1128 true_target = always_true_target;
1129 }
1130 bool falls_through = (false_target == nullptr);
1131
1132 // FP compares don't like null false_targets.
1133 if (false_target == nullptr) {
1134 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1135 }
1136
1137 Primitive::Type type = condition->InputAt(0)->GetType();
1138 switch (type) {
1139 case Primitive::kPrimLong:
1140 GenerateLongComparesAndJumps(condition, true_target, false_target);
1141 break;
1142 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001143 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1144 GenerateFPJumps(condition, true_target, false_target);
1145 break;
1146 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001147 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1148 GenerateFPJumps(condition, true_target, false_target);
1149 break;
1150 default:
1151 LOG(FATAL) << "Unexpected compare type " << type;
1152 }
1153
1154 if (!falls_through) {
1155 __ jmp(false_target);
1156 }
1157}
1158
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001159void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1160 Label* true_target,
1161 Label* false_target,
1162 Label* always_true_target) {
1163 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001164 if (cond->IsIntConstant()) {
1165 // Constant condition, statically compared against 1.
1166 int32_t cond_value = cond->AsIntConstant()->GetValue();
1167 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001168 if (always_true_target != nullptr) {
1169 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001170 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001171 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001172 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001173 DCHECK_EQ(cond_value, 0);
1174 }
1175 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001176 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001177 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1178 // Moves do not affect the eflags register, so if the condition is
1179 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001180 // again. We can't use the eflags on long/FP conditions if they are
1181 // materialized due to the complex branching.
1182 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001183 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001184 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001185 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1186 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001187 if (!eflags_set) {
1188 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001189 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001190 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001191 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001192 } else {
1193 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1194 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001195 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001196 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001197 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001198 }
1199 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001200 // Condition has not been materialized, use its inputs as the
1201 // comparison and its condition as the branch condition.
1202
Mark Mendellc4701932015-04-10 13:18:51 -04001203 // Is this a long or FP comparison that has been folded into the HCondition?
1204 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1205 // Generate the comparison directly.
1206 GenerateCompareTestAndBranch(instruction->AsIf(),
1207 cond->AsCondition(),
1208 true_target,
1209 false_target,
1210 always_true_target);
1211 return;
1212 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001213
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001214 Location lhs = cond->GetLocations()->InAt(0);
1215 Location rhs = cond->GetLocations()->InAt(1);
1216 // LHS is guaranteed to be in a register (see
1217 // LocationsBuilderX86::VisitCondition).
1218 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001219 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001220 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001221 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001222 if (constant == 0) {
1223 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1224 } else {
1225 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1226 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001227 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001228 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001229 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001230 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001231 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001232 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001233 if (false_target != nullptr) {
1234 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001235 }
1236}
1237
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001238void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1239 LocationSummary* locations =
1240 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1241 HInstruction* cond = if_instr->InputAt(0);
1242 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1243 locations->SetInAt(0, Location::Any());
1244 }
1245}
1246
1247void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1248 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1249 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1250 Label* always_true_target = true_target;
1251 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1252 if_instr->IfTrueSuccessor())) {
1253 always_true_target = nullptr;
1254 }
1255 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1256 if_instr->IfFalseSuccessor())) {
1257 false_target = nullptr;
1258 }
1259 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1260}
1261
1262void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1263 LocationSummary* locations = new (GetGraph()->GetArena())
1264 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1265 HInstruction* cond = deoptimize->InputAt(0);
1266 DCHECK(cond->IsCondition());
1267 if (cond->AsCondition()->NeedsMaterialization()) {
1268 locations->SetInAt(0, Location::Any());
1269 }
1270}
1271
1272void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1273 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1274 DeoptimizationSlowPathX86(deoptimize);
1275 codegen_->AddSlowPath(slow_path);
1276 Label* slow_path_entry = slow_path->GetEntryLabel();
1277 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1278}
1279
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001280void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001281 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001282}
1283
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001284void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1285 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001286}
1287
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001288void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001289 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001290}
1291
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001292void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001293 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001294 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001295}
1296
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001297void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001298 LocationSummary* locations =
1299 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001300 switch (store->InputAt(1)->GetType()) {
1301 case Primitive::kPrimBoolean:
1302 case Primitive::kPrimByte:
1303 case Primitive::kPrimChar:
1304 case Primitive::kPrimShort:
1305 case Primitive::kPrimInt:
1306 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001307 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001308 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1309 break;
1310
1311 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001312 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001313 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1314 break;
1315
1316 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001317 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001318 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001319}
1320
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001321void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001322 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001323}
1324
Roland Levillain0d37cd02015-05-27 16:39:19 +01001325void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001326 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001327 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001328 // Handle the long/FP comparisons made in instruction simplification.
1329 switch (cond->InputAt(0)->GetType()) {
1330 case Primitive::kPrimLong: {
1331 locations->SetInAt(0, Location::RequiresRegister());
1332 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1333 if (cond->NeedsMaterialization()) {
1334 locations->SetOut(Location::RequiresRegister());
1335 }
1336 break;
1337 }
1338 case Primitive::kPrimFloat:
1339 case Primitive::kPrimDouble: {
1340 locations->SetInAt(0, Location::RequiresFpuRegister());
1341 locations->SetInAt(1, Location::RequiresFpuRegister());
1342 if (cond->NeedsMaterialization()) {
1343 locations->SetOut(Location::RequiresRegister());
1344 }
1345 break;
1346 }
1347 default:
1348 locations->SetInAt(0, Location::RequiresRegister());
1349 locations->SetInAt(1, Location::Any());
1350 if (cond->NeedsMaterialization()) {
1351 // We need a byte register.
1352 locations->SetOut(Location::RegisterLocation(ECX));
1353 }
1354 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001355 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001356}
1357
Roland Levillain0d37cd02015-05-27 16:39:19 +01001358void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001359 if (!cond->NeedsMaterialization()) {
1360 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001361 }
Mark Mendellc4701932015-04-10 13:18:51 -04001362
1363 LocationSummary* locations = cond->GetLocations();
1364 Location lhs = locations->InAt(0);
1365 Location rhs = locations->InAt(1);
1366 Register reg = locations->Out().AsRegister<Register>();
1367 Label true_label, false_label;
1368
1369 switch (cond->InputAt(0)->GetType()) {
1370 default: {
1371 // Integer case.
1372
1373 // Clear output register: setcc only sets the low byte.
1374 __ xorl(reg, reg);
1375
1376 if (rhs.IsRegister()) {
1377 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1378 } else if (rhs.IsConstant()) {
1379 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1380 if (constant == 0) {
1381 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1382 } else {
1383 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1384 }
1385 } else {
1386 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1387 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001388 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001389 return;
1390 }
1391 case Primitive::kPrimLong:
1392 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1393 break;
1394 case Primitive::kPrimFloat:
1395 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1396 GenerateFPJumps(cond, &true_label, &false_label);
1397 break;
1398 case Primitive::kPrimDouble:
1399 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1400 GenerateFPJumps(cond, &true_label, &false_label);
1401 break;
1402 }
1403
1404 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001405 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001406
Roland Levillain4fa13f62015-07-06 18:11:54 +01001407 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001408 __ Bind(&false_label);
1409 __ xorl(reg, reg);
1410 __ jmp(&done_label);
1411
Roland Levillain4fa13f62015-07-06 18:11:54 +01001412 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001413 __ Bind(&true_label);
1414 __ movl(reg, Immediate(1));
1415 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001416}
1417
1418void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1419 VisitCondition(comp);
1420}
1421
1422void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1423 VisitCondition(comp);
1424}
1425
1426void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1427 VisitCondition(comp);
1428}
1429
1430void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1431 VisitCondition(comp);
1432}
1433
1434void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1435 VisitCondition(comp);
1436}
1437
1438void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1439 VisitCondition(comp);
1440}
1441
1442void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1443 VisitCondition(comp);
1444}
1445
1446void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1447 VisitCondition(comp);
1448}
1449
1450void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1451 VisitCondition(comp);
1452}
1453
1454void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1455 VisitCondition(comp);
1456}
1457
1458void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1459 VisitCondition(comp);
1460}
1461
1462void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1463 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001464}
1465
1466void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001467 LocationSummary* locations =
1468 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001469 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001470}
1471
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001472void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001473 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001474 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001475}
1476
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001477void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1478 LocationSummary* locations =
1479 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1480 locations->SetOut(Location::ConstantLocation(constant));
1481}
1482
1483void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1484 // Will be generated at use site.
1485 UNUSED(constant);
1486}
1487
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001488void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001489 LocationSummary* locations =
1490 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001491 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001492}
1493
1494void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1495 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001496 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001497}
1498
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001499void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1500 LocationSummary* locations =
1501 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1502 locations->SetOut(Location::ConstantLocation(constant));
1503}
1504
1505void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1506 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001507 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001508}
1509
1510void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1511 LocationSummary* locations =
1512 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1513 locations->SetOut(Location::ConstantLocation(constant));
1514}
1515
1516void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1517 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001518 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001519}
1520
Calin Juravle27df7582015-04-17 19:12:31 +01001521void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1522 memory_barrier->SetLocations(nullptr);
1523}
1524
1525void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1526 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1527}
1528
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001529void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001530 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001531}
1532
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001533void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001534 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001535 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001536}
1537
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001538void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001539 LocationSummary* locations =
1540 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001541 switch (ret->InputAt(0)->GetType()) {
1542 case Primitive::kPrimBoolean:
1543 case Primitive::kPrimByte:
1544 case Primitive::kPrimChar:
1545 case Primitive::kPrimShort:
1546 case Primitive::kPrimInt:
1547 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001548 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001549 break;
1550
1551 case Primitive::kPrimLong:
1552 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001553 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001554 break;
1555
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001556 case Primitive::kPrimFloat:
1557 case Primitive::kPrimDouble:
1558 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001559 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001560 break;
1561
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001563 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001564 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001565}
1566
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001567void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001568 if (kIsDebugBuild) {
1569 switch (ret->InputAt(0)->GetType()) {
1570 case Primitive::kPrimBoolean:
1571 case Primitive::kPrimByte:
1572 case Primitive::kPrimChar:
1573 case Primitive::kPrimShort:
1574 case Primitive::kPrimInt:
1575 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001576 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001577 break;
1578
1579 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001580 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1581 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582 break;
1583
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001584 case Primitive::kPrimFloat:
1585 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001586 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001587 break;
1588
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001589 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001590 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 }
1592 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001593 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001594}
1595
Calin Juravle175dc732015-08-25 15:42:32 +01001596void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1597 // The trampoline uses the same calling convention as dex calling conventions,
1598 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1599 // the method_idx.
1600 HandleInvoke(invoke);
1601}
1602
1603void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1604 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1605}
1606
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001607void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001608 // When we do not run baseline, explicit clinit checks triggered by static
1609 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1610 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001611
Mark Mendellfb8d2792015-03-31 22:16:59 -04001612 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001613 if (intrinsic.TryDispatch(invoke)) {
1614 return;
1615 }
1616
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001617 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001618
1619 if (codegen_->IsBaseline()) {
1620 // Baseline does not have enough registers if the current method also
1621 // needs a register. We therefore do not require a register for it, and let
1622 // the code generation of the invoke handle it.
1623 LocationSummary* locations = invoke->GetLocations();
1624 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1625 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1626 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1627 }
1628 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001629}
1630
Mark Mendell09ed1a32015-03-25 08:30:06 -04001631static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1632 if (invoke->GetLocations()->Intrinsified()) {
1633 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1634 intrinsic.Dispatch(invoke);
1635 return true;
1636 }
1637 return false;
1638}
1639
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001640void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001641 // When we do not run baseline, explicit clinit checks triggered by static
1642 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1643 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001644
Mark Mendell09ed1a32015-03-25 08:30:06 -04001645 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1646 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001647 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001648
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001649 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001650 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001651 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001652 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001653}
1654
1655void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1656 HandleInvoke(invoke);
1657}
1658
1659void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001660 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001661 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001662}
1663
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001664void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001665 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1666 return;
1667 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001668
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001669 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001670 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001671 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001672}
1673
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001674void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1675 HandleInvoke(invoke);
1676 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001677 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001678}
1679
1680void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1681 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001682 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001683 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1684 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001685 LocationSummary* locations = invoke->GetLocations();
1686 Location receiver = locations->InAt(0);
1687 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1688
1689 // Set the hidden argument.
1690 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001691 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001692
1693 // temp = object->GetClass();
1694 if (receiver.IsStackSlot()) {
1695 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1696 __ movl(temp, Address(temp, class_offset));
1697 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001698 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001699 }
Roland Levillain4d027112015-07-01 15:41:14 +01001700 codegen_->MaybeRecordImplicitNullCheck(invoke);
1701 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001702 // temp = temp->GetImtEntryAt(method_offset);
1703 __ movl(temp, Address(temp, method_offset));
1704 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001705 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001706 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001707
1708 DCHECK(!codegen_->IsLeafMethod());
1709 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1710}
1711
Roland Levillain88cb1752014-10-20 16:36:47 +01001712void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1713 LocationSummary* locations =
1714 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1715 switch (neg->GetResultType()) {
1716 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001717 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001718 locations->SetInAt(0, Location::RequiresRegister());
1719 locations->SetOut(Location::SameAsFirstInput());
1720 break;
1721
Roland Levillain88cb1752014-10-20 16:36:47 +01001722 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001723 locations->SetInAt(0, Location::RequiresFpuRegister());
1724 locations->SetOut(Location::SameAsFirstInput());
1725 locations->AddTemp(Location::RequiresRegister());
1726 locations->AddTemp(Location::RequiresFpuRegister());
1727 break;
1728
Roland Levillain88cb1752014-10-20 16:36:47 +01001729 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001730 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001731 locations->SetOut(Location::SameAsFirstInput());
1732 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001733 break;
1734
1735 default:
1736 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1737 }
1738}
1739
1740void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1741 LocationSummary* locations = neg->GetLocations();
1742 Location out = locations->Out();
1743 Location in = locations->InAt(0);
1744 switch (neg->GetResultType()) {
1745 case Primitive::kPrimInt:
1746 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001747 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001748 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001749 break;
1750
1751 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001752 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001753 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001754 __ negl(out.AsRegisterPairLow<Register>());
1755 // Negation is similar to subtraction from zero. The least
1756 // significant byte triggers a borrow when it is different from
1757 // zero; to take it into account, add 1 to the most significant
1758 // byte if the carry flag (CF) is set to 1 after the first NEGL
1759 // operation.
1760 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1761 __ negl(out.AsRegisterPairHigh<Register>());
1762 break;
1763
Roland Levillain5368c212014-11-27 15:03:41 +00001764 case Primitive::kPrimFloat: {
1765 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001766 Register constant = locations->GetTemp(0).AsRegister<Register>();
1767 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001768 // Implement float negation with an exclusive or with value
1769 // 0x80000000 (mask for bit 31, representing the sign of a
1770 // single-precision floating-point number).
1771 __ movl(constant, Immediate(INT32_C(0x80000000)));
1772 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001773 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001774 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001775 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001776
Roland Levillain5368c212014-11-27 15:03:41 +00001777 case Primitive::kPrimDouble: {
1778 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001779 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001780 // Implement double negation with an exclusive or with value
1781 // 0x8000000000000000 (mask for bit 63, representing the sign of
1782 // a double-precision floating-point number).
1783 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001784 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001785 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001786 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001787
1788 default:
1789 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1790 }
1791}
1792
Roland Levillaindff1f282014-11-05 14:15:05 +00001793void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001794 Primitive::Type result_type = conversion->GetResultType();
1795 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001796 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001797
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001798 // The float-to-long and double-to-long type conversions rely on a
1799 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001800 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001801 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1802 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001803 ? LocationSummary::kCall
1804 : LocationSummary::kNoCall;
1805 LocationSummary* locations =
1806 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1807
David Brazdilb2bd1c52015-03-25 11:17:37 +00001808 // The Java language does not allow treating boolean as an integral type but
1809 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001810
Roland Levillaindff1f282014-11-05 14:15:05 +00001811 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001812 case Primitive::kPrimByte:
1813 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001814 case Primitive::kPrimBoolean:
1815 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001816 case Primitive::kPrimShort:
1817 case Primitive::kPrimInt:
1818 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001819 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001820 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1821 // Make the output overlap to please the register allocator. This greatly simplifies
1822 // the validation of the linear scan implementation
1823 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001824 break;
1825
1826 default:
1827 LOG(FATAL) << "Unexpected type conversion from " << input_type
1828 << " to " << result_type;
1829 }
1830 break;
1831
Roland Levillain01a8d712014-11-14 16:27:39 +00001832 case Primitive::kPrimShort:
1833 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001834 case Primitive::kPrimBoolean:
1835 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001836 case Primitive::kPrimByte:
1837 case Primitive::kPrimInt:
1838 case Primitive::kPrimChar:
1839 // Processing a Dex `int-to-short' instruction.
1840 locations->SetInAt(0, Location::Any());
1841 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1842 break;
1843
1844 default:
1845 LOG(FATAL) << "Unexpected type conversion from " << input_type
1846 << " to " << result_type;
1847 }
1848 break;
1849
Roland Levillain946e1432014-11-11 17:35:19 +00001850 case Primitive::kPrimInt:
1851 switch (input_type) {
1852 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001853 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001854 locations->SetInAt(0, Location::Any());
1855 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1856 break;
1857
1858 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001859 // Processing a Dex `float-to-int' instruction.
1860 locations->SetInAt(0, Location::RequiresFpuRegister());
1861 locations->SetOut(Location::RequiresRegister());
1862 locations->AddTemp(Location::RequiresFpuRegister());
1863 break;
1864
Roland Levillain946e1432014-11-11 17:35:19 +00001865 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001866 // Processing a Dex `double-to-int' instruction.
1867 locations->SetInAt(0, Location::RequiresFpuRegister());
1868 locations->SetOut(Location::RequiresRegister());
1869 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001870 break;
1871
1872 default:
1873 LOG(FATAL) << "Unexpected type conversion from " << input_type
1874 << " to " << result_type;
1875 }
1876 break;
1877
Roland Levillaindff1f282014-11-05 14:15:05 +00001878 case Primitive::kPrimLong:
1879 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001880 case Primitive::kPrimBoolean:
1881 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001882 case Primitive::kPrimByte:
1883 case Primitive::kPrimShort:
1884 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001885 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001886 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001887 locations->SetInAt(0, Location::RegisterLocation(EAX));
1888 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1889 break;
1890
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001891 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001892 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001893 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001894 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001895 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1896 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1897
Vladimir Marko949c91f2015-01-27 10:48:44 +00001898 // The runtime helper puts the result in EAX, EDX.
1899 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001900 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001901 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001902
1903 default:
1904 LOG(FATAL) << "Unexpected type conversion from " << input_type
1905 << " to " << result_type;
1906 }
1907 break;
1908
Roland Levillain981e4542014-11-14 11:47:14 +00001909 case Primitive::kPrimChar:
1910 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001911 case Primitive::kPrimBoolean:
1912 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001913 case Primitive::kPrimByte:
1914 case Primitive::kPrimShort:
1915 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001916 // Processing a Dex `int-to-char' instruction.
1917 locations->SetInAt(0, Location::Any());
1918 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1919 break;
1920
1921 default:
1922 LOG(FATAL) << "Unexpected type conversion from " << input_type
1923 << " to " << result_type;
1924 }
1925 break;
1926
Roland Levillaindff1f282014-11-05 14:15:05 +00001927 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001928 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001929 case Primitive::kPrimBoolean:
1930 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001931 case Primitive::kPrimByte:
1932 case Primitive::kPrimShort:
1933 case Primitive::kPrimInt:
1934 case Primitive::kPrimChar:
1935 // Processing a Dex `int-to-float' instruction.
1936 locations->SetInAt(0, Location::RequiresRegister());
1937 locations->SetOut(Location::RequiresFpuRegister());
1938 break;
1939
1940 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001941 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001942 locations->SetInAt(0, Location::Any());
1943 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001944 break;
1945
Roland Levillaincff13742014-11-17 14:32:17 +00001946 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001947 // Processing a Dex `double-to-float' instruction.
1948 locations->SetInAt(0, Location::RequiresFpuRegister());
1949 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001950 break;
1951
1952 default:
1953 LOG(FATAL) << "Unexpected type conversion from " << input_type
1954 << " to " << result_type;
1955 };
1956 break;
1957
Roland Levillaindff1f282014-11-05 14:15:05 +00001958 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001959 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001960 case Primitive::kPrimBoolean:
1961 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001962 case Primitive::kPrimByte:
1963 case Primitive::kPrimShort:
1964 case Primitive::kPrimInt:
1965 case Primitive::kPrimChar:
1966 // Processing a Dex `int-to-double' instruction.
1967 locations->SetInAt(0, Location::RequiresRegister());
1968 locations->SetOut(Location::RequiresFpuRegister());
1969 break;
1970
1971 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001972 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001973 locations->SetInAt(0, Location::Any());
1974 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001975 break;
1976
Roland Levillaincff13742014-11-17 14:32:17 +00001977 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001978 // Processing a Dex `float-to-double' instruction.
1979 locations->SetInAt(0, Location::RequiresFpuRegister());
1980 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001981 break;
1982
1983 default:
1984 LOG(FATAL) << "Unexpected type conversion from " << input_type
1985 << " to " << result_type;
1986 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001987 break;
1988
1989 default:
1990 LOG(FATAL) << "Unexpected type conversion from " << input_type
1991 << " to " << result_type;
1992 }
1993}
1994
1995void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1996 LocationSummary* locations = conversion->GetLocations();
1997 Location out = locations->Out();
1998 Location in = locations->InAt(0);
1999 Primitive::Type result_type = conversion->GetResultType();
2000 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002001 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002002 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002003 case Primitive::kPrimByte:
2004 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002005 case Primitive::kPrimBoolean:
2006 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002007 case Primitive::kPrimShort:
2008 case Primitive::kPrimInt:
2009 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002010 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002011 if (in.IsRegister()) {
2012 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002013 } else {
2014 DCHECK(in.GetConstant()->IsIntConstant());
2015 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2016 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2017 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002018 break;
2019
2020 default:
2021 LOG(FATAL) << "Unexpected type conversion from " << input_type
2022 << " to " << result_type;
2023 }
2024 break;
2025
Roland Levillain01a8d712014-11-14 16:27:39 +00002026 case Primitive::kPrimShort:
2027 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002028 case Primitive::kPrimBoolean:
2029 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002030 case Primitive::kPrimByte:
2031 case Primitive::kPrimInt:
2032 case Primitive::kPrimChar:
2033 // Processing a Dex `int-to-short' instruction.
2034 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002035 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002036 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002037 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002038 } else {
2039 DCHECK(in.GetConstant()->IsIntConstant());
2040 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002042 }
2043 break;
2044
2045 default:
2046 LOG(FATAL) << "Unexpected type conversion from " << input_type
2047 << " to " << result_type;
2048 }
2049 break;
2050
Roland Levillain946e1432014-11-11 17:35:19 +00002051 case Primitive::kPrimInt:
2052 switch (input_type) {
2053 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002054 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002055 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002057 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002058 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002059 } else {
2060 DCHECK(in.IsConstant());
2061 DCHECK(in.GetConstant()->IsLongConstant());
2062 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002063 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002064 }
2065 break;
2066
Roland Levillain3f8f9362014-12-02 17:45:01 +00002067 case Primitive::kPrimFloat: {
2068 // Processing a Dex `float-to-int' instruction.
2069 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2070 Register output = out.AsRegister<Register>();
2071 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002072 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002073
2074 __ movl(output, Immediate(kPrimIntMax));
2075 // temp = int-to-float(output)
2076 __ cvtsi2ss(temp, output);
2077 // if input >= temp goto done
2078 __ comiss(input, temp);
2079 __ j(kAboveEqual, &done);
2080 // if input == NaN goto nan
2081 __ j(kUnordered, &nan);
2082 // output = float-to-int-truncate(input)
2083 __ cvttss2si(output, input);
2084 __ jmp(&done);
2085 __ Bind(&nan);
2086 // output = 0
2087 __ xorl(output, output);
2088 __ Bind(&done);
2089 break;
2090 }
2091
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002092 case Primitive::kPrimDouble: {
2093 // Processing a Dex `double-to-int' instruction.
2094 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2095 Register output = out.AsRegister<Register>();
2096 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002097 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002098
2099 __ movl(output, Immediate(kPrimIntMax));
2100 // temp = int-to-double(output)
2101 __ cvtsi2sd(temp, output);
2102 // if input >= temp goto done
2103 __ comisd(input, temp);
2104 __ j(kAboveEqual, &done);
2105 // if input == NaN goto nan
2106 __ j(kUnordered, &nan);
2107 // output = double-to-int-truncate(input)
2108 __ cvttsd2si(output, input);
2109 __ jmp(&done);
2110 __ Bind(&nan);
2111 // output = 0
2112 __ xorl(output, output);
2113 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002114 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002115 }
Roland Levillain946e1432014-11-11 17:35:19 +00002116
2117 default:
2118 LOG(FATAL) << "Unexpected type conversion from " << input_type
2119 << " to " << result_type;
2120 }
2121 break;
2122
Roland Levillaindff1f282014-11-05 14:15:05 +00002123 case Primitive::kPrimLong:
2124 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002125 case Primitive::kPrimBoolean:
2126 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002127 case Primitive::kPrimByte:
2128 case Primitive::kPrimShort:
2129 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002130 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002131 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002132 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2133 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002134 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002135 __ cdq();
2136 break;
2137
2138 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002139 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002140 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2141 conversion,
2142 conversion->GetDexPc(),
2143 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002144 break;
2145
Roland Levillaindff1f282014-11-05 14:15:05 +00002146 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002147 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002148 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2149 conversion,
2150 conversion->GetDexPc(),
2151 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002152 break;
2153
2154 default:
2155 LOG(FATAL) << "Unexpected type conversion from " << input_type
2156 << " to " << result_type;
2157 }
2158 break;
2159
Roland Levillain981e4542014-11-14 11:47:14 +00002160 case Primitive::kPrimChar:
2161 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002162 case Primitive::kPrimBoolean:
2163 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002164 case Primitive::kPrimByte:
2165 case Primitive::kPrimShort:
2166 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002167 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2168 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002169 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002170 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002172 } else {
2173 DCHECK(in.GetConstant()->IsIntConstant());
2174 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002175 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002176 }
2177 break;
2178
2179 default:
2180 LOG(FATAL) << "Unexpected type conversion from " << input_type
2181 << " to " << result_type;
2182 }
2183 break;
2184
Roland Levillaindff1f282014-11-05 14:15:05 +00002185 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002186 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002187 case Primitive::kPrimBoolean:
2188 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002189 case Primitive::kPrimByte:
2190 case Primitive::kPrimShort:
2191 case Primitive::kPrimInt:
2192 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002193 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002194 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002195 break;
2196
Roland Levillain6d0e4832014-11-27 18:31:21 +00002197 case Primitive::kPrimLong: {
2198 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002199 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002200
Roland Levillain232ade02015-04-20 15:14:36 +01002201 // Create stack space for the call to
2202 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2203 // TODO: enhance register allocator to ask for stack temporaries.
2204 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2205 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2206 __ subl(ESP, Immediate(adjustment));
2207 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002208
Roland Levillain232ade02015-04-20 15:14:36 +01002209 // Load the value to the FP stack, using temporaries if needed.
2210 PushOntoFPStack(in, 0, adjustment, false, true);
2211
2212 if (out.IsStackSlot()) {
2213 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2214 } else {
2215 __ fstps(Address(ESP, 0));
2216 Location stack_temp = Location::StackSlot(0);
2217 codegen_->Move32(out, stack_temp);
2218 }
2219
2220 // Remove the temporary stack space we allocated.
2221 if (adjustment != 0) {
2222 __ addl(ESP, Immediate(adjustment));
2223 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002224 break;
2225 }
2226
Roland Levillaincff13742014-11-17 14:32:17 +00002227 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002228 // Processing a Dex `double-to-float' instruction.
2229 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002230 break;
2231
2232 default:
2233 LOG(FATAL) << "Unexpected type conversion from " << input_type
2234 << " to " << result_type;
2235 };
2236 break;
2237
Roland Levillaindff1f282014-11-05 14:15:05 +00002238 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002239 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002240 case Primitive::kPrimBoolean:
2241 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002242 case Primitive::kPrimByte:
2243 case Primitive::kPrimShort:
2244 case Primitive::kPrimInt:
2245 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002246 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002247 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002248 break;
2249
Roland Levillain647b9ed2014-11-27 12:06:00 +00002250 case Primitive::kPrimLong: {
2251 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002252 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002253
Roland Levillain232ade02015-04-20 15:14:36 +01002254 // Create stack space for the call to
2255 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2256 // TODO: enhance register allocator to ask for stack temporaries.
2257 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2258 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2259 __ subl(ESP, Immediate(adjustment));
2260 }
2261
2262 // Load the value to the FP stack, using temporaries if needed.
2263 PushOntoFPStack(in, 0, adjustment, false, true);
2264
2265 if (out.IsDoubleStackSlot()) {
2266 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2267 } else {
2268 __ fstpl(Address(ESP, 0));
2269 Location stack_temp = Location::DoubleStackSlot(0);
2270 codegen_->Move64(out, stack_temp);
2271 }
2272
2273 // Remove the temporary stack space we allocated.
2274 if (adjustment != 0) {
2275 __ addl(ESP, Immediate(adjustment));
2276 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002277 break;
2278 }
2279
Roland Levillaincff13742014-11-17 14:32:17 +00002280 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002281 // Processing a Dex `float-to-double' instruction.
2282 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002283 break;
2284
2285 default:
2286 LOG(FATAL) << "Unexpected type conversion from " << input_type
2287 << " to " << result_type;
2288 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002289 break;
2290
2291 default:
2292 LOG(FATAL) << "Unexpected type conversion from " << input_type
2293 << " to " << result_type;
2294 }
2295}
2296
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002297void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002298 LocationSummary* locations =
2299 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002300 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002301 case Primitive::kPrimInt: {
2302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2305 break;
2306 }
2307
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002308 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002309 locations->SetInAt(0, Location::RequiresRegister());
2310 locations->SetInAt(1, Location::Any());
2311 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002312 break;
2313 }
2314
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002315 case Primitive::kPrimFloat:
2316 case Primitive::kPrimDouble: {
2317 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002318 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002319 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002320 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002321 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002322
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002323 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002324 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2325 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002326 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002327}
2328
2329void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2330 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002331 Location first = locations->InAt(0);
2332 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002333 Location out = locations->Out();
2334
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002335 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002336 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002337 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002338 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2339 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002340 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2341 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002342 } else {
2343 __ leal(out.AsRegister<Register>(), Address(
2344 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2345 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002346 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002347 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2348 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2349 __ addl(out.AsRegister<Register>(), Immediate(value));
2350 } else {
2351 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2352 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002353 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002354 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002355 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002356 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002357 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002358 }
2359
2360 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002361 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002362 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2363 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002364 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002365 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2366 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002367 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002368 } else {
2369 DCHECK(second.IsConstant()) << second;
2370 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2371 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2372 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002373 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002374 break;
2375 }
2376
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002377 case Primitive::kPrimFloat: {
2378 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002379 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002380 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2381 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2382 DCHECK(!const_area->NeedsMaterialization());
2383 __ addss(first.AsFpuRegister<XmmRegister>(),
2384 codegen_->LiteralFloatAddress(
2385 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2386 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2387 } else {
2388 DCHECK(second.IsStackSlot());
2389 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002390 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002391 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002392 }
2393
2394 case Primitive::kPrimDouble: {
2395 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002396 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002397 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2398 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2399 DCHECK(!const_area->NeedsMaterialization());
2400 __ addsd(first.AsFpuRegister<XmmRegister>(),
2401 codegen_->LiteralDoubleAddress(
2402 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2403 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2404 } else {
2405 DCHECK(second.IsDoubleStackSlot());
2406 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002407 }
2408 break;
2409 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002410
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002411 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002412 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002413 }
2414}
2415
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002416void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002417 LocationSummary* locations =
2418 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002419 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002420 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002421 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002422 locations->SetInAt(0, Location::RequiresRegister());
2423 locations->SetInAt(1, Location::Any());
2424 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002425 break;
2426 }
Calin Juravle11351682014-10-23 15:38:15 +01002427 case Primitive::kPrimFloat:
2428 case Primitive::kPrimDouble: {
2429 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002430 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002431 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002432 break;
Calin Juravle11351682014-10-23 15:38:15 +01002433 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002434
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002435 default:
Calin Juravle11351682014-10-23 15:38:15 +01002436 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002437 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002438}
2439
2440void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2441 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002442 Location first = locations->InAt(0);
2443 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002444 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002445 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002446 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002447 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002448 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002449 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002450 __ subl(first.AsRegister<Register>(),
2451 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002452 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002454 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002455 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002456 }
2457
2458 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002459 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002460 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2461 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002462 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002463 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002464 __ sbbl(first.AsRegisterPairHigh<Register>(),
2465 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002466 } else {
2467 DCHECK(second.IsConstant()) << second;
2468 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2469 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2470 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002471 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002472 break;
2473 }
2474
Calin Juravle11351682014-10-23 15:38:15 +01002475 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002476 if (second.IsFpuRegister()) {
2477 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2478 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2479 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2480 DCHECK(!const_area->NeedsMaterialization());
2481 __ subss(first.AsFpuRegister<XmmRegister>(),
2482 codegen_->LiteralFloatAddress(
2483 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2484 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2485 } else {
2486 DCHECK(second.IsStackSlot());
2487 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2488 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002489 break;
Calin Juravle11351682014-10-23 15:38:15 +01002490 }
2491
2492 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002493 if (second.IsFpuRegister()) {
2494 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2495 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2496 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2497 DCHECK(!const_area->NeedsMaterialization());
2498 __ subsd(first.AsFpuRegister<XmmRegister>(),
2499 codegen_->LiteralDoubleAddress(
2500 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2501 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2502 } else {
2503 DCHECK(second.IsDoubleStackSlot());
2504 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2505 }
Calin Juravle11351682014-10-23 15:38:15 +01002506 break;
2507 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002508
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002509 default:
Calin Juravle11351682014-10-23 15:38:15 +01002510 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002511 }
2512}
2513
Calin Juravle34bacdf2014-10-07 20:23:36 +01002514void LocationsBuilderX86::VisitMul(HMul* mul) {
2515 LocationSummary* locations =
2516 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2517 switch (mul->GetResultType()) {
2518 case Primitive::kPrimInt:
2519 locations->SetInAt(0, Location::RequiresRegister());
2520 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002521 if (mul->InputAt(1)->IsIntConstant()) {
2522 // Can use 3 operand multiply.
2523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2524 } else {
2525 locations->SetOut(Location::SameAsFirstInput());
2526 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002527 break;
2528 case Primitive::kPrimLong: {
2529 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002530 locations->SetInAt(1, Location::Any());
2531 locations->SetOut(Location::SameAsFirstInput());
2532 // Needed for imul on 32bits with 64bits output.
2533 locations->AddTemp(Location::RegisterLocation(EAX));
2534 locations->AddTemp(Location::RegisterLocation(EDX));
2535 break;
2536 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002537 case Primitive::kPrimFloat:
2538 case Primitive::kPrimDouble: {
2539 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002540 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002541 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002542 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002543 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002544
2545 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002546 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002547 }
2548}
2549
2550void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2551 LocationSummary* locations = mul->GetLocations();
2552 Location first = locations->InAt(0);
2553 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002554 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002555
2556 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002557 case Primitive::kPrimInt:
2558 // The constant may have ended up in a register, so test explicitly to avoid
2559 // problems where the output may not be the same as the first operand.
2560 if (mul->InputAt(1)->IsIntConstant()) {
2561 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2562 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2563 } else if (second.IsRegister()) {
2564 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002565 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002566 } else {
2567 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002568 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002569 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002570 }
2571 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002572
2573 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002574 Register in1_hi = first.AsRegisterPairHigh<Register>();
2575 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002576 Register eax = locations->GetTemp(0).AsRegister<Register>();
2577 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002578
2579 DCHECK_EQ(EAX, eax);
2580 DCHECK_EQ(EDX, edx);
2581
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002582 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002583 // output: in1
2584 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2585 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2586 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002587 if (second.IsConstant()) {
2588 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002589
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002590 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2591 int32_t low_value = Low32Bits(value);
2592 int32_t high_value = High32Bits(value);
2593 Immediate low(low_value);
2594 Immediate high(high_value);
2595
2596 __ movl(eax, high);
2597 // eax <- in1.lo * in2.hi
2598 __ imull(eax, in1_lo);
2599 // in1.hi <- in1.hi * in2.lo
2600 __ imull(in1_hi, low);
2601 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2602 __ addl(in1_hi, eax);
2603 // move in2_lo to eax to prepare for double precision
2604 __ movl(eax, low);
2605 // edx:eax <- in1.lo * in2.lo
2606 __ mull(in1_lo);
2607 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2608 __ addl(in1_hi, edx);
2609 // in1.lo <- (in1.lo * in2.lo)[31:0];
2610 __ movl(in1_lo, eax);
2611 } else if (second.IsRegisterPair()) {
2612 Register in2_hi = second.AsRegisterPairHigh<Register>();
2613 Register in2_lo = second.AsRegisterPairLow<Register>();
2614
2615 __ movl(eax, in2_hi);
2616 // eax <- in1.lo * in2.hi
2617 __ imull(eax, in1_lo);
2618 // in1.hi <- in1.hi * in2.lo
2619 __ imull(in1_hi, in2_lo);
2620 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2621 __ addl(in1_hi, eax);
2622 // move in1_lo to eax to prepare for double precision
2623 __ movl(eax, in1_lo);
2624 // edx:eax <- in1.lo * in2.lo
2625 __ mull(in2_lo);
2626 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2627 __ addl(in1_hi, edx);
2628 // in1.lo <- (in1.lo * in2.lo)[31:0];
2629 __ movl(in1_lo, eax);
2630 } else {
2631 DCHECK(second.IsDoubleStackSlot()) << second;
2632 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2633 Address in2_lo(ESP, second.GetStackIndex());
2634
2635 __ movl(eax, in2_hi);
2636 // eax <- in1.lo * in2.hi
2637 __ imull(eax, in1_lo);
2638 // in1.hi <- in1.hi * in2.lo
2639 __ imull(in1_hi, in2_lo);
2640 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2641 __ addl(in1_hi, eax);
2642 // move in1_lo to eax to prepare for double precision
2643 __ movl(eax, in1_lo);
2644 // edx:eax <- in1.lo * in2.lo
2645 __ mull(in2_lo);
2646 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2647 __ addl(in1_hi, edx);
2648 // in1.lo <- (in1.lo * in2.lo)[31:0];
2649 __ movl(in1_lo, eax);
2650 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002651
2652 break;
2653 }
2654
Calin Juravleb5bfa962014-10-21 18:02:24 +01002655 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002656 DCHECK(first.Equals(locations->Out()));
2657 if (second.IsFpuRegister()) {
2658 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2659 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2660 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2661 DCHECK(!const_area->NeedsMaterialization());
2662 __ mulss(first.AsFpuRegister<XmmRegister>(),
2663 codegen_->LiteralFloatAddress(
2664 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2665 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2666 } else {
2667 DCHECK(second.IsStackSlot());
2668 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2669 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002670 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002671 }
2672
2673 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002674 DCHECK(first.Equals(locations->Out()));
2675 if (second.IsFpuRegister()) {
2676 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2677 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2678 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2679 DCHECK(!const_area->NeedsMaterialization());
2680 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2681 codegen_->LiteralDoubleAddress(
2682 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2683 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2684 } else {
2685 DCHECK(second.IsDoubleStackSlot());
2686 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2687 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002688 break;
2689 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002690
2691 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002692 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002693 }
2694}
2695
Roland Levillain232ade02015-04-20 15:14:36 +01002696void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2697 uint32_t temp_offset,
2698 uint32_t stack_adjustment,
2699 bool is_fp,
2700 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002701 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002702 DCHECK(!is_wide);
2703 if (is_fp) {
2704 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2705 } else {
2706 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2707 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002708 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002709 DCHECK(is_wide);
2710 if (is_fp) {
2711 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2712 } else {
2713 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2714 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002715 } else {
2716 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002717 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002718 Location stack_temp = Location::StackSlot(temp_offset);
2719 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002720 if (is_fp) {
2721 __ flds(Address(ESP, temp_offset));
2722 } else {
2723 __ filds(Address(ESP, temp_offset));
2724 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002725 } else {
2726 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2727 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002728 if (is_fp) {
2729 __ fldl(Address(ESP, temp_offset));
2730 } else {
2731 __ fildl(Address(ESP, temp_offset));
2732 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002733 }
2734 }
2735}
2736
2737void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2738 Primitive::Type type = rem->GetResultType();
2739 bool is_float = type == Primitive::kPrimFloat;
2740 size_t elem_size = Primitive::ComponentSize(type);
2741 LocationSummary* locations = rem->GetLocations();
2742 Location first = locations->InAt(0);
2743 Location second = locations->InAt(1);
2744 Location out = locations->Out();
2745
2746 // Create stack space for 2 elements.
2747 // TODO: enhance register allocator to ask for stack temporaries.
2748 __ subl(ESP, Immediate(2 * elem_size));
2749
2750 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002751 const bool is_wide = !is_float;
2752 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2753 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002754
2755 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002756 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002757 __ Bind(&retry);
2758 __ fprem();
2759
2760 // Move FP status to AX.
2761 __ fstsw();
2762
2763 // And see if the argument reduction is complete. This is signaled by the
2764 // C2 FPU flag bit set to 0.
2765 __ andl(EAX, Immediate(kC2ConditionMask));
2766 __ j(kNotEqual, &retry);
2767
2768 // We have settled on the final value. Retrieve it into an XMM register.
2769 // Store FP top of stack to real stack.
2770 if (is_float) {
2771 __ fsts(Address(ESP, 0));
2772 } else {
2773 __ fstl(Address(ESP, 0));
2774 }
2775
2776 // Pop the 2 items from the FP stack.
2777 __ fucompp();
2778
2779 // Load the value from the stack into an XMM register.
2780 DCHECK(out.IsFpuRegister()) << out;
2781 if (is_float) {
2782 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2783 } else {
2784 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2785 }
2786
2787 // And remove the temporary stack space we allocated.
2788 __ addl(ESP, Immediate(2 * elem_size));
2789}
2790
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002791
2792void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2793 DCHECK(instruction->IsDiv() || instruction->IsRem());
2794
2795 LocationSummary* locations = instruction->GetLocations();
2796 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002797 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002798
2799 Register out_register = locations->Out().AsRegister<Register>();
2800 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002801 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002802
2803 DCHECK(imm == 1 || imm == -1);
2804
2805 if (instruction->IsRem()) {
2806 __ xorl(out_register, out_register);
2807 } else {
2808 __ movl(out_register, input_register);
2809 if (imm == -1) {
2810 __ negl(out_register);
2811 }
2812 }
2813}
2814
2815
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002816void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002817 LocationSummary* locations = instruction->GetLocations();
2818
2819 Register out_register = locations->Out().AsRegister<Register>();
2820 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002821 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002822
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002823 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002824 Register num = locations->GetTemp(0).AsRegister<Register>();
2825
2826 __ leal(num, Address(input_register, std::abs(imm) - 1));
2827 __ testl(input_register, input_register);
2828 __ cmovl(kGreaterEqual, num, input_register);
2829 int shift = CTZ(imm);
2830 __ sarl(num, Immediate(shift));
2831
2832 if (imm < 0) {
2833 __ negl(num);
2834 }
2835
2836 __ movl(out_register, num);
2837}
2838
2839void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2840 DCHECK(instruction->IsDiv() || instruction->IsRem());
2841
2842 LocationSummary* locations = instruction->GetLocations();
2843 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2844
2845 Register eax = locations->InAt(0).AsRegister<Register>();
2846 Register out = locations->Out().AsRegister<Register>();
2847 Register num;
2848 Register edx;
2849
2850 if (instruction->IsDiv()) {
2851 edx = locations->GetTemp(0).AsRegister<Register>();
2852 num = locations->GetTemp(1).AsRegister<Register>();
2853 } else {
2854 edx = locations->Out().AsRegister<Register>();
2855 num = locations->GetTemp(0).AsRegister<Register>();
2856 }
2857
2858 DCHECK_EQ(EAX, eax);
2859 DCHECK_EQ(EDX, edx);
2860 if (instruction->IsDiv()) {
2861 DCHECK_EQ(EAX, out);
2862 } else {
2863 DCHECK_EQ(EDX, out);
2864 }
2865
2866 int64_t magic;
2867 int shift;
2868 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2869
Mark Mendell0c9497d2015-08-21 09:30:05 -04002870 NearLabel ndiv;
2871 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002872 // If numerator is 0, the result is 0, no computation needed.
2873 __ testl(eax, eax);
2874 __ j(kNotEqual, &ndiv);
2875
2876 __ xorl(out, out);
2877 __ jmp(&end);
2878
2879 __ Bind(&ndiv);
2880
2881 // Save the numerator.
2882 __ movl(num, eax);
2883
2884 // EAX = magic
2885 __ movl(eax, Immediate(magic));
2886
2887 // EDX:EAX = magic * numerator
2888 __ imull(num);
2889
2890 if (imm > 0 && magic < 0) {
2891 // EDX += num
2892 __ addl(edx, num);
2893 } else if (imm < 0 && magic > 0) {
2894 __ subl(edx, num);
2895 }
2896
2897 // Shift if needed.
2898 if (shift != 0) {
2899 __ sarl(edx, Immediate(shift));
2900 }
2901
2902 // EDX += 1 if EDX < 0
2903 __ movl(eax, edx);
2904 __ shrl(edx, Immediate(31));
2905 __ addl(edx, eax);
2906
2907 if (instruction->IsRem()) {
2908 __ movl(eax, num);
2909 __ imull(edx, Immediate(imm));
2910 __ subl(eax, edx);
2911 __ movl(edx, eax);
2912 } else {
2913 __ movl(eax, edx);
2914 }
2915 __ Bind(&end);
2916}
2917
Calin Juravlebacfec32014-11-14 15:54:36 +00002918void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2919 DCHECK(instruction->IsDiv() || instruction->IsRem());
2920
2921 LocationSummary* locations = instruction->GetLocations();
2922 Location out = locations->Out();
2923 Location first = locations->InAt(0);
2924 Location second = locations->InAt(1);
2925 bool is_div = instruction->IsDiv();
2926
2927 switch (instruction->GetResultType()) {
2928 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002929 DCHECK_EQ(EAX, first.AsRegister<Register>());
2930 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002931
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002932 if (instruction->InputAt(1)->IsIntConstant()) {
2933 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002934
2935 if (imm == 0) {
2936 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2937 } else if (imm == 1 || imm == -1) {
2938 DivRemOneOrMinusOne(instruction);
2939 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002940 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002941 } else {
2942 DCHECK(imm <= -2 || imm >= 2);
2943 GenerateDivRemWithAnyConstant(instruction);
2944 }
2945 } else {
2946 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002947 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002948 is_div);
2949 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002950
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002951 Register second_reg = second.AsRegister<Register>();
2952 // 0x80000000/-1 triggers an arithmetic exception!
2953 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2954 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002955
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002956 __ cmpl(second_reg, Immediate(-1));
2957 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002958
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002959 // edx:eax <- sign-extended of eax
2960 __ cdq();
2961 // eax = quotient, edx = remainder
2962 __ idivl(second_reg);
2963 __ Bind(slow_path->GetExitLabel());
2964 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002965 break;
2966 }
2967
2968 case Primitive::kPrimLong: {
2969 InvokeRuntimeCallingConvention calling_convention;
2970 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2971 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2972 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2973 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2974 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2975 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2976
2977 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002978 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2979 instruction,
2980 instruction->GetDexPc(),
2981 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002982 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002983 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2984 instruction,
2985 instruction->GetDexPc(),
2986 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002987 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002988 break;
2989 }
2990
2991 default:
2992 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2993 }
2994}
2995
Calin Juravle7c4954d2014-10-28 16:57:40 +00002996void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002997 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002998 ? LocationSummary::kCall
2999 : LocationSummary::kNoCall;
3000 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3001
Calin Juravle7c4954d2014-10-28 16:57:40 +00003002 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003003 case Primitive::kPrimInt: {
3004 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003005 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003006 locations->SetOut(Location::SameAsFirstInput());
3007 // Intel uses edx:eax as the dividend.
3008 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003009 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3010 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3011 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003012 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003013 locations->AddTemp(Location::RequiresRegister());
3014 }
Calin Juravled0d48522014-11-04 16:40:20 +00003015 break;
3016 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003017 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003018 InvokeRuntimeCallingConvention calling_convention;
3019 locations->SetInAt(0, Location::RegisterPairLocation(
3020 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3021 locations->SetInAt(1, Location::RegisterPairLocation(
3022 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3023 // Runtime helper puts the result in EAX, EDX.
3024 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003025 break;
3026 }
3027 case Primitive::kPrimFloat:
3028 case Primitive::kPrimDouble: {
3029 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003030 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003031 locations->SetOut(Location::SameAsFirstInput());
3032 break;
3033 }
3034
3035 default:
3036 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3037 }
3038}
3039
3040void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3041 LocationSummary* locations = div->GetLocations();
3042 Location first = locations->InAt(0);
3043 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003044
3045 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003046 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003047 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003048 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003049 break;
3050 }
3051
3052 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003053 if (second.IsFpuRegister()) {
3054 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3055 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3056 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3057 DCHECK(!const_area->NeedsMaterialization());
3058 __ divss(first.AsFpuRegister<XmmRegister>(),
3059 codegen_->LiteralFloatAddress(
3060 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3061 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3062 } else {
3063 DCHECK(second.IsStackSlot());
3064 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3065 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003066 break;
3067 }
3068
3069 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003070 if (second.IsFpuRegister()) {
3071 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3072 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3073 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3074 DCHECK(!const_area->NeedsMaterialization());
3075 __ divsd(first.AsFpuRegister<XmmRegister>(),
3076 codegen_->LiteralDoubleAddress(
3077 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3078 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3079 } else {
3080 DCHECK(second.IsDoubleStackSlot());
3081 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3082 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003083 break;
3084 }
3085
3086 default:
3087 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3088 }
3089}
3090
Calin Juravlebacfec32014-11-14 15:54:36 +00003091void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003092 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003093
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003094 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3095 ? LocationSummary::kCall
3096 : LocationSummary::kNoCall;
3097 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003098
Calin Juravled2ec87d2014-12-08 14:24:46 +00003099 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003100 case Primitive::kPrimInt: {
3101 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003102 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003103 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003104 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3105 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3106 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003107 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003108 locations->AddTemp(Location::RequiresRegister());
3109 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003110 break;
3111 }
3112 case Primitive::kPrimLong: {
3113 InvokeRuntimeCallingConvention calling_convention;
3114 locations->SetInAt(0, Location::RegisterPairLocation(
3115 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3116 locations->SetInAt(1, Location::RegisterPairLocation(
3117 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3118 // Runtime helper puts the result in EAX, EDX.
3119 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3120 break;
3121 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003122 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003123 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003124 locations->SetInAt(0, Location::Any());
3125 locations->SetInAt(1, Location::Any());
3126 locations->SetOut(Location::RequiresFpuRegister());
3127 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003128 break;
3129 }
3130
3131 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003132 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003133 }
3134}
3135
3136void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3137 Primitive::Type type = rem->GetResultType();
3138 switch (type) {
3139 case Primitive::kPrimInt:
3140 case Primitive::kPrimLong: {
3141 GenerateDivRemIntegral(rem);
3142 break;
3143 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003144 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003145 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003146 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003147 break;
3148 }
3149 default:
3150 LOG(FATAL) << "Unexpected rem type " << type;
3151 }
3152}
3153
Calin Juravled0d48522014-11-04 16:40:20 +00003154void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003155 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3156 ? LocationSummary::kCallOnSlowPath
3157 : LocationSummary::kNoCall;
3158 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003159 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003160 case Primitive::kPrimByte:
3161 case Primitive::kPrimChar:
3162 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003163 case Primitive::kPrimInt: {
3164 locations->SetInAt(0, Location::Any());
3165 break;
3166 }
3167 case Primitive::kPrimLong: {
3168 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3169 if (!instruction->IsConstant()) {
3170 locations->AddTemp(Location::RequiresRegister());
3171 }
3172 break;
3173 }
3174 default:
3175 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3176 }
Calin Juravled0d48522014-11-04 16:40:20 +00003177 if (instruction->HasUses()) {
3178 locations->SetOut(Location::SameAsFirstInput());
3179 }
3180}
3181
3182void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3183 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3184 codegen_->AddSlowPath(slow_path);
3185
3186 LocationSummary* locations = instruction->GetLocations();
3187 Location value = locations->InAt(0);
3188
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003189 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003190 case Primitive::kPrimByte:
3191 case Primitive::kPrimChar:
3192 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003193 case Primitive::kPrimInt: {
3194 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003195 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003196 __ j(kEqual, slow_path->GetEntryLabel());
3197 } else if (value.IsStackSlot()) {
3198 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3199 __ j(kEqual, slow_path->GetEntryLabel());
3200 } else {
3201 DCHECK(value.IsConstant()) << value;
3202 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3203 __ jmp(slow_path->GetEntryLabel());
3204 }
3205 }
3206 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003207 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003208 case Primitive::kPrimLong: {
3209 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003210 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003211 __ movl(temp, value.AsRegisterPairLow<Register>());
3212 __ orl(temp, value.AsRegisterPairHigh<Register>());
3213 __ j(kEqual, slow_path->GetEntryLabel());
3214 } else {
3215 DCHECK(value.IsConstant()) << value;
3216 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3217 __ jmp(slow_path->GetEntryLabel());
3218 }
3219 }
3220 break;
3221 }
3222 default:
3223 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003224 }
Calin Juravled0d48522014-11-04 16:40:20 +00003225}
3226
Calin Juravle9aec02f2014-11-18 23:06:35 +00003227void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3228 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3229
3230 LocationSummary* locations =
3231 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3232
3233 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003234 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003235 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003236 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003237 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003238 // The shift count needs to be in CL or a constant.
3239 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003240 locations->SetOut(Location::SameAsFirstInput());
3241 break;
3242 }
3243 default:
3244 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3245 }
3246}
3247
3248void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3249 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3250
3251 LocationSummary* locations = op->GetLocations();
3252 Location first = locations->InAt(0);
3253 Location second = locations->InAt(1);
3254 DCHECK(first.Equals(locations->Out()));
3255
3256 switch (op->GetResultType()) {
3257 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003258 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003259 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003260 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003261 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003262 DCHECK_EQ(ECX, second_reg);
3263 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003264 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003265 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003266 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003267 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003268 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003269 }
3270 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003271 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3272 if (shift == 0) {
3273 return;
3274 }
3275 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003276 if (op->IsShl()) {
3277 __ shll(first_reg, imm);
3278 } else if (op->IsShr()) {
3279 __ sarl(first_reg, imm);
3280 } else {
3281 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003282 }
3283 }
3284 break;
3285 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003286 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003287 if (second.IsRegister()) {
3288 Register second_reg = second.AsRegister<Register>();
3289 DCHECK_EQ(ECX, second_reg);
3290 if (op->IsShl()) {
3291 GenerateShlLong(first, second_reg);
3292 } else if (op->IsShr()) {
3293 GenerateShrLong(first, second_reg);
3294 } else {
3295 GenerateUShrLong(first, second_reg);
3296 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003297 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003298 // Shift by a constant.
3299 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3300 // Nothing to do if the shift is 0, as the input is already the output.
3301 if (shift != 0) {
3302 if (op->IsShl()) {
3303 GenerateShlLong(first, shift);
3304 } else if (op->IsShr()) {
3305 GenerateShrLong(first, shift);
3306 } else {
3307 GenerateUShrLong(first, shift);
3308 }
3309 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003310 }
3311 break;
3312 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003313 default:
3314 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3315 }
3316}
3317
Mark P Mendell73945692015-04-29 14:56:17 +00003318void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3319 Register low = loc.AsRegisterPairLow<Register>();
3320 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003321 if (shift == 1) {
3322 // This is just an addition.
3323 __ addl(low, low);
3324 __ adcl(high, high);
3325 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003326 // Shift by 32 is easy. High gets low, and low gets 0.
3327 codegen_->EmitParallelMoves(
3328 loc.ToLow(),
3329 loc.ToHigh(),
3330 Primitive::kPrimInt,
3331 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3332 loc.ToLow(),
3333 Primitive::kPrimInt);
3334 } else if (shift > 32) {
3335 // Low part becomes 0. High part is low part << (shift-32).
3336 __ movl(high, low);
3337 __ shll(high, Immediate(shift - 32));
3338 __ xorl(low, low);
3339 } else {
3340 // Between 1 and 31.
3341 __ shld(high, low, Immediate(shift));
3342 __ shll(low, Immediate(shift));
3343 }
3344}
3345
Calin Juravle9aec02f2014-11-18 23:06:35 +00003346void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003347 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003348 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3349 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3350 __ testl(shifter, Immediate(32));
3351 __ j(kEqual, &done);
3352 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3353 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3354 __ Bind(&done);
3355}
3356
Mark P Mendell73945692015-04-29 14:56:17 +00003357void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3358 Register low = loc.AsRegisterPairLow<Register>();
3359 Register high = loc.AsRegisterPairHigh<Register>();
3360 if (shift == 32) {
3361 // Need to copy the sign.
3362 DCHECK_NE(low, high);
3363 __ movl(low, high);
3364 __ sarl(high, Immediate(31));
3365 } else if (shift > 32) {
3366 DCHECK_NE(low, high);
3367 // High part becomes sign. Low part is shifted by shift - 32.
3368 __ movl(low, high);
3369 __ sarl(high, Immediate(31));
3370 __ sarl(low, Immediate(shift - 32));
3371 } else {
3372 // Between 1 and 31.
3373 __ shrd(low, high, Immediate(shift));
3374 __ sarl(high, Immediate(shift));
3375 }
3376}
3377
Calin Juravle9aec02f2014-11-18 23:06:35 +00003378void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003379 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003380 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3381 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3382 __ testl(shifter, Immediate(32));
3383 __ j(kEqual, &done);
3384 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3385 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3386 __ Bind(&done);
3387}
3388
Mark P Mendell73945692015-04-29 14:56:17 +00003389void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3390 Register low = loc.AsRegisterPairLow<Register>();
3391 Register high = loc.AsRegisterPairHigh<Register>();
3392 if (shift == 32) {
3393 // Shift by 32 is easy. Low gets high, and high gets 0.
3394 codegen_->EmitParallelMoves(
3395 loc.ToHigh(),
3396 loc.ToLow(),
3397 Primitive::kPrimInt,
3398 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3399 loc.ToHigh(),
3400 Primitive::kPrimInt);
3401 } else if (shift > 32) {
3402 // Low part is high >> (shift - 32). High part becomes 0.
3403 __ movl(low, high);
3404 __ shrl(low, Immediate(shift - 32));
3405 __ xorl(high, high);
3406 } else {
3407 // Between 1 and 31.
3408 __ shrd(low, high, Immediate(shift));
3409 __ shrl(high, Immediate(shift));
3410 }
3411}
3412
Calin Juravle9aec02f2014-11-18 23:06:35 +00003413void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003414 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003415 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3416 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3417 __ testl(shifter, Immediate(32));
3418 __ j(kEqual, &done);
3419 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3420 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3421 __ Bind(&done);
3422}
3423
3424void LocationsBuilderX86::VisitShl(HShl* shl) {
3425 HandleShift(shl);
3426}
3427
3428void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3429 HandleShift(shl);
3430}
3431
3432void LocationsBuilderX86::VisitShr(HShr* shr) {
3433 HandleShift(shr);
3434}
3435
3436void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3437 HandleShift(shr);
3438}
3439
3440void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3441 HandleShift(ushr);
3442}
3443
3444void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3445 HandleShift(ushr);
3446}
3447
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003448void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003449 LocationSummary* locations =
3450 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003451 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003452 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003453 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003454 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003455}
3456
3457void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3458 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003459 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003460 // Note: if heap poisoning is enabled, the entry point takes cares
3461 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003462 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3463 instruction,
3464 instruction->GetDexPc(),
3465 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003466 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003467}
3468
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003469void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3470 LocationSummary* locations =
3471 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3472 locations->SetOut(Location::RegisterLocation(EAX));
3473 InvokeRuntimeCallingConvention calling_convention;
3474 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003475 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003476 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003477}
3478
3479void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3480 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003481 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3482
Roland Levillain4d027112015-07-01 15:41:14 +01003483 // Note: if heap poisoning is enabled, the entry point takes cares
3484 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003485 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3486 instruction,
3487 instruction->GetDexPc(),
3488 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003489 DCHECK(!codegen_->IsLeafMethod());
3490}
3491
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003492void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003493 LocationSummary* locations =
3494 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003495 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3496 if (location.IsStackSlot()) {
3497 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3498 } else if (location.IsDoubleStackSlot()) {
3499 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003500 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003501 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003502}
3503
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003504void InstructionCodeGeneratorX86::VisitParameterValue(
3505 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3506}
3507
3508void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3509 LocationSummary* locations =
3510 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3511 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3512}
3513
3514void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003515}
3516
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003517void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003518 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003519 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003520 locations->SetInAt(0, Location::RequiresRegister());
3521 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003522}
3523
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003524void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3525 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003526 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003527 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003528 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003529 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003530 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003531 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003532 break;
3533
3534 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003535 __ notl(out.AsRegisterPairLow<Register>());
3536 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003537 break;
3538
3539 default:
3540 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3541 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003542}
3543
David Brazdil66d126e2015-04-03 16:02:44 +01003544void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3545 LocationSummary* locations =
3546 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3547 locations->SetInAt(0, Location::RequiresRegister());
3548 locations->SetOut(Location::SameAsFirstInput());
3549}
3550
3551void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003552 LocationSummary* locations = bool_not->GetLocations();
3553 Location in = locations->InAt(0);
3554 Location out = locations->Out();
3555 DCHECK(in.Equals(out));
3556 __ xorl(out.AsRegister<Register>(), Immediate(1));
3557}
3558
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003559void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003560 LocationSummary* locations =
3561 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003562 switch (compare->InputAt(0)->GetType()) {
3563 case Primitive::kPrimLong: {
3564 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003565 locations->SetInAt(1, Location::Any());
3566 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3567 break;
3568 }
3569 case Primitive::kPrimFloat:
3570 case Primitive::kPrimDouble: {
3571 locations->SetInAt(0, Location::RequiresFpuRegister());
3572 locations->SetInAt(1, Location::RequiresFpuRegister());
3573 locations->SetOut(Location::RequiresRegister());
3574 break;
3575 }
3576 default:
3577 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3578 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003579}
3580
3581void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003582 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003583 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003584 Location left = locations->InAt(0);
3585 Location right = locations->InAt(1);
3586
Mark Mendell0c9497d2015-08-21 09:30:05 -04003587 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003588 switch (compare->InputAt(0)->GetType()) {
3589 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003590 Register left_low = left.AsRegisterPairLow<Register>();
3591 Register left_high = left.AsRegisterPairHigh<Register>();
3592 int32_t val_low = 0;
3593 int32_t val_high = 0;
3594 bool right_is_const = false;
3595
3596 if (right.IsConstant()) {
3597 DCHECK(right.GetConstant()->IsLongConstant());
3598 right_is_const = true;
3599 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3600 val_low = Low32Bits(val);
3601 val_high = High32Bits(val);
3602 }
3603
Calin Juravleddb7df22014-11-25 20:56:51 +00003604 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003605 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003606 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003607 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003608 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003609 DCHECK(right_is_const) << right;
3610 if (val_high == 0) {
3611 __ testl(left_high, left_high);
3612 } else {
3613 __ cmpl(left_high, Immediate(val_high));
3614 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003615 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003616 __ j(kLess, &less); // Signed compare.
3617 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003618 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003619 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003620 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003621 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003622 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003623 DCHECK(right_is_const) << right;
3624 if (val_low == 0) {
3625 __ testl(left_low, left_low);
3626 } else {
3627 __ cmpl(left_low, Immediate(val_low));
3628 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003629 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003630 break;
3631 }
3632 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003634 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3635 break;
3636 }
3637 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003638 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003639 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003640 break;
3641 }
3642 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003643 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003644 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003645 __ movl(out, Immediate(0));
3646 __ j(kEqual, &done);
3647 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3648
3649 __ Bind(&greater);
3650 __ movl(out, Immediate(1));
3651 __ jmp(&done);
3652
3653 __ Bind(&less);
3654 __ movl(out, Immediate(-1));
3655
3656 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003657}
3658
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003659void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003660 LocationSummary* locations =
3661 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003662 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3663 locations->SetInAt(i, Location::Any());
3664 }
3665 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003666}
3667
3668void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003669 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003670 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003671}
3672
Calin Juravle52c48962014-12-16 17:02:57 +00003673void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3674 /*
3675 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3676 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3677 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3678 */
3679 switch (kind) {
3680 case MemBarrierKind::kAnyAny: {
3681 __ mfence();
3682 break;
3683 }
3684 case MemBarrierKind::kAnyStore:
3685 case MemBarrierKind::kLoadAny:
3686 case MemBarrierKind::kStoreStore: {
3687 // nop
3688 break;
3689 }
3690 default:
3691 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003692 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003693}
3694
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003695
Vladimir Marko58155012015-08-19 12:49:41 +00003696void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3697 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3698 switch (invoke->GetMethodLoadKind()) {
3699 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3700 // temp = thread->string_init_entrypoint
3701 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3702 break;
3703 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3704 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3705 break;
3706 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3707 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3708 break;
3709 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3710 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3711 method_patches_.emplace_back(invoke->GetTargetMethod());
3712 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3713 break;
3714 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3715 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3716 FALLTHROUGH_INTENDED;
3717 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3718 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3719 Register method_reg;
3720 Register reg = temp.AsRegister<Register>();
3721 if (current_method.IsRegister()) {
3722 method_reg = current_method.AsRegister<Register>();
3723 } else {
3724 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3725 DCHECK(!current_method.IsValid());
3726 method_reg = reg;
3727 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3728 }
3729 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003730 __ movl(reg, Address(method_reg,
3731 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003732 // temp = temp[index_in_cache]
3733 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3734 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3735 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003736 }
Vladimir Marko58155012015-08-19 12:49:41 +00003737 }
3738
3739 switch (invoke->GetCodePtrLocation()) {
3740 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3741 __ call(GetFrameEntryLabel());
3742 break;
3743 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3744 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3745 Label* label = &relative_call_patches_.back().label;
3746 __ call(label); // Bind to the patch label, override at link time.
3747 __ Bind(label); // Bind the label at the end of the "call" insn.
3748 break;
3749 }
3750 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3751 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3752 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3753 // (Though the direct CALL ptr16:32 is available for consideration).
3754 FALLTHROUGH_INTENDED;
3755 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3756 // (callee_method + offset_of_quick_compiled_code)()
3757 __ call(Address(callee_method.AsRegister<Register>(),
3758 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3759 kX86WordSize).Int32Value()));
3760 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003761 }
3762
3763 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003764}
3765
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003766void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3767 Register temp = temp_in.AsRegister<Register>();
3768 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3769 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3770 LocationSummary* locations = invoke->GetLocations();
3771 Location receiver = locations->InAt(0);
3772 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3773 // temp = object->GetClass();
3774 DCHECK(receiver.IsRegister());
3775 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3776 MaybeRecordImplicitNullCheck(invoke);
3777 __ MaybeUnpoisonHeapReference(temp);
3778 // temp = temp->GetMethodAt(method_offset);
3779 __ movl(temp, Address(temp, method_offset));
3780 // call temp->GetEntryPoint();
3781 __ call(Address(
3782 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3783}
3784
Vladimir Marko58155012015-08-19 12:49:41 +00003785void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3786 DCHECK(linker_patches->empty());
3787 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3788 for (const MethodPatchInfo<Label>& info : method_patches_) {
3789 // The label points to the end of the "movl" insn but the literal offset for method
3790 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3791 uint32_t literal_offset = info.label.Position() - 4;
3792 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3793 info.target_method.dex_file,
3794 info.target_method.dex_method_index));
3795 }
3796 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3797 // The label points to the end of the "call" insn but the literal offset for method
3798 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3799 uint32_t literal_offset = info.label.Position() - 4;
3800 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3801 info.target_method.dex_file,
3802 info.target_method.dex_method_index));
3803 }
3804}
3805
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003806void CodeGeneratorX86::MarkGCCard(Register temp,
3807 Register card,
3808 Register object,
3809 Register value,
3810 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003811 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003812 if (value_can_be_null) {
3813 __ testl(value, value);
3814 __ j(kEqual, &is_null);
3815 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003816 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3817 __ movl(temp, object);
3818 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003819 __ movb(Address(temp, card, TIMES_1, 0),
3820 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003821 if (value_can_be_null) {
3822 __ Bind(&is_null);
3823 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003824}
3825
Calin Juravle52c48962014-12-16 17:02:57 +00003826void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3827 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003828 LocationSummary* locations =
3829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003830 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003831
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003832 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3833 locations->SetOut(Location::RequiresFpuRegister());
3834 } else {
3835 // The output overlaps in case of long: we don't want the low move to overwrite
3836 // the object's location.
3837 locations->SetOut(Location::RequiresRegister(),
3838 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3839 : Location::kNoOutputOverlap);
3840 }
Calin Juravle52c48962014-12-16 17:02:57 +00003841
3842 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3843 // Long values can be loaded atomically into an XMM using movsd.
3844 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3845 // and then copy the XMM into the output 32bits at a time).
3846 locations->AddTemp(Location::RequiresFpuRegister());
3847 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003848}
3849
Calin Juravle52c48962014-12-16 17:02:57 +00003850void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3851 const FieldInfo& field_info) {
3852 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003853
Calin Juravle52c48962014-12-16 17:02:57 +00003854 LocationSummary* locations = instruction->GetLocations();
3855 Register base = locations->InAt(0).AsRegister<Register>();
3856 Location out = locations->Out();
3857 bool is_volatile = field_info.IsVolatile();
3858 Primitive::Type field_type = field_info.GetFieldType();
3859 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3860
3861 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003863 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003864 break;
3865 }
3866
3867 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003868 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003869 break;
3870 }
3871
3872 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003873 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003874 break;
3875 }
3876
3877 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003878 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003879 break;
3880 }
3881
3882 case Primitive::kPrimInt:
3883 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003884 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003885 break;
3886 }
3887
3888 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003889 if (is_volatile) {
3890 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3891 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003892 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003893 __ movd(out.AsRegisterPairLow<Register>(), temp);
3894 __ psrlq(temp, Immediate(32));
3895 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3896 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003897 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003898 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003899 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003900 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3901 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003902 break;
3903 }
3904
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003905 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003906 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003907 break;
3908 }
3909
3910 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003911 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003912 break;
3913 }
3914
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003915 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003916 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003917 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003918 }
Calin Juravle52c48962014-12-16 17:02:57 +00003919
Calin Juravle77520bc2015-01-12 18:45:46 +00003920 // Longs are handled in the switch.
3921 if (field_type != Primitive::kPrimLong) {
3922 codegen_->MaybeRecordImplicitNullCheck(instruction);
3923 }
3924
Calin Juravle52c48962014-12-16 17:02:57 +00003925 if (is_volatile) {
3926 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3927 }
Roland Levillain4d027112015-07-01 15:41:14 +01003928
3929 if (field_type == Primitive::kPrimNot) {
3930 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3931 }
Calin Juravle52c48962014-12-16 17:02:57 +00003932}
3933
3934void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3935 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3936
3937 LocationSummary* locations =
3938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3939 locations->SetInAt(0, Location::RequiresRegister());
3940 bool is_volatile = field_info.IsVolatile();
3941 Primitive::Type field_type = field_info.GetFieldType();
3942 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3943 || (field_type == Primitive::kPrimByte);
3944
3945 // The register allocator does not support multiple
3946 // inputs that die at entry with one in a specific register.
3947 if (is_byte_type) {
3948 // Ensure the value is in a byte register.
3949 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003950 } else if (Primitive::IsFloatingPointType(field_type)) {
3951 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003952 } else {
3953 locations->SetInAt(1, Location::RequiresRegister());
3954 }
Calin Juravle52c48962014-12-16 17:02:57 +00003955 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003956 // Temporary registers for the write barrier.
3957 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003958 // Ensure the card is in a byte register.
3959 locations->AddTemp(Location::RegisterLocation(ECX));
3960 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3961 // 64bits value can be atomically written to an address with movsd and an XMM register.
3962 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3963 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3964 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3965 // isolated cases when we need this it isn't worth adding the extra complexity.
3966 locations->AddTemp(Location::RequiresFpuRegister());
3967 locations->AddTemp(Location::RequiresFpuRegister());
3968 }
3969}
3970
3971void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003972 const FieldInfo& field_info,
3973 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003974 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3975
3976 LocationSummary* locations = instruction->GetLocations();
3977 Register base = locations->InAt(0).AsRegister<Register>();
3978 Location value = locations->InAt(1);
3979 bool is_volatile = field_info.IsVolatile();
3980 Primitive::Type field_type = field_info.GetFieldType();
3981 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003982 bool needs_write_barrier =
3983 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003984
3985 if (is_volatile) {
3986 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3987 }
3988
3989 switch (field_type) {
3990 case Primitive::kPrimBoolean:
3991 case Primitive::kPrimByte: {
3992 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3993 break;
3994 }
3995
3996 case Primitive::kPrimShort:
3997 case Primitive::kPrimChar: {
3998 __ movw(Address(base, offset), value.AsRegister<Register>());
3999 break;
4000 }
4001
4002 case Primitive::kPrimInt:
4003 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004004 if (kPoisonHeapReferences && needs_write_barrier) {
4005 // Note that in the case where `value` is a null reference,
4006 // we do not enter this block, as the reference does not
4007 // need poisoning.
4008 DCHECK_EQ(field_type, Primitive::kPrimNot);
4009 Register temp = locations->GetTemp(0).AsRegister<Register>();
4010 __ movl(temp, value.AsRegister<Register>());
4011 __ PoisonHeapReference(temp);
4012 __ movl(Address(base, offset), temp);
4013 } else {
4014 __ movl(Address(base, offset), value.AsRegister<Register>());
4015 }
Calin Juravle52c48962014-12-16 17:02:57 +00004016 break;
4017 }
4018
4019 case Primitive::kPrimLong: {
4020 if (is_volatile) {
4021 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4022 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4023 __ movd(temp1, value.AsRegisterPairLow<Register>());
4024 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4025 __ punpckldq(temp1, temp2);
4026 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004028 } else {
4029 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004030 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004031 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4032 }
4033 break;
4034 }
4035
4036 case Primitive::kPrimFloat: {
4037 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4038 break;
4039 }
4040
4041 case Primitive::kPrimDouble: {
4042 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4043 break;
4044 }
4045
4046 case Primitive::kPrimVoid:
4047 LOG(FATAL) << "Unreachable type " << field_type;
4048 UNREACHABLE();
4049 }
4050
Calin Juravle77520bc2015-01-12 18:45:46 +00004051 // Longs are handled in the switch.
4052 if (field_type != Primitive::kPrimLong) {
4053 codegen_->MaybeRecordImplicitNullCheck(instruction);
4054 }
4055
Roland Levillain4d027112015-07-01 15:41:14 +01004056 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004057 Register temp = locations->GetTemp(0).AsRegister<Register>();
4058 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004059 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004060 }
4061
Calin Juravle52c48962014-12-16 17:02:57 +00004062 if (is_volatile) {
4063 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4064 }
4065}
4066
4067void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4068 HandleFieldGet(instruction, instruction->GetFieldInfo());
4069}
4070
4071void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4072 HandleFieldGet(instruction, instruction->GetFieldInfo());
4073}
4074
4075void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4076 HandleFieldSet(instruction, instruction->GetFieldInfo());
4077}
4078
4079void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004080 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004081}
4082
4083void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4084 HandleFieldSet(instruction, instruction->GetFieldInfo());
4085}
4086
4087void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004088 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004089}
4090
4091void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4092 HandleFieldGet(instruction, instruction->GetFieldInfo());
4093}
4094
4095void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4096 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004097}
4098
Calin Juravle23a8e352015-09-08 19:56:31 +01004099void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4100 HUnresolvedInstanceFieldGet* instruction) {
4101 FieldAccessCallingConvetionX86 calling_convention;
4102 codegen_->CreateUnresolvedFieldLocationSummary(
4103 instruction, instruction->GetFieldType(), calling_convention);
4104}
4105
4106void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4107 HUnresolvedInstanceFieldGet* instruction) {
4108 codegen_->GenerateUnresolvedFieldAccess(instruction,
4109 instruction->GetFieldType(),
4110 instruction->GetFieldIndex(),
4111 instruction->GetDexPc());
4112}
4113
4114void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4115 HUnresolvedInstanceFieldSet* instruction) {
4116 FieldAccessCallingConvetionX86 calling_convention;
4117 codegen_->CreateUnresolvedFieldLocationSummary(
4118 instruction, instruction->GetFieldType(), calling_convention);
4119}
4120
4121void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4122 HUnresolvedInstanceFieldSet* instruction) {
4123 codegen_->GenerateUnresolvedFieldAccess(instruction,
4124 instruction->GetFieldType(),
4125 instruction->GetFieldIndex(),
4126 instruction->GetDexPc());
4127}
4128
4129void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4130 HUnresolvedStaticFieldGet* instruction) {
4131 FieldAccessCallingConvetionX86 calling_convention;
4132 codegen_->CreateUnresolvedFieldLocationSummary(
4133 instruction, instruction->GetFieldType(), calling_convention);
4134}
4135
4136void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4137 HUnresolvedStaticFieldGet* instruction) {
4138 codegen_->GenerateUnresolvedFieldAccess(instruction,
4139 instruction->GetFieldType(),
4140 instruction->GetFieldIndex(),
4141 instruction->GetDexPc());
4142}
4143
4144void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4145 HUnresolvedStaticFieldSet* instruction) {
4146 FieldAccessCallingConvetionX86 calling_convention;
4147 codegen_->CreateUnresolvedFieldLocationSummary(
4148 instruction, instruction->GetFieldType(), calling_convention);
4149}
4150
4151void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4152 HUnresolvedStaticFieldSet* instruction) {
4153 codegen_->GenerateUnresolvedFieldAccess(instruction,
4154 instruction->GetFieldType(),
4155 instruction->GetFieldIndex(),
4156 instruction->GetDexPc());
4157}
4158
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004159void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004160 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4161 ? LocationSummary::kCallOnSlowPath
4162 : LocationSummary::kNoCall;
4163 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4164 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004165 ? Location::RequiresRegister()
4166 : Location::Any();
4167 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004168 if (instruction->HasUses()) {
4169 locations->SetOut(Location::SameAsFirstInput());
4170 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004171}
4172
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004173void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004174 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4175 return;
4176 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004177 LocationSummary* locations = instruction->GetLocations();
4178 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004179
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004180 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4181 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4182}
4183
4184void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01004185 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004186 codegen_->AddSlowPath(slow_path);
4187
4188 LocationSummary* locations = instruction->GetLocations();
4189 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004190
4191 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004192 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004193 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004194 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004195 } else {
4196 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004197 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004198 __ jmp(slow_path->GetEntryLabel());
4199 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004200 }
4201 __ j(kEqual, slow_path->GetEntryLabel());
4202}
4203
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004204void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004205 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004206 GenerateImplicitNullCheck(instruction);
4207 } else {
4208 GenerateExplicitNullCheck(instruction);
4209 }
4210}
4211
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004213 LocationSummary* locations =
4214 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004215 locations->SetInAt(0, Location::RequiresRegister());
4216 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004217 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4218 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4219 } else {
4220 // The output overlaps in case of long: we don't want the low move to overwrite
4221 // the array's location.
4222 locations->SetOut(Location::RequiresRegister(),
4223 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4224 : Location::kNoOutputOverlap);
4225 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004226}
4227
4228void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4229 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004230 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004231 Location index = locations->InAt(1);
4232
Calin Juravle77520bc2015-01-12 18:45:46 +00004233 Primitive::Type type = instruction->GetType();
4234 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004235 case Primitive::kPrimBoolean: {
4236 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004237 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004238 if (index.IsConstant()) {
4239 __ movzxb(out, Address(obj,
4240 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4241 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004242 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004243 }
4244 break;
4245 }
4246
4247 case Primitive::kPrimByte: {
4248 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004249 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250 if (index.IsConstant()) {
4251 __ movsxb(out, Address(obj,
4252 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4253 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004254 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004255 }
4256 break;
4257 }
4258
4259 case Primitive::kPrimShort: {
4260 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004261 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004262 if (index.IsConstant()) {
4263 __ movsxw(out, Address(obj,
4264 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4265 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004266 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004267 }
4268 break;
4269 }
4270
4271 case Primitive::kPrimChar: {
4272 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004273 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004274 if (index.IsConstant()) {
4275 __ movzxw(out, Address(obj,
4276 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4277 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004278 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004279 }
4280 break;
4281 }
4282
4283 case Primitive::kPrimInt:
4284 case Primitive::kPrimNot: {
4285 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004286 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004287 if (index.IsConstant()) {
4288 __ movl(out, Address(obj,
4289 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4290 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004291 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004292 }
4293 break;
4294 }
4295
4296 case Primitive::kPrimLong: {
4297 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004298 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004299 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004300 if (index.IsConstant()) {
4301 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004302 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004303 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004304 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004305 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004306 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004307 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004308 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004309 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004310 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004311 }
4312 break;
4313 }
4314
Mark Mendell7c8d0092015-01-26 11:21:33 -05004315 case Primitive::kPrimFloat: {
4316 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4317 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4318 if (index.IsConstant()) {
4319 __ movss(out, Address(obj,
4320 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4321 } else {
4322 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4323 }
4324 break;
4325 }
4326
4327 case Primitive::kPrimDouble: {
4328 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4329 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4330 if (index.IsConstant()) {
4331 __ movsd(out, Address(obj,
4332 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4333 } else {
4334 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4335 }
4336 break;
4337 }
4338
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004339 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004340 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004341 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004342 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004343
4344 if (type != Primitive::kPrimLong) {
4345 codegen_->MaybeRecordImplicitNullCheck(instruction);
4346 }
Roland Levillain4d027112015-07-01 15:41:14 +01004347
4348 if (type == Primitive::kPrimNot) {
4349 Register out = locations->Out().AsRegister<Register>();
4350 __ MaybeUnpoisonHeapReference(out);
4351 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004352}
4353
4354void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004355 // This location builder might end up asking to up to four registers, which is
4356 // not currently possible for baseline. The situation in which we need four
4357 // registers cannot be met by baseline though, because it has not run any
4358 // optimization.
4359
Nicolas Geoffray39468442014-09-02 15:17:15 +01004360 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004361 bool needs_write_barrier =
4362 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4363
Mark Mendell5f874182015-03-04 15:42:45 -05004364 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004365
Nicolas Geoffray39468442014-09-02 15:17:15 +01004366 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4367 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004368 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004369
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004370 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004371 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004372 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4373 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4374 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004375 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004376 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4377 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004378 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004379 // In case of a byte operation, the register allocator does not support multiple
4380 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004381 locations->SetInAt(0, Location::RequiresRegister());
4382 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004383 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004384 // Ensure the value is in a byte register.
4385 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004386 } else if (Primitive::IsFloatingPointType(value_type)) {
4387 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004388 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004389 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004390 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004391 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004392 // Temporary registers for the write barrier.
4393 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004394 // Ensure the card is in a byte register.
4395 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004396 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004397 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004398}
4399
4400void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4401 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004402 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004404 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004405 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004406 bool needs_runtime_call = locations->WillCall();
4407 bool needs_write_barrier =
4408 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004409
4410 switch (value_type) {
4411 case Primitive::kPrimBoolean:
4412 case Primitive::kPrimByte: {
4413 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004414 if (index.IsConstant()) {
4415 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004416 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004417 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004418 } else {
4419 __ movb(Address(obj, offset),
4420 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4421 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004422 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004423 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004424 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004425 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004426 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004427 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004428 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4429 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004430 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004431 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004432 break;
4433 }
4434
4435 case Primitive::kPrimShort:
4436 case Primitive::kPrimChar: {
4437 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004438 if (index.IsConstant()) {
4439 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004440 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004441 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004442 } else {
4443 __ movw(Address(obj, offset),
4444 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4445 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004446 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004447 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004448 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4449 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004450 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004451 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004452 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4453 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004454 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004455 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004456 break;
4457 }
4458
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004459 case Primitive::kPrimInt:
4460 case Primitive::kPrimNot: {
4461 if (!needs_runtime_call) {
4462 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4463 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004464 size_t offset =
4465 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004466 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004467 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4468 Register temp = locations->GetTemp(0).AsRegister<Register>();
4469 __ movl(temp, value.AsRegister<Register>());
4470 __ PoisonHeapReference(temp);
4471 __ movl(Address(obj, offset), temp);
4472 } else {
4473 __ movl(Address(obj, offset), value.AsRegister<Register>());
4474 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004475 } else {
4476 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004477 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4478 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4479 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4480 // Note: if heap poisoning is enabled, no need to poison
4481 // (negate) `v` if it is a reference, as it would be null.
4482 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004483 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004484 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004485 DCHECK(index.IsRegister()) << index;
4486 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004487 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4488 Register temp = locations->GetTemp(0).AsRegister<Register>();
4489 __ movl(temp, value.AsRegister<Register>());
4490 __ PoisonHeapReference(temp);
4491 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4492 } else {
4493 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4494 value.AsRegister<Register>());
4495 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004496 } else {
4497 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004498 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4499 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4500 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4501 // Note: if heap poisoning is enabled, no need to poison
4502 // (negate) `v` if it is a reference, as it would be null.
4503 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004504 }
4505 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004506 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004507
4508 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004509 Register temp = locations->GetTemp(0).AsRegister<Register>();
4510 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004511 codegen_->MarkGCCard(
4512 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004513 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004514 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004515 DCHECK_EQ(value_type, Primitive::kPrimNot);
4516 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004517 // Note: if heap poisoning is enabled, pAputObject takes cares
4518 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004519 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4520 instruction,
4521 instruction->GetDexPc(),
4522 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004523 }
4524 break;
4525 }
4526
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004527 case Primitive::kPrimLong: {
4528 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 if (index.IsConstant()) {
4530 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004531 if (value.IsRegisterPair()) {
4532 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004533 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004534 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004535 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004536 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004537 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4538 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004539 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004540 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4541 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004542 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004543 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004544 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004545 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004546 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004547 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004548 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004549 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004550 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004551 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004552 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004553 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004554 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004555 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004556 Immediate(High32Bits(val)));
4557 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004558 }
4559 break;
4560 }
4561
Mark Mendell7c8d0092015-01-26 11:21:33 -05004562 case Primitive::kPrimFloat: {
4563 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4564 DCHECK(value.IsFpuRegister());
4565 if (index.IsConstant()) {
4566 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4567 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4568 } else {
4569 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4570 value.AsFpuRegister<XmmRegister>());
4571 }
4572 break;
4573 }
4574
4575 case Primitive::kPrimDouble: {
4576 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4577 DCHECK(value.IsFpuRegister());
4578 if (index.IsConstant()) {
4579 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4580 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4581 } else {
4582 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4583 value.AsFpuRegister<XmmRegister>());
4584 }
4585 break;
4586 }
4587
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588 case Primitive::kPrimVoid:
4589 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004590 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591 }
4592}
4593
4594void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004596 locations->SetInAt(0, Location::RequiresRegister());
4597 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004598}
4599
4600void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4601 LocationSummary* locations = instruction->GetLocations();
4602 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004603 Register obj = locations->InAt(0).AsRegister<Register>();
4604 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004606 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004607}
4608
4609void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004610 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4611 ? LocationSummary::kCallOnSlowPath
4612 : LocationSummary::kNoCall;
4613 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004614 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004615 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004616 if (instruction->HasUses()) {
4617 locations->SetOut(Location::SameAsFirstInput());
4618 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004619}
4620
4621void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4622 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004623 Location index_loc = locations->InAt(0);
4624 Location length_loc = locations->InAt(1);
4625 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004626 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004627
Mark Mendell99dbd682015-04-22 16:18:52 -04004628 if (length_loc.IsConstant()) {
4629 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4630 if (index_loc.IsConstant()) {
4631 // BCE will remove the bounds check if we are guarenteed to pass.
4632 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4633 if (index < 0 || index >= length) {
4634 codegen_->AddSlowPath(slow_path);
4635 __ jmp(slow_path->GetEntryLabel());
4636 } else {
4637 // Some optimization after BCE may have generated this, and we should not
4638 // generate a bounds check if it is a valid range.
4639 }
4640 return;
4641 }
4642
4643 // We have to reverse the jump condition because the length is the constant.
4644 Register index_reg = index_loc.AsRegister<Register>();
4645 __ cmpl(index_reg, Immediate(length));
4646 codegen_->AddSlowPath(slow_path);
4647 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004648 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004649 Register length = length_loc.AsRegister<Register>();
4650 if (index_loc.IsConstant()) {
4651 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4652 __ cmpl(length, Immediate(value));
4653 } else {
4654 __ cmpl(length, index_loc.AsRegister<Register>());
4655 }
4656 codegen_->AddSlowPath(slow_path);
4657 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004658 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004659}
4660
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004661void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4662 temp->SetLocations(nullptr);
4663}
4664
4665void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4666 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004667 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668}
4669
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004670void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004671 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004672 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004673}
4674
4675void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004676 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4677}
4678
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004679void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4680 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4681}
4682
4683void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004684 HBasicBlock* block = instruction->GetBlock();
4685 if (block->GetLoopInformation() != nullptr) {
4686 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4687 // The back edge will generate the suspend check.
4688 return;
4689 }
4690 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4691 // The goto will generate the suspend check.
4692 return;
4693 }
4694 GenerateSuspendCheck(instruction, nullptr);
4695}
4696
4697void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4698 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004699 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004700 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4701 if (slow_path == nullptr) {
4702 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4703 instruction->SetSlowPath(slow_path);
4704 codegen_->AddSlowPath(slow_path);
4705 if (successor != nullptr) {
4706 DCHECK(successor->IsLoopHeader());
4707 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4708 }
4709 } else {
4710 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4711 }
4712
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004713 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004714 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004715 if (successor == nullptr) {
4716 __ j(kNotEqual, slow_path->GetEntryLabel());
4717 __ Bind(slow_path->GetReturnLabel());
4718 } else {
4719 __ j(kEqual, codegen_->GetLabelOf(successor));
4720 __ jmp(slow_path->GetEntryLabel());
4721 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004722}
4723
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004724X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4725 return codegen_->GetAssembler();
4726}
4727
Mark Mendell7c8d0092015-01-26 11:21:33 -05004728void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004729 ScratchRegisterScope ensure_scratch(
4730 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4731 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4732 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4733 __ movl(temp_reg, Address(ESP, src + stack_offset));
4734 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004735}
4736
4737void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004738 ScratchRegisterScope ensure_scratch(
4739 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4740 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4741 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4742 __ movl(temp_reg, Address(ESP, src + stack_offset));
4743 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4744 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4745 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004746}
4747
4748void ParallelMoveResolverX86::EmitMove(size_t index) {
4749 MoveOperands* move = moves_.Get(index);
4750 Location source = move->GetSource();
4751 Location destination = move->GetDestination();
4752
4753 if (source.IsRegister()) {
4754 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004755 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004756 } else {
4757 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004758 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004759 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004760 } else if (source.IsFpuRegister()) {
4761 if (destination.IsFpuRegister()) {
4762 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4763 } else if (destination.IsStackSlot()) {
4764 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4765 } else {
4766 DCHECK(destination.IsDoubleStackSlot());
4767 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4768 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004769 } else if (source.IsStackSlot()) {
4770 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004771 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004772 } else if (destination.IsFpuRegister()) {
4773 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004774 } else {
4775 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004776 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4777 }
4778 } else if (source.IsDoubleStackSlot()) {
4779 if (destination.IsFpuRegister()) {
4780 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4781 } else {
4782 DCHECK(destination.IsDoubleStackSlot()) << destination;
4783 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004784 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004785 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004786 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004787 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004788 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004789 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004790 if (value == 0) {
4791 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4792 } else {
4793 __ movl(destination.AsRegister<Register>(), Immediate(value));
4794 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004795 } else {
4796 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004797 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004798 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004799 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004800 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004801 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004802 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004803 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004804 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4805 if (value == 0) {
4806 // Easy handling of 0.0.
4807 __ xorps(dest, dest);
4808 } else {
4809 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004810 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4811 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4812 __ movl(temp, Immediate(value));
4813 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004814 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004815 } else {
4816 DCHECK(destination.IsStackSlot()) << destination;
4817 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4818 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004819 } else if (constant->IsLongConstant()) {
4820 int64_t value = constant->AsLongConstant()->GetValue();
4821 int32_t low_value = Low32Bits(value);
4822 int32_t high_value = High32Bits(value);
4823 Immediate low(low_value);
4824 Immediate high(high_value);
4825 if (destination.IsDoubleStackSlot()) {
4826 __ movl(Address(ESP, destination.GetStackIndex()), low);
4827 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4828 } else {
4829 __ movl(destination.AsRegisterPairLow<Register>(), low);
4830 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4831 }
4832 } else {
4833 DCHECK(constant->IsDoubleConstant());
4834 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004835 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004836 int32_t low_value = Low32Bits(value);
4837 int32_t high_value = High32Bits(value);
4838 Immediate low(low_value);
4839 Immediate high(high_value);
4840 if (destination.IsFpuRegister()) {
4841 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4842 if (value == 0) {
4843 // Easy handling of 0.0.
4844 __ xorpd(dest, dest);
4845 } else {
4846 __ pushl(high);
4847 __ pushl(low);
4848 __ movsd(dest, Address(ESP, 0));
4849 __ addl(ESP, Immediate(8));
4850 }
4851 } else {
4852 DCHECK(destination.IsDoubleStackSlot()) << destination;
4853 __ movl(Address(ESP, destination.GetStackIndex()), low);
4854 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4855 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004856 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004857 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004858 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004859 }
4860}
4861
Mark Mendella5c19ce2015-04-01 12:51:05 -04004862void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004863 Register suggested_scratch = reg == EAX ? EBX : EAX;
4864 ScratchRegisterScope ensure_scratch(
4865 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4866
4867 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4868 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4869 __ movl(Address(ESP, mem + stack_offset), reg);
4870 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004871}
4872
Mark Mendell7c8d0092015-01-26 11:21:33 -05004873void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004874 ScratchRegisterScope ensure_scratch(
4875 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4876
4877 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4878 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4879 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4880 __ movss(Address(ESP, mem + stack_offset), reg);
4881 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004882}
4883
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004884void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004885 ScratchRegisterScope ensure_scratch1(
4886 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004887
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004888 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4889 ScratchRegisterScope ensure_scratch2(
4890 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004891
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004892 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4893 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4894 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4895 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4896 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4897 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004898}
4899
4900void ParallelMoveResolverX86::EmitSwap(size_t index) {
4901 MoveOperands* move = moves_.Get(index);
4902 Location source = move->GetSource();
4903 Location destination = move->GetDestination();
4904
4905 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004906 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4907 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4908 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4909 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4910 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004911 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004912 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004913 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004914 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004915 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4916 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004917 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4918 // Use XOR Swap algorithm to avoid a temporary.
4919 DCHECK_NE(source.reg(), destination.reg());
4920 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4921 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4922 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4923 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4924 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4925 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4926 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004927 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4928 // Take advantage of the 16 bytes in the XMM register.
4929 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4930 Address stack(ESP, destination.GetStackIndex());
4931 // Load the double into the high doubleword.
4932 __ movhpd(reg, stack);
4933
4934 // Store the low double into the destination.
4935 __ movsd(stack, reg);
4936
4937 // Move the high double to the low double.
4938 __ psrldq(reg, Immediate(8));
4939 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4940 // Take advantage of the 16 bytes in the XMM register.
4941 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4942 Address stack(ESP, source.GetStackIndex());
4943 // Load the double into the high doubleword.
4944 __ movhpd(reg, stack);
4945
4946 // Store the low double into the destination.
4947 __ movsd(stack, reg);
4948
4949 // Move the high double to the low double.
4950 __ psrldq(reg, Immediate(8));
4951 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4952 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4953 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004954 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004955 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004956 }
4957}
4958
4959void ParallelMoveResolverX86::SpillScratch(int reg) {
4960 __ pushl(static_cast<Register>(reg));
4961}
4962
4963void ParallelMoveResolverX86::RestoreScratch(int reg) {
4964 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004965}
4966
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004967void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004968 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4969 ? LocationSummary::kCallOnSlowPath
4970 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004971 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004972 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004973 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004974 locations->SetOut(Location::RequiresRegister());
4975}
4976
4977void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004978 LocationSummary* locations = cls->GetLocations();
4979 Register out = locations->Out().AsRegister<Register>();
4980 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004981 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004982 DCHECK(!cls->CanCallRuntime());
4983 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004984 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004985 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004986 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004987 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004988 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004989 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004990 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004991
4992 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4993 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4994 codegen_->AddSlowPath(slow_path);
4995 __ testl(out, out);
4996 __ j(kEqual, slow_path->GetEntryLabel());
4997 if (cls->MustGenerateClinitCheck()) {
4998 GenerateClassInitializationCheck(slow_path, out);
4999 } else {
5000 __ Bind(slow_path->GetExitLabel());
5001 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005002 }
5003}
5004
5005void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5006 LocationSummary* locations =
5007 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5008 locations->SetInAt(0, Location::RequiresRegister());
5009 if (check->HasUses()) {
5010 locations->SetOut(Location::SameAsFirstInput());
5011 }
5012}
5013
5014void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005015 // We assume the class to not be null.
5016 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5017 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005018 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005019 GenerateClassInitializationCheck(slow_path,
5020 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005021}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005022
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005023void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
5024 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005025 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5026 Immediate(mirror::Class::kStatusInitialized));
5027 __ j(kLess, slow_path->GetEntryLabel());
5028 __ Bind(slow_path->GetExitLabel());
5029 // No need for memory fence, thanks to the X86 memory model.
5030}
5031
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005032void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5033 LocationSummary* locations =
5034 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005035 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005036 locations->SetOut(Location::RequiresRegister());
5037}
5038
5039void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
5040 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5041 codegen_->AddSlowPath(slow_path);
5042
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005043 LocationSummary* locations = load->GetLocations();
5044 Register out = locations->Out().AsRegister<Register>();
5045 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07005046 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08005047 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005048 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005049 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005050 __ testl(out, out);
5051 __ j(kEqual, slow_path->GetEntryLabel());
5052 __ Bind(slow_path->GetExitLabel());
5053}
5054
David Brazdilcb1c0552015-08-04 16:22:25 +01005055static Address GetExceptionTlsAddress() {
5056 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5057}
5058
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005059void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5060 LocationSummary* locations =
5061 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5062 locations->SetOut(Location::RequiresRegister());
5063}
5064
5065void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005066 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5067}
5068
5069void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5070 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5071}
5072
5073void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5074 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005075}
5076
5077void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5078 LocationSummary* locations =
5079 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5080 InvokeRuntimeCallingConvention calling_convention;
5081 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5082}
5083
5084void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005085 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5086 instruction,
5087 instruction->GetDexPc(),
5088 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005089}
5090
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005091void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005092 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5093 switch (instruction->GetTypeCheckKind()) {
5094 case TypeCheckKind::kExactCheck:
5095 case TypeCheckKind::kAbstractClassCheck:
5096 case TypeCheckKind::kClassHierarchyCheck:
5097 case TypeCheckKind::kArrayObjectCheck:
5098 call_kind = LocationSummary::kNoCall;
5099 break;
5100 case TypeCheckKind::kInterfaceCheck:
5101 call_kind = LocationSummary::kCall;
5102 break;
5103 case TypeCheckKind::kArrayCheck:
5104 call_kind = LocationSummary::kCallOnSlowPath;
5105 break;
5106 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005108 if (call_kind != LocationSummary::kCall) {
5109 locations->SetInAt(0, Location::RequiresRegister());
5110 locations->SetInAt(1, Location::Any());
5111 // Note that TypeCheckSlowPathX86 uses this register too.
5112 locations->SetOut(Location::RequiresRegister());
5113 } else {
5114 InvokeRuntimeCallingConvention calling_convention;
5115 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5116 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5117 locations->SetOut(Location::RegisterLocation(EAX));
5118 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005119}
5120
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005121void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005122 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005123 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005124 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005125 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005126 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005127 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5128 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5129 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005130 SlowPathCodeX86* slow_path = nullptr;
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005131 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005132
5133 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005134 // Avoid null check if we know obj is not null.
5135 if (instruction->MustDoNullCheck()) {
5136 __ testl(obj, obj);
5137 __ j(kEqual, &zero);
5138 }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005139
5140 // In case of an interface check, we put the object class into the object register.
5141 // This is safe, as the register is caller-save, and the object must be in another
5142 // register if it survives the runtime call.
5143 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck)
5144 ? obj
5145 : out;
5146 __ movl(target, Address(obj, class_offset));
5147 __ MaybeUnpoisonHeapReference(target);
5148
5149 switch (instruction->GetTypeCheckKind()) {
5150 case TypeCheckKind::kExactCheck: {
5151 if (cls.IsRegister()) {
5152 __ cmpl(out, cls.AsRegister<Register>());
5153 } else {
5154 DCHECK(cls.IsStackSlot()) << cls;
5155 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5156 }
5157 // Classes must be equal for the instanceof to succeed.
5158 __ j(kNotEqual, &zero);
5159 __ movl(out, Immediate(1));
5160 __ jmp(&done);
5161 break;
5162 }
5163 case TypeCheckKind::kAbstractClassCheck: {
5164 // If the class is abstract, we eagerly fetch the super class of the
5165 // object to avoid doing a comparison we know will fail.
5166 NearLabel loop;
5167 __ Bind(&loop);
5168 __ movl(out, Address(out, super_offset));
5169 __ MaybeUnpoisonHeapReference(out);
5170 __ testl(out, out);
5171 // If `out` is null, we use it for the result, and jump to `done`.
5172 __ j(kEqual, &done);
5173 if (cls.IsRegister()) {
5174 __ cmpl(out, cls.AsRegister<Register>());
5175 } else {
5176 DCHECK(cls.IsStackSlot()) << cls;
5177 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5178 }
5179 __ j(kNotEqual, &loop);
5180 __ movl(out, Immediate(1));
5181 if (zero.IsLinked()) {
5182 __ jmp(&done);
5183 }
5184 break;
5185 }
5186 case TypeCheckKind::kClassHierarchyCheck: {
5187 // Walk over the class hierarchy to find a match.
5188 NearLabel loop, success;
5189 __ Bind(&loop);
5190 if (cls.IsRegister()) {
5191 __ cmpl(out, cls.AsRegister<Register>());
5192 } else {
5193 DCHECK(cls.IsStackSlot()) << cls;
5194 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5195 }
5196 __ j(kEqual, &success);
5197 __ movl(out, Address(out, super_offset));
5198 __ MaybeUnpoisonHeapReference(out);
5199 __ testl(out, out);
5200 __ j(kNotEqual, &loop);
5201 // If `out` is null, we use it for the result, and jump to `done`.
5202 __ jmp(&done);
5203 __ Bind(&success);
5204 __ movl(out, Immediate(1));
5205 if (zero.IsLinked()) {
5206 __ jmp(&done);
5207 }
5208 break;
5209 }
5210 case TypeCheckKind::kArrayObjectCheck: {
5211 // Just need to check that the object's class is a non primitive array.
5212 __ movl(out, Address(out, component_offset));
5213 __ MaybeUnpoisonHeapReference(out);
5214 __ testl(out, out);
5215 // If `out` is null, we use it for the result, and jump to `done`.
5216 __ j(kEqual, &done);
5217 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5218 __ j(kNotEqual, &zero);
5219 __ movl(out, Immediate(1));
5220 __ jmp(&done);
5221 break;
5222 }
5223 case TypeCheckKind::kArrayCheck: {
5224 if (cls.IsRegister()) {
5225 __ cmpl(out, cls.AsRegister<Register>());
5226 } else {
5227 DCHECK(cls.IsStackSlot()) << cls;
5228 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5229 }
5230 DCHECK(locations->OnlyCallsOnSlowPath());
5231 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5232 instruction, /* is_fatal */ false);
5233 codegen_->AddSlowPath(slow_path);
5234 __ j(kNotEqual, slow_path->GetEntryLabel());
5235 __ movl(out, Immediate(1));
5236 if (zero.IsLinked()) {
5237 __ jmp(&done);
5238 }
5239 break;
5240 }
5241
5242 case TypeCheckKind::kInterfaceCheck:
5243 default: {
5244 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5245 instruction,
5246 instruction->GetDexPc(),
5247 nullptr);
5248 if (zero.IsLinked()) {
5249 __ jmp(&done);
5250 }
5251 break;
5252 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005253 }
5254
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005255 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005256 __ Bind(&zero);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005257 __ xorl(out, out);
5258 }
5259
5260 if (done.IsLinked()) {
5261 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005262 }
5263
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005264 if (slow_path != nullptr) {
5265 __ Bind(slow_path->GetExitLabel());
5266 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005267}
5268
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005269void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005270 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5271 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5272
5273 switch (instruction->GetTypeCheckKind()) {
5274 case TypeCheckKind::kExactCheck:
5275 case TypeCheckKind::kAbstractClassCheck:
5276 case TypeCheckKind::kClassHierarchyCheck:
5277 case TypeCheckKind::kArrayObjectCheck:
5278 call_kind = throws_into_catch
5279 ? LocationSummary::kCallOnSlowPath
5280 : LocationSummary::kNoCall;
5281 break;
5282 case TypeCheckKind::kInterfaceCheck:
5283 call_kind = LocationSummary::kCall;
5284 break;
5285 case TypeCheckKind::kArrayCheck:
5286 call_kind = LocationSummary::kCallOnSlowPath;
5287 break;
5288 }
5289
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005291 instruction, call_kind);
5292 if (call_kind != LocationSummary::kCall) {
5293 locations->SetInAt(0, Location::RequiresRegister());
5294 locations->SetInAt(1, Location::Any());
5295 // Note that TypeCheckSlowPathX86 uses this register too.
5296 locations->AddTemp(Location::RequiresRegister());
5297 } else {
5298 InvokeRuntimeCallingConvention calling_convention;
5299 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5300 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5301 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005302}
5303
5304void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5305 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005306 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005307 Location cls = locations->InAt(1);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005308 Register temp = locations->WillCall()
5309 ? kNoRegister
5310 : locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005311
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005312 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5313 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5314 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5315 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5316 SlowPathCodeX86* slow_path = nullptr;
5317
5318 if (!locations->WillCall()) {
5319 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5320 instruction, !locations->CanCall());
5321 codegen_->AddSlowPath(slow_path);
5322 }
5323
5324 NearLabel done, abstract_entry;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005325 // Avoid null check if we know obj is not null.
5326 if (instruction->MustDoNullCheck()) {
5327 __ testl(obj, obj);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005328 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005329 }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005330
5331 if (locations->WillCall()) {
5332 __ movl(obj, Address(obj, class_offset));
5333 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005334 } else {
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005335 __ movl(temp, Address(obj, class_offset));
5336 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005337 }
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005338
5339 switch (instruction->GetTypeCheckKind()) {
5340 case TypeCheckKind::kExactCheck:
5341 case TypeCheckKind::kArrayCheck: {
5342 if (cls.IsRegister()) {
5343 __ cmpl(temp, cls.AsRegister<Register>());
5344 } else {
5345 DCHECK(cls.IsStackSlot()) << cls;
5346 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5347 }
5348 // Jump to slow path for throwing the exception or doing a
5349 // more involved array check.
5350 __ j(kNotEqual, slow_path->GetEntryLabel());
5351 break;
5352 }
5353 case TypeCheckKind::kAbstractClassCheck: {
5354 // If the class is abstract, we eagerly fetch the super class of the
5355 // object to avoid doing a comparison we know will fail.
5356 NearLabel loop, success;
5357 __ Bind(&loop);
5358 __ movl(temp, Address(temp, super_offset));
5359 __ MaybeUnpoisonHeapReference(temp);
5360 __ testl(temp, temp);
5361 // Jump to the slow path to throw the exception.
5362 __ j(kEqual, slow_path->GetEntryLabel());
5363 if (cls.IsRegister()) {
5364 __ cmpl(temp, cls.AsRegister<Register>());
5365 } else {
5366 DCHECK(cls.IsStackSlot()) << cls;
5367 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5368 }
5369 __ j(kNotEqual, &loop);
5370 break;
5371 }
5372 case TypeCheckKind::kClassHierarchyCheck: {
5373 // Walk over the class hierarchy to find a match.
5374 NearLabel loop, success;
5375 __ Bind(&loop);
5376 if (cls.IsRegister()) {
5377 __ cmpl(temp, cls.AsRegister<Register>());
5378 } else {
5379 DCHECK(cls.IsStackSlot()) << cls;
5380 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5381 }
5382 __ j(kEqual, &success);
5383 __ movl(temp, Address(temp, super_offset));
5384 __ MaybeUnpoisonHeapReference(temp);
5385 __ testl(temp, temp);
5386 __ j(kNotEqual, &loop);
5387 // Jump to the slow path to throw the exception.
5388 __ jmp(slow_path->GetEntryLabel());
5389 __ Bind(&success);
5390 break;
5391 }
5392 case TypeCheckKind::kArrayObjectCheck: {
5393 // Just need to check that the object's class is a non primitive array.
5394 __ movl(temp, Address(temp, component_offset));
5395 __ MaybeUnpoisonHeapReference(temp);
5396 __ testl(temp, temp);
5397 __ j(kEqual, slow_path->GetEntryLabel());
5398 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5399 __ j(kNotEqual, slow_path->GetEntryLabel());
5400 break;
5401 }
5402 case TypeCheckKind::kInterfaceCheck:
5403 default:
5404 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5405 instruction,
5406 instruction->GetDexPc(),
5407 nullptr);
5408 break;
5409 }
5410 __ Bind(&done);
5411
5412 if (slow_path != nullptr) {
5413 __ Bind(slow_path->GetExitLabel());
5414 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005415}
5416
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005417void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5418 LocationSummary* locations =
5419 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5420 InvokeRuntimeCallingConvention calling_convention;
5421 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5422}
5423
5424void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005425 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5426 : QUICK_ENTRY_POINT(pUnlockObject),
5427 instruction,
5428 instruction->GetDexPc(),
5429 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005430}
5431
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005432void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5433void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5434void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5435
5436void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5437 LocationSummary* locations =
5438 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5439 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5440 || instruction->GetResultType() == Primitive::kPrimLong);
5441 locations->SetInAt(0, Location::RequiresRegister());
5442 locations->SetInAt(1, Location::Any());
5443 locations->SetOut(Location::SameAsFirstInput());
5444}
5445
5446void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5447 HandleBitwiseOperation(instruction);
5448}
5449
5450void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5451 HandleBitwiseOperation(instruction);
5452}
5453
5454void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5455 HandleBitwiseOperation(instruction);
5456}
5457
5458void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5459 LocationSummary* locations = instruction->GetLocations();
5460 Location first = locations->InAt(0);
5461 Location second = locations->InAt(1);
5462 DCHECK(first.Equals(locations->Out()));
5463
5464 if (instruction->GetResultType() == Primitive::kPrimInt) {
5465 if (second.IsRegister()) {
5466 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005467 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005468 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005469 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005470 } else {
5471 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005472 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005473 }
5474 } else if (second.IsConstant()) {
5475 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005476 __ andl(first.AsRegister<Register>(),
5477 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005478 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005479 __ orl(first.AsRegister<Register>(),
5480 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005481 } else {
5482 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005483 __ xorl(first.AsRegister<Register>(),
5484 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005485 }
5486 } else {
5487 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005488 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005489 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005490 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005491 } else {
5492 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005493 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005494 }
5495 }
5496 } else {
5497 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5498 if (second.IsRegisterPair()) {
5499 if (instruction->IsAnd()) {
5500 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5501 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5502 } else if (instruction->IsOr()) {
5503 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5504 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5505 } else {
5506 DCHECK(instruction->IsXor());
5507 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5508 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5509 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005510 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005511 if (instruction->IsAnd()) {
5512 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5513 __ andl(first.AsRegisterPairHigh<Register>(),
5514 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5515 } else if (instruction->IsOr()) {
5516 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5517 __ orl(first.AsRegisterPairHigh<Register>(),
5518 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5519 } else {
5520 DCHECK(instruction->IsXor());
5521 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5522 __ xorl(first.AsRegisterPairHigh<Register>(),
5523 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5524 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005525 } else {
5526 DCHECK(second.IsConstant()) << second;
5527 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005528 int32_t low_value = Low32Bits(value);
5529 int32_t high_value = High32Bits(value);
5530 Immediate low(low_value);
5531 Immediate high(high_value);
5532 Register first_low = first.AsRegisterPairLow<Register>();
5533 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005534 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005535 if (low_value == 0) {
5536 __ xorl(first_low, first_low);
5537 } else if (low_value != -1) {
5538 __ andl(first_low, low);
5539 }
5540 if (high_value == 0) {
5541 __ xorl(first_high, first_high);
5542 } else if (high_value != -1) {
5543 __ andl(first_high, high);
5544 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005545 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005546 if (low_value != 0) {
5547 __ orl(first_low, low);
5548 }
5549 if (high_value != 0) {
5550 __ orl(first_high, high);
5551 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005552 } else {
5553 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005554 if (low_value != 0) {
5555 __ xorl(first_low, low);
5556 }
5557 if (high_value != 0) {
5558 __ xorl(first_high, high);
5559 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005560 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005561 }
5562 }
5563}
5564
Calin Juravleb1498f62015-02-16 13:13:29 +00005565void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5566 // Nothing to do, this should be removed during prepare for register allocator.
5567 UNUSED(instruction);
5568 LOG(FATAL) << "Unreachable";
5569}
5570
5571void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5572 // Nothing to do, this should be removed during prepare for register allocator.
5573 UNUSED(instruction);
5574 LOG(FATAL) << "Unreachable";
5575}
5576
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005577void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5578 DCHECK(codegen_->IsBaseline());
5579 LocationSummary* locations =
5580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5581 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5582}
5583
5584void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5585 DCHECK(codegen_->IsBaseline());
5586 // Will be generated at use site.
5587}
5588
Mark Mendell0616ae02015-04-17 12:49:27 -04005589void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5590 HX86ComputeBaseMethodAddress* insn) {
5591 LocationSummary* locations =
5592 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5593 locations->SetOut(Location::RequiresRegister());
5594}
5595
5596void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5597 HX86ComputeBaseMethodAddress* insn) {
5598 LocationSummary* locations = insn->GetLocations();
5599 Register reg = locations->Out().AsRegister<Register>();
5600
5601 // Generate call to next instruction.
5602 Label next_instruction;
5603 __ call(&next_instruction);
5604 __ Bind(&next_instruction);
5605
5606 // Remember this offset for later use with constant area.
5607 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5608
5609 // Grab the return address off the stack.
5610 __ popl(reg);
5611}
5612
5613void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5614 HX86LoadFromConstantTable* insn) {
5615 LocationSummary* locations =
5616 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5617
5618 locations->SetInAt(0, Location::RequiresRegister());
5619 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5620
5621 // If we don't need to be materialized, we only need the inputs to be set.
5622 if (!insn->NeedsMaterialization()) {
5623 return;
5624 }
5625
5626 switch (insn->GetType()) {
5627 case Primitive::kPrimFloat:
5628 case Primitive::kPrimDouble:
5629 locations->SetOut(Location::RequiresFpuRegister());
5630 break;
5631
5632 case Primitive::kPrimInt:
5633 locations->SetOut(Location::RequiresRegister());
5634 break;
5635
5636 default:
5637 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5638 }
5639}
5640
5641void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5642 if (!insn->NeedsMaterialization()) {
5643 return;
5644 }
5645
5646 LocationSummary* locations = insn->GetLocations();
5647 Location out = locations->Out();
5648 Register const_area = locations->InAt(0).AsRegister<Register>();
5649 HConstant *value = insn->GetConstant();
5650
5651 switch (insn->GetType()) {
5652 case Primitive::kPrimFloat:
5653 __ movss(out.AsFpuRegister<XmmRegister>(),
5654 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5655 break;
5656
5657 case Primitive::kPrimDouble:
5658 __ movsd(out.AsFpuRegister<XmmRegister>(),
5659 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5660 break;
5661
5662 case Primitive::kPrimInt:
5663 __ movl(out.AsRegister<Register>(),
5664 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5665 break;
5666
5667 default:
5668 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5669 }
5670}
5671
5672void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5673 // Generate the constant area if needed.
5674 X86Assembler* assembler = GetAssembler();
5675 if (!assembler->IsConstantAreaEmpty()) {
5676 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5677 // byte values.
5678 assembler->Align(4, 0);
5679 constant_area_start_ = assembler->CodeSize();
5680 assembler->AddConstantArea();
5681 }
5682
5683 // And finish up.
5684 CodeGenerator::Finalize(allocator);
5685}
5686
5687/**
5688 * Class to handle late fixup of offsets into constant area.
5689 */
5690class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5691 public:
5692 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5693 : codegen_(codegen), offset_into_constant_area_(offset) {}
5694
5695 private:
5696 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5697 // Patch the correct offset for the instruction. The place to patch is the
5698 // last 4 bytes of the instruction.
5699 // The value to patch is the distance from the offset in the constant area
5700 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5701 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5702 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5703
5704 // Patch in the right value.
5705 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5706 }
5707
5708 const CodeGeneratorX86& codegen_;
5709
5710 // Location in constant area that the fixup refers to.
5711 int offset_into_constant_area_;
5712};
5713
5714Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5715 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5716 return Address(reg, kDummy32BitOffset, fixup);
5717}
5718
5719Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5720 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5721 return Address(reg, kDummy32BitOffset, fixup);
5722}
5723
5724Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5725 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5726 return Address(reg, kDummy32BitOffset, fixup);
5727}
5728
5729Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5730 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5731 return Address(reg, kDummy32BitOffset, fixup);
5732}
5733
5734/**
5735 * Finds instructions that need the constant area base as an input.
5736 */
5737class ConstantHandlerVisitor : public HGraphVisitor {
5738 public:
5739 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5740
5741 private:
5742 void VisitAdd(HAdd* add) OVERRIDE {
5743 BinaryFP(add);
5744 }
5745
5746 void VisitSub(HSub* sub) OVERRIDE {
5747 BinaryFP(sub);
5748 }
5749
5750 void VisitMul(HMul* mul) OVERRIDE {
5751 BinaryFP(mul);
5752 }
5753
5754 void VisitDiv(HDiv* div) OVERRIDE {
5755 BinaryFP(div);
5756 }
5757
5758 void VisitReturn(HReturn* ret) OVERRIDE {
5759 HConstant* value = ret->InputAt(0)->AsConstant();
5760 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5761 ReplaceInput(ret, value, 0, true);
5762 }
5763 }
5764
5765 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5766 HandleInvoke(invoke);
5767 }
5768
5769 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5770 HandleInvoke(invoke);
5771 }
5772
5773 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5774 HandleInvoke(invoke);
5775 }
5776
5777 void BinaryFP(HBinaryOperation* bin) {
5778 HConstant* rhs = bin->InputAt(1)->AsConstant();
5779 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5780 ReplaceInput(bin, rhs, 1, false);
5781 }
5782 }
5783
5784 void InitializeConstantAreaPointer(HInstruction* user) {
5785 // Ensure we only initialize the pointer once.
5786 if (base_ != nullptr) {
5787 return;
5788 }
5789
5790 HGraph* graph = GetGraph();
5791 HBasicBlock* entry = graph->GetEntryBlock();
5792 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5793 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5794 entry->InsertInstructionBefore(base_, insert_pos);
5795 DCHECK(base_ != nullptr);
5796 }
5797
5798 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5799 InitializeConstantAreaPointer(insn);
5800 HGraph* graph = GetGraph();
5801 HBasicBlock* block = insn->GetBlock();
5802 HX86LoadFromConstantTable* load_constant =
5803 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5804 block->InsertInstructionBefore(load_constant, insn);
5805 insn->ReplaceInput(load_constant, input_index);
5806 }
5807
5808 void HandleInvoke(HInvoke* invoke) {
5809 // Ensure that we can load FP arguments from the constant area.
5810 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5811 HConstant* input = invoke->InputAt(i)->AsConstant();
5812 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5813 ReplaceInput(invoke, input, i, true);
5814 }
5815 }
5816 }
5817
5818 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5819 // input to the HX86LoadFromConstantTable instructions.
5820 HX86ComputeBaseMethodAddress* base_;
5821};
5822
5823void ConstantAreaFixups::Run() {
5824 ConstantHandlerVisitor visitor(graph_);
5825 visitor.VisitInsertionOrder();
5826}
5827
Roland Levillain4d027112015-07-01 15:41:14 +01005828#undef __
5829
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005830} // namespace x86
5831} // namespace art