blob: a8dbbdc343cffb34c5887c23afea5f91c88c31bf [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 Geoffray75374372015-09-17 17:12:19 +0000290 explicit TypeCheckSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000291
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000292 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000293 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100294 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
295 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000296 DCHECK(instruction_->IsCheckCast()
297 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000298
299 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
300 __ Bind(GetEntryLabel());
Nicolas Geoffray75374372015-09-17 17:12:19 +0000301 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
303 // We're moving two locations to locations that could overlap, so we need a parallel
304 // move resolver.
305 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000306 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000308 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100309 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100310 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100311 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
312 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000314 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100315 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
316 instruction_,
317 instruction_->GetDexPc(),
318 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000319 } else {
320 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100321 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
322 instruction_,
323 instruction_->GetDexPc(),
324 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 }
326
Nicolas Geoffray75374372015-09-17 17:12:19 +0000327 if (instruction_->IsInstanceOf()) {
328 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffray64acf302015-09-14 22:20:29 +0100329 }
Nicolas Geoffray75374372015-09-17 17:12:19 +0000330 RestoreLiveRegisters(codegen, locations);
331
332 __ jmp(GetExitLabel());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000333 }
334
Alexandre Rames9931f312015-06-19 14:47:01 +0100335 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
336
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000337 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 HInstruction* const instruction_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000339
340 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
341};
342
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700343class DeoptimizationSlowPathX86 : public SlowPathCodeX86 {
344 public:
345 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
346 : instruction_(instruction) {}
347
348 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100349 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100350 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700351 __ Bind(GetEntryLabel());
352 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100353 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
354 instruction_,
355 instruction_->GetDexPc(),
356 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700357 }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
360
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 private:
362 HInstruction* const instruction_;
363 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
364};
365
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100366#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100367#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100368
Roland Levillain4fa13f62015-07-06 18:11:54 +0100369inline Condition X86SignedCondition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700370 switch (cond) {
371 case kCondEQ: return kEqual;
372 case kCondNE: return kNotEqual;
373 case kCondLT: return kLess;
374 case kCondLE: return kLessEqual;
375 case kCondGT: return kGreater;
376 case kCondGE: return kGreaterEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700377 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100378 LOG(FATAL) << "Unreachable";
379 UNREACHABLE();
380}
381
382inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
383 switch (cond) {
384 case kCondEQ: return kEqual;
385 case kCondNE: return kNotEqual;
386 case kCondLT: return kBelow;
387 case kCondLE: return kBelowEqual;
388 case kCondGT: return kAbove;
389 case kCondGE: return kAboveEqual;
390 }
391 LOG(FATAL) << "Unreachable";
392 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700393}
394
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100395void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100396 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100397}
398
399void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100400 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100401}
402
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100403size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
404 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
405 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100406}
407
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100408size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
409 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
410 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100411}
412
Mark Mendell7c8d0092015-01-26 11:21:33 -0500413size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
414 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
415 return GetFloatingPointSpillSlotSize();
416}
417
418size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
419 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
420 return GetFloatingPointSpillSlotSize();
421}
422
Calin Juravle175dc732015-08-25 15:42:32 +0100423void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
424 HInstruction* instruction,
425 uint32_t dex_pc,
426 SlowPathCode* slow_path) {
427 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
428 instruction,
429 dex_pc,
430 slow_path);
431}
432
433void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100434 HInstruction* instruction,
435 uint32_t dex_pc,
436 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100437 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100438 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100439 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100440}
441
Mark Mendellfb8d2792015-03-31 22:16:59 -0400442CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
443 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100444 const CompilerOptions& compiler_options,
445 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500446 : CodeGenerator(graph,
447 kNumberOfCpuRegisters,
448 kNumberOfXmmRegisters,
449 kNumberOfRegisterPairs,
450 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
451 arraysize(kCoreCalleeSaves))
452 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100453 0,
454 compiler_options,
455 stats),
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100456 block_labels_(graph->GetArena(), 0),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100457 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100458 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400459 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000460 isa_features_(isa_features),
461 method_patches_(graph->GetArena()->Adapter()),
462 relative_call_patches_(graph->GetArena()->Adapter()) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000463 // Use a fake return address register to mimic Quick.
464 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100465}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100466
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100467Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100468 switch (type) {
469 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100470 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100471 X86ManagedRegister pair =
472 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100473 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
474 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100475 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
476 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100477 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100478 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100479 }
480
481 case Primitive::kPrimByte:
482 case Primitive::kPrimBoolean:
483 case Primitive::kPrimChar:
484 case Primitive::kPrimShort:
485 case Primitive::kPrimInt:
486 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100487 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100488 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100489 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100490 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
491 X86ManagedRegister current =
492 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
493 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100494 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100495 }
496 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100497 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100498 }
499
500 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100501 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100502 return Location::FpuRegisterLocation(
503 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100504 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100505
506 case Primitive::kPrimVoid:
507 LOG(FATAL) << "Unreachable type " << type;
508 }
509
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100510 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100511}
512
Mark Mendell5f874182015-03-04 15:42:45 -0500513void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100514 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100515 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100516
517 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100518 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100519
Mark Mendell5f874182015-03-04 15:42:45 -0500520 if (is_baseline) {
521 blocked_core_registers_[EBP] = true;
522 blocked_core_registers_[ESI] = true;
523 blocked_core_registers_[EDI] = true;
524 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100525
526 UpdateBlockedPairRegisters();
527}
528
529void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
530 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
531 X86ManagedRegister current =
532 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
533 if (blocked_core_registers_[current.AsRegisterPairLow()]
534 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
535 blocked_register_pairs_[i] = true;
536 }
537 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100538}
539
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100540InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
541 : HGraphVisitor(graph),
542 assembler_(codegen->GetAssembler()),
543 codegen_(codegen) {}
544
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100545static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100546 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100547}
548
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000549void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100550 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000551 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000552 bool skip_overflow_check =
553 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000554 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000555
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000556 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100557 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100558 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100559 }
560
Mark Mendell5f874182015-03-04 15:42:45 -0500561 if (HasEmptyFrame()) {
562 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000563 }
Mark Mendell5f874182015-03-04 15:42:45 -0500564
565 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
566 Register reg = kCoreCalleeSaves[i];
567 if (allocated_registers_.ContainsCoreRegister(reg)) {
568 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100569 __ cfi().AdjustCFAOffset(kX86WordSize);
570 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500571 }
572 }
573
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100574 int adjust = GetFrameSize() - FrameEntrySpillSize();
575 __ subl(ESP, Immediate(adjust));
576 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100577 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000578}
579
580void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100581 __ cfi().RememberState();
582 if (!HasEmptyFrame()) {
583 int adjust = GetFrameSize() - FrameEntrySpillSize();
584 __ addl(ESP, Immediate(adjust));
585 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500586
David Srbeckyc34dc932015-04-12 09:27:43 +0100587 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
588 Register reg = kCoreCalleeSaves[i];
589 if (allocated_registers_.ContainsCoreRegister(reg)) {
590 __ popl(reg);
591 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
592 __ cfi().Restore(DWARFReg(reg));
593 }
Mark Mendell5f874182015-03-04 15:42:45 -0500594 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000595 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100596 __ ret();
597 __ cfi().RestoreState();
598 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000599}
600
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100601void CodeGeneratorX86::Bind(HBasicBlock* block) {
602 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000603}
604
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100605Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
606 switch (load->GetType()) {
607 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100608 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100609 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100610
611 case Primitive::kPrimInt:
612 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100613 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100614 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100615
616 case Primitive::kPrimBoolean:
617 case Primitive::kPrimByte:
618 case Primitive::kPrimChar:
619 case Primitive::kPrimShort:
620 case Primitive::kPrimVoid:
621 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700622 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100623 }
624
625 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700626 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100627}
628
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100629Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
630 switch (type) {
631 case Primitive::kPrimBoolean:
632 case Primitive::kPrimByte:
633 case Primitive::kPrimChar:
634 case Primitive::kPrimShort:
635 case Primitive::kPrimInt:
636 case Primitive::kPrimNot:
637 return Location::RegisterLocation(EAX);
638
639 case Primitive::kPrimLong:
640 return Location::RegisterPairLocation(EAX, EDX);
641
642 case Primitive::kPrimVoid:
643 return Location::NoLocation();
644
645 case Primitive::kPrimDouble:
646 case Primitive::kPrimFloat:
647 return Location::FpuRegisterLocation(XMM0);
648 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100649
650 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100651}
652
653Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
654 return Location::RegisterLocation(kMethodRegisterArgument);
655}
656
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100657Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100658 switch (type) {
659 case Primitive::kPrimBoolean:
660 case Primitive::kPrimByte:
661 case Primitive::kPrimChar:
662 case Primitive::kPrimShort:
663 case Primitive::kPrimInt:
664 case Primitive::kPrimNot: {
665 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000666 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100667 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100668 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100669 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000670 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100671 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100672 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100673
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000674 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100675 uint32_t index = gp_index_;
676 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000677 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100678 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100679 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
680 calling_convention.GetRegisterPairAt(index));
681 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100682 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000683 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
684 }
685 }
686
687 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100688 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000689 stack_index_++;
690 if (index < calling_convention.GetNumberOfFpuRegisters()) {
691 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
692 } else {
693 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
694 }
695 }
696
697 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100698 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000699 stack_index_ += 2;
700 if (index < calling_convention.GetNumberOfFpuRegisters()) {
701 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
702 } else {
703 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100704 }
705 }
706
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100707 case Primitive::kPrimVoid:
708 LOG(FATAL) << "Unexpected parameter type " << type;
709 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100710 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100711 return Location();
712}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100713
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100714void CodeGeneratorX86::Move32(Location destination, Location source) {
715 if (source.Equals(destination)) {
716 return;
717 }
718 if (destination.IsRegister()) {
719 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000720 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100721 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000722 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100723 } else {
724 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000725 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100726 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100727 } else if (destination.IsFpuRegister()) {
728 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000729 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100730 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000731 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100732 } else {
733 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000734 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100735 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100736 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000737 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100738 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000739 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100740 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000741 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500742 } else if (source.IsConstant()) {
743 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000744 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500745 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100746 } else {
747 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100748 __ pushl(Address(ESP, source.GetStackIndex()));
749 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100750 }
751 }
752}
753
754void CodeGeneratorX86::Move64(Location destination, Location source) {
755 if (source.Equals(destination)) {
756 return;
757 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100758 if (destination.IsRegisterPair()) {
759 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000760 EmitParallelMoves(
761 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
762 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100763 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000764 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100765 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
766 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100767 } else if (source.IsFpuRegister()) {
768 LOG(FATAL) << "Unimplemented";
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100769 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000770 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100771 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100772 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
773 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100774 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
775 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100776 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500777 if (source.IsFpuRegister()) {
778 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
779 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000780 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100781 } else {
782 LOG(FATAL) << "Unimplemented";
783 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100784 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000785 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100786 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000787 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100788 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100790 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000792 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000793 } else if (source.IsConstant()) {
794 HConstant* constant = source.GetConstant();
795 int64_t value;
796 if (constant->IsLongConstant()) {
797 value = constant->AsLongConstant()->GetValue();
798 } else {
799 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000800 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000801 }
802 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
803 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100804 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000805 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000806 EmitParallelMoves(
807 Location::StackSlot(source.GetStackIndex()),
808 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100809 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000810 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100811 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
812 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100813 }
814 }
815}
816
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100817void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000818 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100819 if (instruction->IsCurrentMethod()) {
820 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
821 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000822 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100823 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000824 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000825 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
826 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000827 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000828 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000829 } else if (location.IsStackSlot()) {
830 __ movl(Address(ESP, location.GetStackIndex()), imm);
831 } else {
832 DCHECK(location.IsConstant());
833 DCHECK_EQ(location.GetConstant(), const_to_move);
834 }
835 } else if (const_to_move->IsLongConstant()) {
836 int64_t value = const_to_move->AsLongConstant()->GetValue();
837 if (location.IsRegisterPair()) {
838 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
839 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
840 } else if (location.IsDoubleStackSlot()) {
841 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000842 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
843 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000844 } else {
845 DCHECK(location.IsConstant());
846 DCHECK_EQ(location.GetConstant(), instruction);
847 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100848 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000849 } else if (instruction->IsTemporary()) {
850 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000851 if (temp_location.IsStackSlot()) {
852 Move32(location, temp_location);
853 } else {
854 DCHECK(temp_location.IsDoubleStackSlot());
855 Move64(location, temp_location);
856 }
Roland Levillain476df552014-10-09 17:51:36 +0100857 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100859 switch (instruction->GetType()) {
860 case Primitive::kPrimBoolean:
861 case Primitive::kPrimByte:
862 case Primitive::kPrimChar:
863 case Primitive::kPrimShort:
864 case Primitive::kPrimInt:
865 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100866 case Primitive::kPrimFloat:
867 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100868 break;
869
870 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100871 case Primitive::kPrimDouble:
872 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100873 break;
874
875 default:
876 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
877 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000878 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100879 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100880 switch (instruction->GetType()) {
881 case Primitive::kPrimBoolean:
882 case Primitive::kPrimByte:
883 case Primitive::kPrimChar:
884 case Primitive::kPrimShort:
885 case Primitive::kPrimInt:
886 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100887 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000888 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100889 break;
890
891 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100892 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000893 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100894 break;
895
896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100897 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100898 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000899 }
900}
901
Calin Juravle175dc732015-08-25 15:42:32 +0100902void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
903 DCHECK(location.IsRegister());
904 __ movl(location.AsRegister<Register>(), Immediate(value));
905}
906
Calin Juravle23a8e352015-09-08 19:56:31 +0100907void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
908 if (location.IsRegister()) {
909 locations->AddTemp(location);
910 } else if (location.IsRegisterPair()) {
911 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
912 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
913 } else {
914 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
915 }
916}
917
918void CodeGeneratorX86::MoveLocationToTemp(Location source,
919 const LocationSummary& locations,
920 int temp_index,
921 Primitive::Type type) {
922 if (!Primitive::IsFloatingPointType(type)) {
923 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
924 }
925
926 DCHECK(source.IsFpuRegister()) << source;
927 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
928 if (type == Primitive::kPrimFloat) {
929 __ movd(locations.GetTemp(temp_index).AsRegister<Register>(), src_reg);
930 } else {
931 DCHECK(type == Primitive::kPrimDouble);
932 __ movd(locations.GetTemp(temp_index).AsRegister<Register>(), src_reg);
933 __ psrlq(src_reg, Immediate(32));
934 __ movd(locations.GetTemp(temp_index + 1).AsRegister<Register>(), src_reg);
935 }
936}
937
938void CodeGeneratorX86::MoveTempToLocation(const LocationSummary& locations,
939 int temp_index,
940 Location destination,
941 Primitive::Type type) {
942 if (!Primitive::IsFloatingPointType(type)) {
943 UNIMPLEMENTED(FATAL) << "MoveLocationToTemp not implemented for type " << type;
944 }
945
946 DCHECK(destination.IsFpuRegister()) << destination;
947 XmmRegister dst_reg = destination.AsFpuRegister<XmmRegister>();
948 if (type == Primitive::kPrimFloat) {
949 __ movd(dst_reg, locations.GetTemp(temp_index).AsRegister<Register>());
950 } else {
951 DCHECK(type == Primitive::kPrimDouble);
952 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
953 // Create stack space for 2 elements.
954 __ subl(ESP, Immediate(2 * elem_size));
955 __ movl(Address(ESP, 0), locations.GetTemp(temp_index).AsRegister<Register>());
956 __ movl(Address(ESP, elem_size), locations.GetTemp(temp_index + 1).AsRegister<Register>());
957 __ movsd(dst_reg, Address(ESP, 0));
958 // And remove the temporary stack space we allocated.
959 __ addl(ESP, Immediate(2 * elem_size));
960 }
961}
962
David Brazdilfc6a86a2015-06-26 10:33:45 +0000963void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100964 DCHECK(!successor->IsExitBlock());
965
966 HBasicBlock* block = got->GetBlock();
967 HInstruction* previous = got->GetPrevious();
968
969 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +0000970 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100971 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
972 return;
973 }
974
975 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
976 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
977 }
978 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000979 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000980 }
981}
982
David Brazdilfc6a86a2015-06-26 10:33:45 +0000983void LocationsBuilderX86::VisitGoto(HGoto* got) {
984 got->SetLocations(nullptr);
985}
986
987void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
988 HandleGoto(got, got->GetSuccessor());
989}
990
991void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
992 try_boundary->SetLocations(nullptr);
993}
994
995void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
996 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
997 if (!successor->IsExitBlock()) {
998 HandleGoto(try_boundary, successor);
999 }
1000}
1001
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001002void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001003 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001004}
1005
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001006void InstructionCodeGeneratorX86::VisitExit(HExit* exit) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001007 UNUSED(exit);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001008}
1009
Mark Mendellc4701932015-04-10 13:18:51 -04001010void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1011 Label* true_label,
1012 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001013 if (cond->IsFPConditionTrueIfNaN()) {
1014 __ j(kUnordered, true_label);
1015 } else if (cond->IsFPConditionFalseIfNaN()) {
1016 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001017 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001018 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001019}
1020
1021void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1022 Label* true_label,
1023 Label* false_label) {
1024 LocationSummary* locations = cond->GetLocations();
1025 Location left = locations->InAt(0);
1026 Location right = locations->InAt(1);
1027 IfCondition if_cond = cond->GetCondition();
1028
Mark Mendellc4701932015-04-10 13:18:51 -04001029 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001030 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001031 IfCondition true_high_cond = if_cond;
1032 IfCondition false_high_cond = cond->GetOppositeCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001033 Condition final_condition = X86UnsignedOrFPCondition(if_cond);
Mark Mendellc4701932015-04-10 13:18:51 -04001034
1035 // Set the conditions for the test, remembering that == needs to be
1036 // decided using the low words.
1037 switch (if_cond) {
1038 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001039 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001040 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001041 break;
1042 case kCondLT:
1043 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001044 break;
1045 case kCondLE:
1046 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001047 break;
1048 case kCondGT:
1049 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001050 break;
1051 case kCondGE:
1052 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001053 break;
1054 }
1055
1056 if (right.IsConstant()) {
1057 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001058 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001059 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001060
1061 if (val_high == 0) {
1062 __ testl(left_high, left_high);
1063 } else {
1064 __ cmpl(left_high, Immediate(val_high));
1065 }
1066 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001067 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001068 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001069 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001070 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001071 __ j(X86SignedCondition(true_high_cond), true_label);
1072 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001073 }
1074 // Must be equal high, so compare the lows.
1075 if (val_low == 0) {
1076 __ testl(left_low, left_low);
1077 } else {
1078 __ cmpl(left_low, Immediate(val_low));
1079 }
1080 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001081 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001082 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001083
1084 __ cmpl(left_high, right_high);
1085 if (if_cond == kCondNE) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001086 __ j(X86SignedCondition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001087 } else if (if_cond == kCondEQ) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001088 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001089 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001090 __ j(X86SignedCondition(true_high_cond), true_label);
1091 __ j(X86SignedCondition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001092 }
1093 // Must be equal high, so compare the lows.
1094 __ cmpl(left_low, right_low);
1095 }
1096 // The last comparison might be unsigned.
1097 __ j(final_condition, true_label);
1098}
1099
1100void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1101 HCondition* condition,
1102 Label* true_target,
1103 Label* false_target,
1104 Label* always_true_target) {
1105 LocationSummary* locations = condition->GetLocations();
1106 Location left = locations->InAt(0);
1107 Location right = locations->InAt(1);
1108
1109 // We don't want true_target as a nullptr.
1110 if (true_target == nullptr) {
1111 true_target = always_true_target;
1112 }
1113 bool falls_through = (false_target == nullptr);
1114
1115 // FP compares don't like null false_targets.
1116 if (false_target == nullptr) {
1117 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1118 }
1119
1120 Primitive::Type type = condition->InputAt(0)->GetType();
1121 switch (type) {
1122 case Primitive::kPrimLong:
1123 GenerateLongComparesAndJumps(condition, true_target, false_target);
1124 break;
1125 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001126 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1127 GenerateFPJumps(condition, true_target, false_target);
1128 break;
1129 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001130 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1131 GenerateFPJumps(condition, true_target, false_target);
1132 break;
1133 default:
1134 LOG(FATAL) << "Unexpected compare type " << type;
1135 }
1136
1137 if (!falls_through) {
1138 __ jmp(false_target);
1139 }
1140}
1141
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001142void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1143 Label* true_target,
1144 Label* false_target,
1145 Label* always_true_target) {
1146 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001147 if (cond->IsIntConstant()) {
1148 // Constant condition, statically compared against 1.
1149 int32_t cond_value = cond->AsIntConstant()->GetValue();
1150 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001151 if (always_true_target != nullptr) {
1152 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001153 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001154 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001155 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001156 DCHECK_EQ(cond_value, 0);
1157 }
1158 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001159 bool is_materialized =
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001160 !cond->IsCondition() || cond->AsCondition()->NeedsMaterialization();
1161 // Moves do not affect the eflags register, so if the condition is
1162 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001163 // again. We can't use the eflags on long/FP conditions if they are
1164 // materialized due to the complex branching.
1165 Primitive::Type type = cond->IsCondition() ? cond->InputAt(0)->GetType() : Primitive::kPrimInt;
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001166 bool eflags_set = cond->IsCondition()
Mark Mendellc4701932015-04-10 13:18:51 -04001167 && cond->AsCondition()->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001168 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
1169 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001170 if (!eflags_set) {
1171 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001172 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001173 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001174 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001175 } else {
1176 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1177 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001178 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001179 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001180 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001181 }
1182 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001183 // Condition has not been materialized, use its inputs as the
1184 // comparison and its condition as the branch condition.
1185
Mark Mendellc4701932015-04-10 13:18:51 -04001186 // Is this a long or FP comparison that has been folded into the HCondition?
1187 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1188 // Generate the comparison directly.
1189 GenerateCompareTestAndBranch(instruction->AsIf(),
1190 cond->AsCondition(),
1191 true_target,
1192 false_target,
1193 always_true_target);
1194 return;
1195 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001196
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001197 Location lhs = cond->GetLocations()->InAt(0);
1198 Location rhs = cond->GetLocations()->InAt(1);
1199 // LHS is guaranteed to be in a register (see
1200 // LocationsBuilderX86::VisitCondition).
1201 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001202 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001203 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001204 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001205 if (constant == 0) {
1206 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1207 } else {
1208 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1209 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001210 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001211 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001212 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001213 __ j(X86SignedCondition(cond->AsCondition()->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001214 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001215 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001216 if (false_target != nullptr) {
1217 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001218 }
1219}
1220
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001221void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1222 LocationSummary* locations =
1223 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1224 HInstruction* cond = if_instr->InputAt(0);
1225 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1226 locations->SetInAt(0, Location::Any());
1227 }
1228}
1229
1230void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1231 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1232 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1233 Label* always_true_target = true_target;
1234 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1235 if_instr->IfTrueSuccessor())) {
1236 always_true_target = nullptr;
1237 }
1238 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1239 if_instr->IfFalseSuccessor())) {
1240 false_target = nullptr;
1241 }
1242 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1243}
1244
1245void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1246 LocationSummary* locations = new (GetGraph()->GetArena())
1247 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1248 HInstruction* cond = deoptimize->InputAt(0);
1249 DCHECK(cond->IsCondition());
1250 if (cond->AsCondition()->NeedsMaterialization()) {
1251 locations->SetInAt(0, Location::Any());
1252 }
1253}
1254
1255void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1256 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena())
1257 DeoptimizationSlowPathX86(deoptimize);
1258 codegen_->AddSlowPath(slow_path);
1259 Label* slow_path_entry = slow_path->GetEntryLabel();
1260 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1261}
1262
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001263void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001264 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001265}
1266
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001267void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1268 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001269}
1270
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001271void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001272 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001273}
1274
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001275void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001276 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001277 UNUSED(load);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001278}
1279
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001280void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001281 LocationSummary* locations =
1282 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001283 switch (store->InputAt(1)->GetType()) {
1284 case Primitive::kPrimBoolean:
1285 case Primitive::kPrimByte:
1286 case Primitive::kPrimChar:
1287 case Primitive::kPrimShort:
1288 case Primitive::kPrimInt:
1289 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001290 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001291 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1292 break;
1293
1294 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001295 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1297 break;
1298
1299 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001300 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001301 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001302}
1303
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001304void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001305 UNUSED(store);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001306}
1307
Roland Levillain0d37cd02015-05-27 16:39:19 +01001308void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001309 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001310 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001311 // Handle the long/FP comparisons made in instruction simplification.
1312 switch (cond->InputAt(0)->GetType()) {
1313 case Primitive::kPrimLong: {
1314 locations->SetInAt(0, Location::RequiresRegister());
1315 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1316 if (cond->NeedsMaterialization()) {
1317 locations->SetOut(Location::RequiresRegister());
1318 }
1319 break;
1320 }
1321 case Primitive::kPrimFloat:
1322 case Primitive::kPrimDouble: {
1323 locations->SetInAt(0, Location::RequiresFpuRegister());
1324 locations->SetInAt(1, Location::RequiresFpuRegister());
1325 if (cond->NeedsMaterialization()) {
1326 locations->SetOut(Location::RequiresRegister());
1327 }
1328 break;
1329 }
1330 default:
1331 locations->SetInAt(0, Location::RequiresRegister());
1332 locations->SetInAt(1, Location::Any());
1333 if (cond->NeedsMaterialization()) {
1334 // We need a byte register.
1335 locations->SetOut(Location::RegisterLocation(ECX));
1336 }
1337 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001338 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001339}
1340
Roland Levillain0d37cd02015-05-27 16:39:19 +01001341void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001342 if (!cond->NeedsMaterialization()) {
1343 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001344 }
Mark Mendellc4701932015-04-10 13:18:51 -04001345
1346 LocationSummary* locations = cond->GetLocations();
1347 Location lhs = locations->InAt(0);
1348 Location rhs = locations->InAt(1);
1349 Register reg = locations->Out().AsRegister<Register>();
1350 Label true_label, false_label;
1351
1352 switch (cond->InputAt(0)->GetType()) {
1353 default: {
1354 // Integer case.
1355
1356 // Clear output register: setcc only sets the low byte.
1357 __ xorl(reg, reg);
1358
1359 if (rhs.IsRegister()) {
1360 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1361 } else if (rhs.IsConstant()) {
1362 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1363 if (constant == 0) {
1364 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1365 } else {
1366 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1367 }
1368 } else {
1369 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1370 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371 __ setb(X86SignedCondition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001372 return;
1373 }
1374 case Primitive::kPrimLong:
1375 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1376 break;
1377 case Primitive::kPrimFloat:
1378 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1379 GenerateFPJumps(cond, &true_label, &false_label);
1380 break;
1381 case Primitive::kPrimDouble:
1382 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1383 GenerateFPJumps(cond, &true_label, &false_label);
1384 break;
1385 }
1386
1387 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001388 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001389
Roland Levillain4fa13f62015-07-06 18:11:54 +01001390 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001391 __ Bind(&false_label);
1392 __ xorl(reg, reg);
1393 __ jmp(&done_label);
1394
Roland Levillain4fa13f62015-07-06 18:11:54 +01001395 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001396 __ Bind(&true_label);
1397 __ movl(reg, Immediate(1));
1398 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001399}
1400
1401void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1402 VisitCondition(comp);
1403}
1404
1405void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1406 VisitCondition(comp);
1407}
1408
1409void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1410 VisitCondition(comp);
1411}
1412
1413void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1414 VisitCondition(comp);
1415}
1416
1417void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1418 VisitCondition(comp);
1419}
1420
1421void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1422 VisitCondition(comp);
1423}
1424
1425void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1426 VisitCondition(comp);
1427}
1428
1429void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1430 VisitCondition(comp);
1431}
1432
1433void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1434 VisitCondition(comp);
1435}
1436
1437void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1438 VisitCondition(comp);
1439}
1440
1441void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1442 VisitCondition(comp);
1443}
1444
1445void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1446 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001447}
1448
1449void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001450 LocationSummary* locations =
1451 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001452 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001453}
1454
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001455void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001456 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001457 UNUSED(constant);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001458}
1459
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001460void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1461 LocationSummary* locations =
1462 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1463 locations->SetOut(Location::ConstantLocation(constant));
1464}
1465
1466void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant) {
1467 // Will be generated at use site.
1468 UNUSED(constant);
1469}
1470
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001471void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001472 LocationSummary* locations =
1473 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001474 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001475}
1476
1477void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant) {
1478 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001479 UNUSED(constant);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001480}
1481
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001482void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1483 LocationSummary* locations =
1484 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1485 locations->SetOut(Location::ConstantLocation(constant));
1486}
1487
1488void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant) {
1489 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001490 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001491}
1492
1493void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1494 LocationSummary* locations =
1495 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1496 locations->SetOut(Location::ConstantLocation(constant));
1497}
1498
1499void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant) {
1500 // Will be generated at use site.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001501 UNUSED(constant);
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001502}
1503
Calin Juravle27df7582015-04-17 19:12:31 +01001504void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1505 memory_barrier->SetLocations(nullptr);
1506}
1507
1508void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1509 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1510}
1511
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001512void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001513 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001514}
1515
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001516void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001517 UNUSED(ret);
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001518 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001519}
1520
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001521void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001522 LocationSummary* locations =
1523 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001524 switch (ret->InputAt(0)->GetType()) {
1525 case Primitive::kPrimBoolean:
1526 case Primitive::kPrimByte:
1527 case Primitive::kPrimChar:
1528 case Primitive::kPrimShort:
1529 case Primitive::kPrimInt:
1530 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001531 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001532 break;
1533
1534 case Primitive::kPrimLong:
1535 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001536 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001537 break;
1538
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001539 case Primitive::kPrimFloat:
1540 case Primitive::kPrimDouble:
1541 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001542 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001543 break;
1544
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001545 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001546 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001547 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001548}
1549
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001550void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001551 if (kIsDebugBuild) {
1552 switch (ret->InputAt(0)->GetType()) {
1553 case Primitive::kPrimBoolean:
1554 case Primitive::kPrimByte:
1555 case Primitive::kPrimChar:
1556 case Primitive::kPrimShort:
1557 case Primitive::kPrimInt:
1558 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001559 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001560 break;
1561
1562 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001563 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1564 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 break;
1566
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001567 case Primitive::kPrimFloat:
1568 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001569 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001570 break;
1571
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001572 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001573 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001574 }
1575 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001576 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001577}
1578
Calin Juravle175dc732015-08-25 15:42:32 +01001579void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1580 // The trampoline uses the same calling convention as dex calling conventions,
1581 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1582 // the method_idx.
1583 HandleInvoke(invoke);
1584}
1585
1586void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1587 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1588}
1589
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001590void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001591 // When we do not run baseline, explicit clinit checks triggered by static
1592 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1593 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001594
Mark Mendellfb8d2792015-03-31 22:16:59 -04001595 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001596 if (intrinsic.TryDispatch(invoke)) {
1597 return;
1598 }
1599
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001600 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001601
1602 if (codegen_->IsBaseline()) {
1603 // Baseline does not have enough registers if the current method also
1604 // needs a register. We therefore do not require a register for it, and let
1605 // the code generation of the invoke handle it.
1606 LocationSummary* locations = invoke->GetLocations();
1607 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1608 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1609 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1610 }
1611 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001612}
1613
Mark Mendell09ed1a32015-03-25 08:30:06 -04001614static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1615 if (invoke->GetLocations()->Intrinsified()) {
1616 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1617 intrinsic.Dispatch(invoke);
1618 return true;
1619 }
1620 return false;
1621}
1622
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001623void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001624 // When we do not run baseline, explicit clinit checks triggered by static
1625 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1626 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001627
Mark Mendell09ed1a32015-03-25 08:30:06 -04001628 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1629 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001630 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001631
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001632 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001633 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001634 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001635 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001636}
1637
1638void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1639 HandleInvoke(invoke);
1640}
1641
1642void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001643 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001644 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001645}
1646
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001647void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001648 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1649 return;
1650 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001651
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001652 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001653 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001654 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001655}
1656
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001657void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1658 HandleInvoke(invoke);
1659 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001660 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001661}
1662
1663void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1664 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001665 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001666 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1667 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001668 LocationSummary* locations = invoke->GetLocations();
1669 Location receiver = locations->InAt(0);
1670 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1671
1672 // Set the hidden argument.
1673 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001674 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001675
1676 // temp = object->GetClass();
1677 if (receiver.IsStackSlot()) {
1678 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1679 __ movl(temp, Address(temp, class_offset));
1680 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001681 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001682 }
Roland Levillain4d027112015-07-01 15:41:14 +01001683 codegen_->MaybeRecordImplicitNullCheck(invoke);
1684 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001685 // temp = temp->GetImtEntryAt(method_offset);
1686 __ movl(temp, Address(temp, method_offset));
1687 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001688 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001689 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001690
1691 DCHECK(!codegen_->IsLeafMethod());
1692 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1693}
1694
Roland Levillain88cb1752014-10-20 16:36:47 +01001695void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1696 LocationSummary* locations =
1697 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1698 switch (neg->GetResultType()) {
1699 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001700 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001701 locations->SetInAt(0, Location::RequiresRegister());
1702 locations->SetOut(Location::SameAsFirstInput());
1703 break;
1704
Roland Levillain88cb1752014-10-20 16:36:47 +01001705 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001706 locations->SetInAt(0, Location::RequiresFpuRegister());
1707 locations->SetOut(Location::SameAsFirstInput());
1708 locations->AddTemp(Location::RequiresRegister());
1709 locations->AddTemp(Location::RequiresFpuRegister());
1710 break;
1711
Roland Levillain88cb1752014-10-20 16:36:47 +01001712 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001713 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001714 locations->SetOut(Location::SameAsFirstInput());
1715 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001716 break;
1717
1718 default:
1719 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1720 }
1721}
1722
1723void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1724 LocationSummary* locations = neg->GetLocations();
1725 Location out = locations->Out();
1726 Location in = locations->InAt(0);
1727 switch (neg->GetResultType()) {
1728 case Primitive::kPrimInt:
1729 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001730 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001731 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001732 break;
1733
1734 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001735 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001736 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001737 __ negl(out.AsRegisterPairLow<Register>());
1738 // Negation is similar to subtraction from zero. The least
1739 // significant byte triggers a borrow when it is different from
1740 // zero; to take it into account, add 1 to the most significant
1741 // byte if the carry flag (CF) is set to 1 after the first NEGL
1742 // operation.
1743 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1744 __ negl(out.AsRegisterPairHigh<Register>());
1745 break;
1746
Roland Levillain5368c212014-11-27 15:03:41 +00001747 case Primitive::kPrimFloat: {
1748 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001749 Register constant = locations->GetTemp(0).AsRegister<Register>();
1750 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001751 // Implement float negation with an exclusive or with value
1752 // 0x80000000 (mask for bit 31, representing the sign of a
1753 // single-precision floating-point number).
1754 __ movl(constant, Immediate(INT32_C(0x80000000)));
1755 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001756 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001757 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001758 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001759
Roland Levillain5368c212014-11-27 15:03:41 +00001760 case Primitive::kPrimDouble: {
1761 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001762 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001763 // Implement double negation with an exclusive or with value
1764 // 0x8000000000000000 (mask for bit 63, representing the sign of
1765 // a double-precision floating-point number).
1766 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001767 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001768 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001769 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001770
1771 default:
1772 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1773 }
1774}
1775
Roland Levillaindff1f282014-11-05 14:15:05 +00001776void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001777 Primitive::Type result_type = conversion->GetResultType();
1778 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001779 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001780
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001781 // The float-to-long and double-to-long type conversions rely on a
1782 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001783 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001784 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1785 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001786 ? LocationSummary::kCall
1787 : LocationSummary::kNoCall;
1788 LocationSummary* locations =
1789 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1790
David Brazdilb2bd1c52015-03-25 11:17:37 +00001791 // The Java language does not allow treating boolean as an integral type but
1792 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001793
Roland Levillaindff1f282014-11-05 14:15:05 +00001794 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001795 case Primitive::kPrimByte:
1796 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001797 case Primitive::kPrimBoolean:
1798 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001799 case Primitive::kPrimShort:
1800 case Primitive::kPrimInt:
1801 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001802 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001803 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1804 // Make the output overlap to please the register allocator. This greatly simplifies
1805 // the validation of the linear scan implementation
1806 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001807 break;
1808
1809 default:
1810 LOG(FATAL) << "Unexpected type conversion from " << input_type
1811 << " to " << result_type;
1812 }
1813 break;
1814
Roland Levillain01a8d712014-11-14 16:27:39 +00001815 case Primitive::kPrimShort:
1816 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001817 case Primitive::kPrimBoolean:
1818 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001819 case Primitive::kPrimByte:
1820 case Primitive::kPrimInt:
1821 case Primitive::kPrimChar:
1822 // Processing a Dex `int-to-short' instruction.
1823 locations->SetInAt(0, Location::Any());
1824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1825 break;
1826
1827 default:
1828 LOG(FATAL) << "Unexpected type conversion from " << input_type
1829 << " to " << result_type;
1830 }
1831 break;
1832
Roland Levillain946e1432014-11-11 17:35:19 +00001833 case Primitive::kPrimInt:
1834 switch (input_type) {
1835 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001836 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001837 locations->SetInAt(0, Location::Any());
1838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1839 break;
1840
1841 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001842 // Processing a Dex `float-to-int' instruction.
1843 locations->SetInAt(0, Location::RequiresFpuRegister());
1844 locations->SetOut(Location::RequiresRegister());
1845 locations->AddTemp(Location::RequiresFpuRegister());
1846 break;
1847
Roland Levillain946e1432014-11-11 17:35:19 +00001848 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001849 // Processing a Dex `double-to-int' instruction.
1850 locations->SetInAt(0, Location::RequiresFpuRegister());
1851 locations->SetOut(Location::RequiresRegister());
1852 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001853 break;
1854
1855 default:
1856 LOG(FATAL) << "Unexpected type conversion from " << input_type
1857 << " to " << result_type;
1858 }
1859 break;
1860
Roland Levillaindff1f282014-11-05 14:15:05 +00001861 case Primitive::kPrimLong:
1862 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001863 case Primitive::kPrimBoolean:
1864 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001865 case Primitive::kPrimByte:
1866 case Primitive::kPrimShort:
1867 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001868 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001869 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001870 locations->SetInAt(0, Location::RegisterLocation(EAX));
1871 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1872 break;
1873
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001874 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001875 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001876 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001877 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001878 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1879 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1880
Vladimir Marko949c91f2015-01-27 10:48:44 +00001881 // The runtime helper puts the result in EAX, EDX.
1882 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001883 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001884 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001885
1886 default:
1887 LOG(FATAL) << "Unexpected type conversion from " << input_type
1888 << " to " << result_type;
1889 }
1890 break;
1891
Roland Levillain981e4542014-11-14 11:47:14 +00001892 case Primitive::kPrimChar:
1893 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001894 case Primitive::kPrimBoolean:
1895 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00001896 case Primitive::kPrimByte:
1897 case Primitive::kPrimShort:
1898 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00001899 // Processing a Dex `int-to-char' instruction.
1900 locations->SetInAt(0, Location::Any());
1901 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1902 break;
1903
1904 default:
1905 LOG(FATAL) << "Unexpected type conversion from " << input_type
1906 << " to " << result_type;
1907 }
1908 break;
1909
Roland Levillaindff1f282014-11-05 14:15:05 +00001910 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00001911 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001912 case Primitive::kPrimBoolean:
1913 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001914 case Primitive::kPrimByte:
1915 case Primitive::kPrimShort:
1916 case Primitive::kPrimInt:
1917 case Primitive::kPrimChar:
1918 // Processing a Dex `int-to-float' instruction.
1919 locations->SetInAt(0, Location::RequiresRegister());
1920 locations->SetOut(Location::RequiresFpuRegister());
1921 break;
1922
1923 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00001924 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001925 locations->SetInAt(0, Location::Any());
1926 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00001927 break;
1928
Roland Levillaincff13742014-11-17 14:32:17 +00001929 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001930 // Processing a Dex `double-to-float' instruction.
1931 locations->SetInAt(0, Location::RequiresFpuRegister());
1932 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001933 break;
1934
1935 default:
1936 LOG(FATAL) << "Unexpected type conversion from " << input_type
1937 << " to " << result_type;
1938 };
1939 break;
1940
Roland Levillaindff1f282014-11-05 14:15:05 +00001941 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00001942 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001943 case Primitive::kPrimBoolean:
1944 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00001945 case Primitive::kPrimByte:
1946 case Primitive::kPrimShort:
1947 case Primitive::kPrimInt:
1948 case Primitive::kPrimChar:
1949 // Processing a Dex `int-to-double' instruction.
1950 locations->SetInAt(0, Location::RequiresRegister());
1951 locations->SetOut(Location::RequiresFpuRegister());
1952 break;
1953
1954 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00001955 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01001956 locations->SetInAt(0, Location::Any());
1957 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00001958 break;
1959
Roland Levillaincff13742014-11-17 14:32:17 +00001960 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00001961 // Processing a Dex `float-to-double' instruction.
1962 locations->SetInAt(0, Location::RequiresFpuRegister());
1963 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00001964 break;
1965
1966 default:
1967 LOG(FATAL) << "Unexpected type conversion from " << input_type
1968 << " to " << result_type;
1969 }
Roland Levillaindff1f282014-11-05 14:15:05 +00001970 break;
1971
1972 default:
1973 LOG(FATAL) << "Unexpected type conversion from " << input_type
1974 << " to " << result_type;
1975 }
1976}
1977
1978void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
1979 LocationSummary* locations = conversion->GetLocations();
1980 Location out = locations->Out();
1981 Location in = locations->InAt(0);
1982 Primitive::Type result_type = conversion->GetResultType();
1983 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001984 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00001985 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001986 case Primitive::kPrimByte:
1987 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001988 case Primitive::kPrimBoolean:
1989 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001990 case Primitive::kPrimShort:
1991 case Primitive::kPrimInt:
1992 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001993 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001994 if (in.IsRegister()) {
1995 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00001996 } else {
1997 DCHECK(in.GetConstant()->IsIntConstant());
1998 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
1999 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2000 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002001 break;
2002
2003 default:
2004 LOG(FATAL) << "Unexpected type conversion from " << input_type
2005 << " to " << result_type;
2006 }
2007 break;
2008
Roland Levillain01a8d712014-11-14 16:27:39 +00002009 case Primitive::kPrimShort:
2010 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002011 case Primitive::kPrimBoolean:
2012 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002013 case Primitive::kPrimByte:
2014 case Primitive::kPrimInt:
2015 case Primitive::kPrimChar:
2016 // Processing a Dex `int-to-short' instruction.
2017 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002018 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002019 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002020 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002021 } else {
2022 DCHECK(in.GetConstant()->IsIntConstant());
2023 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002024 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002025 }
2026 break;
2027
2028 default:
2029 LOG(FATAL) << "Unexpected type conversion from " << input_type
2030 << " to " << result_type;
2031 }
2032 break;
2033
Roland Levillain946e1432014-11-11 17:35:19 +00002034 case Primitive::kPrimInt:
2035 switch (input_type) {
2036 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002037 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002038 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002039 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002040 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002041 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002042 } else {
2043 DCHECK(in.IsConstant());
2044 DCHECK(in.GetConstant()->IsLongConstant());
2045 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002046 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002047 }
2048 break;
2049
Roland Levillain3f8f9362014-12-02 17:45:01 +00002050 case Primitive::kPrimFloat: {
2051 // Processing a Dex `float-to-int' instruction.
2052 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2053 Register output = out.AsRegister<Register>();
2054 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002055 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002056
2057 __ movl(output, Immediate(kPrimIntMax));
2058 // temp = int-to-float(output)
2059 __ cvtsi2ss(temp, output);
2060 // if input >= temp goto done
2061 __ comiss(input, temp);
2062 __ j(kAboveEqual, &done);
2063 // if input == NaN goto nan
2064 __ j(kUnordered, &nan);
2065 // output = float-to-int-truncate(input)
2066 __ cvttss2si(output, input);
2067 __ jmp(&done);
2068 __ Bind(&nan);
2069 // output = 0
2070 __ xorl(output, output);
2071 __ Bind(&done);
2072 break;
2073 }
2074
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002075 case Primitive::kPrimDouble: {
2076 // Processing a Dex `double-to-int' instruction.
2077 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2078 Register output = out.AsRegister<Register>();
2079 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002080 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002081
2082 __ movl(output, Immediate(kPrimIntMax));
2083 // temp = int-to-double(output)
2084 __ cvtsi2sd(temp, output);
2085 // if input >= temp goto done
2086 __ comisd(input, temp);
2087 __ j(kAboveEqual, &done);
2088 // if input == NaN goto nan
2089 __ j(kUnordered, &nan);
2090 // output = double-to-int-truncate(input)
2091 __ cvttsd2si(output, input);
2092 __ jmp(&done);
2093 __ Bind(&nan);
2094 // output = 0
2095 __ xorl(output, output);
2096 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002097 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002098 }
Roland Levillain946e1432014-11-11 17:35:19 +00002099
2100 default:
2101 LOG(FATAL) << "Unexpected type conversion from " << input_type
2102 << " to " << result_type;
2103 }
2104 break;
2105
Roland Levillaindff1f282014-11-05 14:15:05 +00002106 case Primitive::kPrimLong:
2107 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002108 case Primitive::kPrimBoolean:
2109 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002110 case Primitive::kPrimByte:
2111 case Primitive::kPrimShort:
2112 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002113 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002114 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002115 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2116 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002117 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002118 __ cdq();
2119 break;
2120
2121 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002122 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002123 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2124 conversion,
2125 conversion->GetDexPc(),
2126 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002127 break;
2128
Roland Levillaindff1f282014-11-05 14:15:05 +00002129 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002130 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002131 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2132 conversion,
2133 conversion->GetDexPc(),
2134 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002135 break;
2136
2137 default:
2138 LOG(FATAL) << "Unexpected type conversion from " << input_type
2139 << " to " << result_type;
2140 }
2141 break;
2142
Roland Levillain981e4542014-11-14 11:47:14 +00002143 case Primitive::kPrimChar:
2144 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002145 case Primitive::kPrimBoolean:
2146 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002147 case Primitive::kPrimByte:
2148 case Primitive::kPrimShort:
2149 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002150 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2151 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002152 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002153 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002154 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002155 } else {
2156 DCHECK(in.GetConstant()->IsIntConstant());
2157 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002158 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002159 }
2160 break;
2161
2162 default:
2163 LOG(FATAL) << "Unexpected type conversion from " << input_type
2164 << " to " << result_type;
2165 }
2166 break;
2167
Roland Levillaindff1f282014-11-05 14:15:05 +00002168 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002169 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002170 case Primitive::kPrimBoolean:
2171 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002172 case Primitive::kPrimByte:
2173 case Primitive::kPrimShort:
2174 case Primitive::kPrimInt:
2175 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002176 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002177 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002178 break;
2179
Roland Levillain6d0e4832014-11-27 18:31:21 +00002180 case Primitive::kPrimLong: {
2181 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002182 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002183
Roland Levillain232ade02015-04-20 15:14:36 +01002184 // Create stack space for the call to
2185 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2186 // TODO: enhance register allocator to ask for stack temporaries.
2187 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2188 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2189 __ subl(ESP, Immediate(adjustment));
2190 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002191
Roland Levillain232ade02015-04-20 15:14:36 +01002192 // Load the value to the FP stack, using temporaries if needed.
2193 PushOntoFPStack(in, 0, adjustment, false, true);
2194
2195 if (out.IsStackSlot()) {
2196 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2197 } else {
2198 __ fstps(Address(ESP, 0));
2199 Location stack_temp = Location::StackSlot(0);
2200 codegen_->Move32(out, stack_temp);
2201 }
2202
2203 // Remove the temporary stack space we allocated.
2204 if (adjustment != 0) {
2205 __ addl(ESP, Immediate(adjustment));
2206 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002207 break;
2208 }
2209
Roland Levillaincff13742014-11-17 14:32:17 +00002210 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002211 // Processing a Dex `double-to-float' instruction.
2212 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002213 break;
2214
2215 default:
2216 LOG(FATAL) << "Unexpected type conversion from " << input_type
2217 << " to " << result_type;
2218 };
2219 break;
2220
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002222 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002223 case Primitive::kPrimBoolean:
2224 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002225 case Primitive::kPrimByte:
2226 case Primitive::kPrimShort:
2227 case Primitive::kPrimInt:
2228 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002229 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002230 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002231 break;
2232
Roland Levillain647b9ed2014-11-27 12:06:00 +00002233 case Primitive::kPrimLong: {
2234 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002235 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002236
Roland Levillain232ade02015-04-20 15:14:36 +01002237 // Create stack space for the call to
2238 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2239 // TODO: enhance register allocator to ask for stack temporaries.
2240 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2241 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2242 __ subl(ESP, Immediate(adjustment));
2243 }
2244
2245 // Load the value to the FP stack, using temporaries if needed.
2246 PushOntoFPStack(in, 0, adjustment, false, true);
2247
2248 if (out.IsDoubleStackSlot()) {
2249 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2250 } else {
2251 __ fstpl(Address(ESP, 0));
2252 Location stack_temp = Location::DoubleStackSlot(0);
2253 codegen_->Move64(out, stack_temp);
2254 }
2255
2256 // Remove the temporary stack space we allocated.
2257 if (adjustment != 0) {
2258 __ addl(ESP, Immediate(adjustment));
2259 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002260 break;
2261 }
2262
Roland Levillaincff13742014-11-17 14:32:17 +00002263 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002264 // Processing a Dex `float-to-double' instruction.
2265 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002266 break;
2267
2268 default:
2269 LOG(FATAL) << "Unexpected type conversion from " << input_type
2270 << " to " << result_type;
2271 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002272 break;
2273
2274 default:
2275 LOG(FATAL) << "Unexpected type conversion from " << input_type
2276 << " to " << result_type;
2277 }
2278}
2279
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002280void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002281 LocationSummary* locations =
2282 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002283 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002284 case Primitive::kPrimInt: {
2285 locations->SetInAt(0, Location::RequiresRegister());
2286 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2288 break;
2289 }
2290
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002291 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002292 locations->SetInAt(0, Location::RequiresRegister());
2293 locations->SetInAt(1, Location::Any());
2294 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002295 break;
2296 }
2297
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002298 case Primitive::kPrimFloat:
2299 case Primitive::kPrimDouble: {
2300 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002301 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002302 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002303 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002304 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002305
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002306 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002307 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2308 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002309 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002310}
2311
2312void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2313 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002314 Location first = locations->InAt(0);
2315 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002316 Location out = locations->Out();
2317
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002318 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002319 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002320 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002321 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2322 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002323 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2324 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002325 } else {
2326 __ leal(out.AsRegister<Register>(), Address(
2327 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2328 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002329 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002330 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2331 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2332 __ addl(out.AsRegister<Register>(), Immediate(value));
2333 } else {
2334 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2335 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002336 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002337 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002338 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002339 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002340 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002341 }
2342
2343 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002344 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002345 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2346 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002347 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002348 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2349 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002350 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002351 } else {
2352 DCHECK(second.IsConstant()) << second;
2353 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2354 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2355 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002356 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002357 break;
2358 }
2359
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002360 case Primitive::kPrimFloat: {
2361 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002363 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2364 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2365 DCHECK(!const_area->NeedsMaterialization());
2366 __ addss(first.AsFpuRegister<XmmRegister>(),
2367 codegen_->LiteralFloatAddress(
2368 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2369 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2370 } else {
2371 DCHECK(second.IsStackSlot());
2372 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002373 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002374 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002375 }
2376
2377 case Primitive::kPrimDouble: {
2378 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002379 __ addsd(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 __ addsd(first.AsFpuRegister<XmmRegister>(),
2384 codegen_->LiteralDoubleAddress(
2385 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2386 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2387 } else {
2388 DCHECK(second.IsDoubleStackSlot());
2389 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002390 }
2391 break;
2392 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002393
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002394 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002395 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002396 }
2397}
2398
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002399void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002400 LocationSummary* locations =
2401 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002402 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002403 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002404 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002405 locations->SetInAt(0, Location::RequiresRegister());
2406 locations->SetInAt(1, Location::Any());
2407 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002408 break;
2409 }
Calin Juravle11351682014-10-23 15:38:15 +01002410 case Primitive::kPrimFloat:
2411 case Primitive::kPrimDouble: {
2412 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002413 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002414 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002415 break;
Calin Juravle11351682014-10-23 15:38:15 +01002416 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002417
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002418 default:
Calin Juravle11351682014-10-23 15:38:15 +01002419 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002420 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002421}
2422
2423void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2424 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002425 Location first = locations->InAt(0);
2426 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002427 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002428 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002429 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002430 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002431 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002432 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002433 __ subl(first.AsRegister<Register>(),
2434 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002435 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002436 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002437 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002438 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002439 }
2440
2441 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002442 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002443 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2444 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002445 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002446 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002447 __ sbbl(first.AsRegisterPairHigh<Register>(),
2448 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002449 } else {
2450 DCHECK(second.IsConstant()) << second;
2451 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2452 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2453 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002454 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002455 break;
2456 }
2457
Calin Juravle11351682014-10-23 15:38:15 +01002458 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002459 if (second.IsFpuRegister()) {
2460 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2461 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2462 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2463 DCHECK(!const_area->NeedsMaterialization());
2464 __ subss(first.AsFpuRegister<XmmRegister>(),
2465 codegen_->LiteralFloatAddress(
2466 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2467 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2468 } else {
2469 DCHECK(second.IsStackSlot());
2470 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2471 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002472 break;
Calin Juravle11351682014-10-23 15:38:15 +01002473 }
2474
2475 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002476 if (second.IsFpuRegister()) {
2477 __ subsd(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 __ subsd(first.AsFpuRegister<XmmRegister>(),
2482 codegen_->LiteralDoubleAddress(
2483 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2484 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2485 } else {
2486 DCHECK(second.IsDoubleStackSlot());
2487 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2488 }
Calin Juravle11351682014-10-23 15:38:15 +01002489 break;
2490 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002491
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002492 default:
Calin Juravle11351682014-10-23 15:38:15 +01002493 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002494 }
2495}
2496
Calin Juravle34bacdf2014-10-07 20:23:36 +01002497void LocationsBuilderX86::VisitMul(HMul* mul) {
2498 LocationSummary* locations =
2499 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2500 switch (mul->GetResultType()) {
2501 case Primitive::kPrimInt:
2502 locations->SetInAt(0, Location::RequiresRegister());
2503 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002504 if (mul->InputAt(1)->IsIntConstant()) {
2505 // Can use 3 operand multiply.
2506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2507 } else {
2508 locations->SetOut(Location::SameAsFirstInput());
2509 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002510 break;
2511 case Primitive::kPrimLong: {
2512 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002513 locations->SetInAt(1, Location::Any());
2514 locations->SetOut(Location::SameAsFirstInput());
2515 // Needed for imul on 32bits with 64bits output.
2516 locations->AddTemp(Location::RegisterLocation(EAX));
2517 locations->AddTemp(Location::RegisterLocation(EDX));
2518 break;
2519 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002520 case Primitive::kPrimFloat:
2521 case Primitive::kPrimDouble: {
2522 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002523 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002524 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002525 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002526 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002527
2528 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002529 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002530 }
2531}
2532
2533void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2534 LocationSummary* locations = mul->GetLocations();
2535 Location first = locations->InAt(0);
2536 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002537 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002538
2539 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002540 case Primitive::kPrimInt:
2541 // The constant may have ended up in a register, so test explicitly to avoid
2542 // problems where the output may not be the same as the first operand.
2543 if (mul->InputAt(1)->IsIntConstant()) {
2544 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2545 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2546 } else if (second.IsRegister()) {
2547 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002548 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002549 } else {
2550 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002551 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002552 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002553 }
2554 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002555
2556 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002557 Register in1_hi = first.AsRegisterPairHigh<Register>();
2558 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002559 Register eax = locations->GetTemp(0).AsRegister<Register>();
2560 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002561
2562 DCHECK_EQ(EAX, eax);
2563 DCHECK_EQ(EDX, edx);
2564
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002565 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002566 // output: in1
2567 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2568 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2569 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002570 if (second.IsConstant()) {
2571 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002572
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002573 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2574 int32_t low_value = Low32Bits(value);
2575 int32_t high_value = High32Bits(value);
2576 Immediate low(low_value);
2577 Immediate high(high_value);
2578
2579 __ movl(eax, high);
2580 // eax <- in1.lo * in2.hi
2581 __ imull(eax, in1_lo);
2582 // in1.hi <- in1.hi * in2.lo
2583 __ imull(in1_hi, low);
2584 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2585 __ addl(in1_hi, eax);
2586 // move in2_lo to eax to prepare for double precision
2587 __ movl(eax, low);
2588 // edx:eax <- in1.lo * in2.lo
2589 __ mull(in1_lo);
2590 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2591 __ addl(in1_hi, edx);
2592 // in1.lo <- (in1.lo * in2.lo)[31:0];
2593 __ movl(in1_lo, eax);
2594 } else if (second.IsRegisterPair()) {
2595 Register in2_hi = second.AsRegisterPairHigh<Register>();
2596 Register in2_lo = second.AsRegisterPairLow<Register>();
2597
2598 __ movl(eax, in2_hi);
2599 // eax <- in1.lo * in2.hi
2600 __ imull(eax, in1_lo);
2601 // in1.hi <- in1.hi * in2.lo
2602 __ imull(in1_hi, in2_lo);
2603 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2604 __ addl(in1_hi, eax);
2605 // move in1_lo to eax to prepare for double precision
2606 __ movl(eax, in1_lo);
2607 // edx:eax <- in1.lo * in2.lo
2608 __ mull(in2_lo);
2609 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2610 __ addl(in1_hi, edx);
2611 // in1.lo <- (in1.lo * in2.lo)[31:0];
2612 __ movl(in1_lo, eax);
2613 } else {
2614 DCHECK(second.IsDoubleStackSlot()) << second;
2615 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2616 Address in2_lo(ESP, second.GetStackIndex());
2617
2618 __ movl(eax, in2_hi);
2619 // eax <- in1.lo * in2.hi
2620 __ imull(eax, in1_lo);
2621 // in1.hi <- in1.hi * in2.lo
2622 __ imull(in1_hi, in2_lo);
2623 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2624 __ addl(in1_hi, eax);
2625 // move in1_lo to eax to prepare for double precision
2626 __ movl(eax, in1_lo);
2627 // edx:eax <- in1.lo * in2.lo
2628 __ mull(in2_lo);
2629 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2630 __ addl(in1_hi, edx);
2631 // in1.lo <- (in1.lo * in2.lo)[31:0];
2632 __ movl(in1_lo, eax);
2633 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002634
2635 break;
2636 }
2637
Calin Juravleb5bfa962014-10-21 18:02:24 +01002638 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002639 DCHECK(first.Equals(locations->Out()));
2640 if (second.IsFpuRegister()) {
2641 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2642 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2643 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2644 DCHECK(!const_area->NeedsMaterialization());
2645 __ mulss(first.AsFpuRegister<XmmRegister>(),
2646 codegen_->LiteralFloatAddress(
2647 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2648 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2649 } else {
2650 DCHECK(second.IsStackSlot());
2651 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2652 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002653 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002654 }
2655
2656 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002657 DCHECK(first.Equals(locations->Out()));
2658 if (second.IsFpuRegister()) {
2659 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2660 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2661 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2662 DCHECK(!const_area->NeedsMaterialization());
2663 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2664 codegen_->LiteralDoubleAddress(
2665 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2666 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2667 } else {
2668 DCHECK(second.IsDoubleStackSlot());
2669 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2670 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002671 break;
2672 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002673
2674 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002675 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002676 }
2677}
2678
Roland Levillain232ade02015-04-20 15:14:36 +01002679void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2680 uint32_t temp_offset,
2681 uint32_t stack_adjustment,
2682 bool is_fp,
2683 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002684 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002685 DCHECK(!is_wide);
2686 if (is_fp) {
2687 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2688 } else {
2689 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2690 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002691 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002692 DCHECK(is_wide);
2693 if (is_fp) {
2694 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2695 } else {
2696 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2697 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002698 } else {
2699 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002700 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002701 Location stack_temp = Location::StackSlot(temp_offset);
2702 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002703 if (is_fp) {
2704 __ flds(Address(ESP, temp_offset));
2705 } else {
2706 __ filds(Address(ESP, temp_offset));
2707 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002708 } else {
2709 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2710 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002711 if (is_fp) {
2712 __ fldl(Address(ESP, temp_offset));
2713 } else {
2714 __ fildl(Address(ESP, temp_offset));
2715 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002716 }
2717 }
2718}
2719
2720void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2721 Primitive::Type type = rem->GetResultType();
2722 bool is_float = type == Primitive::kPrimFloat;
2723 size_t elem_size = Primitive::ComponentSize(type);
2724 LocationSummary* locations = rem->GetLocations();
2725 Location first = locations->InAt(0);
2726 Location second = locations->InAt(1);
2727 Location out = locations->Out();
2728
2729 // Create stack space for 2 elements.
2730 // TODO: enhance register allocator to ask for stack temporaries.
2731 __ subl(ESP, Immediate(2 * elem_size));
2732
2733 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002734 const bool is_wide = !is_float;
2735 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2736 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002737
2738 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002739 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002740 __ Bind(&retry);
2741 __ fprem();
2742
2743 // Move FP status to AX.
2744 __ fstsw();
2745
2746 // And see if the argument reduction is complete. This is signaled by the
2747 // C2 FPU flag bit set to 0.
2748 __ andl(EAX, Immediate(kC2ConditionMask));
2749 __ j(kNotEqual, &retry);
2750
2751 // We have settled on the final value. Retrieve it into an XMM register.
2752 // Store FP top of stack to real stack.
2753 if (is_float) {
2754 __ fsts(Address(ESP, 0));
2755 } else {
2756 __ fstl(Address(ESP, 0));
2757 }
2758
2759 // Pop the 2 items from the FP stack.
2760 __ fucompp();
2761
2762 // Load the value from the stack into an XMM register.
2763 DCHECK(out.IsFpuRegister()) << out;
2764 if (is_float) {
2765 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2766 } else {
2767 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2768 }
2769
2770 // And remove the temporary stack space we allocated.
2771 __ addl(ESP, Immediate(2 * elem_size));
2772}
2773
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002774
2775void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2776 DCHECK(instruction->IsDiv() || instruction->IsRem());
2777
2778 LocationSummary* locations = instruction->GetLocations();
2779 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002780 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002781
2782 Register out_register = locations->Out().AsRegister<Register>();
2783 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002784 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002785
2786 DCHECK(imm == 1 || imm == -1);
2787
2788 if (instruction->IsRem()) {
2789 __ xorl(out_register, out_register);
2790 } else {
2791 __ movl(out_register, input_register);
2792 if (imm == -1) {
2793 __ negl(out_register);
2794 }
2795 }
2796}
2797
2798
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002799void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002800 LocationSummary* locations = instruction->GetLocations();
2801
2802 Register out_register = locations->Out().AsRegister<Register>();
2803 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002804 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002805
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002806 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002807 Register num = locations->GetTemp(0).AsRegister<Register>();
2808
2809 __ leal(num, Address(input_register, std::abs(imm) - 1));
2810 __ testl(input_register, input_register);
2811 __ cmovl(kGreaterEqual, num, input_register);
2812 int shift = CTZ(imm);
2813 __ sarl(num, Immediate(shift));
2814
2815 if (imm < 0) {
2816 __ negl(num);
2817 }
2818
2819 __ movl(out_register, num);
2820}
2821
2822void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2823 DCHECK(instruction->IsDiv() || instruction->IsRem());
2824
2825 LocationSummary* locations = instruction->GetLocations();
2826 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2827
2828 Register eax = locations->InAt(0).AsRegister<Register>();
2829 Register out = locations->Out().AsRegister<Register>();
2830 Register num;
2831 Register edx;
2832
2833 if (instruction->IsDiv()) {
2834 edx = locations->GetTemp(0).AsRegister<Register>();
2835 num = locations->GetTemp(1).AsRegister<Register>();
2836 } else {
2837 edx = locations->Out().AsRegister<Register>();
2838 num = locations->GetTemp(0).AsRegister<Register>();
2839 }
2840
2841 DCHECK_EQ(EAX, eax);
2842 DCHECK_EQ(EDX, edx);
2843 if (instruction->IsDiv()) {
2844 DCHECK_EQ(EAX, out);
2845 } else {
2846 DCHECK_EQ(EDX, out);
2847 }
2848
2849 int64_t magic;
2850 int shift;
2851 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2852
Mark Mendell0c9497d2015-08-21 09:30:05 -04002853 NearLabel ndiv;
2854 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002855 // If numerator is 0, the result is 0, no computation needed.
2856 __ testl(eax, eax);
2857 __ j(kNotEqual, &ndiv);
2858
2859 __ xorl(out, out);
2860 __ jmp(&end);
2861
2862 __ Bind(&ndiv);
2863
2864 // Save the numerator.
2865 __ movl(num, eax);
2866
2867 // EAX = magic
2868 __ movl(eax, Immediate(magic));
2869
2870 // EDX:EAX = magic * numerator
2871 __ imull(num);
2872
2873 if (imm > 0 && magic < 0) {
2874 // EDX += num
2875 __ addl(edx, num);
2876 } else if (imm < 0 && magic > 0) {
2877 __ subl(edx, num);
2878 }
2879
2880 // Shift if needed.
2881 if (shift != 0) {
2882 __ sarl(edx, Immediate(shift));
2883 }
2884
2885 // EDX += 1 if EDX < 0
2886 __ movl(eax, edx);
2887 __ shrl(edx, Immediate(31));
2888 __ addl(edx, eax);
2889
2890 if (instruction->IsRem()) {
2891 __ movl(eax, num);
2892 __ imull(edx, Immediate(imm));
2893 __ subl(eax, edx);
2894 __ movl(edx, eax);
2895 } else {
2896 __ movl(eax, edx);
2897 }
2898 __ Bind(&end);
2899}
2900
Calin Juravlebacfec32014-11-14 15:54:36 +00002901void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
2902 DCHECK(instruction->IsDiv() || instruction->IsRem());
2903
2904 LocationSummary* locations = instruction->GetLocations();
2905 Location out = locations->Out();
2906 Location first = locations->InAt(0);
2907 Location second = locations->InAt(1);
2908 bool is_div = instruction->IsDiv();
2909
2910 switch (instruction->GetResultType()) {
2911 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002912 DCHECK_EQ(EAX, first.AsRegister<Register>());
2913 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00002914
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002915 if (instruction->InputAt(1)->IsIntConstant()) {
2916 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002917
2918 if (imm == 0) {
2919 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
2920 } else if (imm == 1 || imm == -1) {
2921 DivRemOneOrMinusOne(instruction);
2922 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002923 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002924 } else {
2925 DCHECK(imm <= -2 || imm >= 2);
2926 GenerateDivRemWithAnyConstant(instruction);
2927 }
2928 } else {
2929 SlowPathCodeX86* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00002930 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002931 is_div);
2932 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00002933
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002934 Register second_reg = second.AsRegister<Register>();
2935 // 0x80000000/-1 triggers an arithmetic exception!
2936 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
2937 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00002938
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002939 __ cmpl(second_reg, Immediate(-1));
2940 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00002941
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002942 // edx:eax <- sign-extended of eax
2943 __ cdq();
2944 // eax = quotient, edx = remainder
2945 __ idivl(second_reg);
2946 __ Bind(slow_path->GetExitLabel());
2947 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002948 break;
2949 }
2950
2951 case Primitive::kPrimLong: {
2952 InvokeRuntimeCallingConvention calling_convention;
2953 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
2954 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
2955 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
2956 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
2957 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
2958 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
2959
2960 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01002961 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
2962 instruction,
2963 instruction->GetDexPc(),
2964 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002965 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01002966 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
2967 instruction,
2968 instruction->GetDexPc(),
2969 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00002970 }
Calin Juravlebacfec32014-11-14 15:54:36 +00002971 break;
2972 }
2973
2974 default:
2975 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
2976 }
2977}
2978
Calin Juravle7c4954d2014-10-28 16:57:40 +00002979void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002980 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00002981 ? LocationSummary::kCall
2982 : LocationSummary::kNoCall;
2983 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
2984
Calin Juravle7c4954d2014-10-28 16:57:40 +00002985 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00002986 case Primitive::kPrimInt: {
2987 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002988 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00002989 locations->SetOut(Location::SameAsFirstInput());
2990 // Intel uses edx:eax as the dividend.
2991 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002992 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
2993 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
2994 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002995 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002996 locations->AddTemp(Location::RequiresRegister());
2997 }
Calin Juravled0d48522014-11-04 16:40:20 +00002998 break;
2999 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003000 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003001 InvokeRuntimeCallingConvention calling_convention;
3002 locations->SetInAt(0, Location::RegisterPairLocation(
3003 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3004 locations->SetInAt(1, Location::RegisterPairLocation(
3005 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3006 // Runtime helper puts the result in EAX, EDX.
3007 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003008 break;
3009 }
3010 case Primitive::kPrimFloat:
3011 case Primitive::kPrimDouble: {
3012 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003013 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003014 locations->SetOut(Location::SameAsFirstInput());
3015 break;
3016 }
3017
3018 default:
3019 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3020 }
3021}
3022
3023void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3024 LocationSummary* locations = div->GetLocations();
3025 Location first = locations->InAt(0);
3026 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003027
3028 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003029 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003030 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003031 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003032 break;
3033 }
3034
3035 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003036 if (second.IsFpuRegister()) {
3037 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3038 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3039 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3040 DCHECK(!const_area->NeedsMaterialization());
3041 __ divss(first.AsFpuRegister<XmmRegister>(),
3042 codegen_->LiteralFloatAddress(
3043 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3044 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3045 } else {
3046 DCHECK(second.IsStackSlot());
3047 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3048 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003049 break;
3050 }
3051
3052 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003053 if (second.IsFpuRegister()) {
3054 __ divsd(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 __ divsd(first.AsFpuRegister<XmmRegister>(),
3059 codegen_->LiteralDoubleAddress(
3060 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3061 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3062 } else {
3063 DCHECK(second.IsDoubleStackSlot());
3064 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3065 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003066 break;
3067 }
3068
3069 default:
3070 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3071 }
3072}
3073
Calin Juravlebacfec32014-11-14 15:54:36 +00003074void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003075 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003076
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003077 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3078 ? LocationSummary::kCall
3079 : LocationSummary::kNoCall;
3080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003081
Calin Juravled2ec87d2014-12-08 14:24:46 +00003082 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003083 case Primitive::kPrimInt: {
3084 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003085 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003086 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003087 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3088 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3089 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003090 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003091 locations->AddTemp(Location::RequiresRegister());
3092 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003093 break;
3094 }
3095 case Primitive::kPrimLong: {
3096 InvokeRuntimeCallingConvention calling_convention;
3097 locations->SetInAt(0, Location::RegisterPairLocation(
3098 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3099 locations->SetInAt(1, Location::RegisterPairLocation(
3100 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3101 // Runtime helper puts the result in EAX, EDX.
3102 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3103 break;
3104 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003105 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003106 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003107 locations->SetInAt(0, Location::Any());
3108 locations->SetInAt(1, Location::Any());
3109 locations->SetOut(Location::RequiresFpuRegister());
3110 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003111 break;
3112 }
3113
3114 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003115 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003116 }
3117}
3118
3119void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3120 Primitive::Type type = rem->GetResultType();
3121 switch (type) {
3122 case Primitive::kPrimInt:
3123 case Primitive::kPrimLong: {
3124 GenerateDivRemIntegral(rem);
3125 break;
3126 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003127 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003128 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003129 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003130 break;
3131 }
3132 default:
3133 LOG(FATAL) << "Unexpected rem type " << type;
3134 }
3135}
3136
Calin Juravled0d48522014-11-04 16:40:20 +00003137void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003138 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3139 ? LocationSummary::kCallOnSlowPath
3140 : LocationSummary::kNoCall;
3141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003142 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003143 case Primitive::kPrimByte:
3144 case Primitive::kPrimChar:
3145 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003146 case Primitive::kPrimInt: {
3147 locations->SetInAt(0, Location::Any());
3148 break;
3149 }
3150 case Primitive::kPrimLong: {
3151 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3152 if (!instruction->IsConstant()) {
3153 locations->AddTemp(Location::RequiresRegister());
3154 }
3155 break;
3156 }
3157 default:
3158 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3159 }
Calin Juravled0d48522014-11-04 16:40:20 +00003160 if (instruction->HasUses()) {
3161 locations->SetOut(Location::SameAsFirstInput());
3162 }
3163}
3164
3165void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3166 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
3167 codegen_->AddSlowPath(slow_path);
3168
3169 LocationSummary* locations = instruction->GetLocations();
3170 Location value = locations->InAt(0);
3171
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003172 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003173 case Primitive::kPrimByte:
3174 case Primitive::kPrimChar:
3175 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003176 case Primitive::kPrimInt: {
3177 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003178 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003179 __ j(kEqual, slow_path->GetEntryLabel());
3180 } else if (value.IsStackSlot()) {
3181 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3182 __ j(kEqual, slow_path->GetEntryLabel());
3183 } else {
3184 DCHECK(value.IsConstant()) << value;
3185 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3186 __ jmp(slow_path->GetEntryLabel());
3187 }
3188 }
3189 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003190 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003191 case Primitive::kPrimLong: {
3192 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003193 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003194 __ movl(temp, value.AsRegisterPairLow<Register>());
3195 __ orl(temp, value.AsRegisterPairHigh<Register>());
3196 __ j(kEqual, slow_path->GetEntryLabel());
3197 } else {
3198 DCHECK(value.IsConstant()) << value;
3199 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3200 __ jmp(slow_path->GetEntryLabel());
3201 }
3202 }
3203 break;
3204 }
3205 default:
3206 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003207 }
Calin Juravled0d48522014-11-04 16:40:20 +00003208}
3209
Calin Juravle9aec02f2014-11-18 23:06:35 +00003210void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3211 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3212
3213 LocationSummary* locations =
3214 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3215
3216 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003217 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003218 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003219 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003220 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003221 // The shift count needs to be in CL or a constant.
3222 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003223 locations->SetOut(Location::SameAsFirstInput());
3224 break;
3225 }
3226 default:
3227 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3228 }
3229}
3230
3231void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3232 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3233
3234 LocationSummary* locations = op->GetLocations();
3235 Location first = locations->InAt(0);
3236 Location second = locations->InAt(1);
3237 DCHECK(first.Equals(locations->Out()));
3238
3239 switch (op->GetResultType()) {
3240 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003241 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003242 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003243 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003244 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003245 DCHECK_EQ(ECX, second_reg);
3246 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003247 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003248 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003249 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003250 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003251 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003252 }
3253 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003254 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3255 if (shift == 0) {
3256 return;
3257 }
3258 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003259 if (op->IsShl()) {
3260 __ shll(first_reg, imm);
3261 } else if (op->IsShr()) {
3262 __ sarl(first_reg, imm);
3263 } else {
3264 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003265 }
3266 }
3267 break;
3268 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003269 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003270 if (second.IsRegister()) {
3271 Register second_reg = second.AsRegister<Register>();
3272 DCHECK_EQ(ECX, second_reg);
3273 if (op->IsShl()) {
3274 GenerateShlLong(first, second_reg);
3275 } else if (op->IsShr()) {
3276 GenerateShrLong(first, second_reg);
3277 } else {
3278 GenerateUShrLong(first, second_reg);
3279 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003280 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003281 // Shift by a constant.
3282 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3283 // Nothing to do if the shift is 0, as the input is already the output.
3284 if (shift != 0) {
3285 if (op->IsShl()) {
3286 GenerateShlLong(first, shift);
3287 } else if (op->IsShr()) {
3288 GenerateShrLong(first, shift);
3289 } else {
3290 GenerateUShrLong(first, shift);
3291 }
3292 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003293 }
3294 break;
3295 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003296 default:
3297 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3298 }
3299}
3300
Mark P Mendell73945692015-04-29 14:56:17 +00003301void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3302 Register low = loc.AsRegisterPairLow<Register>();
3303 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003304 if (shift == 1) {
3305 // This is just an addition.
3306 __ addl(low, low);
3307 __ adcl(high, high);
3308 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003309 // Shift by 32 is easy. High gets low, and low gets 0.
3310 codegen_->EmitParallelMoves(
3311 loc.ToLow(),
3312 loc.ToHigh(),
3313 Primitive::kPrimInt,
3314 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3315 loc.ToLow(),
3316 Primitive::kPrimInt);
3317 } else if (shift > 32) {
3318 // Low part becomes 0. High part is low part << (shift-32).
3319 __ movl(high, low);
3320 __ shll(high, Immediate(shift - 32));
3321 __ xorl(low, low);
3322 } else {
3323 // Between 1 and 31.
3324 __ shld(high, low, Immediate(shift));
3325 __ shll(low, Immediate(shift));
3326 }
3327}
3328
Calin Juravle9aec02f2014-11-18 23:06:35 +00003329void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003330 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003331 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3332 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3333 __ testl(shifter, Immediate(32));
3334 __ j(kEqual, &done);
3335 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3336 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3337 __ Bind(&done);
3338}
3339
Mark P Mendell73945692015-04-29 14:56:17 +00003340void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3341 Register low = loc.AsRegisterPairLow<Register>();
3342 Register high = loc.AsRegisterPairHigh<Register>();
3343 if (shift == 32) {
3344 // Need to copy the sign.
3345 DCHECK_NE(low, high);
3346 __ movl(low, high);
3347 __ sarl(high, Immediate(31));
3348 } else if (shift > 32) {
3349 DCHECK_NE(low, high);
3350 // High part becomes sign. Low part is shifted by shift - 32.
3351 __ movl(low, high);
3352 __ sarl(high, Immediate(31));
3353 __ sarl(low, Immediate(shift - 32));
3354 } else {
3355 // Between 1 and 31.
3356 __ shrd(low, high, Immediate(shift));
3357 __ sarl(high, Immediate(shift));
3358 }
3359}
3360
Calin Juravle9aec02f2014-11-18 23:06:35 +00003361void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003362 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003363 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3364 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3365 __ testl(shifter, Immediate(32));
3366 __ j(kEqual, &done);
3367 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3368 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3369 __ Bind(&done);
3370}
3371
Mark P Mendell73945692015-04-29 14:56:17 +00003372void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3373 Register low = loc.AsRegisterPairLow<Register>();
3374 Register high = loc.AsRegisterPairHigh<Register>();
3375 if (shift == 32) {
3376 // Shift by 32 is easy. Low gets high, and high gets 0.
3377 codegen_->EmitParallelMoves(
3378 loc.ToHigh(),
3379 loc.ToLow(),
3380 Primitive::kPrimInt,
3381 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3382 loc.ToHigh(),
3383 Primitive::kPrimInt);
3384 } else if (shift > 32) {
3385 // Low part is high >> (shift - 32). High part becomes 0.
3386 __ movl(low, high);
3387 __ shrl(low, Immediate(shift - 32));
3388 __ xorl(high, high);
3389 } else {
3390 // Between 1 and 31.
3391 __ shrd(low, high, Immediate(shift));
3392 __ shrl(high, Immediate(shift));
3393 }
3394}
3395
Calin Juravle9aec02f2014-11-18 23:06:35 +00003396void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003397 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003398 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3399 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3400 __ testl(shifter, Immediate(32));
3401 __ j(kEqual, &done);
3402 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3403 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3404 __ Bind(&done);
3405}
3406
3407void LocationsBuilderX86::VisitShl(HShl* shl) {
3408 HandleShift(shl);
3409}
3410
3411void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3412 HandleShift(shl);
3413}
3414
3415void LocationsBuilderX86::VisitShr(HShr* shr) {
3416 HandleShift(shr);
3417}
3418
3419void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3420 HandleShift(shr);
3421}
3422
3423void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3424 HandleShift(ushr);
3425}
3426
3427void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3428 HandleShift(ushr);
3429}
3430
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003431void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003432 LocationSummary* locations =
3433 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003434 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003435 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003436 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003437 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003438}
3439
3440void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3441 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003442 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003443 // Note: if heap poisoning is enabled, the entry point takes cares
3444 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003445 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3446 instruction,
3447 instruction->GetDexPc(),
3448 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003449 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003450}
3451
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003452void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3453 LocationSummary* locations =
3454 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3455 locations->SetOut(Location::RegisterLocation(EAX));
3456 InvokeRuntimeCallingConvention calling_convention;
3457 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003458 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003459 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003460}
3461
3462void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3463 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003464 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3465
Roland Levillain4d027112015-07-01 15:41:14 +01003466 // Note: if heap poisoning is enabled, the entry point takes cares
3467 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003468 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3469 instruction,
3470 instruction->GetDexPc(),
3471 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003472 DCHECK(!codegen_->IsLeafMethod());
3473}
3474
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003475void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003476 LocationSummary* locations =
3477 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003478 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3479 if (location.IsStackSlot()) {
3480 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3481 } else if (location.IsDoubleStackSlot()) {
3482 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003483 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003484 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003485}
3486
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003487void InstructionCodeGeneratorX86::VisitParameterValue(
3488 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3489}
3490
3491void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3492 LocationSummary* locations =
3493 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3494 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3495}
3496
3497void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003498}
3499
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003500void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003501 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003502 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003503 locations->SetInAt(0, Location::RequiresRegister());
3504 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003505}
3506
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003507void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3508 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003509 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003510 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003511 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003512 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003513 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003514 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003515 break;
3516
3517 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003518 __ notl(out.AsRegisterPairLow<Register>());
3519 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003520 break;
3521
3522 default:
3523 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3524 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003525}
3526
David Brazdil66d126e2015-04-03 16:02:44 +01003527void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3528 LocationSummary* locations =
3529 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3530 locations->SetInAt(0, Location::RequiresRegister());
3531 locations->SetOut(Location::SameAsFirstInput());
3532}
3533
3534void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003535 LocationSummary* locations = bool_not->GetLocations();
3536 Location in = locations->InAt(0);
3537 Location out = locations->Out();
3538 DCHECK(in.Equals(out));
3539 __ xorl(out.AsRegister<Register>(), Immediate(1));
3540}
3541
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003542void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003543 LocationSummary* locations =
3544 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003545 switch (compare->InputAt(0)->GetType()) {
3546 case Primitive::kPrimLong: {
3547 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003548 locations->SetInAt(1, Location::Any());
3549 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3550 break;
3551 }
3552 case Primitive::kPrimFloat:
3553 case Primitive::kPrimDouble: {
3554 locations->SetInAt(0, Location::RequiresFpuRegister());
3555 locations->SetInAt(1, Location::RequiresFpuRegister());
3556 locations->SetOut(Location::RequiresRegister());
3557 break;
3558 }
3559 default:
3560 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3561 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003562}
3563
3564void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003565 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003566 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003567 Location left = locations->InAt(0);
3568 Location right = locations->InAt(1);
3569
Mark Mendell0c9497d2015-08-21 09:30:05 -04003570 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003571 switch (compare->InputAt(0)->GetType()) {
3572 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003573 Register left_low = left.AsRegisterPairLow<Register>();
3574 Register left_high = left.AsRegisterPairHigh<Register>();
3575 int32_t val_low = 0;
3576 int32_t val_high = 0;
3577 bool right_is_const = false;
3578
3579 if (right.IsConstant()) {
3580 DCHECK(right.GetConstant()->IsLongConstant());
3581 right_is_const = true;
3582 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3583 val_low = Low32Bits(val);
3584 val_high = High32Bits(val);
3585 }
3586
Calin Juravleddb7df22014-11-25 20:56:51 +00003587 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003588 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003589 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003590 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003591 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003592 DCHECK(right_is_const) << right;
3593 if (val_high == 0) {
3594 __ testl(left_high, left_high);
3595 } else {
3596 __ cmpl(left_high, Immediate(val_high));
3597 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003598 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003599 __ j(kLess, &less); // Signed compare.
3600 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003601 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003602 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003603 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003604 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003605 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003606 DCHECK(right_is_const) << right;
3607 if (val_low == 0) {
3608 __ testl(left_low, left_low);
3609 } else {
3610 __ cmpl(left_low, Immediate(val_low));
3611 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003612 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003613 break;
3614 }
3615 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003617 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3618 break;
3619 }
3620 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003621 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003622 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003623 break;
3624 }
3625 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003626 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003627 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003628 __ movl(out, Immediate(0));
3629 __ j(kEqual, &done);
3630 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3631
3632 __ Bind(&greater);
3633 __ movl(out, Immediate(1));
3634 __ jmp(&done);
3635
3636 __ Bind(&less);
3637 __ movl(out, Immediate(-1));
3638
3639 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003640}
3641
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003642void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003643 LocationSummary* locations =
3644 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003645 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3646 locations->SetInAt(i, Location::Any());
3647 }
3648 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003649}
3650
3651void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07003652 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003653 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003654}
3655
Calin Juravle52c48962014-12-16 17:02:57 +00003656void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3657 /*
3658 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3659 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3660 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3661 */
3662 switch (kind) {
3663 case MemBarrierKind::kAnyAny: {
3664 __ mfence();
3665 break;
3666 }
3667 case MemBarrierKind::kAnyStore:
3668 case MemBarrierKind::kLoadAny:
3669 case MemBarrierKind::kStoreStore: {
3670 // nop
3671 break;
3672 }
3673 default:
3674 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003675 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003676}
3677
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003678
Vladimir Marko58155012015-08-19 12:49:41 +00003679void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3680 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3681 switch (invoke->GetMethodLoadKind()) {
3682 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3683 // temp = thread->string_init_entrypoint
3684 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3685 break;
3686 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3687 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3688 break;
3689 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3690 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3691 break;
3692 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3693 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3694 method_patches_.emplace_back(invoke->GetTargetMethod());
3695 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3696 break;
3697 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative:
3698 // TODO: Implement this type. For the moment, we fall back to kDexCacheViaMethod.
3699 FALLTHROUGH_INTENDED;
3700 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3701 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3702 Register method_reg;
3703 Register reg = temp.AsRegister<Register>();
3704 if (current_method.IsRegister()) {
3705 method_reg = current_method.AsRegister<Register>();
3706 } else {
3707 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3708 DCHECK(!current_method.IsValid());
3709 method_reg = reg;
3710 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3711 }
3712 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003713 __ movl(reg, Address(method_reg,
3714 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003715 // temp = temp[index_in_cache]
3716 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3717 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3718 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003719 }
Vladimir Marko58155012015-08-19 12:49:41 +00003720 }
3721
3722 switch (invoke->GetCodePtrLocation()) {
3723 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3724 __ call(GetFrameEntryLabel());
3725 break;
3726 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3727 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3728 Label* label = &relative_call_patches_.back().label;
3729 __ call(label); // Bind to the patch label, override at link time.
3730 __ Bind(label); // Bind the label at the end of the "call" insn.
3731 break;
3732 }
3733 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3734 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3735 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3736 // (Though the direct CALL ptr16:32 is available for consideration).
3737 FALLTHROUGH_INTENDED;
3738 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3739 // (callee_method + offset_of_quick_compiled_code)()
3740 __ call(Address(callee_method.AsRegister<Register>(),
3741 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3742 kX86WordSize).Int32Value()));
3743 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003744 }
3745
3746 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003747}
3748
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003749void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3750 Register temp = temp_in.AsRegister<Register>();
3751 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3752 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3753 LocationSummary* locations = invoke->GetLocations();
3754 Location receiver = locations->InAt(0);
3755 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3756 // temp = object->GetClass();
3757 DCHECK(receiver.IsRegister());
3758 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3759 MaybeRecordImplicitNullCheck(invoke);
3760 __ MaybeUnpoisonHeapReference(temp);
3761 // temp = temp->GetMethodAt(method_offset);
3762 __ movl(temp, Address(temp, method_offset));
3763 // call temp->GetEntryPoint();
3764 __ call(Address(
3765 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3766}
3767
Vladimir Marko58155012015-08-19 12:49:41 +00003768void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3769 DCHECK(linker_patches->empty());
3770 linker_patches->reserve(method_patches_.size() + relative_call_patches_.size());
3771 for (const MethodPatchInfo<Label>& info : method_patches_) {
3772 // The label points to the end of the "movl" insn but the literal offset for method
3773 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3774 uint32_t literal_offset = info.label.Position() - 4;
3775 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3776 info.target_method.dex_file,
3777 info.target_method.dex_method_index));
3778 }
3779 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
3780 // The label points to the end of the "call" insn but the literal offset for method
3781 // patch x86 needs to point to the embedded constant which occupies the last 4 bytes.
3782 uint32_t literal_offset = info.label.Position() - 4;
3783 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3784 info.target_method.dex_file,
3785 info.target_method.dex_method_index));
3786 }
3787}
3788
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003789void CodeGeneratorX86::MarkGCCard(Register temp,
3790 Register card,
3791 Register object,
3792 Register value,
3793 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003794 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003795 if (value_can_be_null) {
3796 __ testl(value, value);
3797 __ j(kEqual, &is_null);
3798 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003799 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3800 __ movl(temp, object);
3801 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003802 __ movb(Address(temp, card, TIMES_1, 0),
3803 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003804 if (value_can_be_null) {
3805 __ Bind(&is_null);
3806 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003807}
3808
Calin Juravle52c48962014-12-16 17:02:57 +00003809void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3810 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003811 LocationSummary* locations =
3812 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003813 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003814
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003815 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3816 locations->SetOut(Location::RequiresFpuRegister());
3817 } else {
3818 // The output overlaps in case of long: we don't want the low move to overwrite
3819 // the object's location.
3820 locations->SetOut(Location::RequiresRegister(),
3821 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3822 : Location::kNoOutputOverlap);
3823 }
Calin Juravle52c48962014-12-16 17:02:57 +00003824
3825 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3826 // Long values can be loaded atomically into an XMM using movsd.
3827 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
3828 // and then copy the XMM into the output 32bits at a time).
3829 locations->AddTemp(Location::RequiresFpuRegister());
3830 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003831}
3832
Calin Juravle52c48962014-12-16 17:02:57 +00003833void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
3834 const FieldInfo& field_info) {
3835 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003836
Calin Juravle52c48962014-12-16 17:02:57 +00003837 LocationSummary* locations = instruction->GetLocations();
3838 Register base = locations->InAt(0).AsRegister<Register>();
3839 Location out = locations->Out();
3840 bool is_volatile = field_info.IsVolatile();
3841 Primitive::Type field_type = field_info.GetFieldType();
3842 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
3843
3844 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003845 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00003846 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003847 break;
3848 }
3849
3850 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00003851 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003852 break;
3853 }
3854
3855 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00003856 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003857 break;
3858 }
3859
3860 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00003861 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003862 break;
3863 }
3864
3865 case Primitive::kPrimInt:
3866 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00003867 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003868 break;
3869 }
3870
3871 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00003872 if (is_volatile) {
3873 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
3874 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003875 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003876 __ movd(out.AsRegisterPairLow<Register>(), temp);
3877 __ psrlq(temp, Immediate(32));
3878 __ movd(out.AsRegisterPairHigh<Register>(), temp);
3879 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003880 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00003881 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00003882 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00003883 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
3884 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003885 break;
3886 }
3887
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003888 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00003889 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003890 break;
3891 }
3892
3893 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00003894 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00003895 break;
3896 }
3897
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003898 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00003899 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07003900 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003901 }
Calin Juravle52c48962014-12-16 17:02:57 +00003902
Calin Juravle77520bc2015-01-12 18:45:46 +00003903 // Longs are handled in the switch.
3904 if (field_type != Primitive::kPrimLong) {
3905 codegen_->MaybeRecordImplicitNullCheck(instruction);
3906 }
3907
Calin Juravle52c48962014-12-16 17:02:57 +00003908 if (is_volatile) {
3909 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
3910 }
Roland Levillain4d027112015-07-01 15:41:14 +01003911
3912 if (field_type == Primitive::kPrimNot) {
3913 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
3914 }
Calin Juravle52c48962014-12-16 17:02:57 +00003915}
3916
3917void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
3918 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3919
3920 LocationSummary* locations =
3921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3922 locations->SetInAt(0, Location::RequiresRegister());
3923 bool is_volatile = field_info.IsVolatile();
3924 Primitive::Type field_type = field_info.GetFieldType();
3925 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
3926 || (field_type == Primitive::kPrimByte);
3927
3928 // The register allocator does not support multiple
3929 // inputs that die at entry with one in a specific register.
3930 if (is_byte_type) {
3931 // Ensure the value is in a byte register.
3932 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003933 } else if (Primitive::IsFloatingPointType(field_type)) {
3934 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00003935 } else {
3936 locations->SetInAt(1, Location::RequiresRegister());
3937 }
Calin Juravle52c48962014-12-16 17:02:57 +00003938 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
Roland Levillain4d027112015-07-01 15:41:14 +01003939 // Temporary registers for the write barrier.
3940 locations->AddTemp(Location::RequiresRegister()); // Possibly used for reference poisoning too.
Calin Juravle52c48962014-12-16 17:02:57 +00003941 // Ensure the card is in a byte register.
3942 locations->AddTemp(Location::RegisterLocation(ECX));
3943 } else if (is_volatile && (field_type == Primitive::kPrimLong)) {
3944 // 64bits value can be atomically written to an address with movsd and an XMM register.
3945 // We need two XMM registers because there's no easier way to (bit) copy a register pair
3946 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
3947 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
3948 // isolated cases when we need this it isn't worth adding the extra complexity.
3949 locations->AddTemp(Location::RequiresFpuRegister());
3950 locations->AddTemp(Location::RequiresFpuRegister());
3951 }
3952}
3953
3954void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003955 const FieldInfo& field_info,
3956 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00003957 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
3958
3959 LocationSummary* locations = instruction->GetLocations();
3960 Register base = locations->InAt(0).AsRegister<Register>();
3961 Location value = locations->InAt(1);
3962 bool is_volatile = field_info.IsVolatile();
3963 Primitive::Type field_type = field_info.GetFieldType();
3964 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01003965 bool needs_write_barrier =
3966 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00003967
3968 if (is_volatile) {
3969 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
3970 }
3971
3972 switch (field_type) {
3973 case Primitive::kPrimBoolean:
3974 case Primitive::kPrimByte: {
3975 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
3976 break;
3977 }
3978
3979 case Primitive::kPrimShort:
3980 case Primitive::kPrimChar: {
3981 __ movw(Address(base, offset), value.AsRegister<Register>());
3982 break;
3983 }
3984
3985 case Primitive::kPrimInt:
3986 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01003987 if (kPoisonHeapReferences && needs_write_barrier) {
3988 // Note that in the case where `value` is a null reference,
3989 // we do not enter this block, as the reference does not
3990 // need poisoning.
3991 DCHECK_EQ(field_type, Primitive::kPrimNot);
3992 Register temp = locations->GetTemp(0).AsRegister<Register>();
3993 __ movl(temp, value.AsRegister<Register>());
3994 __ PoisonHeapReference(temp);
3995 __ movl(Address(base, offset), temp);
3996 } else {
3997 __ movl(Address(base, offset), value.AsRegister<Register>());
3998 }
Calin Juravle52c48962014-12-16 17:02:57 +00003999 break;
4000 }
4001
4002 case Primitive::kPrimLong: {
4003 if (is_volatile) {
4004 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4005 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4006 __ movd(temp1, value.AsRegisterPairLow<Register>());
4007 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4008 __ punpckldq(temp1, temp2);
4009 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004010 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004011 } else {
4012 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004013 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004014 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4015 }
4016 break;
4017 }
4018
4019 case Primitive::kPrimFloat: {
4020 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4021 break;
4022 }
4023
4024 case Primitive::kPrimDouble: {
4025 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4026 break;
4027 }
4028
4029 case Primitive::kPrimVoid:
4030 LOG(FATAL) << "Unreachable type " << field_type;
4031 UNREACHABLE();
4032 }
4033
Calin Juravle77520bc2015-01-12 18:45:46 +00004034 // Longs are handled in the switch.
4035 if (field_type != Primitive::kPrimLong) {
4036 codegen_->MaybeRecordImplicitNullCheck(instruction);
4037 }
4038
Roland Levillain4d027112015-07-01 15:41:14 +01004039 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004040 Register temp = locations->GetTemp(0).AsRegister<Register>();
4041 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004042 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004043 }
4044
Calin Juravle52c48962014-12-16 17:02:57 +00004045 if (is_volatile) {
4046 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4047 }
4048}
4049
4050void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4051 HandleFieldGet(instruction, instruction->GetFieldInfo());
4052}
4053
4054void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4055 HandleFieldGet(instruction, instruction->GetFieldInfo());
4056}
4057
4058void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4059 HandleFieldSet(instruction, instruction->GetFieldInfo());
4060}
4061
4062void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004063 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004064}
4065
4066void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4067 HandleFieldSet(instruction, instruction->GetFieldInfo());
4068}
4069
4070void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004071 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004072}
4073
4074void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4075 HandleFieldGet(instruction, instruction->GetFieldInfo());
4076}
4077
4078void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4079 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004080}
4081
Calin Juravle23a8e352015-09-08 19:56:31 +01004082void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4083 HUnresolvedInstanceFieldGet* instruction) {
4084 FieldAccessCallingConvetionX86 calling_convention;
4085 codegen_->CreateUnresolvedFieldLocationSummary(
4086 instruction, instruction->GetFieldType(), calling_convention);
4087}
4088
4089void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4090 HUnresolvedInstanceFieldGet* instruction) {
4091 codegen_->GenerateUnresolvedFieldAccess(instruction,
4092 instruction->GetFieldType(),
4093 instruction->GetFieldIndex(),
4094 instruction->GetDexPc());
4095}
4096
4097void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4098 HUnresolvedInstanceFieldSet* instruction) {
4099 FieldAccessCallingConvetionX86 calling_convention;
4100 codegen_->CreateUnresolvedFieldLocationSummary(
4101 instruction, instruction->GetFieldType(), calling_convention);
4102}
4103
4104void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4105 HUnresolvedInstanceFieldSet* instruction) {
4106 codegen_->GenerateUnresolvedFieldAccess(instruction,
4107 instruction->GetFieldType(),
4108 instruction->GetFieldIndex(),
4109 instruction->GetDexPc());
4110}
4111
4112void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4113 HUnresolvedStaticFieldGet* instruction) {
4114 FieldAccessCallingConvetionX86 calling_convention;
4115 codegen_->CreateUnresolvedFieldLocationSummary(
4116 instruction, instruction->GetFieldType(), calling_convention);
4117}
4118
4119void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4120 HUnresolvedStaticFieldGet* instruction) {
4121 codegen_->GenerateUnresolvedFieldAccess(instruction,
4122 instruction->GetFieldType(),
4123 instruction->GetFieldIndex(),
4124 instruction->GetDexPc());
4125}
4126
4127void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4128 HUnresolvedStaticFieldSet* instruction) {
4129 FieldAccessCallingConvetionX86 calling_convention;
4130 codegen_->CreateUnresolvedFieldLocationSummary(
4131 instruction, instruction->GetFieldType(), calling_convention);
4132}
4133
4134void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4135 HUnresolvedStaticFieldSet* instruction) {
4136 codegen_->GenerateUnresolvedFieldAccess(instruction,
4137 instruction->GetFieldType(),
4138 instruction->GetFieldIndex(),
4139 instruction->GetDexPc());
4140}
4141
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004142void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004143 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4144 ? LocationSummary::kCallOnSlowPath
4145 : LocationSummary::kNoCall;
4146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4147 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004148 ? Location::RequiresRegister()
4149 : Location::Any();
4150 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004151 if (instruction->HasUses()) {
4152 locations->SetOut(Location::SameAsFirstInput());
4153 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004154}
4155
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004156void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004157 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4158 return;
4159 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004160 LocationSummary* locations = instruction->GetLocations();
4161 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004162
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004163 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4164 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4165}
4166
4167void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01004168 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004169 codegen_->AddSlowPath(slow_path);
4170
4171 LocationSummary* locations = instruction->GetLocations();
4172 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004173
4174 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004175 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004176 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004177 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004178 } else {
4179 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004180 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004181 __ jmp(slow_path->GetEntryLabel());
4182 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004183 }
4184 __ j(kEqual, slow_path->GetEntryLabel());
4185}
4186
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004187void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004188 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004189 GenerateImplicitNullCheck(instruction);
4190 } else {
4191 GenerateExplicitNullCheck(instruction);
4192 }
4193}
4194
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004195void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004196 LocationSummary* locations =
4197 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004198 locations->SetInAt(0, Location::RequiresRegister());
4199 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004200 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4201 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4202 } else {
4203 // The output overlaps in case of long: we don't want the low move to overwrite
4204 // the array's location.
4205 locations->SetOut(Location::RequiresRegister(),
4206 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4207 : Location::kNoOutputOverlap);
4208 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004209}
4210
4211void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4212 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004213 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004214 Location index = locations->InAt(1);
4215
Calin Juravle77520bc2015-01-12 18:45:46 +00004216 Primitive::Type type = instruction->GetType();
4217 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004218 case Primitive::kPrimBoolean: {
4219 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004220 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004221 if (index.IsConstant()) {
4222 __ movzxb(out, Address(obj,
4223 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4224 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004225 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004226 }
4227 break;
4228 }
4229
4230 case Primitive::kPrimByte: {
4231 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004232 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233 if (index.IsConstant()) {
4234 __ movsxb(out, Address(obj,
4235 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4236 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004237 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004238 }
4239 break;
4240 }
4241
4242 case Primitive::kPrimShort: {
4243 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004244 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004245 if (index.IsConstant()) {
4246 __ movsxw(out, Address(obj,
4247 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4248 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004249 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004250 }
4251 break;
4252 }
4253
4254 case Primitive::kPrimChar: {
4255 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004256 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004257 if (index.IsConstant()) {
4258 __ movzxw(out, Address(obj,
4259 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4260 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004261 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004262 }
4263 break;
4264 }
4265
4266 case Primitive::kPrimInt:
4267 case Primitive::kPrimNot: {
4268 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004269 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004270 if (index.IsConstant()) {
4271 __ movl(out, Address(obj,
4272 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4273 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004274 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004275 }
4276 break;
4277 }
4278
4279 case Primitive::kPrimLong: {
4280 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004281 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004282 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004283 if (index.IsConstant()) {
4284 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004285 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004286 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004287 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004288 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004289 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004290 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004291 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004292 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004293 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004294 }
4295 break;
4296 }
4297
Mark Mendell7c8d0092015-01-26 11:21:33 -05004298 case Primitive::kPrimFloat: {
4299 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4300 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4301 if (index.IsConstant()) {
4302 __ movss(out, Address(obj,
4303 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4304 } else {
4305 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4306 }
4307 break;
4308 }
4309
4310 case Primitive::kPrimDouble: {
4311 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4312 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4313 if (index.IsConstant()) {
4314 __ movsd(out, Address(obj,
4315 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4316 } else {
4317 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4318 }
4319 break;
4320 }
4321
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004322 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004323 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004324 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004325 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004326
4327 if (type != Primitive::kPrimLong) {
4328 codegen_->MaybeRecordImplicitNullCheck(instruction);
4329 }
Roland Levillain4d027112015-07-01 15:41:14 +01004330
4331 if (type == Primitive::kPrimNot) {
4332 Register out = locations->Out().AsRegister<Register>();
4333 __ MaybeUnpoisonHeapReference(out);
4334 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004335}
4336
4337void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004338 // This location builder might end up asking to up to four registers, which is
4339 // not currently possible for baseline. The situation in which we need four
4340 // registers cannot be met by baseline though, because it has not run any
4341 // optimization.
4342
Nicolas Geoffray39468442014-09-02 15:17:15 +01004343 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004344 bool needs_write_barrier =
4345 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4346
Mark Mendell5f874182015-03-04 15:42:45 -05004347 bool needs_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004348
Nicolas Geoffray39468442014-09-02 15:17:15 +01004349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4350 instruction,
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004351 needs_runtime_call ? LocationSummary::kCall : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004352
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004353 if (needs_runtime_call) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004354 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004355 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4356 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4357 locations->SetInAt(2, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004358 } else {
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004359 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4360 || (value_type == Primitive::kPrimByte);
Nicolas Geoffray9ae0daa2014-09-30 22:40:23 +01004361 // We need the inputs to be different than the output in case of long operation.
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004362 // In case of a byte operation, the register allocator does not support multiple
4363 // inputs that die at entry with one in a specific register.
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004364 locations->SetInAt(0, Location::RequiresRegister());
4365 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray7adfcc82014-10-07 12:24:52 +01004366 if (is_byte_type) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004367 // Ensure the value is in a byte register.
4368 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004369 } else if (Primitive::IsFloatingPointType(value_type)) {
4370 locations->SetInAt(2, Location::RequiresFpuRegister());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004371 } else {
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004372 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004373 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004374 if (needs_write_barrier) {
Roland Levillain4d027112015-07-01 15:41:14 +01004375 // Temporary registers for the write barrier.
4376 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004377 // Ensure the card is in a byte register.
4378 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004379 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004380 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004381}
4382
4383void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4384 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004385 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004387 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004388 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004389 bool needs_runtime_call = locations->WillCall();
4390 bool needs_write_barrier =
4391 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004392
4393 switch (value_type) {
4394 case Primitive::kPrimBoolean:
4395 case Primitive::kPrimByte: {
4396 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004397 if (index.IsConstant()) {
4398 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004399 if (value.IsRegister()) {
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004400 __ movb(Address(obj, offset), value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004401 } else {
4402 __ movb(Address(obj, offset),
4403 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4404 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004405 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004406 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004407 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004408 value.AsRegister<ByteRegister>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004409 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004410 __ movb(Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004411 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4412 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004413 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004414 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004415 break;
4416 }
4417
4418 case Primitive::kPrimShort:
4419 case Primitive::kPrimChar: {
4420 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004421 if (index.IsConstant()) {
4422 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004423 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004424 __ movw(Address(obj, offset), value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004425 } else {
4426 __ movw(Address(obj, offset),
4427 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4428 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004429 } else {
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004430 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004431 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
4432 value.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004433 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004434 __ movw(Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004435 Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
4436 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004437 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004438 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004439 break;
4440 }
4441
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004442 case Primitive::kPrimInt:
4443 case Primitive::kPrimNot: {
4444 if (!needs_runtime_call) {
4445 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4446 if (index.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00004447 size_t offset =
4448 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004449 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004450 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4451 Register temp = locations->GetTemp(0).AsRegister<Register>();
4452 __ movl(temp, value.AsRegister<Register>());
4453 __ PoisonHeapReference(temp);
4454 __ movl(Address(obj, offset), temp);
4455 } else {
4456 __ movl(Address(obj, offset), value.AsRegister<Register>());
4457 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004458 } else {
4459 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004460 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4461 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4462 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4463 // Note: if heap poisoning is enabled, no need to poison
4464 // (negate) `v` if it is a reference, as it would be null.
4465 __ movl(Address(obj, offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004466 }
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004467 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004468 DCHECK(index.IsRegister()) << index;
4469 if (value.IsRegister()) {
Roland Levillain4d027112015-07-01 15:41:14 +01004470 if (kPoisonHeapReferences && value_type == Primitive::kPrimNot) {
4471 Register temp = locations->GetTemp(0).AsRegister<Register>();
4472 __ movl(temp, value.AsRegister<Register>());
4473 __ PoisonHeapReference(temp);
4474 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), temp);
4475 } else {
4476 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4477 value.AsRegister<Register>());
4478 }
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004479 } else {
4480 DCHECK(value.IsConstant()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004481 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4482 // `value_type == Primitive::kPrimNot` implies `v == 0`.
4483 DCHECK((value_type != Primitive::kPrimNot) || (v == 0));
4484 // Note: if heap poisoning is enabled, no need to poison
4485 // (negate) `v` if it is a reference, as it would be null.
4486 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset), Immediate(v));
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004487 }
4488 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004489 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004490
4491 if (needs_write_barrier) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004492 Register temp = locations->GetTemp(0).AsRegister<Register>();
4493 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004494 codegen_->MarkGCCard(
4495 temp, card, obj, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004496 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004497 } else {
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004498 DCHECK_EQ(value_type, Primitive::kPrimNot);
4499 DCHECK(!codegen_->IsLeafMethod());
Roland Levillain4d027112015-07-01 15:41:14 +01004500 // Note: if heap poisoning is enabled, pAputObject takes cares
4501 // of poisoning the reference.
Alexandre Rames8158f282015-08-07 10:26:17 +01004502 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
4503 instruction,
4504 instruction->GetDexPc(),
4505 nullptr);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004506 }
4507 break;
4508 }
4509
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004510 case Primitive::kPrimLong: {
4511 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004512 if (index.IsConstant()) {
4513 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004514 if (value.IsRegisterPair()) {
4515 __ movl(Address(obj, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004516 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004517 __ movl(Address(obj, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004518 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004519 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004520 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
4521 __ movl(Address(obj, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004522 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004523 __ movl(Address(obj, offset + kX86WordSize), Immediate(High32Bits(val)));
4524 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004525 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004526 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004527 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004528 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004529 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004530 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004531 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004532 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004533 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004534 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004535 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004536 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004537 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00004538 __ movl(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004539 Immediate(High32Bits(val)));
4540 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004541 }
4542 break;
4543 }
4544
Mark Mendell7c8d0092015-01-26 11:21:33 -05004545 case Primitive::kPrimFloat: {
4546 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4547 DCHECK(value.IsFpuRegister());
4548 if (index.IsConstant()) {
4549 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4550 __ movss(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4551 } else {
4552 __ movss(Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset),
4553 value.AsFpuRegister<XmmRegister>());
4554 }
4555 break;
4556 }
4557
4558 case Primitive::kPrimDouble: {
4559 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4560 DCHECK(value.IsFpuRegister());
4561 if (index.IsConstant()) {
4562 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
4563 __ movsd(Address(obj, offset), value.AsFpuRegister<XmmRegister>());
4564 } else {
4565 __ movsd(Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset),
4566 value.AsFpuRegister<XmmRegister>());
4567 }
4568 break;
4569 }
4570
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004571 case Primitive::kPrimVoid:
4572 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004573 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004574 }
4575}
4576
4577void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4578 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004579 locations->SetInAt(0, Location::RequiresRegister());
4580 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004581}
4582
4583void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4584 LocationSummary* locations = instruction->GetLocations();
4585 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004586 Register obj = locations->InAt(0).AsRegister<Register>();
4587 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004588 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004589 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590}
4591
4592void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004593 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4594 ? LocationSummary::kCallOnSlowPath
4595 : LocationSummary::kNoCall;
4596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004597 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004598 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004599 if (instruction->HasUses()) {
4600 locations->SetOut(Location::SameAsFirstInput());
4601 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004602}
4603
4604void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4605 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004606 Location index_loc = locations->InAt(0);
4607 Location length_loc = locations->InAt(1);
4608 SlowPathCodeX86* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004609 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004610
Mark Mendell99dbd682015-04-22 16:18:52 -04004611 if (length_loc.IsConstant()) {
4612 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4613 if (index_loc.IsConstant()) {
4614 // BCE will remove the bounds check if we are guarenteed to pass.
4615 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4616 if (index < 0 || index >= length) {
4617 codegen_->AddSlowPath(slow_path);
4618 __ jmp(slow_path->GetEntryLabel());
4619 } else {
4620 // Some optimization after BCE may have generated this, and we should not
4621 // generate a bounds check if it is a valid range.
4622 }
4623 return;
4624 }
4625
4626 // We have to reverse the jump condition because the length is the constant.
4627 Register index_reg = index_loc.AsRegister<Register>();
4628 __ cmpl(index_reg, Immediate(length));
4629 codegen_->AddSlowPath(slow_path);
4630 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004631 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004632 Register length = length_loc.AsRegister<Register>();
4633 if (index_loc.IsConstant()) {
4634 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4635 __ cmpl(length, Immediate(value));
4636 } else {
4637 __ cmpl(length, index_loc.AsRegister<Register>());
4638 }
4639 codegen_->AddSlowPath(slow_path);
4640 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004641 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004642}
4643
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004644void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4645 temp->SetLocations(nullptr);
4646}
4647
4648void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp) {
4649 // Nothing to do, this is driven by the code generator.
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004650 UNUSED(temp);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004651}
4652
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004653void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07004654 UNUSED(instruction);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004655 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004656}
4657
4658void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004659 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4660}
4661
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004662void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4663 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4664}
4665
4666void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004667 HBasicBlock* block = instruction->GetBlock();
4668 if (block->GetLoopInformation() != nullptr) {
4669 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4670 // The back edge will generate the suspend check.
4671 return;
4672 }
4673 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4674 // The goto will generate the suspend check.
4675 return;
4676 }
4677 GenerateSuspendCheck(instruction, nullptr);
4678}
4679
4680void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4681 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004682 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004683 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4684 if (slow_path == nullptr) {
4685 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4686 instruction->SetSlowPath(slow_path);
4687 codegen_->AddSlowPath(slow_path);
4688 if (successor != nullptr) {
4689 DCHECK(successor->IsLoopHeader());
4690 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4691 }
4692 } else {
4693 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4694 }
4695
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004696 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004697 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004698 if (successor == nullptr) {
4699 __ j(kNotEqual, slow_path->GetEntryLabel());
4700 __ Bind(slow_path->GetReturnLabel());
4701 } else {
4702 __ j(kEqual, codegen_->GetLabelOf(successor));
4703 __ jmp(slow_path->GetEntryLabel());
4704 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004705}
4706
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004707X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4708 return codegen_->GetAssembler();
4709}
4710
Mark Mendell7c8d0092015-01-26 11:21:33 -05004711void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004712 ScratchRegisterScope ensure_scratch(
4713 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4714 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4715 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4716 __ movl(temp_reg, Address(ESP, src + stack_offset));
4717 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004718}
4719
4720void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004721 ScratchRegisterScope ensure_scratch(
4722 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4723 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4724 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4725 __ movl(temp_reg, Address(ESP, src + stack_offset));
4726 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4727 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4728 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004729}
4730
4731void ParallelMoveResolverX86::EmitMove(size_t index) {
4732 MoveOperands* move = moves_.Get(index);
4733 Location source = move->GetSource();
4734 Location destination = move->GetDestination();
4735
4736 if (source.IsRegister()) {
4737 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004738 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004739 } else {
4740 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004741 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004742 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004743 } else if (source.IsFpuRegister()) {
4744 if (destination.IsFpuRegister()) {
4745 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4746 } else if (destination.IsStackSlot()) {
4747 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4748 } else {
4749 DCHECK(destination.IsDoubleStackSlot());
4750 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4751 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004752 } else if (source.IsStackSlot()) {
4753 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004754 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004755 } else if (destination.IsFpuRegister()) {
4756 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004757 } else {
4758 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004759 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4760 }
4761 } else if (source.IsDoubleStackSlot()) {
4762 if (destination.IsFpuRegister()) {
4763 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4764 } else {
4765 DCHECK(destination.IsDoubleStackSlot()) << destination;
4766 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004767 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004768 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004769 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004770 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004771 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004772 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004773 if (value == 0) {
4774 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
4775 } else {
4776 __ movl(destination.AsRegister<Register>(), Immediate(value));
4777 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004778 } else {
4779 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05004780 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004781 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004782 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004783 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004784 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004785 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004786 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004787 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4788 if (value == 0) {
4789 // Easy handling of 0.0.
4790 __ xorps(dest, dest);
4791 } else {
4792 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004793 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4794 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
4795 __ movl(temp, Immediate(value));
4796 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004797 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004798 } else {
4799 DCHECK(destination.IsStackSlot()) << destination;
4800 __ movl(Address(ESP, destination.GetStackIndex()), imm);
4801 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004802 } else if (constant->IsLongConstant()) {
4803 int64_t value = constant->AsLongConstant()->GetValue();
4804 int32_t low_value = Low32Bits(value);
4805 int32_t high_value = High32Bits(value);
4806 Immediate low(low_value);
4807 Immediate high(high_value);
4808 if (destination.IsDoubleStackSlot()) {
4809 __ movl(Address(ESP, destination.GetStackIndex()), low);
4810 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4811 } else {
4812 __ movl(destination.AsRegisterPairLow<Register>(), low);
4813 __ movl(destination.AsRegisterPairHigh<Register>(), high);
4814 }
4815 } else {
4816 DCHECK(constant->IsDoubleConstant());
4817 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00004818 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004819 int32_t low_value = Low32Bits(value);
4820 int32_t high_value = High32Bits(value);
4821 Immediate low(low_value);
4822 Immediate high(high_value);
4823 if (destination.IsFpuRegister()) {
4824 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
4825 if (value == 0) {
4826 // Easy handling of 0.0.
4827 __ xorpd(dest, dest);
4828 } else {
4829 __ pushl(high);
4830 __ pushl(low);
4831 __ movsd(dest, Address(ESP, 0));
4832 __ addl(ESP, Immediate(8));
4833 }
4834 } else {
4835 DCHECK(destination.IsDoubleStackSlot()) << destination;
4836 __ movl(Address(ESP, destination.GetStackIndex()), low);
4837 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
4838 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004839 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004840 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00004841 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004842 }
4843}
4844
Mark Mendella5c19ce2015-04-01 12:51:05 -04004845void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004846 Register suggested_scratch = reg == EAX ? EBX : EAX;
4847 ScratchRegisterScope ensure_scratch(
4848 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
4849
4850 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4851 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
4852 __ movl(Address(ESP, mem + stack_offset), reg);
4853 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004854}
4855
Mark Mendell7c8d0092015-01-26 11:21:33 -05004856void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004857 ScratchRegisterScope ensure_scratch(
4858 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4859
4860 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4861 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4862 __ movl(temp_reg, Address(ESP, mem + stack_offset));
4863 __ movss(Address(ESP, mem + stack_offset), reg);
4864 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004865}
4866
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004867void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004868 ScratchRegisterScope ensure_scratch1(
4869 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004870
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004871 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
4872 ScratchRegisterScope ensure_scratch2(
4873 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01004874
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004875 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
4876 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
4877 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
4878 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
4879 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
4880 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004881}
4882
4883void ParallelMoveResolverX86::EmitSwap(size_t index) {
4884 MoveOperands* move = moves_.Get(index);
4885 Location source = move->GetSource();
4886 Location destination = move->GetDestination();
4887
4888 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04004889 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
4890 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
4891 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
4892 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
4893 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004894 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004895 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004896 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004897 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004898 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
4899 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004900 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
4901 // Use XOR Swap algorithm to avoid a temporary.
4902 DCHECK_NE(source.reg(), destination.reg());
4903 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4904 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
4905 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4906 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
4907 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
4908 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
4909 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004910 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
4911 // Take advantage of the 16 bytes in the XMM register.
4912 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
4913 Address stack(ESP, destination.GetStackIndex());
4914 // Load the double into the high doubleword.
4915 __ movhpd(reg, stack);
4916
4917 // Store the low double into the destination.
4918 __ movsd(stack, reg);
4919
4920 // Move the high double to the low double.
4921 __ psrldq(reg, Immediate(8));
4922 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
4923 // Take advantage of the 16 bytes in the XMM register.
4924 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
4925 Address stack(ESP, source.GetStackIndex());
4926 // Load the double into the high doubleword.
4927 __ movhpd(reg, stack);
4928
4929 // Store the low double into the destination.
4930 __ movsd(stack, reg);
4931
4932 // Move the high double to the low double.
4933 __ psrldq(reg, Immediate(8));
4934 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
4935 Exchange(destination.GetStackIndex(), source.GetStackIndex());
4936 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004937 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004938 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004939 }
4940}
4941
4942void ParallelMoveResolverX86::SpillScratch(int reg) {
4943 __ pushl(static_cast<Register>(reg));
4944}
4945
4946void ParallelMoveResolverX86::RestoreScratch(int reg) {
4947 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004948}
4949
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004950void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004951 LocationSummary::CallKind call_kind = cls->CanCallRuntime()
4952 ? LocationSummary::kCallOnSlowPath
4953 : LocationSummary::kNoCall;
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004954 LocationSummary* locations =
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004955 new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004956 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004957 locations->SetOut(Location::RequiresRegister());
4958}
4959
4960void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004961 LocationSummary* locations = cls->GetLocations();
4962 Register out = locations->Out().AsRegister<Register>();
4963 Register current_method = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004964 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004965 DCHECK(!cls->CanCallRuntime());
4966 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07004967 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004968 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004969 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004970 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01004971 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004972 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01004973 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004974
4975 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
4976 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
4977 codegen_->AddSlowPath(slow_path);
4978 __ testl(out, out);
4979 __ j(kEqual, slow_path->GetEntryLabel());
4980 if (cls->MustGenerateClinitCheck()) {
4981 GenerateClassInitializationCheck(slow_path, out);
4982 } else {
4983 __ Bind(slow_path->GetExitLabel());
4984 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01004985 }
4986}
4987
4988void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
4989 LocationSummary* locations =
4990 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
4991 locations->SetInAt(0, Location::RequiresRegister());
4992 if (check->HasUses()) {
4993 locations->SetOut(Location::SameAsFirstInput());
4994 }
4995}
4996
4997void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00004998 // We assume the class to not be null.
4999 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5000 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005001 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005002 GenerateClassInitializationCheck(slow_path,
5003 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005004}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005005
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005006void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
5007 SlowPathCodeX86* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005008 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5009 Immediate(mirror::Class::kStatusInitialized));
5010 __ j(kLess, slow_path->GetEntryLabel());
5011 __ Bind(slow_path->GetExitLabel());
5012 // No need for memory fence, thanks to the X86 memory model.
5013}
5014
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005015void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5016 LocationSummary* locations =
5017 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005018 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005019 locations->SetOut(Location::RequiresRegister());
5020}
5021
5022void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
5023 SlowPathCodeX86* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5024 codegen_->AddSlowPath(slow_path);
5025
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005026 LocationSummary* locations = load->GetLocations();
5027 Register out = locations->Out().AsRegister<Register>();
5028 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07005029 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08005030 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005031 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005032 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005033 __ testl(out, out);
5034 __ j(kEqual, slow_path->GetEntryLabel());
5035 __ Bind(slow_path->GetExitLabel());
5036}
5037
David Brazdilcb1c0552015-08-04 16:22:25 +01005038static Address GetExceptionTlsAddress() {
5039 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5040}
5041
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005042void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5043 LocationSummary* locations =
5044 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5045 locations->SetOut(Location::RequiresRegister());
5046}
5047
5048void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005049 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5050}
5051
5052void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5053 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5054}
5055
5056void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5057 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005058}
5059
5060void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5061 LocationSummary* locations =
5062 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5063 InvokeRuntimeCallingConvention calling_convention;
5064 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5065}
5066
5067void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005068 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5069 instruction,
5070 instruction->GetDexPc(),
5071 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005072}
5073
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005074void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray75374372015-09-17 17:12:19 +00005075 LocationSummary::CallKind call_kind = instruction->IsClassFinal()
5076 ? LocationSummary::kNoCall
5077 : LocationSummary::kCallOnSlowPath;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005078 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005079 locations->SetInAt(0, Location::RequiresRegister());
5080 locations->SetInAt(1, Location::Any());
5081 // Note that TypeCheckSlowPathX86 uses this register too.
5082 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005083}
5084
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005085void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005086 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005087 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005088 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005089 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005090 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005091 NearLabel done, zero;
Nicolas Geoffray75374372015-09-17 17:12:19 +00005092 SlowPathCodeX86* slow_path = nullptr;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005093
5094 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005095 // Avoid null check if we know obj is not null.
5096 if (instruction->MustDoNullCheck()) {
5097 __ testl(obj, obj);
5098 __ j(kEqual, &zero);
5099 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00005100 // Compare the class of `obj` with `cls`.
5101 __ movl(out, Address(obj, class_offset));
5102 __ MaybeUnpoisonHeapReference(out);
5103 if (cls.IsRegister()) {
5104 __ cmpl(out, cls.AsRegister<Register>());
5105 } else {
5106 DCHECK(cls.IsStackSlot()) << cls;
5107 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005108 }
5109
Nicolas Geoffray75374372015-09-17 17:12:19 +00005110 if (instruction->IsClassFinal()) {
5111 // Classes must be equal for the instanceof to succeed.
5112 __ j(kNotEqual, &zero);
5113 __ movl(out, Immediate(1));
5114 __ jmp(&done);
5115 } else {
5116 // If the classes are not equal, we go into a slow path.
5117 DCHECK(locations->OnlyCallsOnSlowPath());
5118 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
5119 codegen_->AddSlowPath(slow_path);
5120 __ j(kNotEqual, slow_path->GetEntryLabel());
5121 __ movl(out, Immediate(1));
5122 __ jmp(&done);
5123 }
5124
5125 if (instruction->MustDoNullCheck() || instruction->IsClassFinal()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005126 __ Bind(&zero);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005127 __ movl(out, Immediate(0));
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005128 }
5129
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005130 if (slow_path != nullptr) {
5131 __ Bind(slow_path->GetExitLabel());
5132 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00005133 __ Bind(&done);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005134}
5135
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005136void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
5137 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray75374372015-09-17 17:12:19 +00005138 instruction, LocationSummary::kCallOnSlowPath);
5139 locations->SetInAt(0, Location::RequiresRegister());
5140 locations->SetInAt(1, Location::Any());
5141 // Note that TypeCheckSlowPathX86 uses this register too.
5142 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005143}
5144
5145void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5146 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005147 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005148 Location cls = locations->InAt(1);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005149 Register temp = locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005150 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray75374372015-09-17 17:12:19 +00005151 SlowPathCodeX86* slow_path =
5152 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction);
5153 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005154
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005155 // Avoid null check if we know obj is not null.
5156 if (instruction->MustDoNullCheck()) {
5157 __ testl(obj, obj);
Nicolas Geoffray75374372015-09-17 17:12:19 +00005158 __ j(kEqual, slow_path->GetExitLabel());
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005159 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00005160 // Compare the class of `obj` with `cls`.
5161 __ movl(temp, Address(obj, class_offset));
5162 __ MaybeUnpoisonHeapReference(temp);
5163 if (cls.IsRegister()) {
5164 __ cmpl(temp, cls.AsRegister<Register>());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005165 } else {
Nicolas Geoffray75374372015-09-17 17:12:19 +00005166 DCHECK(cls.IsStackSlot()) << cls;
5167 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005168 }
Nicolas Geoffray75374372015-09-17 17:12:19 +00005169 // The checkcast succeeds if the classes are equal (fast path).
5170 // Otherwise, we need to go into the slow path to check the types.
5171 __ j(kNotEqual, slow_path->GetEntryLabel());
5172 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005173}
5174
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005175void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5176 LocationSummary* locations =
5177 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5178 InvokeRuntimeCallingConvention calling_convention;
5179 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5180}
5181
5182void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005183 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5184 : QUICK_ENTRY_POINT(pUnlockObject),
5185 instruction,
5186 instruction->GetDexPc(),
5187 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005188}
5189
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005190void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5191void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5192void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5193
5194void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5195 LocationSummary* locations =
5196 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5197 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5198 || instruction->GetResultType() == Primitive::kPrimLong);
5199 locations->SetInAt(0, Location::RequiresRegister());
5200 locations->SetInAt(1, Location::Any());
5201 locations->SetOut(Location::SameAsFirstInput());
5202}
5203
5204void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5205 HandleBitwiseOperation(instruction);
5206}
5207
5208void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5209 HandleBitwiseOperation(instruction);
5210}
5211
5212void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5213 HandleBitwiseOperation(instruction);
5214}
5215
5216void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5217 LocationSummary* locations = instruction->GetLocations();
5218 Location first = locations->InAt(0);
5219 Location second = locations->InAt(1);
5220 DCHECK(first.Equals(locations->Out()));
5221
5222 if (instruction->GetResultType() == Primitive::kPrimInt) {
5223 if (second.IsRegister()) {
5224 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005225 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005226 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005227 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005228 } else {
5229 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005230 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005231 }
5232 } else if (second.IsConstant()) {
5233 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005234 __ andl(first.AsRegister<Register>(),
5235 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005236 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005237 __ orl(first.AsRegister<Register>(),
5238 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005239 } else {
5240 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005241 __ xorl(first.AsRegister<Register>(),
5242 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005243 }
5244 } else {
5245 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005246 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005247 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005248 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005249 } else {
5250 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005251 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005252 }
5253 }
5254 } else {
5255 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5256 if (second.IsRegisterPair()) {
5257 if (instruction->IsAnd()) {
5258 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5259 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5260 } else if (instruction->IsOr()) {
5261 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5262 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5263 } else {
5264 DCHECK(instruction->IsXor());
5265 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5266 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5267 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005268 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005269 if (instruction->IsAnd()) {
5270 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5271 __ andl(first.AsRegisterPairHigh<Register>(),
5272 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5273 } else if (instruction->IsOr()) {
5274 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5275 __ orl(first.AsRegisterPairHigh<Register>(),
5276 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5277 } else {
5278 DCHECK(instruction->IsXor());
5279 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5280 __ xorl(first.AsRegisterPairHigh<Register>(),
5281 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5282 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005283 } else {
5284 DCHECK(second.IsConstant()) << second;
5285 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005286 int32_t low_value = Low32Bits(value);
5287 int32_t high_value = High32Bits(value);
5288 Immediate low(low_value);
5289 Immediate high(high_value);
5290 Register first_low = first.AsRegisterPairLow<Register>();
5291 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005292 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005293 if (low_value == 0) {
5294 __ xorl(first_low, first_low);
5295 } else if (low_value != -1) {
5296 __ andl(first_low, low);
5297 }
5298 if (high_value == 0) {
5299 __ xorl(first_high, first_high);
5300 } else if (high_value != -1) {
5301 __ andl(first_high, high);
5302 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005303 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005304 if (low_value != 0) {
5305 __ orl(first_low, low);
5306 }
5307 if (high_value != 0) {
5308 __ orl(first_high, high);
5309 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005310 } else {
5311 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005312 if (low_value != 0) {
5313 __ xorl(first_low, low);
5314 }
5315 if (high_value != 0) {
5316 __ xorl(first_high, high);
5317 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005318 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005319 }
5320 }
5321}
5322
Calin Juravleb1498f62015-02-16 13:13:29 +00005323void LocationsBuilderX86::VisitBoundType(HBoundType* instruction) {
5324 // Nothing to do, this should be removed during prepare for register allocator.
5325 UNUSED(instruction);
5326 LOG(FATAL) << "Unreachable";
5327}
5328
5329void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction) {
5330 // Nothing to do, this should be removed during prepare for register allocator.
5331 UNUSED(instruction);
5332 LOG(FATAL) << "Unreachable";
5333}
5334
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005335void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5336 DCHECK(codegen_->IsBaseline());
5337 LocationSummary* locations =
5338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5339 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5340}
5341
5342void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5343 DCHECK(codegen_->IsBaseline());
5344 // Will be generated at use site.
5345}
5346
Mark Mendell0616ae02015-04-17 12:49:27 -04005347void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5348 HX86ComputeBaseMethodAddress* insn) {
5349 LocationSummary* locations =
5350 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5351 locations->SetOut(Location::RequiresRegister());
5352}
5353
5354void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5355 HX86ComputeBaseMethodAddress* insn) {
5356 LocationSummary* locations = insn->GetLocations();
5357 Register reg = locations->Out().AsRegister<Register>();
5358
5359 // Generate call to next instruction.
5360 Label next_instruction;
5361 __ call(&next_instruction);
5362 __ Bind(&next_instruction);
5363
5364 // Remember this offset for later use with constant area.
5365 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5366
5367 // Grab the return address off the stack.
5368 __ popl(reg);
5369}
5370
5371void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5372 HX86LoadFromConstantTable* insn) {
5373 LocationSummary* locations =
5374 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5375
5376 locations->SetInAt(0, Location::RequiresRegister());
5377 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5378
5379 // If we don't need to be materialized, we only need the inputs to be set.
5380 if (!insn->NeedsMaterialization()) {
5381 return;
5382 }
5383
5384 switch (insn->GetType()) {
5385 case Primitive::kPrimFloat:
5386 case Primitive::kPrimDouble:
5387 locations->SetOut(Location::RequiresFpuRegister());
5388 break;
5389
5390 case Primitive::kPrimInt:
5391 locations->SetOut(Location::RequiresRegister());
5392 break;
5393
5394 default:
5395 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5396 }
5397}
5398
5399void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5400 if (!insn->NeedsMaterialization()) {
5401 return;
5402 }
5403
5404 LocationSummary* locations = insn->GetLocations();
5405 Location out = locations->Out();
5406 Register const_area = locations->InAt(0).AsRegister<Register>();
5407 HConstant *value = insn->GetConstant();
5408
5409 switch (insn->GetType()) {
5410 case Primitive::kPrimFloat:
5411 __ movss(out.AsFpuRegister<XmmRegister>(),
5412 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5413 break;
5414
5415 case Primitive::kPrimDouble:
5416 __ movsd(out.AsFpuRegister<XmmRegister>(),
5417 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5418 break;
5419
5420 case Primitive::kPrimInt:
5421 __ movl(out.AsRegister<Register>(),
5422 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5423 break;
5424
5425 default:
5426 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5427 }
5428}
5429
5430void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
5431 // Generate the constant area if needed.
5432 X86Assembler* assembler = GetAssembler();
5433 if (!assembler->IsConstantAreaEmpty()) {
5434 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
5435 // byte values.
5436 assembler->Align(4, 0);
5437 constant_area_start_ = assembler->CodeSize();
5438 assembler->AddConstantArea();
5439 }
5440
5441 // And finish up.
5442 CodeGenerator::Finalize(allocator);
5443}
5444
5445/**
5446 * Class to handle late fixup of offsets into constant area.
5447 */
5448class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocMisc> {
5449 public:
5450 RIPFixup(const CodeGeneratorX86& codegen, int offset)
5451 : codegen_(codegen), offset_into_constant_area_(offset) {}
5452
5453 private:
5454 void Process(const MemoryRegion& region, int pos) OVERRIDE {
5455 // Patch the correct offset for the instruction. The place to patch is the
5456 // last 4 bytes of the instruction.
5457 // The value to patch is the distance from the offset in the constant area
5458 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
5459 int32_t constant_offset = codegen_.ConstantAreaStart() + offset_into_constant_area_;
5460 int32_t relative_position = constant_offset - codegen_.GetMethodAddressOffset();;
5461
5462 // Patch in the right value.
5463 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
5464 }
5465
5466 const CodeGeneratorX86& codegen_;
5467
5468 // Location in constant area that the fixup refers to.
5469 int offset_into_constant_area_;
5470};
5471
5472Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
5473 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
5474 return Address(reg, kDummy32BitOffset, fixup);
5475}
5476
5477Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
5478 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
5479 return Address(reg, kDummy32BitOffset, fixup);
5480}
5481
5482Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
5483 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
5484 return Address(reg, kDummy32BitOffset, fixup);
5485}
5486
5487Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
5488 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
5489 return Address(reg, kDummy32BitOffset, fixup);
5490}
5491
5492/**
5493 * Finds instructions that need the constant area base as an input.
5494 */
5495class ConstantHandlerVisitor : public HGraphVisitor {
5496 public:
5497 explicit ConstantHandlerVisitor(HGraph* graph) : HGraphVisitor(graph), base_(nullptr) {}
5498
5499 private:
5500 void VisitAdd(HAdd* add) OVERRIDE {
5501 BinaryFP(add);
5502 }
5503
5504 void VisitSub(HSub* sub) OVERRIDE {
5505 BinaryFP(sub);
5506 }
5507
5508 void VisitMul(HMul* mul) OVERRIDE {
5509 BinaryFP(mul);
5510 }
5511
5512 void VisitDiv(HDiv* div) OVERRIDE {
5513 BinaryFP(div);
5514 }
5515
5516 void VisitReturn(HReturn* ret) OVERRIDE {
5517 HConstant* value = ret->InputAt(0)->AsConstant();
5518 if ((value != nullptr && Primitive::IsFloatingPointType(value->GetType()))) {
5519 ReplaceInput(ret, value, 0, true);
5520 }
5521 }
5522
5523 void VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) OVERRIDE {
5524 HandleInvoke(invoke);
5525 }
5526
5527 void VisitInvokeVirtual(HInvokeVirtual* invoke) OVERRIDE {
5528 HandleInvoke(invoke);
5529 }
5530
5531 void VisitInvokeInterface(HInvokeInterface* invoke) OVERRIDE {
5532 HandleInvoke(invoke);
5533 }
5534
5535 void BinaryFP(HBinaryOperation* bin) {
5536 HConstant* rhs = bin->InputAt(1)->AsConstant();
5537 if (rhs != nullptr && Primitive::IsFloatingPointType(bin->GetResultType())) {
5538 ReplaceInput(bin, rhs, 1, false);
5539 }
5540 }
5541
5542 void InitializeConstantAreaPointer(HInstruction* user) {
5543 // Ensure we only initialize the pointer once.
5544 if (base_ != nullptr) {
5545 return;
5546 }
5547
5548 HGraph* graph = GetGraph();
5549 HBasicBlock* entry = graph->GetEntryBlock();
5550 base_ = new (graph->GetArena()) HX86ComputeBaseMethodAddress();
5551 HInstruction* insert_pos = (user->GetBlock() == entry) ? user : entry->GetLastInstruction();
5552 entry->InsertInstructionBefore(base_, insert_pos);
5553 DCHECK(base_ != nullptr);
5554 }
5555
5556 void ReplaceInput(HInstruction* insn, HConstant* value, int input_index, bool materialize) {
5557 InitializeConstantAreaPointer(insn);
5558 HGraph* graph = GetGraph();
5559 HBasicBlock* block = insn->GetBlock();
5560 HX86LoadFromConstantTable* load_constant =
5561 new (graph->GetArena()) HX86LoadFromConstantTable(base_, value, materialize);
5562 block->InsertInstructionBefore(load_constant, insn);
5563 insn->ReplaceInput(load_constant, input_index);
5564 }
5565
5566 void HandleInvoke(HInvoke* invoke) {
5567 // Ensure that we can load FP arguments from the constant area.
5568 for (size_t i = 0, e = invoke->InputCount(); i < e; i++) {
5569 HConstant* input = invoke->InputAt(i)->AsConstant();
5570 if (input != nullptr && Primitive::IsFloatingPointType(input->GetType())) {
5571 ReplaceInput(invoke, input, i, true);
5572 }
5573 }
5574 }
5575
5576 // The generated HX86ComputeBaseMethodAddress in the entry block needed as an
5577 // input to the HX86LoadFromConstantTable instructions.
5578 HX86ComputeBaseMethodAddress* base_;
5579};
5580
5581void ConstantAreaFixups::Run() {
5582 ConstantHandlerVisitor visitor(graph_);
5583 visitor.VisitInsertionOrder();
5584}
5585
Roland Levillain4d027112015-07-01 15:41:14 +01005586#undef __
5587
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00005588} // namespace x86
5589} // namespace art