blob: d5d6c210bf5f9d064b7ea3c124f3e9266738b471 [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"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Vladimir Marko0f7dca42015-11-02 14:36:43 +000029#include "pc_relative_fixups_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000038namespace x86 {
39
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010040static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010041static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042
Mark Mendell5f874182015-03-04 15:42:45 -050043static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010044
Mark Mendell24f2dfa2015-01-14 19:51:45 -050045static constexpr int kC2ConditionMask = 0x400;
46
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000047static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000048
Roland Levillain62a46b22015-06-01 18:24:13 +010049#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010050#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010051
Andreas Gampe85b62f22015-09-09 13:15:38 -070052class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010054 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055
Alexandre Rames2ed20af2015-03-06 13:55:35 +000056 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010057 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000059 if (instruction_->CanThrowIntoCatchBlock()) {
60 // Live registers will be restored in the catch block if caught.
61 SaveLiveRegisters(codegen, instruction_->GetLocations());
62 }
Alexandre Rames8158f282015-08-07 10:26:17 +010063 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
64 instruction_,
65 instruction_->GetDexPc(),
66 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010067 }
68
Alexandre Rames8158f282015-08-07 10:26:17 +010069 bool IsFatal() const OVERRIDE { return true; }
70
Alexandre Rames9931f312015-06-19 14:47:01 +010071 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
72
Nicolas Geoffraye5038322014-07-04 09:41:32 +010073 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010074 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010075 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
76};
77
Andreas Gampe85b62f22015-09-09 13:15:38 -070078class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000079 public:
80 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
81
Alexandre Rames2ed20af2015-03-06 13:55:35 +000082 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010083 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000084 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000085 if (instruction_->CanThrowIntoCatchBlock()) {
86 // Live registers will be restored in the catch block if caught.
87 SaveLiveRegisters(codegen, instruction_->GetLocations());
88 }
Alexandre Rames8158f282015-08-07 10:26:17 +010089 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
90 instruction_,
91 instruction_->GetDexPc(),
92 this);
Calin Juravled0d48522014-11-04 16:40:20 +000093 }
94
Alexandre Rames8158f282015-08-07 10:26:17 +010095 bool IsFatal() const OVERRIDE { return true; }
96
Alexandre Rames9931f312015-06-19 14:47:01 +010097 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
98
Calin Juravled0d48522014-11-04 16:40:20 +000099 private:
100 HDivZeroCheck* const instruction_;
101 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
102};
103
Andreas Gampe85b62f22015-09-09 13:15:38 -0700104class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100106 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000107
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000108 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000110 if (is_div_) {
111 __ negl(reg_);
112 } else {
113 __ movl(reg_, Immediate(0));
114 }
Calin Juravled0d48522014-11-04 16:40:20 +0000115 __ jmp(GetExitLabel());
116 }
117
Alexandre Rames9931f312015-06-19 14:47:01 +0100118 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
119
Calin Juravled0d48522014-11-04 16:40:20 +0000120 private:
121 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000122 bool is_div_;
123 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000124};
125
Andreas Gampe85b62f22015-09-09 13:15:38 -0700126class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100127 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100128 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000130 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100132 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000134 // We're moving two locations to locations that could overlap, so we need a parallel
135 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000136 if (instruction_->CanThrowIntoCatchBlock()) {
137 // Live registers will be restored in the catch block if caught.
138 SaveLiveRegisters(codegen, instruction_->GetLocations());
139 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000141 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100142 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000143 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100144 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100146 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
147 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100148 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
149 instruction_,
150 instruction_->GetDexPc(),
151 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100152 }
153
Alexandre Rames8158f282015-08-07 10:26:17 +0100154 bool IsFatal() const OVERRIDE { return true; }
155
Alexandre Rames9931f312015-06-19 14:47:01 +0100156 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
157
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100159 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160
161 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
162};
163
Andreas Gampe85b62f22015-09-09 13:15:38 -0700164class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000165 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000166 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100167 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000169 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100170 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100173 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
174 instruction_,
175 instruction_->GetDexPc(),
176 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100178 if (successor_ == nullptr) {
179 __ jmp(GetReturnLabel());
180 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100181 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100182 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000183 }
184
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 Label* GetReturnLabel() {
186 DCHECK(successor_ == nullptr);
187 return &return_label_;
188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100190 HBasicBlock* GetSuccessor() const {
191 return successor_;
192 }
193
Alexandre Rames9931f312015-06-19 14:47:01 +0100194 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
195
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196 private:
197 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100198 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 Label return_label_;
200
201 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
202};
203
Andreas Gampe85b62f22015-09-09 13:15:38 -0700204class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000205 public:
206 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
207
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000208 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000209 LocationSummary* locations = instruction_->GetLocations();
210 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
211
212 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
213 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000214 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215
216 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800217 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100218 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
219 instruction_,
220 instruction_->GetDexPc(),
221 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000222 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000223 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000224
225 __ jmp(GetExitLabel());
226 }
227
Alexandre Rames9931f312015-06-19 14:47:01 +0100228 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
229
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 private:
231 HLoadString* const instruction_;
232
233 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
234};
235
Andreas Gampe85b62f22015-09-09 13:15:38 -0700236class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000237 public:
238 LoadClassSlowPathX86(HLoadClass* cls,
239 HInstruction* at,
240 uint32_t dex_pc,
241 bool do_clinit)
242 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
243 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
244 }
245
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000246 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000247 LocationSummary* locations = at_->GetLocations();
248 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
249 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000250 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000251
252 InvokeRuntimeCallingConvention calling_convention;
253 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100254 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
255 : QUICK_ENTRY_POINT(pInitializeType),
256 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
258 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000259 Location out = locations->Out();
260 if (out.IsValid()) {
261 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
262 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000263 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000264
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 __ jmp(GetExitLabel());
267 }
268
Alexandre Rames9931f312015-06-19 14:47:01 +0100269 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
270
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000271 private:
272 // The class this slow path will load.
273 HLoadClass* const cls_;
274
275 // The instruction where this slow path is happening.
276 // (Might be the load class or an initialization check).
277 HInstruction* const at_;
278
279 // The dex PC of `at_`.
280 const uint32_t dex_pc_;
281
282 // Whether to initialize the class.
283 const bool do_clinit_;
284
285 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
286};
287
Andreas Gampe85b62f22015-09-09 13:15:38 -0700288class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000289 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000290 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
291 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000293 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000294 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100295 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
296 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000297 DCHECK(instruction_->IsCheckCast()
298 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
300 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
301 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302
303 if (instruction_->IsCheckCast()) {
304 // The codegen for the instruction overwrites `temp`, so put it back in place.
305 Register obj = locations->InAt(0).AsRegister<Register>();
306 Register temp = locations->GetTemp(0).AsRegister<Register>();
307 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
308 __ movl(temp, Address(obj, class_offset));
309 __ MaybeUnpoisonHeapReference(temp);
310 }
311
312 if (!is_fatal_) {
313 SaveLiveRegisters(codegen, locations);
314 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000315
316 // We're moving two locations to locations that could overlap, so we need a parallel
317 // move resolver.
318 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000319 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100320 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100322 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
325 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000326
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000327 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100328 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
329 instruction_,
330 instruction_->GetDexPc(),
331 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 } else {
333 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100334 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
335 instruction_,
336 instruction_->GetDexPc(),
337 this);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 }
339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000340 if (!is_fatal_) {
341 if (instruction_->IsInstanceOf()) {
342 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
343 }
344 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 __ jmp(GetExitLabel());
347 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000348 }
349
Alexandre Rames9931f312015-06-19 14:47:01 +0100350 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100352
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000354 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356
357 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
358};
359
Andreas Gampe85b62f22015-09-09 13:15:38 -0700360class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 public:
362 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
363 : instruction_(instruction) {}
364
365 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100366 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100367 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368 __ Bind(GetEntryLabel());
369 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100370 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
371 instruction_,
372 instruction_->GetDexPc(),
373 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 }
375
Alexandre Rames9931f312015-06-19 14:47:01 +0100376 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
377
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 private:
379 HInstruction* const instruction_;
380 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
381};
382
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100383class ArraySetSlowPathX86 : public SlowPathCode {
384 public:
385 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
386
387 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
388 LocationSummary* locations = instruction_->GetLocations();
389 __ Bind(GetEntryLabel());
390 SaveLiveRegisters(codegen, locations);
391
392 InvokeRuntimeCallingConvention calling_convention;
393 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
394 parallel_move.AddMove(
395 locations->InAt(0),
396 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
397 Primitive::kPrimNot,
398 nullptr);
399 parallel_move.AddMove(
400 locations->InAt(1),
401 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
402 Primitive::kPrimInt,
403 nullptr);
404 parallel_move.AddMove(
405 locations->InAt(2),
406 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
407 Primitive::kPrimNot,
408 nullptr);
409 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
410
411 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
412 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
413 instruction_,
414 instruction_->GetDexPc(),
415 this);
416 RestoreLiveRegisters(codegen, locations);
417 __ jmp(GetExitLabel());
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
421
422 private:
423 HInstruction* const instruction_;
424
425 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
426};
427
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100428#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100429#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100430
Aart Bike9f37602015-10-09 11:15:55 -0700431inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700432 switch (cond) {
433 case kCondEQ: return kEqual;
434 case kCondNE: return kNotEqual;
435 case kCondLT: return kLess;
436 case kCondLE: return kLessEqual;
437 case kCondGT: return kGreater;
438 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700439 case kCondB: return kBelow;
440 case kCondBE: return kBelowEqual;
441 case kCondA: return kAbove;
442 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700443 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100444 LOG(FATAL) << "Unreachable";
445 UNREACHABLE();
446}
447
Aart Bike9f37602015-10-09 11:15:55 -0700448// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100449inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
450 switch (cond) {
451 case kCondEQ: return kEqual;
452 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700453 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100454 case kCondLT: return kBelow;
455 case kCondLE: return kBelowEqual;
456 case kCondGT: return kAbove;
457 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700458 // Unsigned remain unchanged.
459 case kCondB: return kBelow;
460 case kCondBE: return kBelowEqual;
461 case kCondA: return kAbove;
462 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100463 }
464 LOG(FATAL) << "Unreachable";
465 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700466}
467
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100468void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100469 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100470}
471
472void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100473 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100474}
475
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100476size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
477 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
478 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100479}
480
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100481size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
482 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
483 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100484}
485
Mark Mendell7c8d0092015-01-26 11:21:33 -0500486size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
487 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
488 return GetFloatingPointSpillSlotSize();
489}
490
491size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
492 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
493 return GetFloatingPointSpillSlotSize();
494}
495
Calin Juravle175dc732015-08-25 15:42:32 +0100496void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
497 HInstruction* instruction,
498 uint32_t dex_pc,
499 SlowPathCode* slow_path) {
500 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
501 instruction,
502 dex_pc,
503 slow_path);
504}
505
506void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100507 HInstruction* instruction,
508 uint32_t dex_pc,
509 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100510 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100511 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100512 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100513}
514
Mark Mendellfb8d2792015-03-31 22:16:59 -0400515CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
516 const X86InstructionSetFeatures& isa_features,
Serban Constantinescuecc43662015-08-13 13:33:12 +0100517 const CompilerOptions& compiler_options,
518 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500519 : CodeGenerator(graph,
520 kNumberOfCpuRegisters,
521 kNumberOfXmmRegisters,
522 kNumberOfRegisterPairs,
523 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
524 arraysize(kCoreCalleeSaves))
525 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100526 0,
527 compiler_options,
528 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100529 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100530 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100531 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400532 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000533 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100534 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400535 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000536 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400537 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000538 // Use a fake return address register to mimic Quick.
539 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100540}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100541
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100542Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100543 switch (type) {
544 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100545 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100546 X86ManagedRegister pair =
547 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100548 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
549 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100550 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
551 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100552 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100553 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100554 }
555
556 case Primitive::kPrimByte:
557 case Primitive::kPrimBoolean:
558 case Primitive::kPrimChar:
559 case Primitive::kPrimShort:
560 case Primitive::kPrimInt:
561 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100562 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100563 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100564 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100565 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
566 X86ManagedRegister current =
567 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
568 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100569 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100570 }
571 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100572 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100573 }
574
575 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100576 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100577 return Location::FpuRegisterLocation(
578 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100579 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100580
581 case Primitive::kPrimVoid:
582 LOG(FATAL) << "Unreachable type " << type;
583 }
584
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100585 return Location();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100586}
587
Mark Mendell5f874182015-03-04 15:42:45 -0500588void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100589 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100590 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100591
592 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100593 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100594
Mark Mendell5f874182015-03-04 15:42:45 -0500595 if (is_baseline) {
596 blocked_core_registers_[EBP] = true;
597 blocked_core_registers_[ESI] = true;
598 blocked_core_registers_[EDI] = true;
599 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100600
601 UpdateBlockedPairRegisters();
602}
603
604void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
605 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
606 X86ManagedRegister current =
607 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
608 if (blocked_core_registers_[current.AsRegisterPairLow()]
609 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
610 blocked_register_pairs_[i] = true;
611 }
612 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100613}
614
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100615InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
616 : HGraphVisitor(graph),
617 assembler_(codegen->GetAssembler()),
618 codegen_(codegen) {}
619
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100620static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100621 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100622}
623
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000624void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100625 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000626 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000627 bool skip_overflow_check =
628 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000629 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000630
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000631 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100632 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100633 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100634 }
635
Mark Mendell5f874182015-03-04 15:42:45 -0500636 if (HasEmptyFrame()) {
637 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000638 }
Mark Mendell5f874182015-03-04 15:42:45 -0500639
640 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
641 Register reg = kCoreCalleeSaves[i];
642 if (allocated_registers_.ContainsCoreRegister(reg)) {
643 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100644 __ cfi().AdjustCFAOffset(kX86WordSize);
645 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500646 }
647 }
648
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100649 int adjust = GetFrameSize() - FrameEntrySpillSize();
650 __ subl(ESP, Immediate(adjust));
651 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100652 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000653}
654
655void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100656 __ cfi().RememberState();
657 if (!HasEmptyFrame()) {
658 int adjust = GetFrameSize() - FrameEntrySpillSize();
659 __ addl(ESP, Immediate(adjust));
660 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500661
David Srbeckyc34dc932015-04-12 09:27:43 +0100662 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
663 Register reg = kCoreCalleeSaves[i];
664 if (allocated_registers_.ContainsCoreRegister(reg)) {
665 __ popl(reg);
666 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
667 __ cfi().Restore(DWARFReg(reg));
668 }
Mark Mendell5f874182015-03-04 15:42:45 -0500669 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000670 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100671 __ ret();
672 __ cfi().RestoreState();
673 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000674}
675
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100676void CodeGeneratorX86::Bind(HBasicBlock* block) {
677 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000678}
679
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100680Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
681 switch (load->GetType()) {
682 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100683 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100684 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100685
686 case Primitive::kPrimInt:
687 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100688 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100689 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100690
691 case Primitive::kPrimBoolean:
692 case Primitive::kPrimByte:
693 case Primitive::kPrimChar:
694 case Primitive::kPrimShort:
695 case Primitive::kPrimVoid:
696 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700697 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100698 }
699
700 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700701 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100702}
703
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100704Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
705 switch (type) {
706 case Primitive::kPrimBoolean:
707 case Primitive::kPrimByte:
708 case Primitive::kPrimChar:
709 case Primitive::kPrimShort:
710 case Primitive::kPrimInt:
711 case Primitive::kPrimNot:
712 return Location::RegisterLocation(EAX);
713
714 case Primitive::kPrimLong:
715 return Location::RegisterPairLocation(EAX, EDX);
716
717 case Primitive::kPrimVoid:
718 return Location::NoLocation();
719
720 case Primitive::kPrimDouble:
721 case Primitive::kPrimFloat:
722 return Location::FpuRegisterLocation(XMM0);
723 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100724
725 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100726}
727
728Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
729 return Location::RegisterLocation(kMethodRegisterArgument);
730}
731
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100732Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100733 switch (type) {
734 case Primitive::kPrimBoolean:
735 case Primitive::kPrimByte:
736 case Primitive::kPrimChar:
737 case Primitive::kPrimShort:
738 case Primitive::kPrimInt:
739 case Primitive::kPrimNot: {
740 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000741 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100742 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100743 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100744 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000745 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100746 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100747 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100748
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000749 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100750 uint32_t index = gp_index_;
751 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000752 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100753 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100754 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
755 calling_convention.GetRegisterPairAt(index));
756 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100757 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000758 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
759 }
760 }
761
762 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100763 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000764 stack_index_++;
765 if (index < calling_convention.GetNumberOfFpuRegisters()) {
766 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
767 } else {
768 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
769 }
770 }
771
772 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100773 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000774 stack_index_ += 2;
775 if (index < calling_convention.GetNumberOfFpuRegisters()) {
776 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
777 } else {
778 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100779 }
780 }
781
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100782 case Primitive::kPrimVoid:
783 LOG(FATAL) << "Unexpected parameter type " << type;
784 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100785 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100786 return Location();
787}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100788
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100789void CodeGeneratorX86::Move32(Location destination, Location source) {
790 if (source.Equals(destination)) {
791 return;
792 }
793 if (destination.IsRegister()) {
794 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000795 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100796 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000797 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100798 } else {
799 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000800 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100801 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100802 } else if (destination.IsFpuRegister()) {
803 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000804 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100805 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000806 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100807 } else {
808 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000809 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100810 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100811 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000812 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100813 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000814 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100815 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000816 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -0500817 } else if (source.IsConstant()) {
818 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000819 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -0500820 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100821 } else {
822 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100823 __ pushl(Address(ESP, source.GetStackIndex()));
824 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100825 }
826 }
827}
828
829void CodeGeneratorX86::Move64(Location destination, Location source) {
830 if (source.Equals(destination)) {
831 return;
832 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100833 if (destination.IsRegisterPair()) {
834 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000835 EmitParallelMoves(
836 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
837 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100838 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000839 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100840 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
841 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100842 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +0100843 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
844 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
845 __ psrlq(src_reg, Immediate(32));
846 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100847 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000848 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100849 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100850 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
851 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100852 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
853 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100854 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -0500855 if (source.IsFpuRegister()) {
856 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
857 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000858 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +0100859 } else if (source.IsRegisterPair()) {
860 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
861 // Create stack space for 2 elements.
862 __ subl(ESP, Immediate(2 * elem_size));
863 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
864 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
865 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
866 // And remove the temporary stack space we allocated.
867 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100868 } else {
869 LOG(FATAL) << "Unimplemented";
870 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100871 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +0000872 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100873 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000874 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100875 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100876 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100877 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100878 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000879 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000880 } else if (source.IsConstant()) {
881 HConstant* constant = source.GetConstant();
882 int64_t value;
883 if (constant->IsLongConstant()) {
884 value = constant->AsLongConstant()->GetValue();
885 } else {
886 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +0000887 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000888 }
889 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
890 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100891 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +0000892 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000893 EmitParallelMoves(
894 Location::StackSlot(source.GetStackIndex()),
895 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100896 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +0000897 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100898 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
899 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100900 }
901 }
902}
903
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100904void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000905 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100906 if (instruction->IsCurrentMethod()) {
907 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
908 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000909 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100910 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +0000911 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +0000912 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
913 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +0000914 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000915 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +0000916 } else if (location.IsStackSlot()) {
917 __ movl(Address(ESP, location.GetStackIndex()), imm);
918 } else {
919 DCHECK(location.IsConstant());
920 DCHECK_EQ(location.GetConstant(), const_to_move);
921 }
922 } else if (const_to_move->IsLongConstant()) {
923 int64_t value = const_to_move->AsLongConstant()->GetValue();
924 if (location.IsRegisterPair()) {
925 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
926 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
927 } else if (location.IsDoubleStackSlot()) {
928 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +0000929 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
930 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +0000931 } else {
932 DCHECK(location.IsConstant());
933 DCHECK_EQ(location.GetConstant(), instruction);
934 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100935 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +0000936 } else if (instruction->IsTemporary()) {
937 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +0000938 if (temp_location.IsStackSlot()) {
939 Move32(location, temp_location);
940 } else {
941 DCHECK(temp_location.IsDoubleStackSlot());
942 Move64(location, temp_location);
943 }
Roland Levillain476df552014-10-09 17:51:36 +0100944 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100945 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100946 switch (instruction->GetType()) {
947 case Primitive::kPrimBoolean:
948 case Primitive::kPrimByte:
949 case Primitive::kPrimChar:
950 case Primitive::kPrimShort:
951 case Primitive::kPrimInt:
952 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100953 case Primitive::kPrimFloat:
954 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100955 break;
956
957 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100958 case Primitive::kPrimDouble:
959 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100960 break;
961
962 default:
963 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
964 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000965 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100966 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100967 switch (instruction->GetType()) {
968 case Primitive::kPrimBoolean:
969 case Primitive::kPrimByte:
970 case Primitive::kPrimChar:
971 case Primitive::kPrimShort:
972 case Primitive::kPrimInt:
973 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100974 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +0000975 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100976 break;
977
978 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100979 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +0000980 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100981 break;
982
983 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100984 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100985 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000986 }
987}
988
Calin Juravle175dc732015-08-25 15:42:32 +0100989void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
990 DCHECK(location.IsRegister());
991 __ movl(location.AsRegister<Register>(), Immediate(value));
992}
993
Calin Juravlee460d1d2015-09-29 04:52:17 +0100994void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
995 if (Primitive::Is64BitType(dst_type)) {
996 Move64(dst, src);
997 } else {
998 Move32(dst, src);
999 }
1000}
1001
1002void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1003 if (location.IsRegister()) {
1004 locations->AddTemp(location);
1005 } else if (location.IsRegisterPair()) {
1006 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1007 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1008 } else {
1009 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1010 }
1011}
1012
David Brazdilfc6a86a2015-06-26 10:33:45 +00001013void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001014 DCHECK(!successor->IsExitBlock());
1015
1016 HBasicBlock* block = got->GetBlock();
1017 HInstruction* previous = got->GetPrevious();
1018
1019 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001020 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001021 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1022 return;
1023 }
1024
1025 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1026 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1027 }
1028 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001029 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001030 }
1031}
1032
David Brazdilfc6a86a2015-06-26 10:33:45 +00001033void LocationsBuilderX86::VisitGoto(HGoto* got) {
1034 got->SetLocations(nullptr);
1035}
1036
1037void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1038 HandleGoto(got, got->GetSuccessor());
1039}
1040
1041void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1042 try_boundary->SetLocations(nullptr);
1043}
1044
1045void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1046 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1047 if (!successor->IsExitBlock()) {
1048 HandleGoto(try_boundary, successor);
1049 }
1050}
1051
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001052void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001053 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001054}
1055
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001056void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001057}
1058
Mark Mendellc4701932015-04-10 13:18:51 -04001059void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1060 Label* true_label,
1061 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001062 if (cond->IsFPConditionTrueIfNaN()) {
1063 __ j(kUnordered, true_label);
1064 } else if (cond->IsFPConditionFalseIfNaN()) {
1065 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001066 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001067 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001068}
1069
1070void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1071 Label* true_label,
1072 Label* false_label) {
1073 LocationSummary* locations = cond->GetLocations();
1074 Location left = locations->InAt(0);
1075 Location right = locations->InAt(1);
1076 IfCondition if_cond = cond->GetCondition();
1077
Mark Mendellc4701932015-04-10 13:18:51 -04001078 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001079 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001080 IfCondition true_high_cond = if_cond;
1081 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001082 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001083
1084 // Set the conditions for the test, remembering that == needs to be
1085 // decided using the low words.
1086 switch (if_cond) {
1087 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001088 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001089 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001090 break;
1091 case kCondLT:
1092 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001093 break;
1094 case kCondLE:
1095 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001096 break;
1097 case kCondGT:
1098 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001099 break;
1100 case kCondGE:
1101 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001102 break;
Aart Bike9f37602015-10-09 11:15:55 -07001103 case kCondB:
1104 false_high_cond = kCondA;
1105 break;
1106 case kCondBE:
1107 true_high_cond = kCondB;
1108 break;
1109 case kCondA:
1110 false_high_cond = kCondB;
1111 break;
1112 case kCondAE:
1113 true_high_cond = kCondA;
1114 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001115 }
1116
1117 if (right.IsConstant()) {
1118 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001119 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001120 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001121
1122 if (val_high == 0) {
1123 __ testl(left_high, left_high);
1124 } else {
1125 __ cmpl(left_high, Immediate(val_high));
1126 }
1127 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001128 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001129 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001130 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001131 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001132 __ j(X86Condition(true_high_cond), true_label);
1133 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001134 }
1135 // Must be equal high, so compare the lows.
1136 if (val_low == 0) {
1137 __ testl(left_low, left_low);
1138 } else {
1139 __ cmpl(left_low, Immediate(val_low));
1140 }
1141 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001142 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001143 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001144
1145 __ cmpl(left_high, right_high);
1146 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001147 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001148 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001149 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001150 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001151 __ j(X86Condition(true_high_cond), true_label);
1152 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001153 }
1154 // Must be equal high, so compare the lows.
1155 __ cmpl(left_low, right_low);
1156 }
1157 // The last comparison might be unsigned.
1158 __ j(final_condition, true_label);
1159}
1160
1161void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1162 HCondition* condition,
1163 Label* true_target,
1164 Label* false_target,
1165 Label* always_true_target) {
1166 LocationSummary* locations = condition->GetLocations();
1167 Location left = locations->InAt(0);
1168 Location right = locations->InAt(1);
1169
1170 // We don't want true_target as a nullptr.
1171 if (true_target == nullptr) {
1172 true_target = always_true_target;
1173 }
1174 bool falls_through = (false_target == nullptr);
1175
1176 // FP compares don't like null false_targets.
1177 if (false_target == nullptr) {
1178 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1179 }
1180
1181 Primitive::Type type = condition->InputAt(0)->GetType();
1182 switch (type) {
1183 case Primitive::kPrimLong:
1184 GenerateLongComparesAndJumps(condition, true_target, false_target);
1185 break;
1186 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001187 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1188 GenerateFPJumps(condition, true_target, false_target);
1189 break;
1190 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001191 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1192 GenerateFPJumps(condition, true_target, false_target);
1193 break;
1194 default:
1195 LOG(FATAL) << "Unexpected compare type " << type;
1196 }
1197
1198 if (!falls_through) {
1199 __ jmp(false_target);
1200 }
1201}
1202
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001203void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1204 Label* true_target,
1205 Label* false_target,
1206 Label* always_true_target) {
1207 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001208 if (cond->IsIntConstant()) {
1209 // Constant condition, statically compared against 1.
1210 int32_t cond_value = cond->AsIntConstant()->GetValue();
1211 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001212 if (always_true_target != nullptr) {
1213 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001214 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001215 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001216 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001217 DCHECK_EQ(cond_value, 0);
1218 }
1219 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001220 HCondition* condition = cond->AsCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001221 bool is_materialized =
Mark Mendellb8b97692015-05-22 16:58:19 -04001222 condition == nullptr || condition->NeedsMaterialization();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001223 // Moves do not affect the eflags register, so if the condition is
1224 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001225 // again. We can't use the eflags on long/FP conditions if they are
1226 // materialized due to the complex branching.
Mark Mendellb8b97692015-05-22 16:58:19 -04001227 Primitive::Type type = (condition != nullptr)
1228 ? cond->InputAt(0)->GetType()
1229 : Primitive::kPrimInt;
1230 bool eflags_set = condition != nullptr
1231 && condition->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001232 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
Mark Mendellb8b97692015-05-22 16:58:19 -04001233 // Can we optimize the jump if we know that the next block is the true case?
1234 bool can_jump_to_false = CanReverseCondition(always_true_target, false_target, condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001235 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001236 if (!eflags_set) {
1237 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001238 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001239 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001240 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001241 } else {
1242 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1243 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001244 if (can_jump_to_false) {
1245 __ j(kEqual, false_target);
1246 return;
1247 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001248 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001249 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001250 if (can_jump_to_false) {
1251 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1252 return;
1253 }
1254 __ j(X86Condition(condition->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001255 }
1256 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001257 // Condition has not been materialized, use its inputs as the
1258 // comparison and its condition as the branch condition.
1259
Mark Mendellc4701932015-04-10 13:18:51 -04001260 // Is this a long or FP comparison that has been folded into the HCondition?
1261 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1262 // Generate the comparison directly.
1263 GenerateCompareTestAndBranch(instruction->AsIf(),
Mark Mendellb8b97692015-05-22 16:58:19 -04001264 condition,
Mark Mendellc4701932015-04-10 13:18:51 -04001265 true_target,
1266 false_target,
1267 always_true_target);
1268 return;
1269 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001270
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001271 Location lhs = cond->GetLocations()->InAt(0);
1272 Location rhs = cond->GetLocations()->InAt(1);
1273 // LHS is guaranteed to be in a register (see
1274 // LocationsBuilderX86::VisitCondition).
1275 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001276 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001277 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001278 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001279 if (constant == 0) {
1280 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1281 } else {
1282 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1283 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001284 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001285 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001286 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001287
1288 if (can_jump_to_false) {
1289 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1290 return;
1291 }
1292
1293 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001294 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001295 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001296 if (false_target != nullptr) {
1297 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001298 }
1299}
1300
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001301void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1302 LocationSummary* locations =
1303 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1304 HInstruction* cond = if_instr->InputAt(0);
1305 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1306 locations->SetInAt(0, Location::Any());
1307 }
1308}
1309
1310void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1311 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1312 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1313 Label* always_true_target = true_target;
1314 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1315 if_instr->IfTrueSuccessor())) {
1316 always_true_target = nullptr;
1317 }
1318 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1319 if_instr->IfFalseSuccessor())) {
1320 false_target = nullptr;
1321 }
1322 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1323}
1324
1325void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1326 LocationSummary* locations = new (GetGraph()->GetArena())
1327 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1328 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001329 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001330 locations->SetInAt(0, Location::Any());
1331 }
1332}
1333
1334void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001335 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001336 DeoptimizationSlowPathX86(deoptimize);
1337 codegen_->AddSlowPath(slow_path);
1338 Label* slow_path_entry = slow_path->GetEntryLabel();
1339 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1340}
1341
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001342void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001343 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001344}
1345
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001346void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1347 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001348}
1349
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001350void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001351 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001352}
1353
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001354void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001355 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001356}
1357
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001358void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001359 LocationSummary* locations =
1360 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001361 switch (store->InputAt(1)->GetType()) {
1362 case Primitive::kPrimBoolean:
1363 case Primitive::kPrimByte:
1364 case Primitive::kPrimChar:
1365 case Primitive::kPrimShort:
1366 case Primitive::kPrimInt:
1367 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001368 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001369 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1370 break;
1371
1372 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001373 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001374 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1375 break;
1376
1377 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001378 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001379 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001380}
1381
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001382void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001383}
1384
Roland Levillain0d37cd02015-05-27 16:39:19 +01001385void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001386 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001387 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001388 // Handle the long/FP comparisons made in instruction simplification.
1389 switch (cond->InputAt(0)->GetType()) {
1390 case Primitive::kPrimLong: {
1391 locations->SetInAt(0, Location::RequiresRegister());
1392 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1393 if (cond->NeedsMaterialization()) {
1394 locations->SetOut(Location::RequiresRegister());
1395 }
1396 break;
1397 }
1398 case Primitive::kPrimFloat:
1399 case Primitive::kPrimDouble: {
1400 locations->SetInAt(0, Location::RequiresFpuRegister());
1401 locations->SetInAt(1, Location::RequiresFpuRegister());
1402 if (cond->NeedsMaterialization()) {
1403 locations->SetOut(Location::RequiresRegister());
1404 }
1405 break;
1406 }
1407 default:
1408 locations->SetInAt(0, Location::RequiresRegister());
1409 locations->SetInAt(1, Location::Any());
1410 if (cond->NeedsMaterialization()) {
1411 // We need a byte register.
1412 locations->SetOut(Location::RegisterLocation(ECX));
1413 }
1414 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001415 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001416}
1417
Roland Levillain0d37cd02015-05-27 16:39:19 +01001418void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001419 if (!cond->NeedsMaterialization()) {
1420 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001421 }
Mark Mendellc4701932015-04-10 13:18:51 -04001422
1423 LocationSummary* locations = cond->GetLocations();
1424 Location lhs = locations->InAt(0);
1425 Location rhs = locations->InAt(1);
1426 Register reg = locations->Out().AsRegister<Register>();
1427 Label true_label, false_label;
1428
1429 switch (cond->InputAt(0)->GetType()) {
1430 default: {
1431 // Integer case.
1432
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001433 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001434 __ xorl(reg, reg);
1435
1436 if (rhs.IsRegister()) {
1437 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1438 } else if (rhs.IsConstant()) {
1439 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1440 if (constant == 0) {
1441 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1442 } else {
1443 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1444 }
1445 } else {
1446 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1447 }
Aart Bike9f37602015-10-09 11:15:55 -07001448 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001449 return;
1450 }
1451 case Primitive::kPrimLong:
1452 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1453 break;
1454 case Primitive::kPrimFloat:
1455 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1456 GenerateFPJumps(cond, &true_label, &false_label);
1457 break;
1458 case Primitive::kPrimDouble:
1459 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1460 GenerateFPJumps(cond, &true_label, &false_label);
1461 break;
1462 }
1463
1464 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001465 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001466
Roland Levillain4fa13f62015-07-06 18:11:54 +01001467 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001468 __ Bind(&false_label);
1469 __ xorl(reg, reg);
1470 __ jmp(&done_label);
1471
Roland Levillain4fa13f62015-07-06 18:11:54 +01001472 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001473 __ Bind(&true_label);
1474 __ movl(reg, Immediate(1));
1475 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001476}
1477
1478void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1479 VisitCondition(comp);
1480}
1481
1482void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1483 VisitCondition(comp);
1484}
1485
1486void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1487 VisitCondition(comp);
1488}
1489
1490void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1491 VisitCondition(comp);
1492}
1493
1494void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1495 VisitCondition(comp);
1496}
1497
1498void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1499 VisitCondition(comp);
1500}
1501
1502void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1503 VisitCondition(comp);
1504}
1505
1506void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1507 VisitCondition(comp);
1508}
1509
1510void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1511 VisitCondition(comp);
1512}
1513
1514void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1515 VisitCondition(comp);
1516}
1517
1518void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1519 VisitCondition(comp);
1520}
1521
1522void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1523 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001524}
1525
Aart Bike9f37602015-10-09 11:15:55 -07001526void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1527 VisitCondition(comp);
1528}
1529
1530void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1531 VisitCondition(comp);
1532}
1533
1534void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1535 VisitCondition(comp);
1536}
1537
1538void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1539 VisitCondition(comp);
1540}
1541
1542void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1543 VisitCondition(comp);
1544}
1545
1546void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1547 VisitCondition(comp);
1548}
1549
1550void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1551 VisitCondition(comp);
1552}
1553
1554void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1555 VisitCondition(comp);
1556}
1557
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001558void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001559 LocationSummary* locations =
1560 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001561 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001562}
1563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001564void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001565 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001566}
1567
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001568void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1571 locations->SetOut(Location::ConstantLocation(constant));
1572}
1573
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001574void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001575 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001576}
1577
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001579 LocationSummary* locations =
1580 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001581 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582}
1583
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001584void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001585 // Will be generated at use site.
1586}
1587
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001588void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1589 LocationSummary* locations =
1590 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1591 locations->SetOut(Location::ConstantLocation(constant));
1592}
1593
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001594void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001595 // Will be generated at use site.
1596}
1597
1598void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1599 LocationSummary* locations =
1600 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1601 locations->SetOut(Location::ConstantLocation(constant));
1602}
1603
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001604void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001605 // Will be generated at use site.
1606}
1607
Calin Juravle27df7582015-04-17 19:12:31 +01001608void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1609 memory_barrier->SetLocations(nullptr);
1610}
1611
1612void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1613 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1614}
1615
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001616void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001617 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001618}
1619
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001620void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001621 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001622}
1623
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001624void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001625 LocationSummary* locations =
1626 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001627 switch (ret->InputAt(0)->GetType()) {
1628 case Primitive::kPrimBoolean:
1629 case Primitive::kPrimByte:
1630 case Primitive::kPrimChar:
1631 case Primitive::kPrimShort:
1632 case Primitive::kPrimInt:
1633 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001634 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001635 break;
1636
1637 case Primitive::kPrimLong:
1638 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001639 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001640 break;
1641
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001642 case Primitive::kPrimFloat:
1643 case Primitive::kPrimDouble:
1644 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001645 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001646 break;
1647
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001648 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001649 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001650 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001651}
1652
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001653void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001654 if (kIsDebugBuild) {
1655 switch (ret->InputAt(0)->GetType()) {
1656 case Primitive::kPrimBoolean:
1657 case Primitive::kPrimByte:
1658 case Primitive::kPrimChar:
1659 case Primitive::kPrimShort:
1660 case Primitive::kPrimInt:
1661 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001662 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001663 break;
1664
1665 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001666 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1667 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001668 break;
1669
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001670 case Primitive::kPrimFloat:
1671 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001672 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001673 break;
1674
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001675 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001676 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001677 }
1678 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001679 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001680}
1681
Calin Juravle175dc732015-08-25 15:42:32 +01001682void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1683 // The trampoline uses the same calling convention as dex calling conventions,
1684 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1685 // the method_idx.
1686 HandleInvoke(invoke);
1687}
1688
1689void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1690 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1691}
1692
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001693void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001694 // When we do not run baseline, explicit clinit checks triggered by static
1695 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1696 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001697
Mark Mendellfb8d2792015-03-31 22:16:59 -04001698 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001699 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001700 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1701 invoke->GetLocations()->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::Any());
1702 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001703 return;
1704 }
1705
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001706 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001707
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001708 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1709 if (invoke->HasPcRelativeDexCache()) {
1710 invoke->GetLocations()->SetInAt(invoke->GetCurrentMethodInputIndex(),
1711 Location::RequiresRegister());
1712 }
1713
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001714 if (codegen_->IsBaseline()) {
1715 // Baseline does not have enough registers if the current method also
1716 // needs a register. We therefore do not require a register for it, and let
1717 // the code generation of the invoke handle it.
1718 LocationSummary* locations = invoke->GetLocations();
1719 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1720 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1721 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1722 }
1723 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001724}
1725
Mark Mendell09ed1a32015-03-25 08:30:06 -04001726static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1727 if (invoke->GetLocations()->Intrinsified()) {
1728 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1729 intrinsic.Dispatch(invoke);
1730 return true;
1731 }
1732 return false;
1733}
1734
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001735void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001736 // When we do not run baseline, explicit clinit checks triggered by static
1737 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1738 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001739
Mark Mendell09ed1a32015-03-25 08:30:06 -04001740 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1741 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001742 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001743
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001744 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001745 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001746 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001747 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001748}
1749
1750void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1751 HandleInvoke(invoke);
1752}
1753
1754void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001755 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001756 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001757}
1758
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001759void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001760 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1761 return;
1762 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001763
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001764 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001765 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001766 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001767}
1768
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001769void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1770 HandleInvoke(invoke);
1771 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001772 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001773}
1774
1775void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1776 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain271ab9c2014-11-27 15:23:57 +00001777 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001778 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1779 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001780 LocationSummary* locations = invoke->GetLocations();
1781 Location receiver = locations->InAt(0);
1782 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1783
1784 // Set the hidden argument.
1785 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001786 __ movd(invoke->GetLocations()->GetTemp(1).AsFpuRegister<XmmRegister>(), temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001787
1788 // temp = object->GetClass();
1789 if (receiver.IsStackSlot()) {
1790 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
1791 __ movl(temp, Address(temp, class_offset));
1792 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001793 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001794 }
Roland Levillain4d027112015-07-01 15:41:14 +01001795 codegen_->MaybeRecordImplicitNullCheck(invoke);
1796 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001797 // temp = temp->GetImtEntryAt(method_offset);
1798 __ movl(temp, Address(temp, method_offset));
1799 // call temp->GetEntryPoint();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001800 __ call(Address(temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Nicolas Geoffray86a8d7a2014-11-19 08:47:18 +00001801 kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001802
1803 DCHECK(!codegen_->IsLeafMethod());
1804 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
1805}
1806
Roland Levillain88cb1752014-10-20 16:36:47 +01001807void LocationsBuilderX86::VisitNeg(HNeg* neg) {
1808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
1810 switch (neg->GetResultType()) {
1811 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001812 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01001813 locations->SetInAt(0, Location::RequiresRegister());
1814 locations->SetOut(Location::SameAsFirstInput());
1815 break;
1816
Roland Levillain88cb1752014-10-20 16:36:47 +01001817 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00001818 locations->SetInAt(0, Location::RequiresFpuRegister());
1819 locations->SetOut(Location::SameAsFirstInput());
1820 locations->AddTemp(Location::RequiresRegister());
1821 locations->AddTemp(Location::RequiresFpuRegister());
1822 break;
1823
Roland Levillain88cb1752014-10-20 16:36:47 +01001824 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00001825 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00001826 locations->SetOut(Location::SameAsFirstInput());
1827 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01001828 break;
1829
1830 default:
1831 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1832 }
1833}
1834
1835void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
1836 LocationSummary* locations = neg->GetLocations();
1837 Location out = locations->Out();
1838 Location in = locations->InAt(0);
1839 switch (neg->GetResultType()) {
1840 case Primitive::kPrimInt:
1841 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001842 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001843 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01001844 break;
1845
1846 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001847 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00001848 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01001849 __ negl(out.AsRegisterPairLow<Register>());
1850 // Negation is similar to subtraction from zero. The least
1851 // significant byte triggers a borrow when it is different from
1852 // zero; to take it into account, add 1 to the most significant
1853 // byte if the carry flag (CF) is set to 1 after the first NEGL
1854 // operation.
1855 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
1856 __ negl(out.AsRegisterPairHigh<Register>());
1857 break;
1858
Roland Levillain5368c212014-11-27 15:03:41 +00001859 case Primitive::kPrimFloat: {
1860 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001861 Register constant = locations->GetTemp(0).AsRegister<Register>();
1862 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001863 // Implement float negation with an exclusive or with value
1864 // 0x80000000 (mask for bit 31, representing the sign of a
1865 // single-precision floating-point number).
1866 __ movl(constant, Immediate(INT32_C(0x80000000)));
1867 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00001869 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001870 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00001871
Roland Levillain5368c212014-11-27 15:03:41 +00001872 case Primitive::kPrimDouble: {
1873 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001874 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00001875 // Implement double negation with an exclusive or with value
1876 // 0x8000000000000000 (mask for bit 63, representing the sign of
1877 // a double-precision floating-point number).
1878 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00001879 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01001880 break;
Roland Levillain5368c212014-11-27 15:03:41 +00001881 }
Roland Levillain88cb1752014-10-20 16:36:47 +01001882
1883 default:
1884 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
1885 }
1886}
1887
Roland Levillaindff1f282014-11-05 14:15:05 +00001888void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00001889 Primitive::Type result_type = conversion->GetResultType();
1890 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00001891 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00001892
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001893 // The float-to-long and double-to-long type conversions rely on a
1894 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00001895 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001896 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
1897 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00001898 ? LocationSummary::kCall
1899 : LocationSummary::kNoCall;
1900 LocationSummary* locations =
1901 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
1902
David Brazdilb2bd1c52015-03-25 11:17:37 +00001903 // The Java language does not allow treating boolean as an integral type but
1904 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00001905
Roland Levillaindff1f282014-11-05 14:15:05 +00001906 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00001907 case Primitive::kPrimByte:
1908 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001909 case Primitive::kPrimBoolean:
1910 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00001911 case Primitive::kPrimShort:
1912 case Primitive::kPrimInt:
1913 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001914 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05001915 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
1916 // Make the output overlap to please the register allocator. This greatly simplifies
1917 // the validation of the linear scan implementation
1918 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00001919 break;
1920
1921 default:
1922 LOG(FATAL) << "Unexpected type conversion from " << input_type
1923 << " to " << result_type;
1924 }
1925 break;
1926
Roland Levillain01a8d712014-11-14 16:27:39 +00001927 case Primitive::kPrimShort:
1928 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001929 case Primitive::kPrimBoolean:
1930 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00001931 case Primitive::kPrimByte:
1932 case Primitive::kPrimInt:
1933 case Primitive::kPrimChar:
1934 // Processing a Dex `int-to-short' instruction.
1935 locations->SetInAt(0, Location::Any());
1936 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1937 break;
1938
1939 default:
1940 LOG(FATAL) << "Unexpected type conversion from " << input_type
1941 << " to " << result_type;
1942 }
1943 break;
1944
Roland Levillain946e1432014-11-11 17:35:19 +00001945 case Primitive::kPrimInt:
1946 switch (input_type) {
1947 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00001948 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00001949 locations->SetInAt(0, Location::Any());
1950 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1951 break;
1952
1953 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00001954 // Processing a Dex `float-to-int' instruction.
1955 locations->SetInAt(0, Location::RequiresFpuRegister());
1956 locations->SetOut(Location::RequiresRegister());
1957 locations->AddTemp(Location::RequiresFpuRegister());
1958 break;
1959
Roland Levillain946e1432014-11-11 17:35:19 +00001960 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00001961 // Processing a Dex `double-to-int' instruction.
1962 locations->SetInAt(0, Location::RequiresFpuRegister());
1963 locations->SetOut(Location::RequiresRegister());
1964 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00001965 break;
1966
1967 default:
1968 LOG(FATAL) << "Unexpected type conversion from " << input_type
1969 << " to " << result_type;
1970 }
1971 break;
1972
Roland Levillaindff1f282014-11-05 14:15:05 +00001973 case Primitive::kPrimLong:
1974 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00001975 case Primitive::kPrimBoolean:
1976 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00001977 case Primitive::kPrimByte:
1978 case Primitive::kPrimShort:
1979 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00001980 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00001981 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00001982 locations->SetInAt(0, Location::RegisterLocation(EAX));
1983 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
1984 break;
1985
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001986 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00001987 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001988 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00001989 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001990 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
1991 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
1992
Vladimir Marko949c91f2015-01-27 10:48:44 +00001993 // The runtime helper puts the result in EAX, EDX.
1994 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00001995 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001996 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00001997
1998 default:
1999 LOG(FATAL) << "Unexpected type conversion from " << input_type
2000 << " to " << result_type;
2001 }
2002 break;
2003
Roland Levillain981e4542014-11-14 11:47:14 +00002004 case Primitive::kPrimChar:
2005 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002006 case Primitive::kPrimBoolean:
2007 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002008 case Primitive::kPrimByte:
2009 case Primitive::kPrimShort:
2010 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002011 // Processing a Dex `int-to-char' instruction.
2012 locations->SetInAt(0, Location::Any());
2013 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2014 break;
2015
2016 default:
2017 LOG(FATAL) << "Unexpected type conversion from " << input_type
2018 << " to " << result_type;
2019 }
2020 break;
2021
Roland Levillaindff1f282014-11-05 14:15:05 +00002022 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002023 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002024 case Primitive::kPrimBoolean:
2025 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002026 case Primitive::kPrimByte:
2027 case Primitive::kPrimShort:
2028 case Primitive::kPrimInt:
2029 case Primitive::kPrimChar:
2030 // Processing a Dex `int-to-float' instruction.
2031 locations->SetInAt(0, Location::RequiresRegister());
2032 locations->SetOut(Location::RequiresFpuRegister());
2033 break;
2034
2035 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002036 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002037 locations->SetInAt(0, Location::Any());
2038 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002039 break;
2040
Roland Levillaincff13742014-11-17 14:32:17 +00002041 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002042 // Processing a Dex `double-to-float' instruction.
2043 locations->SetInAt(0, Location::RequiresFpuRegister());
2044 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002045 break;
2046
2047 default:
2048 LOG(FATAL) << "Unexpected type conversion from " << input_type
2049 << " to " << result_type;
2050 };
2051 break;
2052
Roland Levillaindff1f282014-11-05 14:15:05 +00002053 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002054 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002055 case Primitive::kPrimBoolean:
2056 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002057 case Primitive::kPrimByte:
2058 case Primitive::kPrimShort:
2059 case Primitive::kPrimInt:
2060 case Primitive::kPrimChar:
2061 // Processing a Dex `int-to-double' instruction.
2062 locations->SetInAt(0, Location::RequiresRegister());
2063 locations->SetOut(Location::RequiresFpuRegister());
2064 break;
2065
2066 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002067 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002068 locations->SetInAt(0, Location::Any());
2069 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002070 break;
2071
Roland Levillaincff13742014-11-17 14:32:17 +00002072 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002073 // Processing a Dex `float-to-double' instruction.
2074 locations->SetInAt(0, Location::RequiresFpuRegister());
2075 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002076 break;
2077
2078 default:
2079 LOG(FATAL) << "Unexpected type conversion from " << input_type
2080 << " to " << result_type;
2081 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002082 break;
2083
2084 default:
2085 LOG(FATAL) << "Unexpected type conversion from " << input_type
2086 << " to " << result_type;
2087 }
2088}
2089
2090void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2091 LocationSummary* locations = conversion->GetLocations();
2092 Location out = locations->Out();
2093 Location in = locations->InAt(0);
2094 Primitive::Type result_type = conversion->GetResultType();
2095 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002096 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002097 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002098 case Primitive::kPrimByte:
2099 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002100 case Primitive::kPrimBoolean:
2101 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002102 case Primitive::kPrimShort:
2103 case Primitive::kPrimInt:
2104 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002105 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002106 if (in.IsRegister()) {
2107 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002108 } else {
2109 DCHECK(in.GetConstant()->IsIntConstant());
2110 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2111 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2112 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002113 break;
2114
2115 default:
2116 LOG(FATAL) << "Unexpected type conversion from " << input_type
2117 << " to " << result_type;
2118 }
2119 break;
2120
Roland Levillain01a8d712014-11-14 16:27:39 +00002121 case Primitive::kPrimShort:
2122 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002123 case Primitive::kPrimBoolean:
2124 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002125 case Primitive::kPrimByte:
2126 case Primitive::kPrimInt:
2127 case Primitive::kPrimChar:
2128 // Processing a Dex `int-to-short' instruction.
2129 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002130 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002131 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002133 } else {
2134 DCHECK(in.GetConstant()->IsIntConstant());
2135 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002136 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002137 }
2138 break;
2139
2140 default:
2141 LOG(FATAL) << "Unexpected type conversion from " << input_type
2142 << " to " << result_type;
2143 }
2144 break;
2145
Roland Levillain946e1432014-11-11 17:35:19 +00002146 case Primitive::kPrimInt:
2147 switch (input_type) {
2148 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002149 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002150 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002151 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002152 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002153 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002154 } else {
2155 DCHECK(in.IsConstant());
2156 DCHECK(in.GetConstant()->IsLongConstant());
2157 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002158 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002159 }
2160 break;
2161
Roland Levillain3f8f9362014-12-02 17:45:01 +00002162 case Primitive::kPrimFloat: {
2163 // Processing a Dex `float-to-int' instruction.
2164 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2165 Register output = out.AsRegister<Register>();
2166 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002167 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002168
2169 __ movl(output, Immediate(kPrimIntMax));
2170 // temp = int-to-float(output)
2171 __ cvtsi2ss(temp, output);
2172 // if input >= temp goto done
2173 __ comiss(input, temp);
2174 __ j(kAboveEqual, &done);
2175 // if input == NaN goto nan
2176 __ j(kUnordered, &nan);
2177 // output = float-to-int-truncate(input)
2178 __ cvttss2si(output, input);
2179 __ jmp(&done);
2180 __ Bind(&nan);
2181 // output = 0
2182 __ xorl(output, output);
2183 __ Bind(&done);
2184 break;
2185 }
2186
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002187 case Primitive::kPrimDouble: {
2188 // Processing a Dex `double-to-int' instruction.
2189 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2190 Register output = out.AsRegister<Register>();
2191 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002192 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002193
2194 __ movl(output, Immediate(kPrimIntMax));
2195 // temp = int-to-double(output)
2196 __ cvtsi2sd(temp, output);
2197 // if input >= temp goto done
2198 __ comisd(input, temp);
2199 __ j(kAboveEqual, &done);
2200 // if input == NaN goto nan
2201 __ j(kUnordered, &nan);
2202 // output = double-to-int-truncate(input)
2203 __ cvttsd2si(output, input);
2204 __ jmp(&done);
2205 __ Bind(&nan);
2206 // output = 0
2207 __ xorl(output, output);
2208 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002209 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002210 }
Roland Levillain946e1432014-11-11 17:35:19 +00002211
2212 default:
2213 LOG(FATAL) << "Unexpected type conversion from " << input_type
2214 << " to " << result_type;
2215 }
2216 break;
2217
Roland Levillaindff1f282014-11-05 14:15:05 +00002218 case Primitive::kPrimLong:
2219 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002220 case Primitive::kPrimBoolean:
2221 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002222 case Primitive::kPrimByte:
2223 case Primitive::kPrimShort:
2224 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002225 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002226 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2228 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002229 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002230 __ cdq();
2231 break;
2232
2233 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002234 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002235 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2236 conversion,
2237 conversion->GetDexPc(),
2238 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002239 break;
2240
Roland Levillaindff1f282014-11-05 14:15:05 +00002241 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002242 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002243 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2244 conversion,
2245 conversion->GetDexPc(),
2246 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002247 break;
2248
2249 default:
2250 LOG(FATAL) << "Unexpected type conversion from " << input_type
2251 << " to " << result_type;
2252 }
2253 break;
2254
Roland Levillain981e4542014-11-14 11:47:14 +00002255 case Primitive::kPrimChar:
2256 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002257 case Primitive::kPrimBoolean:
2258 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002259 case Primitive::kPrimByte:
2260 case Primitive::kPrimShort:
2261 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002262 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2263 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002264 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002265 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002266 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002267 } else {
2268 DCHECK(in.GetConstant()->IsIntConstant());
2269 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002270 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002271 }
2272 break;
2273
2274 default:
2275 LOG(FATAL) << "Unexpected type conversion from " << input_type
2276 << " to " << result_type;
2277 }
2278 break;
2279
Roland Levillaindff1f282014-11-05 14:15:05 +00002280 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002281 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002282 case Primitive::kPrimBoolean:
2283 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002284 case Primitive::kPrimByte:
2285 case Primitive::kPrimShort:
2286 case Primitive::kPrimInt:
2287 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002288 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002289 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002290 break;
2291
Roland Levillain6d0e4832014-11-27 18:31:21 +00002292 case Primitive::kPrimLong: {
2293 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002294 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002295
Roland Levillain232ade02015-04-20 15:14:36 +01002296 // Create stack space for the call to
2297 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2298 // TODO: enhance register allocator to ask for stack temporaries.
2299 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2300 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2301 __ subl(ESP, Immediate(adjustment));
2302 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002303
Roland Levillain232ade02015-04-20 15:14:36 +01002304 // Load the value to the FP stack, using temporaries if needed.
2305 PushOntoFPStack(in, 0, adjustment, false, true);
2306
2307 if (out.IsStackSlot()) {
2308 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2309 } else {
2310 __ fstps(Address(ESP, 0));
2311 Location stack_temp = Location::StackSlot(0);
2312 codegen_->Move32(out, stack_temp);
2313 }
2314
2315 // Remove the temporary stack space we allocated.
2316 if (adjustment != 0) {
2317 __ addl(ESP, Immediate(adjustment));
2318 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002319 break;
2320 }
2321
Roland Levillaincff13742014-11-17 14:32:17 +00002322 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002323 // Processing a Dex `double-to-float' instruction.
2324 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002325 break;
2326
2327 default:
2328 LOG(FATAL) << "Unexpected type conversion from " << input_type
2329 << " to " << result_type;
2330 };
2331 break;
2332
Roland Levillaindff1f282014-11-05 14:15:05 +00002333 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002334 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002335 case Primitive::kPrimBoolean:
2336 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002337 case Primitive::kPrimByte:
2338 case Primitive::kPrimShort:
2339 case Primitive::kPrimInt:
2340 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002341 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002342 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002343 break;
2344
Roland Levillain647b9ed2014-11-27 12:06:00 +00002345 case Primitive::kPrimLong: {
2346 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002347 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002348
Roland Levillain232ade02015-04-20 15:14:36 +01002349 // Create stack space for the call to
2350 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2351 // TODO: enhance register allocator to ask for stack temporaries.
2352 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2353 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2354 __ subl(ESP, Immediate(adjustment));
2355 }
2356
2357 // Load the value to the FP stack, using temporaries if needed.
2358 PushOntoFPStack(in, 0, adjustment, false, true);
2359
2360 if (out.IsDoubleStackSlot()) {
2361 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2362 } else {
2363 __ fstpl(Address(ESP, 0));
2364 Location stack_temp = Location::DoubleStackSlot(0);
2365 codegen_->Move64(out, stack_temp);
2366 }
2367
2368 // Remove the temporary stack space we allocated.
2369 if (adjustment != 0) {
2370 __ addl(ESP, Immediate(adjustment));
2371 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002372 break;
2373 }
2374
Roland Levillaincff13742014-11-17 14:32:17 +00002375 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002376 // Processing a Dex `float-to-double' instruction.
2377 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002378 break;
2379
2380 default:
2381 LOG(FATAL) << "Unexpected type conversion from " << input_type
2382 << " to " << result_type;
2383 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002384 break;
2385
2386 default:
2387 LOG(FATAL) << "Unexpected type conversion from " << input_type
2388 << " to " << result_type;
2389 }
2390}
2391
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002392void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002393 LocationSummary* locations =
2394 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002395 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002396 case Primitive::kPrimInt: {
2397 locations->SetInAt(0, Location::RequiresRegister());
2398 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2400 break;
2401 }
2402
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002403 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002404 locations->SetInAt(0, Location::RequiresRegister());
2405 locations->SetInAt(1, Location::Any());
2406 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002407 break;
2408 }
2409
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +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());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002414 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002415 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002416 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002417
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002418 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002419 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2420 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002421 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002422}
2423
2424void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2425 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002426 Location first = locations->InAt(0);
2427 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002428 Location out = locations->Out();
2429
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002430 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002431 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002432 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002433 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2434 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002435 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2436 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002437 } else {
2438 __ leal(out.AsRegister<Register>(), Address(
2439 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2440 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002441 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002442 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2443 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2444 __ addl(out.AsRegister<Register>(), Immediate(value));
2445 } else {
2446 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2447 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002448 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002449 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002451 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002452 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002453 }
2454
2455 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002456 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002457 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2458 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002459 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002460 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2461 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002462 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002463 } else {
2464 DCHECK(second.IsConstant()) << second;
2465 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2466 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2467 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002468 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002469 break;
2470 }
2471
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002472 case Primitive::kPrimFloat: {
2473 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002474 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002475 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2476 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2477 DCHECK(!const_area->NeedsMaterialization());
2478 __ addss(first.AsFpuRegister<XmmRegister>(),
2479 codegen_->LiteralFloatAddress(
2480 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2481 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2482 } else {
2483 DCHECK(second.IsStackSlot());
2484 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002485 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002486 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002487 }
2488
2489 case Primitive::kPrimDouble: {
2490 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002491 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002492 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2493 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2494 DCHECK(!const_area->NeedsMaterialization());
2495 __ addsd(first.AsFpuRegister<XmmRegister>(),
2496 codegen_->LiteralDoubleAddress(
2497 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2498 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2499 } else {
2500 DCHECK(second.IsDoubleStackSlot());
2501 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002502 }
2503 break;
2504 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002505
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002506 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002507 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002508 }
2509}
2510
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002511void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002512 LocationSummary* locations =
2513 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002514 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002515 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002516 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002517 locations->SetInAt(0, Location::RequiresRegister());
2518 locations->SetInAt(1, Location::Any());
2519 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002520 break;
2521 }
Calin Juravle11351682014-10-23 15:38:15 +01002522 case Primitive::kPrimFloat:
2523 case Primitive::kPrimDouble: {
2524 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002525 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002526 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002527 break;
Calin Juravle11351682014-10-23 15:38:15 +01002528 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002529
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002530 default:
Calin Juravle11351682014-10-23 15:38:15 +01002531 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002532 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002533}
2534
2535void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2536 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002537 Location first = locations->InAt(0);
2538 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002539 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002540 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002541 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002542 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002544 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002545 __ subl(first.AsRegister<Register>(),
2546 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002547 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002548 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002549 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002550 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002551 }
2552
2553 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002554 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002555 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2556 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002557 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002558 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002559 __ sbbl(first.AsRegisterPairHigh<Register>(),
2560 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002561 } else {
2562 DCHECK(second.IsConstant()) << second;
2563 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2564 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2565 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002566 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002567 break;
2568 }
2569
Calin Juravle11351682014-10-23 15:38:15 +01002570 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002571 if (second.IsFpuRegister()) {
2572 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2573 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2574 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2575 DCHECK(!const_area->NeedsMaterialization());
2576 __ subss(first.AsFpuRegister<XmmRegister>(),
2577 codegen_->LiteralFloatAddress(
2578 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2579 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2580 } else {
2581 DCHECK(second.IsStackSlot());
2582 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2583 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002584 break;
Calin Juravle11351682014-10-23 15:38:15 +01002585 }
2586
2587 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002588 if (second.IsFpuRegister()) {
2589 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2590 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2591 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2592 DCHECK(!const_area->NeedsMaterialization());
2593 __ subsd(first.AsFpuRegister<XmmRegister>(),
2594 codegen_->LiteralDoubleAddress(
2595 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2596 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2597 } else {
2598 DCHECK(second.IsDoubleStackSlot());
2599 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2600 }
Calin Juravle11351682014-10-23 15:38:15 +01002601 break;
2602 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002603
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002604 default:
Calin Juravle11351682014-10-23 15:38:15 +01002605 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002606 }
2607}
2608
Calin Juravle34bacdf2014-10-07 20:23:36 +01002609void LocationsBuilderX86::VisitMul(HMul* mul) {
2610 LocationSummary* locations =
2611 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2612 switch (mul->GetResultType()) {
2613 case Primitive::kPrimInt:
2614 locations->SetInAt(0, Location::RequiresRegister());
2615 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002616 if (mul->InputAt(1)->IsIntConstant()) {
2617 // Can use 3 operand multiply.
2618 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2619 } else {
2620 locations->SetOut(Location::SameAsFirstInput());
2621 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002622 break;
2623 case Primitive::kPrimLong: {
2624 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002625 locations->SetInAt(1, Location::Any());
2626 locations->SetOut(Location::SameAsFirstInput());
2627 // Needed for imul on 32bits with 64bits output.
2628 locations->AddTemp(Location::RegisterLocation(EAX));
2629 locations->AddTemp(Location::RegisterLocation(EDX));
2630 break;
2631 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002632 case Primitive::kPrimFloat:
2633 case Primitive::kPrimDouble: {
2634 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002635 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002636 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002637 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002638 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002639
2640 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002641 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002642 }
2643}
2644
2645void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2646 LocationSummary* locations = mul->GetLocations();
2647 Location first = locations->InAt(0);
2648 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002649 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002650
2651 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002652 case Primitive::kPrimInt:
2653 // The constant may have ended up in a register, so test explicitly to avoid
2654 // problems where the output may not be the same as the first operand.
2655 if (mul->InputAt(1)->IsIntConstant()) {
2656 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2657 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2658 } else if (second.IsRegister()) {
2659 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002660 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002661 } else {
2662 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002663 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002665 }
2666 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002667
2668 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002669 Register in1_hi = first.AsRegisterPairHigh<Register>();
2670 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002671 Register eax = locations->GetTemp(0).AsRegister<Register>();
2672 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002673
2674 DCHECK_EQ(EAX, eax);
2675 DCHECK_EQ(EDX, edx);
2676
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002677 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002678 // output: in1
2679 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2680 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2681 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002682 if (second.IsConstant()) {
2683 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002684
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002685 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2686 int32_t low_value = Low32Bits(value);
2687 int32_t high_value = High32Bits(value);
2688 Immediate low(low_value);
2689 Immediate high(high_value);
2690
2691 __ movl(eax, high);
2692 // eax <- in1.lo * in2.hi
2693 __ imull(eax, in1_lo);
2694 // in1.hi <- in1.hi * in2.lo
2695 __ imull(in1_hi, low);
2696 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2697 __ addl(in1_hi, eax);
2698 // move in2_lo to eax to prepare for double precision
2699 __ movl(eax, low);
2700 // edx:eax <- in1.lo * in2.lo
2701 __ mull(in1_lo);
2702 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2703 __ addl(in1_hi, edx);
2704 // in1.lo <- (in1.lo * in2.lo)[31:0];
2705 __ movl(in1_lo, eax);
2706 } else if (second.IsRegisterPair()) {
2707 Register in2_hi = second.AsRegisterPairHigh<Register>();
2708 Register in2_lo = second.AsRegisterPairLow<Register>();
2709
2710 __ movl(eax, in2_hi);
2711 // eax <- in1.lo * in2.hi
2712 __ imull(eax, in1_lo);
2713 // in1.hi <- in1.hi * in2.lo
2714 __ imull(in1_hi, in2_lo);
2715 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2716 __ addl(in1_hi, eax);
2717 // move in1_lo to eax to prepare for double precision
2718 __ movl(eax, in1_lo);
2719 // edx:eax <- in1.lo * in2.lo
2720 __ mull(in2_lo);
2721 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2722 __ addl(in1_hi, edx);
2723 // in1.lo <- (in1.lo * in2.lo)[31:0];
2724 __ movl(in1_lo, eax);
2725 } else {
2726 DCHECK(second.IsDoubleStackSlot()) << second;
2727 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2728 Address in2_lo(ESP, second.GetStackIndex());
2729
2730 __ movl(eax, in2_hi);
2731 // eax <- in1.lo * in2.hi
2732 __ imull(eax, in1_lo);
2733 // in1.hi <- in1.hi * in2.lo
2734 __ imull(in1_hi, in2_lo);
2735 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2736 __ addl(in1_hi, eax);
2737 // move in1_lo to eax to prepare for double precision
2738 __ movl(eax, in1_lo);
2739 // edx:eax <- in1.lo * in2.lo
2740 __ mull(in2_lo);
2741 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2742 __ addl(in1_hi, edx);
2743 // in1.lo <- (in1.lo * in2.lo)[31:0];
2744 __ movl(in1_lo, eax);
2745 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002746
2747 break;
2748 }
2749
Calin Juravleb5bfa962014-10-21 18:02:24 +01002750 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002751 DCHECK(first.Equals(locations->Out()));
2752 if (second.IsFpuRegister()) {
2753 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2754 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2755 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2756 DCHECK(!const_area->NeedsMaterialization());
2757 __ mulss(first.AsFpuRegister<XmmRegister>(),
2758 codegen_->LiteralFloatAddress(
2759 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2760 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2761 } else {
2762 DCHECK(second.IsStackSlot());
2763 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2764 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002765 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002766 }
2767
2768 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002769 DCHECK(first.Equals(locations->Out()));
2770 if (second.IsFpuRegister()) {
2771 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2772 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2773 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2774 DCHECK(!const_area->NeedsMaterialization());
2775 __ mulsd(first.AsFpuRegister<XmmRegister>(),
2776 codegen_->LiteralDoubleAddress(
2777 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2778 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2779 } else {
2780 DCHECK(second.IsDoubleStackSlot());
2781 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2782 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002783 break;
2784 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002785
2786 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002787 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002788 }
2789}
2790
Roland Levillain232ade02015-04-20 15:14:36 +01002791void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
2792 uint32_t temp_offset,
2793 uint32_t stack_adjustment,
2794 bool is_fp,
2795 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002796 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002797 DCHECK(!is_wide);
2798 if (is_fp) {
2799 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2800 } else {
2801 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
2802 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002803 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01002804 DCHECK(is_wide);
2805 if (is_fp) {
2806 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2807 } else {
2808 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
2809 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002810 } else {
2811 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01002812 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002813 Location stack_temp = Location::StackSlot(temp_offset);
2814 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002815 if (is_fp) {
2816 __ flds(Address(ESP, temp_offset));
2817 } else {
2818 __ filds(Address(ESP, temp_offset));
2819 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002820 } else {
2821 Location stack_temp = Location::DoubleStackSlot(temp_offset);
2822 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01002823 if (is_fp) {
2824 __ fldl(Address(ESP, temp_offset));
2825 } else {
2826 __ fildl(Address(ESP, temp_offset));
2827 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002828 }
2829 }
2830}
2831
2832void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
2833 Primitive::Type type = rem->GetResultType();
2834 bool is_float = type == Primitive::kPrimFloat;
2835 size_t elem_size = Primitive::ComponentSize(type);
2836 LocationSummary* locations = rem->GetLocations();
2837 Location first = locations->InAt(0);
2838 Location second = locations->InAt(1);
2839 Location out = locations->Out();
2840
2841 // Create stack space for 2 elements.
2842 // TODO: enhance register allocator to ask for stack temporaries.
2843 __ subl(ESP, Immediate(2 * elem_size));
2844
2845 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01002846 const bool is_wide = !is_float;
2847 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
2848 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002849
2850 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04002851 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05002852 __ Bind(&retry);
2853 __ fprem();
2854
2855 // Move FP status to AX.
2856 __ fstsw();
2857
2858 // And see if the argument reduction is complete. This is signaled by the
2859 // C2 FPU flag bit set to 0.
2860 __ andl(EAX, Immediate(kC2ConditionMask));
2861 __ j(kNotEqual, &retry);
2862
2863 // We have settled on the final value. Retrieve it into an XMM register.
2864 // Store FP top of stack to real stack.
2865 if (is_float) {
2866 __ fsts(Address(ESP, 0));
2867 } else {
2868 __ fstl(Address(ESP, 0));
2869 }
2870
2871 // Pop the 2 items from the FP stack.
2872 __ fucompp();
2873
2874 // Load the value from the stack into an XMM register.
2875 DCHECK(out.IsFpuRegister()) << out;
2876 if (is_float) {
2877 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2878 } else {
2879 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
2880 }
2881
2882 // And remove the temporary stack space we allocated.
2883 __ addl(ESP, Immediate(2 * elem_size));
2884}
2885
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002886
2887void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
2888 DCHECK(instruction->IsDiv() || instruction->IsRem());
2889
2890 LocationSummary* locations = instruction->GetLocations();
2891 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002892 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002893
2894 Register out_register = locations->Out().AsRegister<Register>();
2895 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002896 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002897
2898 DCHECK(imm == 1 || imm == -1);
2899
2900 if (instruction->IsRem()) {
2901 __ xorl(out_register, out_register);
2902 } else {
2903 __ movl(out_register, input_register);
2904 if (imm == -1) {
2905 __ negl(out_register);
2906 }
2907 }
2908}
2909
2910
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002911void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002912 LocationSummary* locations = instruction->GetLocations();
2913
2914 Register out_register = locations->Out().AsRegister<Register>();
2915 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002916 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002917
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01002918 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002919 Register num = locations->GetTemp(0).AsRegister<Register>();
2920
2921 __ leal(num, Address(input_register, std::abs(imm) - 1));
2922 __ testl(input_register, input_register);
2923 __ cmovl(kGreaterEqual, num, input_register);
2924 int shift = CTZ(imm);
2925 __ sarl(num, Immediate(shift));
2926
2927 if (imm < 0) {
2928 __ negl(num);
2929 }
2930
2931 __ movl(out_register, num);
2932}
2933
2934void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
2935 DCHECK(instruction->IsDiv() || instruction->IsRem());
2936
2937 LocationSummary* locations = instruction->GetLocations();
2938 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
2939
2940 Register eax = locations->InAt(0).AsRegister<Register>();
2941 Register out = locations->Out().AsRegister<Register>();
2942 Register num;
2943 Register edx;
2944
2945 if (instruction->IsDiv()) {
2946 edx = locations->GetTemp(0).AsRegister<Register>();
2947 num = locations->GetTemp(1).AsRegister<Register>();
2948 } else {
2949 edx = locations->Out().AsRegister<Register>();
2950 num = locations->GetTemp(0).AsRegister<Register>();
2951 }
2952
2953 DCHECK_EQ(EAX, eax);
2954 DCHECK_EQ(EDX, edx);
2955 if (instruction->IsDiv()) {
2956 DCHECK_EQ(EAX, out);
2957 } else {
2958 DCHECK_EQ(EDX, out);
2959 }
2960
2961 int64_t magic;
2962 int shift;
2963 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
2964
Mark Mendell0c9497d2015-08-21 09:30:05 -04002965 NearLabel ndiv;
2966 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01002967 // If numerator is 0, the result is 0, no computation needed.
2968 __ testl(eax, eax);
2969 __ j(kNotEqual, &ndiv);
2970
2971 __ xorl(out, out);
2972 __ jmp(&end);
2973
2974 __ Bind(&ndiv);
2975
2976 // Save the numerator.
2977 __ movl(num, eax);
2978
2979 // EAX = magic
2980 __ movl(eax, Immediate(magic));
2981
2982 // EDX:EAX = magic * numerator
2983 __ imull(num);
2984
2985 if (imm > 0 && magic < 0) {
2986 // EDX += num
2987 __ addl(edx, num);
2988 } else if (imm < 0 && magic > 0) {
2989 __ subl(edx, num);
2990 }
2991
2992 // Shift if needed.
2993 if (shift != 0) {
2994 __ sarl(edx, Immediate(shift));
2995 }
2996
2997 // EDX += 1 if EDX < 0
2998 __ movl(eax, edx);
2999 __ shrl(edx, Immediate(31));
3000 __ addl(edx, eax);
3001
3002 if (instruction->IsRem()) {
3003 __ movl(eax, num);
3004 __ imull(edx, Immediate(imm));
3005 __ subl(eax, edx);
3006 __ movl(edx, eax);
3007 } else {
3008 __ movl(eax, edx);
3009 }
3010 __ Bind(&end);
3011}
3012
Calin Juravlebacfec32014-11-14 15:54:36 +00003013void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3014 DCHECK(instruction->IsDiv() || instruction->IsRem());
3015
3016 LocationSummary* locations = instruction->GetLocations();
3017 Location out = locations->Out();
3018 Location first = locations->InAt(0);
3019 Location second = locations->InAt(1);
3020 bool is_div = instruction->IsDiv();
3021
3022 switch (instruction->GetResultType()) {
3023 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003024 DCHECK_EQ(EAX, first.AsRegister<Register>());
3025 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003026
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003027 if (instruction->InputAt(1)->IsIntConstant()) {
3028 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003029
3030 if (imm == 0) {
3031 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3032 } else if (imm == 1 || imm == -1) {
3033 DivRemOneOrMinusOne(instruction);
3034 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003035 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003036 } else {
3037 DCHECK(imm <= -2 || imm >= 2);
3038 GenerateDivRemWithAnyConstant(instruction);
3039 }
3040 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003041 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003042 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003043 is_div);
3044 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003045
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003046 Register second_reg = second.AsRegister<Register>();
3047 // 0x80000000/-1 triggers an arithmetic exception!
3048 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3049 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003050
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003051 __ cmpl(second_reg, Immediate(-1));
3052 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003053
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003054 // edx:eax <- sign-extended of eax
3055 __ cdq();
3056 // eax = quotient, edx = remainder
3057 __ idivl(second_reg);
3058 __ Bind(slow_path->GetExitLabel());
3059 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003060 break;
3061 }
3062
3063 case Primitive::kPrimLong: {
3064 InvokeRuntimeCallingConvention calling_convention;
3065 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3066 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3067 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3068 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3069 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3070 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3071
3072 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003073 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3074 instruction,
3075 instruction->GetDexPc(),
3076 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003077 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003078 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3079 instruction,
3080 instruction->GetDexPc(),
3081 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003082 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003083 break;
3084 }
3085
3086 default:
3087 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3088 }
3089}
3090
Calin Juravle7c4954d2014-10-28 16:57:40 +00003091void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003092 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003093 ? LocationSummary::kCall
3094 : LocationSummary::kNoCall;
3095 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3096
Calin Juravle7c4954d2014-10-28 16:57:40 +00003097 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003098 case Primitive::kPrimInt: {
3099 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003100 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003101 locations->SetOut(Location::SameAsFirstInput());
3102 // Intel uses edx:eax as the dividend.
3103 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003104 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3105 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3106 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003107 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003108 locations->AddTemp(Location::RequiresRegister());
3109 }
Calin Juravled0d48522014-11-04 16:40:20 +00003110 break;
3111 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003112 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003113 InvokeRuntimeCallingConvention calling_convention;
3114 locations->SetInAt(0, Location::RegisterPairLocation(
3115 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3116 locations->SetInAt(1, Location::RegisterPairLocation(
3117 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3118 // Runtime helper puts the result in EAX, EDX.
3119 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003120 break;
3121 }
3122 case Primitive::kPrimFloat:
3123 case Primitive::kPrimDouble: {
3124 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003125 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003126 locations->SetOut(Location::SameAsFirstInput());
3127 break;
3128 }
3129
3130 default:
3131 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3132 }
3133}
3134
3135void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3136 LocationSummary* locations = div->GetLocations();
3137 Location first = locations->InAt(0);
3138 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003139
3140 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003141 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003142 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003143 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003144 break;
3145 }
3146
3147 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003148 if (second.IsFpuRegister()) {
3149 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3150 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3151 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3152 DCHECK(!const_area->NeedsMaterialization());
3153 __ divss(first.AsFpuRegister<XmmRegister>(),
3154 codegen_->LiteralFloatAddress(
3155 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3156 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3157 } else {
3158 DCHECK(second.IsStackSlot());
3159 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3160 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003161 break;
3162 }
3163
3164 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003165 if (second.IsFpuRegister()) {
3166 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3167 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3168 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3169 DCHECK(!const_area->NeedsMaterialization());
3170 __ divsd(first.AsFpuRegister<XmmRegister>(),
3171 codegen_->LiteralDoubleAddress(
3172 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3173 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3174 } else {
3175 DCHECK(second.IsDoubleStackSlot());
3176 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3177 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003178 break;
3179 }
3180
3181 default:
3182 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3183 }
3184}
3185
Calin Juravlebacfec32014-11-14 15:54:36 +00003186void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003187 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003188
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003189 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3190 ? LocationSummary::kCall
3191 : LocationSummary::kNoCall;
3192 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003193
Calin Juravled2ec87d2014-12-08 14:24:46 +00003194 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003195 case Primitive::kPrimInt: {
3196 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003197 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003198 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003199 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3200 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3201 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003202 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003203 locations->AddTemp(Location::RequiresRegister());
3204 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003205 break;
3206 }
3207 case Primitive::kPrimLong: {
3208 InvokeRuntimeCallingConvention calling_convention;
3209 locations->SetInAt(0, Location::RegisterPairLocation(
3210 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3211 locations->SetInAt(1, Location::RegisterPairLocation(
3212 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3213 // Runtime helper puts the result in EAX, EDX.
3214 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3215 break;
3216 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003217 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003218 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003219 locations->SetInAt(0, Location::Any());
3220 locations->SetInAt(1, Location::Any());
3221 locations->SetOut(Location::RequiresFpuRegister());
3222 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003223 break;
3224 }
3225
3226 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003227 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003228 }
3229}
3230
3231void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3232 Primitive::Type type = rem->GetResultType();
3233 switch (type) {
3234 case Primitive::kPrimInt:
3235 case Primitive::kPrimLong: {
3236 GenerateDivRemIntegral(rem);
3237 break;
3238 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003239 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003240 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003241 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003242 break;
3243 }
3244 default:
3245 LOG(FATAL) << "Unexpected rem type " << type;
3246 }
3247}
3248
Calin Juravled0d48522014-11-04 16:40:20 +00003249void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003250 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3251 ? LocationSummary::kCallOnSlowPath
3252 : LocationSummary::kNoCall;
3253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003254 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003255 case Primitive::kPrimByte:
3256 case Primitive::kPrimChar:
3257 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003258 case Primitive::kPrimInt: {
3259 locations->SetInAt(0, Location::Any());
3260 break;
3261 }
3262 case Primitive::kPrimLong: {
3263 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3264 if (!instruction->IsConstant()) {
3265 locations->AddTemp(Location::RequiresRegister());
3266 }
3267 break;
3268 }
3269 default:
3270 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3271 }
Calin Juravled0d48522014-11-04 16:40:20 +00003272 if (instruction->HasUses()) {
3273 locations->SetOut(Location::SameAsFirstInput());
3274 }
3275}
3276
3277void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003278 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003279 codegen_->AddSlowPath(slow_path);
3280
3281 LocationSummary* locations = instruction->GetLocations();
3282 Location value = locations->InAt(0);
3283
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003284 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003285 case Primitive::kPrimByte:
3286 case Primitive::kPrimChar:
3287 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003288 case Primitive::kPrimInt: {
3289 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003290 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003291 __ j(kEqual, slow_path->GetEntryLabel());
3292 } else if (value.IsStackSlot()) {
3293 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3294 __ j(kEqual, slow_path->GetEntryLabel());
3295 } else {
3296 DCHECK(value.IsConstant()) << value;
3297 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3298 __ jmp(slow_path->GetEntryLabel());
3299 }
3300 }
3301 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003302 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003303 case Primitive::kPrimLong: {
3304 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003305 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003306 __ movl(temp, value.AsRegisterPairLow<Register>());
3307 __ orl(temp, value.AsRegisterPairHigh<Register>());
3308 __ j(kEqual, slow_path->GetEntryLabel());
3309 } else {
3310 DCHECK(value.IsConstant()) << value;
3311 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3312 __ jmp(slow_path->GetEntryLabel());
3313 }
3314 }
3315 break;
3316 }
3317 default:
3318 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003319 }
Calin Juravled0d48522014-11-04 16:40:20 +00003320}
3321
Calin Juravle9aec02f2014-11-18 23:06:35 +00003322void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3323 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3324
3325 LocationSummary* locations =
3326 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3327
3328 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003329 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003330 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003331 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003332 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003333 // The shift count needs to be in CL or a constant.
3334 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003335 locations->SetOut(Location::SameAsFirstInput());
3336 break;
3337 }
3338 default:
3339 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3340 }
3341}
3342
3343void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3344 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3345
3346 LocationSummary* locations = op->GetLocations();
3347 Location first = locations->InAt(0);
3348 Location second = locations->InAt(1);
3349 DCHECK(first.Equals(locations->Out()));
3350
3351 switch (op->GetResultType()) {
3352 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003353 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003354 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003355 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003356 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003357 DCHECK_EQ(ECX, second_reg);
3358 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003359 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003360 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003361 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003362 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003363 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003364 }
3365 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003366 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3367 if (shift == 0) {
3368 return;
3369 }
3370 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003371 if (op->IsShl()) {
3372 __ shll(first_reg, imm);
3373 } else if (op->IsShr()) {
3374 __ sarl(first_reg, imm);
3375 } else {
3376 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003377 }
3378 }
3379 break;
3380 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003381 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003382 if (second.IsRegister()) {
3383 Register second_reg = second.AsRegister<Register>();
3384 DCHECK_EQ(ECX, second_reg);
3385 if (op->IsShl()) {
3386 GenerateShlLong(first, second_reg);
3387 } else if (op->IsShr()) {
3388 GenerateShrLong(first, second_reg);
3389 } else {
3390 GenerateUShrLong(first, second_reg);
3391 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003392 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003393 // Shift by a constant.
3394 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3395 // Nothing to do if the shift is 0, as the input is already the output.
3396 if (shift != 0) {
3397 if (op->IsShl()) {
3398 GenerateShlLong(first, shift);
3399 } else if (op->IsShr()) {
3400 GenerateShrLong(first, shift);
3401 } else {
3402 GenerateUShrLong(first, shift);
3403 }
3404 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003405 }
3406 break;
3407 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003408 default:
3409 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3410 }
3411}
3412
Mark P Mendell73945692015-04-29 14:56:17 +00003413void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3414 Register low = loc.AsRegisterPairLow<Register>();
3415 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003416 if (shift == 1) {
3417 // This is just an addition.
3418 __ addl(low, low);
3419 __ adcl(high, high);
3420 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003421 // Shift by 32 is easy. High gets low, and low gets 0.
3422 codegen_->EmitParallelMoves(
3423 loc.ToLow(),
3424 loc.ToHigh(),
3425 Primitive::kPrimInt,
3426 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3427 loc.ToLow(),
3428 Primitive::kPrimInt);
3429 } else if (shift > 32) {
3430 // Low part becomes 0. High part is low part << (shift-32).
3431 __ movl(high, low);
3432 __ shll(high, Immediate(shift - 32));
3433 __ xorl(low, low);
3434 } else {
3435 // Between 1 and 31.
3436 __ shld(high, low, Immediate(shift));
3437 __ shll(low, Immediate(shift));
3438 }
3439}
3440
Calin Juravle9aec02f2014-11-18 23:06:35 +00003441void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003442 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003443 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3444 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3445 __ testl(shifter, Immediate(32));
3446 __ j(kEqual, &done);
3447 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3448 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3449 __ Bind(&done);
3450}
3451
Mark P Mendell73945692015-04-29 14:56:17 +00003452void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3453 Register low = loc.AsRegisterPairLow<Register>();
3454 Register high = loc.AsRegisterPairHigh<Register>();
3455 if (shift == 32) {
3456 // Need to copy the sign.
3457 DCHECK_NE(low, high);
3458 __ movl(low, high);
3459 __ sarl(high, Immediate(31));
3460 } else if (shift > 32) {
3461 DCHECK_NE(low, high);
3462 // High part becomes sign. Low part is shifted by shift - 32.
3463 __ movl(low, high);
3464 __ sarl(high, Immediate(31));
3465 __ sarl(low, Immediate(shift - 32));
3466 } else {
3467 // Between 1 and 31.
3468 __ shrd(low, high, Immediate(shift));
3469 __ sarl(high, Immediate(shift));
3470 }
3471}
3472
Calin Juravle9aec02f2014-11-18 23:06:35 +00003473void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003474 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003475 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3476 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3477 __ testl(shifter, Immediate(32));
3478 __ j(kEqual, &done);
3479 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3480 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3481 __ Bind(&done);
3482}
3483
Mark P Mendell73945692015-04-29 14:56:17 +00003484void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3485 Register low = loc.AsRegisterPairLow<Register>();
3486 Register high = loc.AsRegisterPairHigh<Register>();
3487 if (shift == 32) {
3488 // Shift by 32 is easy. Low gets high, and high gets 0.
3489 codegen_->EmitParallelMoves(
3490 loc.ToHigh(),
3491 loc.ToLow(),
3492 Primitive::kPrimInt,
3493 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3494 loc.ToHigh(),
3495 Primitive::kPrimInt);
3496 } else if (shift > 32) {
3497 // Low part is high >> (shift - 32). High part becomes 0.
3498 __ movl(low, high);
3499 __ shrl(low, Immediate(shift - 32));
3500 __ xorl(high, high);
3501 } else {
3502 // Between 1 and 31.
3503 __ shrd(low, high, Immediate(shift));
3504 __ shrl(high, Immediate(shift));
3505 }
3506}
3507
Calin Juravle9aec02f2014-11-18 23:06:35 +00003508void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003509 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003510 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3511 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3512 __ testl(shifter, Immediate(32));
3513 __ j(kEqual, &done);
3514 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3515 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3516 __ Bind(&done);
3517}
3518
3519void LocationsBuilderX86::VisitShl(HShl* shl) {
3520 HandleShift(shl);
3521}
3522
3523void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3524 HandleShift(shl);
3525}
3526
3527void LocationsBuilderX86::VisitShr(HShr* shr) {
3528 HandleShift(shr);
3529}
3530
3531void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3532 HandleShift(shr);
3533}
3534
3535void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3536 HandleShift(ushr);
3537}
3538
3539void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3540 HandleShift(ushr);
3541}
3542
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003543void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003544 LocationSummary* locations =
3545 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003546 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003547 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003548 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003549 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003550}
3551
3552void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3553 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003554 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003555 // Note: if heap poisoning is enabled, the entry point takes cares
3556 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003557 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3558 instruction,
3559 instruction->GetDexPc(),
3560 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003561 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003562}
3563
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003564void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3565 LocationSummary* locations =
3566 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3567 locations->SetOut(Location::RegisterLocation(EAX));
3568 InvokeRuntimeCallingConvention calling_convention;
3569 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003570 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003571 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003572}
3573
3574void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3575 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003576 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3577
Roland Levillain4d027112015-07-01 15:41:14 +01003578 // Note: if heap poisoning is enabled, the entry point takes cares
3579 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003580 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3581 instruction,
3582 instruction->GetDexPc(),
3583 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003584 DCHECK(!codegen_->IsLeafMethod());
3585}
3586
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003587void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003588 LocationSummary* locations =
3589 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003590 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3591 if (location.IsStackSlot()) {
3592 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3593 } else if (location.IsDoubleStackSlot()) {
3594 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003595 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003596 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003597}
3598
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003599void InstructionCodeGeneratorX86::VisitParameterValue(
3600 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3601}
3602
3603void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3604 LocationSummary* locations =
3605 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3606 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3607}
3608
3609void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003610}
3611
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003612void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003613 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003614 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003615 locations->SetInAt(0, Location::RequiresRegister());
3616 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003617}
3618
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003619void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3620 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003621 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003622 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003623 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003624 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003625 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003626 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003627 break;
3628
3629 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003630 __ notl(out.AsRegisterPairLow<Register>());
3631 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003632 break;
3633
3634 default:
3635 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3636 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003637}
3638
David Brazdil66d126e2015-04-03 16:02:44 +01003639void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3640 LocationSummary* locations =
3641 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3642 locations->SetInAt(0, Location::RequiresRegister());
3643 locations->SetOut(Location::SameAsFirstInput());
3644}
3645
3646void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003647 LocationSummary* locations = bool_not->GetLocations();
3648 Location in = locations->InAt(0);
3649 Location out = locations->Out();
3650 DCHECK(in.Equals(out));
3651 __ xorl(out.AsRegister<Register>(), Immediate(1));
3652}
3653
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003654void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003655 LocationSummary* locations =
3656 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003657 switch (compare->InputAt(0)->GetType()) {
3658 case Primitive::kPrimLong: {
3659 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003660 locations->SetInAt(1, Location::Any());
3661 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3662 break;
3663 }
3664 case Primitive::kPrimFloat:
3665 case Primitive::kPrimDouble: {
3666 locations->SetInAt(0, Location::RequiresFpuRegister());
3667 locations->SetInAt(1, Location::RequiresFpuRegister());
3668 locations->SetOut(Location::RequiresRegister());
3669 break;
3670 }
3671 default:
3672 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3673 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003674}
3675
3676void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003677 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003678 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003679 Location left = locations->InAt(0);
3680 Location right = locations->InAt(1);
3681
Mark Mendell0c9497d2015-08-21 09:30:05 -04003682 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003683 switch (compare->InputAt(0)->GetType()) {
3684 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003685 Register left_low = left.AsRegisterPairLow<Register>();
3686 Register left_high = left.AsRegisterPairHigh<Register>();
3687 int32_t val_low = 0;
3688 int32_t val_high = 0;
3689 bool right_is_const = false;
3690
3691 if (right.IsConstant()) {
3692 DCHECK(right.GetConstant()->IsLongConstant());
3693 right_is_const = true;
3694 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3695 val_low = Low32Bits(val);
3696 val_high = High32Bits(val);
3697 }
3698
Calin Juravleddb7df22014-11-25 20:56:51 +00003699 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003700 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003701 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003702 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003703 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003704 DCHECK(right_is_const) << right;
3705 if (val_high == 0) {
3706 __ testl(left_high, left_high);
3707 } else {
3708 __ cmpl(left_high, Immediate(val_high));
3709 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003710 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003711 __ j(kLess, &less); // Signed compare.
3712 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003713 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003714 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003715 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003716 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003717 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003718 DCHECK(right_is_const) << right;
3719 if (val_low == 0) {
3720 __ testl(left_low, left_low);
3721 } else {
3722 __ cmpl(left_low, Immediate(val_low));
3723 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003724 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003725 break;
3726 }
3727 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003728 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003729 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3730 break;
3731 }
3732 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003733 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003734 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003735 break;
3736 }
3737 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003738 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003739 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003740 __ movl(out, Immediate(0));
3741 __ j(kEqual, &done);
3742 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3743
3744 __ Bind(&greater);
3745 __ movl(out, Immediate(1));
3746 __ jmp(&done);
3747
3748 __ Bind(&less);
3749 __ movl(out, Immediate(-1));
3750
3751 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003752}
3753
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003754void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003755 LocationSummary* locations =
3756 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003757 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3758 locations->SetInAt(i, Location::Any());
3759 }
3760 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003761}
3762
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003763void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003764 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003765}
3766
Calin Juravle52c48962014-12-16 17:02:57 +00003767void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3768 /*
3769 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3770 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3771 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3772 */
3773 switch (kind) {
3774 case MemBarrierKind::kAnyAny: {
3775 __ mfence();
3776 break;
3777 }
3778 case MemBarrierKind::kAnyStore:
3779 case MemBarrierKind::kLoadAny:
3780 case MemBarrierKind::kStoreStore: {
3781 // nop
3782 break;
3783 }
3784 default:
3785 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01003786 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003787}
3788
Vladimir Markodc151b22015-10-15 18:02:30 +01003789HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
3790 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
3791 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01003792 switch (desired_dispatch_info.code_ptr_location) {
3793 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3794 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
3795 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
3796 // (Though the direct CALL ptr16:32 is available for consideration).
3797 return HInvokeStaticOrDirect::DispatchInfo {
3798 desired_dispatch_info.method_load_kind,
3799 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
3800 desired_dispatch_info.method_load_data,
3801 0u
3802 };
3803 default:
3804 return desired_dispatch_info;
3805 }
3806}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003807
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003808Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
3809 Register temp) {
3810 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
3811 Location location = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3812 if (!invoke->GetLocations()->Intrinsified()) {
3813 return location.AsRegister<Register>();
3814 }
3815 // For intrinsics we allow any location, so it may be on the stack.
3816 if (!location.IsRegister()) {
3817 __ movl(temp, Address(ESP, location.GetStackIndex()));
3818 return temp;
3819 }
3820 // For register locations, check if the register was saved. If so, get it from the stack.
3821 // Note: There is a chance that the register was saved but not overwritten, so we could
3822 // save one load. However, since this is just an intrinsic slow path we prefer this
3823 // simple and more robust approach rather that trying to determine if that's the case.
3824 SlowPathCode* slow_path = GetCurrentSlowPath();
3825 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
3826 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
3827 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
3828 __ movl(temp, Address(ESP, stack_offset));
3829 return temp;
3830 }
3831 return location.AsRegister<Register>();
3832}
3833
Vladimir Marko58155012015-08-19 12:49:41 +00003834void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
3835 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
3836 switch (invoke->GetMethodLoadKind()) {
3837 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
3838 // temp = thread->string_init_entrypoint
3839 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
3840 break;
3841 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
3842 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3843 break;
3844 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
3845 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
3846 break;
3847 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
3848 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
3849 method_patches_.emplace_back(invoke->GetTargetMethod());
3850 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
3851 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003852 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
3853 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
3854 temp.AsRegister<Register>());
3855 uint32_t offset = invoke->GetDexCacheArrayOffset();
3856 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
3857 // Add the patch entry and bind its label at the end of the instruction.
3858 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
3859 __ Bind(&pc_relative_dex_cache_patches_.back().label);
3860 break;
3861 }
Vladimir Marko58155012015-08-19 12:49:41 +00003862 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
3863 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
3864 Register method_reg;
3865 Register reg = temp.AsRegister<Register>();
3866 if (current_method.IsRegister()) {
3867 method_reg = current_method.AsRegister<Register>();
3868 } else {
3869 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
3870 DCHECK(!current_method.IsValid());
3871 method_reg = reg;
3872 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
3873 }
3874 // temp = temp->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01003875 __ movl(reg, Address(method_reg,
3876 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00003877 // temp = temp[index_in_cache]
3878 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
3879 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
3880 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01003881 }
Vladimir Marko58155012015-08-19 12:49:41 +00003882 }
3883
3884 switch (invoke->GetCodePtrLocation()) {
3885 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
3886 __ call(GetFrameEntryLabel());
3887 break;
3888 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
3889 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
3890 Label* label = &relative_call_patches_.back().label;
3891 __ call(label); // Bind to the patch label, override at link time.
3892 __ Bind(label); // Bind the label at the end of the "call" insn.
3893 break;
3894 }
3895 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
3896 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01003897 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
3898 LOG(FATAL) << "Unsupported";
3899 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00003900 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
3901 // (callee_method + offset_of_quick_compiled_code)()
3902 __ call(Address(callee_method.AsRegister<Register>(),
3903 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
3904 kX86WordSize).Int32Value()));
3905 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04003906 }
3907
3908 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04003909}
3910
Andreas Gampebfb5ba92015-09-01 15:45:02 +00003911void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
3912 Register temp = temp_in.AsRegister<Register>();
3913 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3914 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
3915 LocationSummary* locations = invoke->GetLocations();
3916 Location receiver = locations->InAt(0);
3917 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3918 // temp = object->GetClass();
3919 DCHECK(receiver.IsRegister());
3920 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
3921 MaybeRecordImplicitNullCheck(invoke);
3922 __ MaybeUnpoisonHeapReference(temp);
3923 // temp = temp->GetMethodAt(method_offset);
3924 __ movl(temp, Address(temp, method_offset));
3925 // call temp->GetEntryPoint();
3926 __ call(Address(
3927 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
3928}
3929
Vladimir Marko58155012015-08-19 12:49:41 +00003930void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
3931 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003932 size_t size =
3933 method_patches_.size() +
3934 relative_call_patches_.size() +
3935 pc_relative_dex_cache_patches_.size();
3936 linker_patches->reserve(size);
3937 // The label points to the end of the "movl" insn but the literal offset for method
3938 // patch needs to point to the embedded constant which occupies the last 4 bytes.
3939 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00003940 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003941 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00003942 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
3943 info.target_method.dex_file,
3944 info.target_method.dex_method_index));
3945 }
3946 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003947 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00003948 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
3949 info.target_method.dex_file,
3950 info.target_method.dex_method_index));
3951 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00003952 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
3953 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
3954 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
3955 &info.target_dex_file,
3956 GetMethodAddressOffset(),
3957 info.element_offset));
3958 }
Vladimir Marko58155012015-08-19 12:49:41 +00003959}
3960
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003961void CodeGeneratorX86::MarkGCCard(Register temp,
3962 Register card,
3963 Register object,
3964 Register value,
3965 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003966 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003967 if (value_can_be_null) {
3968 __ testl(value, value);
3969 __ j(kEqual, &is_null);
3970 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003971 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
3972 __ movl(temp, object);
3973 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00003974 __ movb(Address(temp, card, TIMES_1, 0),
3975 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01003976 if (value_can_be_null) {
3977 __ Bind(&is_null);
3978 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01003979}
3980
Calin Juravle52c48962014-12-16 17:02:57 +00003981void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
3982 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffray39468442014-09-02 15:17:15 +01003983 LocationSummary* locations =
3984 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01003985 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003986
Alexandre Rames88c13cd2015-04-14 17:35:39 +01003987 if (Primitive::IsFloatingPointType(instruction->GetType())) {
3988 locations->SetOut(Location::RequiresFpuRegister());
3989 } else {
3990 // The output overlaps in case of long: we don't want the low move to overwrite
3991 // the object's location.
3992 locations->SetOut(Location::RequiresRegister(),
3993 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
3994 : Location::kNoOutputOverlap);
3995 }
Calin Juravle52c48962014-12-16 17:02:57 +00003996
3997 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
3998 // Long values can be loaded atomically into an XMM using movsd.
3999 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
4000 // and then copy the XMM into the output 32bits at a time).
4001 locations->AddTemp(Location::RequiresFpuRegister());
4002 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004003}
4004
Calin Juravle52c48962014-12-16 17:02:57 +00004005void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4006 const FieldInfo& field_info) {
4007 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004008
Calin Juravle52c48962014-12-16 17:02:57 +00004009 LocationSummary* locations = instruction->GetLocations();
4010 Register base = locations->InAt(0).AsRegister<Register>();
4011 Location out = locations->Out();
4012 bool is_volatile = field_info.IsVolatile();
4013 Primitive::Type field_type = field_info.GetFieldType();
4014 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4015
4016 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004017 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004018 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004019 break;
4020 }
4021
4022 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004023 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004024 break;
4025 }
4026
4027 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004028 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004029 break;
4030 }
4031
4032 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004033 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004034 break;
4035 }
4036
4037 case Primitive::kPrimInt:
4038 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00004039 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004040 break;
4041 }
4042
4043 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004044 if (is_volatile) {
4045 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4046 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004047 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004048 __ movd(out.AsRegisterPairLow<Register>(), temp);
4049 __ psrlq(temp, Immediate(32));
4050 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4051 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004052 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004053 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004054 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004055 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4056 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004057 break;
4058 }
4059
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004060 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004061 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004062 break;
4063 }
4064
4065 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004066 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004067 break;
4068 }
4069
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004070 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004071 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004072 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004073 }
Calin Juravle52c48962014-12-16 17:02:57 +00004074
Calin Juravle77520bc2015-01-12 18:45:46 +00004075 // Longs are handled in the switch.
4076 if (field_type != Primitive::kPrimLong) {
4077 codegen_->MaybeRecordImplicitNullCheck(instruction);
4078 }
4079
Calin Juravle52c48962014-12-16 17:02:57 +00004080 if (is_volatile) {
4081 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4082 }
Roland Levillain4d027112015-07-01 15:41:14 +01004083
4084 if (field_type == Primitive::kPrimNot) {
4085 __ MaybeUnpoisonHeapReference(out.AsRegister<Register>());
4086 }
Calin Juravle52c48962014-12-16 17:02:57 +00004087}
4088
4089void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4090 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4091
4092 LocationSummary* locations =
4093 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4094 locations->SetInAt(0, Location::RequiresRegister());
4095 bool is_volatile = field_info.IsVolatile();
4096 Primitive::Type field_type = field_info.GetFieldType();
4097 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4098 || (field_type == Primitive::kPrimByte);
4099
4100 // The register allocator does not support multiple
4101 // inputs that die at entry with one in a specific register.
4102 if (is_byte_type) {
4103 // Ensure the value is in a byte register.
4104 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004105 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004106 if (is_volatile && field_type == Primitive::kPrimDouble) {
4107 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4108 locations->SetInAt(1, Location::RequiresFpuRegister());
4109 } else {
4110 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4111 }
4112 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4113 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004114 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004115
Calin Juravle52c48962014-12-16 17:02:57 +00004116 // 64bits value can be atomically written to an address with movsd and an XMM register.
4117 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4118 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4119 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4120 // isolated cases when we need this it isn't worth adding the extra complexity.
4121 locations->AddTemp(Location::RequiresFpuRegister());
4122 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004123 } else {
4124 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4125
4126 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4127 // Temporary registers for the write barrier.
4128 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4129 // Ensure the card is in a byte register.
4130 locations->AddTemp(Location::RegisterLocation(ECX));
4131 }
Calin Juravle52c48962014-12-16 17:02:57 +00004132 }
4133}
4134
4135void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004136 const FieldInfo& field_info,
4137 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004138 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4139
4140 LocationSummary* locations = instruction->GetLocations();
4141 Register base = locations->InAt(0).AsRegister<Register>();
4142 Location value = locations->InAt(1);
4143 bool is_volatile = field_info.IsVolatile();
4144 Primitive::Type field_type = field_info.GetFieldType();
4145 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004146 bool needs_write_barrier =
4147 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004148
4149 if (is_volatile) {
4150 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4151 }
4152
Mark Mendell81489372015-11-04 11:30:41 -05004153 bool maybe_record_implicit_null_check_done = false;
4154
Calin Juravle52c48962014-12-16 17:02:57 +00004155 switch (field_type) {
4156 case Primitive::kPrimBoolean:
4157 case Primitive::kPrimByte: {
4158 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4159 break;
4160 }
4161
4162 case Primitive::kPrimShort:
4163 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004164 if (value.IsConstant()) {
4165 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4166 __ movw(Address(base, offset), Immediate(v));
4167 } else {
4168 __ movw(Address(base, offset), value.AsRegister<Register>());
4169 }
Calin Juravle52c48962014-12-16 17:02:57 +00004170 break;
4171 }
4172
4173 case Primitive::kPrimInt:
4174 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004175 if (kPoisonHeapReferences && needs_write_barrier) {
4176 // Note that in the case where `value` is a null reference,
4177 // we do not enter this block, as the reference does not
4178 // need poisoning.
4179 DCHECK_EQ(field_type, Primitive::kPrimNot);
4180 Register temp = locations->GetTemp(0).AsRegister<Register>();
4181 __ movl(temp, value.AsRegister<Register>());
4182 __ PoisonHeapReference(temp);
4183 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004184 } else if (value.IsConstant()) {
4185 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4186 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004187 } else {
4188 __ movl(Address(base, offset), value.AsRegister<Register>());
4189 }
Calin Juravle52c48962014-12-16 17:02:57 +00004190 break;
4191 }
4192
4193 case Primitive::kPrimLong: {
4194 if (is_volatile) {
4195 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4196 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4197 __ movd(temp1, value.AsRegisterPairLow<Register>());
4198 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4199 __ punpckldq(temp1, temp2);
4200 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004201 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004202 } else if (value.IsConstant()) {
4203 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4204 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4205 codegen_->MaybeRecordImplicitNullCheck(instruction);
4206 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004207 } else {
4208 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004209 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004210 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4211 }
Mark Mendell81489372015-11-04 11:30:41 -05004212 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004213 break;
4214 }
4215
4216 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004217 if (value.IsConstant()) {
4218 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4219 __ movl(Address(base, offset), Immediate(v));
4220 } else {
4221 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4222 }
Calin Juravle52c48962014-12-16 17:02:57 +00004223 break;
4224 }
4225
4226 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004227 if (value.IsConstant()) {
4228 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4229 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4230 codegen_->MaybeRecordImplicitNullCheck(instruction);
4231 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4232 maybe_record_implicit_null_check_done = true;
4233 } else {
4234 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4235 }
Calin Juravle52c48962014-12-16 17:02:57 +00004236 break;
4237 }
4238
4239 case Primitive::kPrimVoid:
4240 LOG(FATAL) << "Unreachable type " << field_type;
4241 UNREACHABLE();
4242 }
4243
Mark Mendell81489372015-11-04 11:30:41 -05004244 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004245 codegen_->MaybeRecordImplicitNullCheck(instruction);
4246 }
4247
Roland Levillain4d027112015-07-01 15:41:14 +01004248 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004249 Register temp = locations->GetTemp(0).AsRegister<Register>();
4250 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004251 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004252 }
4253
Calin Juravle52c48962014-12-16 17:02:57 +00004254 if (is_volatile) {
4255 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4256 }
4257}
4258
4259void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4260 HandleFieldGet(instruction, instruction->GetFieldInfo());
4261}
4262
4263void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4264 HandleFieldGet(instruction, instruction->GetFieldInfo());
4265}
4266
4267void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4268 HandleFieldSet(instruction, instruction->GetFieldInfo());
4269}
4270
4271void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004272 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004273}
4274
4275void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4276 HandleFieldSet(instruction, instruction->GetFieldInfo());
4277}
4278
4279void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004280 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004281}
4282
4283void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4284 HandleFieldGet(instruction, instruction->GetFieldInfo());
4285}
4286
4287void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4288 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289}
4290
Calin Juravlee460d1d2015-09-29 04:52:17 +01004291void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4292 HUnresolvedInstanceFieldGet* instruction) {
4293 FieldAccessCallingConventionX86 calling_convention;
4294 codegen_->CreateUnresolvedFieldLocationSummary(
4295 instruction, instruction->GetFieldType(), calling_convention);
4296}
4297
4298void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4299 HUnresolvedInstanceFieldGet* instruction) {
4300 FieldAccessCallingConventionX86 calling_convention;
4301 codegen_->GenerateUnresolvedFieldAccess(instruction,
4302 instruction->GetFieldType(),
4303 instruction->GetFieldIndex(),
4304 instruction->GetDexPc(),
4305 calling_convention);
4306}
4307
4308void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4309 HUnresolvedInstanceFieldSet* instruction) {
4310 FieldAccessCallingConventionX86 calling_convention;
4311 codegen_->CreateUnresolvedFieldLocationSummary(
4312 instruction, instruction->GetFieldType(), calling_convention);
4313}
4314
4315void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4316 HUnresolvedInstanceFieldSet* instruction) {
4317 FieldAccessCallingConventionX86 calling_convention;
4318 codegen_->GenerateUnresolvedFieldAccess(instruction,
4319 instruction->GetFieldType(),
4320 instruction->GetFieldIndex(),
4321 instruction->GetDexPc(),
4322 calling_convention);
4323}
4324
4325void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4326 HUnresolvedStaticFieldGet* instruction) {
4327 FieldAccessCallingConventionX86 calling_convention;
4328 codegen_->CreateUnresolvedFieldLocationSummary(
4329 instruction, instruction->GetFieldType(), calling_convention);
4330}
4331
4332void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4333 HUnresolvedStaticFieldGet* instruction) {
4334 FieldAccessCallingConventionX86 calling_convention;
4335 codegen_->GenerateUnresolvedFieldAccess(instruction,
4336 instruction->GetFieldType(),
4337 instruction->GetFieldIndex(),
4338 instruction->GetDexPc(),
4339 calling_convention);
4340}
4341
4342void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4343 HUnresolvedStaticFieldSet* instruction) {
4344 FieldAccessCallingConventionX86 calling_convention;
4345 codegen_->CreateUnresolvedFieldLocationSummary(
4346 instruction, instruction->GetFieldType(), calling_convention);
4347}
4348
4349void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4350 HUnresolvedStaticFieldSet* instruction) {
4351 FieldAccessCallingConventionX86 calling_convention;
4352 codegen_->GenerateUnresolvedFieldAccess(instruction,
4353 instruction->GetFieldType(),
4354 instruction->GetFieldIndex(),
4355 instruction->GetDexPc(),
4356 calling_convention);
4357}
4358
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004359void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004360 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4361 ? LocationSummary::kCallOnSlowPath
4362 : LocationSummary::kNoCall;
4363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4364 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004365 ? Location::RequiresRegister()
4366 : Location::Any();
4367 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004368 if (instruction->HasUses()) {
4369 locations->SetOut(Location::SameAsFirstInput());
4370 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371}
4372
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004373void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004374 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4375 return;
4376 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004377 LocationSummary* locations = instruction->GetLocations();
4378 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004379
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004380 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4381 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4382}
4383
4384void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004385 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004386 codegen_->AddSlowPath(slow_path);
4387
4388 LocationSummary* locations = instruction->GetLocations();
4389 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004390
4391 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004392 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004393 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004394 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004395 } else {
4396 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004397 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004398 __ jmp(slow_path->GetEntryLabel());
4399 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004400 }
4401 __ j(kEqual, slow_path->GetEntryLabel());
4402}
4403
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004404void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004405 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004406 GenerateImplicitNullCheck(instruction);
4407 } else {
4408 GenerateExplicitNullCheck(instruction);
4409 }
4410}
4411
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004412void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004413 LocationSummary* locations =
4414 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004415 locations->SetInAt(0, Location::RequiresRegister());
4416 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004417 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4418 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4419 } else {
4420 // The output overlaps in case of long: we don't want the low move to overwrite
4421 // the array's location.
4422 locations->SetOut(Location::RequiresRegister(),
4423 (instruction->GetType() == Primitive::kPrimLong) ? Location::kOutputOverlap
4424 : Location::kNoOutputOverlap);
4425 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004426}
4427
4428void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4429 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004430 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004431 Location index = locations->InAt(1);
4432
Calin Juravle77520bc2015-01-12 18:45:46 +00004433 Primitive::Type type = instruction->GetType();
4434 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004435 case Primitive::kPrimBoolean: {
4436 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004437 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004438 if (index.IsConstant()) {
4439 __ movzxb(out, Address(obj,
4440 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4441 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004442 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004443 }
4444 break;
4445 }
4446
4447 case Primitive::kPrimByte: {
4448 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004449 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004450 if (index.IsConstant()) {
4451 __ movsxb(out, Address(obj,
4452 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4453 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004454 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004455 }
4456 break;
4457 }
4458
4459 case Primitive::kPrimShort: {
4460 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004461 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004462 if (index.IsConstant()) {
4463 __ movsxw(out, Address(obj,
4464 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4465 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004466 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004467 }
4468 break;
4469 }
4470
4471 case Primitive::kPrimChar: {
4472 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004473 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004474 if (index.IsConstant()) {
4475 __ movzxw(out, Address(obj,
4476 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4477 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004478 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004479 }
4480 break;
4481 }
4482
4483 case Primitive::kPrimInt:
4484 case Primitive::kPrimNot: {
4485 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004486 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004487 if (index.IsConstant()) {
4488 __ movl(out, Address(obj,
4489 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4490 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004491 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004492 }
4493 break;
4494 }
4495
4496 case Primitive::kPrimLong: {
4497 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004498 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004499 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004500 if (index.IsConstant()) {
4501 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004502 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004503 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004504 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004505 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004506 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004507 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004508 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004509 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004510 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004511 }
4512 break;
4513 }
4514
Mark Mendell7c8d0092015-01-26 11:21:33 -05004515 case Primitive::kPrimFloat: {
4516 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4517 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4518 if (index.IsConstant()) {
4519 __ movss(out, Address(obj,
4520 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4521 } else {
4522 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4523 }
4524 break;
4525 }
4526
4527 case Primitive::kPrimDouble: {
4528 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4529 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4530 if (index.IsConstant()) {
4531 __ movsd(out, Address(obj,
4532 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4533 } else {
4534 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4535 }
4536 break;
4537 }
4538
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004539 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004540 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004541 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004542 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004543
4544 if (type != Primitive::kPrimLong) {
4545 codegen_->MaybeRecordImplicitNullCheck(instruction);
4546 }
Roland Levillain4d027112015-07-01 15:41:14 +01004547
4548 if (type == Primitive::kPrimNot) {
4549 Register out = locations->Out().AsRegister<Register>();
4550 __ MaybeUnpoisonHeapReference(out);
4551 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004552}
4553
4554void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004555 // This location builder might end up asking to up to four registers, which is
4556 // not currently possible for baseline. The situation in which we need four
4557 // registers cannot be met by baseline though, because it has not run any
4558 // optimization.
4559
Nicolas Geoffray39468442014-09-02 15:17:15 +01004560 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004561 bool needs_write_barrier =
4562 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
4563
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004564 bool may_need_runtime_call = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004565
Nicolas Geoffray39468442014-09-02 15:17:15 +01004566 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4567 instruction,
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004568 may_need_runtime_call ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004569
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004570 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4571 || (value_type == Primitive::kPrimByte);
4572 // We need the inputs to be different than the output in case of long operation.
4573 // In case of a byte operation, the register allocator does not support multiple
4574 // inputs that die at entry with one in a specific register.
4575 locations->SetInAt(0, Location::RequiresRegister());
4576 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4577 if (is_byte_type) {
4578 // Ensure the value is in a byte register.
4579 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4580 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004581 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004582 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004583 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4584 }
4585 if (needs_write_barrier) {
4586 // Temporary registers for the write barrier.
4587 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4588 // Ensure the card is in a byte register.
4589 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004590 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004591}
4592
4593void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4594 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004595 Register array = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004596 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004597 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004598 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004599 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4600 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4601 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
4602 bool may_need_runtime_call = locations->CanCall();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004603 bool needs_write_barrier =
4604 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004605
4606 switch (value_type) {
4607 case Primitive::kPrimBoolean:
4608 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004609 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4610 Address address = index.IsConstant()
4611 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4612 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4613 if (value.IsRegister()) {
4614 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004615 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004616 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004617 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004618 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004619 break;
4620 }
4621
4622 case Primitive::kPrimShort:
4623 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004624 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4625 Address address = index.IsConstant()
4626 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4627 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4628 if (value.IsRegister()) {
4629 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004630 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004631 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004632 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004633 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004634 break;
4635 }
4636
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004637 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004638 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4639 Address address = index.IsConstant()
4640 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4641 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4642 if (!value.IsRegister()) {
4643 // Just setting null.
4644 DCHECK(instruction->InputAt(2)->IsNullConstant());
4645 DCHECK(value.IsConstant()) << value;
4646 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004647 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004648 DCHECK(!needs_write_barrier);
4649 DCHECK(!may_need_runtime_call);
4650 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004651 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004652
4653 DCHECK(needs_write_barrier);
4654 Register register_value = value.AsRegister<Register>();
4655 NearLabel done, not_null, do_put;
4656 SlowPathCode* slow_path = nullptr;
4657 Register temp = locations->GetTemp(0).AsRegister<Register>();
4658 if (may_need_runtime_call) {
4659 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4660 codegen_->AddSlowPath(slow_path);
4661 if (instruction->GetValueCanBeNull()) {
4662 __ testl(register_value, register_value);
4663 __ j(kNotEqual, &not_null);
4664 __ movl(address, Immediate(0));
4665 codegen_->MaybeRecordImplicitNullCheck(instruction);
4666 __ jmp(&done);
4667 __ Bind(&not_null);
4668 }
4669
4670 __ movl(temp, Address(array, class_offset));
4671 codegen_->MaybeRecordImplicitNullCheck(instruction);
4672 __ MaybeUnpoisonHeapReference(temp);
4673 __ movl(temp, Address(temp, component_offset));
4674 // No need to poison/unpoison, we're comparing two poisoned references.
4675 __ cmpl(temp, Address(register_value, class_offset));
4676 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4677 __ j(kEqual, &do_put);
4678 __ MaybeUnpoisonHeapReference(temp);
4679 __ movl(temp, Address(temp, super_offset));
4680 // No need to unpoison, we're comparing against null..
4681 __ testl(temp, temp);
4682 __ j(kNotEqual, slow_path->GetEntryLabel());
4683 __ Bind(&do_put);
4684 } else {
4685 __ j(kNotEqual, slow_path->GetEntryLabel());
4686 }
4687 }
4688
4689 if (kPoisonHeapReferences) {
4690 __ movl(temp, register_value);
4691 __ PoisonHeapReference(temp);
4692 __ movl(address, temp);
4693 } else {
4694 __ movl(address, register_value);
4695 }
4696 if (!may_need_runtime_call) {
4697 codegen_->MaybeRecordImplicitNullCheck(instruction);
4698 }
4699
4700 Register card = locations->GetTemp(1).AsRegister<Register>();
4701 codegen_->MarkGCCard(
4702 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
4703 __ Bind(&done);
4704
4705 if (slow_path != nullptr) {
4706 __ Bind(slow_path->GetExitLabel());
4707 }
4708
4709 break;
4710 }
4711 case Primitive::kPrimInt: {
4712 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4713 Address address = index.IsConstant()
4714 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4715 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
4716 if (value.IsRegister()) {
4717 __ movl(address, value.AsRegister<Register>());
4718 } else {
4719 DCHECK(value.IsConstant()) << value;
4720 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4721 __ movl(address, Immediate(v));
4722 }
4723 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004724 break;
4725 }
4726
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727 case Primitive::kPrimLong: {
4728 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004729 if (index.IsConstant()) {
4730 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004731 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004732 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004733 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004734 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004735 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004736 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004737 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004738 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004739 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004740 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004741 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004742 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004743 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004744 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004745 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004746 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004747 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004748 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004749 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004750 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004751 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004752 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004753 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00004754 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004755 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004756 Immediate(High32Bits(val)));
4757 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004758 }
4759 break;
4760 }
4761
Mark Mendell7c8d0092015-01-26 11:21:33 -05004762 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004763 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4764 Address address = index.IsConstant()
4765 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4766 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05004767 if (value.IsFpuRegister()) {
4768 __ movss(address, value.AsFpuRegister<XmmRegister>());
4769 } else {
4770 DCHECK(value.IsConstant());
4771 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
4772 __ movl(address, Immediate(v));
4773 }
4774 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004775 break;
4776 }
4777
4778 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004779 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4780 Address address = index.IsConstant()
4781 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
4782 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05004783 if (value.IsFpuRegister()) {
4784 __ movsd(address, value.AsFpuRegister<XmmRegister>());
4785 } else {
4786 DCHECK(value.IsConstant());
4787 Address address_hi = index.IsConstant() ?
4788 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
4789 offset + kX86WordSize) :
4790 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
4791 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
4792 __ movl(address, Immediate(Low32Bits(v)));
4793 codegen_->MaybeRecordImplicitNullCheck(instruction);
4794 __ movl(address_hi, Immediate(High32Bits(v)));
4795 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004796 break;
4797 }
4798
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799 case Primitive::kPrimVoid:
4800 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07004801 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004802 }
4803}
4804
4805void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
4806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004807 locations->SetInAt(0, Location::RequiresRegister());
4808 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004809}
4810
4811void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
4812 LocationSummary* locations = instruction->GetLocations();
4813 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004814 Register obj = locations->InAt(0).AsRegister<Register>();
4815 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004816 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818}
4819
4820void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004821 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4822 ? LocationSummary::kCallOnSlowPath
4823 : LocationSummary::kNoCall;
4824 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05004825 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04004826 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004827 if (instruction->HasUses()) {
4828 locations->SetOut(Location::SameAsFirstInput());
4829 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004830}
4831
4832void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
4833 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05004834 Location index_loc = locations->InAt(0);
4835 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07004836 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01004837 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004838
Mark Mendell99dbd682015-04-22 16:18:52 -04004839 if (length_loc.IsConstant()) {
4840 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
4841 if (index_loc.IsConstant()) {
4842 // BCE will remove the bounds check if we are guarenteed to pass.
4843 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4844 if (index < 0 || index >= length) {
4845 codegen_->AddSlowPath(slow_path);
4846 __ jmp(slow_path->GetEntryLabel());
4847 } else {
4848 // Some optimization after BCE may have generated this, and we should not
4849 // generate a bounds check if it is a valid range.
4850 }
4851 return;
4852 }
4853
4854 // We have to reverse the jump condition because the length is the constant.
4855 Register index_reg = index_loc.AsRegister<Register>();
4856 __ cmpl(index_reg, Immediate(length));
4857 codegen_->AddSlowPath(slow_path);
4858 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004859 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04004860 Register length = length_loc.AsRegister<Register>();
4861 if (index_loc.IsConstant()) {
4862 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
4863 __ cmpl(length, Immediate(value));
4864 } else {
4865 __ cmpl(length, index_loc.AsRegister<Register>());
4866 }
4867 codegen_->AddSlowPath(slow_path);
4868 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05004869 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004870}
4871
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004872void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
4873 temp->SetLocations(nullptr);
4874}
4875
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004876void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004877 // Nothing to do, this is driven by the code generator.
4878}
4879
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004880void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004881 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01004882}
4883
4884void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004885 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
4886}
4887
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004888void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
4889 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
4890}
4891
4892void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004893 HBasicBlock* block = instruction->GetBlock();
4894 if (block->GetLoopInformation() != nullptr) {
4895 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
4896 // The back edge will generate the suspend check.
4897 return;
4898 }
4899 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
4900 // The goto will generate the suspend check.
4901 return;
4902 }
4903 GenerateSuspendCheck(instruction, nullptr);
4904}
4905
4906void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
4907 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004908 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01004909 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
4910 if (slow_path == nullptr) {
4911 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
4912 instruction->SetSlowPath(slow_path);
4913 codegen_->AddSlowPath(slow_path);
4914 if (successor != nullptr) {
4915 DCHECK(successor->IsLoopHeader());
4916 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
4917 }
4918 } else {
4919 DCHECK_EQ(slow_path->GetSuccessor(), successor);
4920 }
4921
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004922 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004923 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01004924 if (successor == nullptr) {
4925 __ j(kNotEqual, slow_path->GetEntryLabel());
4926 __ Bind(slow_path->GetReturnLabel());
4927 } else {
4928 __ j(kEqual, codegen_->GetLabelOf(successor));
4929 __ jmp(slow_path->GetEntryLabel());
4930 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00004931}
4932
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004933X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
4934 return codegen_->GetAssembler();
4935}
4936
Mark Mendell7c8d0092015-01-26 11:21:33 -05004937void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004938 ScratchRegisterScope ensure_scratch(
4939 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4940 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4941 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4942 __ movl(temp_reg, Address(ESP, src + stack_offset));
4943 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004944}
4945
4946void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00004947 ScratchRegisterScope ensure_scratch(
4948 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
4949 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
4950 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
4951 __ movl(temp_reg, Address(ESP, src + stack_offset));
4952 __ movl(Address(ESP, dst + stack_offset), temp_reg);
4953 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
4954 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004955}
4956
4957void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01004958 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004959 Location source = move->GetSource();
4960 Location destination = move->GetDestination();
4961
4962 if (source.IsRegister()) {
4963 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004964 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004965 } else {
4966 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00004967 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004968 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05004969 } else if (source.IsFpuRegister()) {
4970 if (destination.IsFpuRegister()) {
4971 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
4972 } else if (destination.IsStackSlot()) {
4973 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4974 } else {
4975 DCHECK(destination.IsDoubleStackSlot());
4976 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
4977 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004978 } else if (source.IsStackSlot()) {
4979 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004980 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05004981 } else if (destination.IsFpuRegister()) {
4982 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004983 } else {
4984 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05004985 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
4986 }
4987 } else if (source.IsDoubleStackSlot()) {
4988 if (destination.IsFpuRegister()) {
4989 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
4990 } else {
4991 DCHECK(destination.IsDoubleStackSlot()) << destination;
4992 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004993 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01004994 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05004995 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00004996 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004997 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05004998 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05004999 if (value == 0) {
5000 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5001 } else {
5002 __ movl(destination.AsRegister<Register>(), Immediate(value));
5003 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005004 } else {
5005 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005006 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005007 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005008 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005009 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005010 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005011 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005012 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005013 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5014 if (value == 0) {
5015 // Easy handling of 0.0.
5016 __ xorps(dest, dest);
5017 } else {
5018 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005019 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5020 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5021 __ movl(temp, Immediate(value));
5022 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005023 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005024 } else {
5025 DCHECK(destination.IsStackSlot()) << destination;
5026 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5027 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005028 } else if (constant->IsLongConstant()) {
5029 int64_t value = constant->AsLongConstant()->GetValue();
5030 int32_t low_value = Low32Bits(value);
5031 int32_t high_value = High32Bits(value);
5032 Immediate low(low_value);
5033 Immediate high(high_value);
5034 if (destination.IsDoubleStackSlot()) {
5035 __ movl(Address(ESP, destination.GetStackIndex()), low);
5036 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5037 } else {
5038 __ movl(destination.AsRegisterPairLow<Register>(), low);
5039 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5040 }
5041 } else {
5042 DCHECK(constant->IsDoubleConstant());
5043 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005044 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005045 int32_t low_value = Low32Bits(value);
5046 int32_t high_value = High32Bits(value);
5047 Immediate low(low_value);
5048 Immediate high(high_value);
5049 if (destination.IsFpuRegister()) {
5050 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5051 if (value == 0) {
5052 // Easy handling of 0.0.
5053 __ xorpd(dest, dest);
5054 } else {
5055 __ pushl(high);
5056 __ pushl(low);
5057 __ movsd(dest, Address(ESP, 0));
5058 __ addl(ESP, Immediate(8));
5059 }
5060 } else {
5061 DCHECK(destination.IsDoubleStackSlot()) << destination;
5062 __ movl(Address(ESP, destination.GetStackIndex()), low);
5063 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5064 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005065 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005066 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005067 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005068 }
5069}
5070
Mark Mendella5c19ce2015-04-01 12:51:05 -04005071void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005072 Register suggested_scratch = reg == EAX ? EBX : EAX;
5073 ScratchRegisterScope ensure_scratch(
5074 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5075
5076 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5077 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5078 __ movl(Address(ESP, mem + stack_offset), reg);
5079 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005080}
5081
Mark Mendell7c8d0092015-01-26 11:21:33 -05005082void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005083 ScratchRegisterScope ensure_scratch(
5084 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5085
5086 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5087 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5088 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5089 __ movss(Address(ESP, mem + stack_offset), reg);
5090 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005091}
5092
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005093void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005094 ScratchRegisterScope ensure_scratch1(
5095 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005096
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005097 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5098 ScratchRegisterScope ensure_scratch2(
5099 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005100
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005101 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5102 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5103 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5104 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5105 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5106 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005107}
5108
5109void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005110 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005111 Location source = move->GetSource();
5112 Location destination = move->GetDestination();
5113
5114 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005115 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5116 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5117 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5118 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5119 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005120 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005121 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005122 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005123 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005124 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5125 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005126 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5127 // Use XOR Swap algorithm to avoid a temporary.
5128 DCHECK_NE(source.reg(), destination.reg());
5129 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5130 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5131 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5132 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5133 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5134 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5135 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005136 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5137 // Take advantage of the 16 bytes in the XMM register.
5138 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5139 Address stack(ESP, destination.GetStackIndex());
5140 // Load the double into the high doubleword.
5141 __ movhpd(reg, stack);
5142
5143 // Store the low double into the destination.
5144 __ movsd(stack, reg);
5145
5146 // Move the high double to the low double.
5147 __ psrldq(reg, Immediate(8));
5148 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5149 // Take advantage of the 16 bytes in the XMM register.
5150 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5151 Address stack(ESP, source.GetStackIndex());
5152 // Load the double into the high doubleword.
5153 __ movhpd(reg, stack);
5154
5155 // Store the low double into the destination.
5156 __ movsd(stack, reg);
5157
5158 // Move the high double to the low double.
5159 __ psrldq(reg, Immediate(8));
5160 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5161 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5162 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005163 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005164 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005165 }
5166}
5167
5168void ParallelMoveResolverX86::SpillScratch(int reg) {
5169 __ pushl(static_cast<Register>(reg));
5170}
5171
5172void ParallelMoveResolverX86::RestoreScratch(int reg) {
5173 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005174}
5175
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005176void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005177 InvokeRuntimeCallingConvention calling_convention;
5178 CodeGenerator::CreateLoadClassLocationSummary(
5179 cls,
5180 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5181 Location::RegisterLocation(EAX));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005182}
5183
5184void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005185 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005186 if (cls->NeedsAccessCheck()) {
5187 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5188 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5189 cls,
5190 cls->GetDexPc(),
5191 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005192 return;
5193 }
5194
5195 Register out = locations->Out().AsRegister<Register>();
5196 Register current_method = locations->InAt(0).AsRegister<Register>();
5197 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005198 DCHECK(!cls->CanCallRuntime());
5199 DCHECK(!cls->MustGenerateClinitCheck());
Mathieu Chartiere401d142015-04-22 13:56:20 -07005200 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005201 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005202 DCHECK(cls->CanCallRuntime());
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005203 __ movl(out, Address(
Vladimir Marko05792b92015-08-03 11:56:49 +01005204 current_method, ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005205 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005206 // TODO: We will need a read barrier here.
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005207
Andreas Gampe85b62f22015-09-09 13:15:38 -07005208 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005209 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5210 codegen_->AddSlowPath(slow_path);
5211 __ testl(out, out);
5212 __ j(kEqual, slow_path->GetEntryLabel());
5213 if (cls->MustGenerateClinitCheck()) {
5214 GenerateClassInitializationCheck(slow_path, out);
5215 } else {
5216 __ Bind(slow_path->GetExitLabel());
5217 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005218 }
5219}
5220
5221void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5222 LocationSummary* locations =
5223 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5224 locations->SetInAt(0, Location::RequiresRegister());
5225 if (check->HasUses()) {
5226 locations->SetOut(Location::SameAsFirstInput());
5227 }
5228}
5229
5230void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005231 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005232 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005233 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005234 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005235 GenerateClassInitializationCheck(slow_path,
5236 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005237}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005238
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005239void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005240 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005241 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5242 Immediate(mirror::Class::kStatusInitialized));
5243 __ j(kLess, slow_path->GetEntryLabel());
5244 __ Bind(slow_path->GetExitLabel());
5245 // No need for memory fence, thanks to the X86 memory model.
5246}
5247
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005248void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5249 LocationSummary* locations =
5250 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005251 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005252 locations->SetOut(Location::RequiresRegister());
5253}
5254
5255void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005256 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005257 codegen_->AddSlowPath(slow_path);
5258
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005259 LocationSummary* locations = load->GetLocations();
5260 Register out = locations->Out().AsRegister<Register>();
5261 Register current_method = locations->InAt(0).AsRegister<Register>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07005262 __ movl(out, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Mathieu Chartiereace4582014-11-24 18:29:54 -08005263 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005264 __ movl(out, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
Vladimir Marko05792b92015-08-03 11:56:49 +01005265 // TODO: We will need a read barrier here.
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005266 __ testl(out, out);
5267 __ j(kEqual, slow_path->GetEntryLabel());
5268 __ Bind(slow_path->GetExitLabel());
5269}
5270
David Brazdilcb1c0552015-08-04 16:22:25 +01005271static Address GetExceptionTlsAddress() {
5272 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5273}
5274
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005275void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5276 LocationSummary* locations =
5277 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5278 locations->SetOut(Location::RequiresRegister());
5279}
5280
5281void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005282 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5283}
5284
5285void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5286 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5287}
5288
5289void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5290 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005291}
5292
5293void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5294 LocationSummary* locations =
5295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5296 InvokeRuntimeCallingConvention calling_convention;
5297 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5298}
5299
5300void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005301 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5302 instruction,
5303 instruction->GetDexPc(),
5304 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005305}
5306
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005307void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005308 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5309 switch (instruction->GetTypeCheckKind()) {
5310 case TypeCheckKind::kExactCheck:
5311 case TypeCheckKind::kAbstractClassCheck:
5312 case TypeCheckKind::kClassHierarchyCheck:
5313 case TypeCheckKind::kArrayObjectCheck:
5314 call_kind = LocationSummary::kNoCall;
5315 break;
Calin Juravle98893e12015-10-02 21:05:03 +01005316 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005317 case TypeCheckKind::kInterfaceCheck:
5318 call_kind = LocationSummary::kCall;
5319 break;
5320 case TypeCheckKind::kArrayCheck:
5321 call_kind = LocationSummary::kCallOnSlowPath;
5322 break;
5323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005324 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005325 if (call_kind != LocationSummary::kCall) {
5326 locations->SetInAt(0, Location::RequiresRegister());
5327 locations->SetInAt(1, Location::Any());
5328 // Note that TypeCheckSlowPathX86 uses this register too.
5329 locations->SetOut(Location::RequiresRegister());
5330 } else {
5331 InvokeRuntimeCallingConvention calling_convention;
5332 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5333 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5334 locations->SetOut(Location::RegisterLocation(EAX));
5335 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005336}
5337
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005338void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005339 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005340 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005341 Location cls = locations->InAt(1);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005342 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005343 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005344 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5345 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5346 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005347 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005348 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005349
5350 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005351 // Avoid null check if we know obj is not null.
5352 if (instruction->MustDoNullCheck()) {
5353 __ testl(obj, obj);
5354 __ j(kEqual, &zero);
5355 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005356
Calin Juravle98893e12015-10-02 21:05:03 +01005357 // In case of an interface/unresolved check, we put the object class into the object register.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005358 // This is safe, as the register is caller-save, and the object must be in another
5359 // register if it survives the runtime call.
Calin Juravle98893e12015-10-02 21:05:03 +01005360 Register target = (instruction->GetTypeCheckKind() == TypeCheckKind::kInterfaceCheck) ||
5361 (instruction->GetTypeCheckKind() == TypeCheckKind::kUnresolvedCheck)
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005362 ? obj
5363 : out;
5364 __ movl(target, Address(obj, class_offset));
5365 __ MaybeUnpoisonHeapReference(target);
5366
5367 switch (instruction->GetTypeCheckKind()) {
5368 case TypeCheckKind::kExactCheck: {
5369 if (cls.IsRegister()) {
5370 __ cmpl(out, cls.AsRegister<Register>());
5371 } else {
5372 DCHECK(cls.IsStackSlot()) << cls;
5373 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5374 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005375
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005376 // Classes must be equal for the instanceof to succeed.
5377 __ j(kNotEqual, &zero);
5378 __ movl(out, Immediate(1));
5379 __ jmp(&done);
5380 break;
5381 }
5382 case TypeCheckKind::kAbstractClassCheck: {
5383 // If the class is abstract, we eagerly fetch the super class of the
5384 // object to avoid doing a comparison we know will fail.
5385 NearLabel loop;
5386 __ Bind(&loop);
5387 __ movl(out, Address(out, super_offset));
5388 __ MaybeUnpoisonHeapReference(out);
5389 __ testl(out, out);
5390 // If `out` is null, we use it for the result, and jump to `done`.
5391 __ j(kEqual, &done);
5392 if (cls.IsRegister()) {
5393 __ cmpl(out, cls.AsRegister<Register>());
5394 } else {
5395 DCHECK(cls.IsStackSlot()) << cls;
5396 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5397 }
5398 __ j(kNotEqual, &loop);
5399 __ movl(out, Immediate(1));
5400 if (zero.IsLinked()) {
5401 __ jmp(&done);
5402 }
5403 break;
5404 }
5405 case TypeCheckKind::kClassHierarchyCheck: {
5406 // Walk over the class hierarchy to find a match.
5407 NearLabel loop, success;
5408 __ Bind(&loop);
5409 if (cls.IsRegister()) {
5410 __ cmpl(out, cls.AsRegister<Register>());
5411 } else {
5412 DCHECK(cls.IsStackSlot()) << cls;
5413 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5414 }
5415 __ j(kEqual, &success);
5416 __ movl(out, Address(out, super_offset));
5417 __ MaybeUnpoisonHeapReference(out);
5418 __ testl(out, out);
5419 __ j(kNotEqual, &loop);
5420 // If `out` is null, we use it for the result, and jump to `done`.
5421 __ jmp(&done);
5422 __ Bind(&success);
5423 __ movl(out, Immediate(1));
5424 if (zero.IsLinked()) {
5425 __ jmp(&done);
5426 }
5427 break;
5428 }
5429 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005430 // Do an exact check.
5431 NearLabel exact_check;
5432 if (cls.IsRegister()) {
5433 __ cmpl(out, cls.AsRegister<Register>());
5434 } else {
5435 DCHECK(cls.IsStackSlot()) << cls;
5436 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5437 }
5438 __ j(kEqual, &exact_check);
5439 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005440 __ movl(out, Address(out, component_offset));
5441 __ MaybeUnpoisonHeapReference(out);
5442 __ testl(out, out);
5443 // If `out` is null, we use it for the result, and jump to `done`.
5444 __ j(kEqual, &done);
5445 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5446 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005447 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005448 __ movl(out, Immediate(1));
5449 __ jmp(&done);
5450 break;
5451 }
5452 case TypeCheckKind::kArrayCheck: {
5453 if (cls.IsRegister()) {
5454 __ cmpl(out, cls.AsRegister<Register>());
5455 } else {
5456 DCHECK(cls.IsStackSlot()) << cls;
5457 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5458 }
5459 DCHECK(locations->OnlyCallsOnSlowPath());
5460 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5461 instruction, /* is_fatal */ false);
5462 codegen_->AddSlowPath(slow_path);
5463 __ j(kNotEqual, slow_path->GetEntryLabel());
5464 __ movl(out, Immediate(1));
5465 if (zero.IsLinked()) {
5466 __ jmp(&done);
5467 }
5468 break;
5469 }
Calin Juravle98893e12015-10-02 21:05:03 +01005470 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005471 case TypeCheckKind::kInterfaceCheck:
5472 default: {
5473 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
5474 instruction,
5475 instruction->GetDexPc(),
5476 nullptr);
5477 if (zero.IsLinked()) {
5478 __ jmp(&done);
5479 }
5480 break;
5481 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005482 }
5483
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005484 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005485 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005486 __ xorl(out, out);
5487 }
5488
5489 if (done.IsLinked()) {
5490 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005491 }
5492
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005493 if (slow_path != nullptr) {
5494 __ Bind(slow_path->GetExitLabel());
5495 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005496}
5497
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005498void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005499 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5500 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
5501
5502 switch (instruction->GetTypeCheckKind()) {
5503 case TypeCheckKind::kExactCheck:
5504 case TypeCheckKind::kAbstractClassCheck:
5505 case TypeCheckKind::kClassHierarchyCheck:
5506 case TypeCheckKind::kArrayObjectCheck:
5507 call_kind = throws_into_catch
5508 ? LocationSummary::kCallOnSlowPath
5509 : LocationSummary::kNoCall;
5510 break;
5511 case TypeCheckKind::kInterfaceCheck:
Calin Juravle98893e12015-10-02 21:05:03 +01005512 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005513 call_kind = LocationSummary::kCall;
5514 break;
5515 case TypeCheckKind::kArrayCheck:
5516 call_kind = LocationSummary::kCallOnSlowPath;
5517 break;
5518 }
5519
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005521 instruction, call_kind);
5522 if (call_kind != LocationSummary::kCall) {
5523 locations->SetInAt(0, Location::RequiresRegister());
5524 locations->SetInAt(1, Location::Any());
5525 // Note that TypeCheckSlowPathX86 uses this register too.
5526 locations->AddTemp(Location::RequiresRegister());
5527 } else {
5528 InvokeRuntimeCallingConvention calling_convention;
5529 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5530 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5531 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005532}
5533
5534void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5535 LocationSummary* locations = instruction->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005536 Register obj = locations->InAt(0).AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005537 Location cls = locations->InAt(1);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005538 Register temp = locations->WillCall()
5539 ? kNoRegister
5540 : locations->GetTemp(0).AsRegister<Register>();
Nicolas Geoffray64acf302015-09-14 22:20:29 +01005541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005542 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5543 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5544 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5545 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
5546 SlowPathCode* slow_path = nullptr;
5547
5548 if (!locations->WillCall()) {
5549 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(
5550 instruction, !locations->CanCall());
5551 codegen_->AddSlowPath(slow_path);
5552 }
5553
5554 NearLabel done, abstract_entry;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005555 // Avoid null check if we know obj is not null.
5556 if (instruction->MustDoNullCheck()) {
5557 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005558 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005559 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005560
5561 if (locations->WillCall()) {
5562 __ movl(obj, Address(obj, class_offset));
5563 __ MaybeUnpoisonHeapReference(obj);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005564 } else {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005565 __ movl(temp, Address(obj, class_offset));
5566 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005567 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005568
5569 switch (instruction->GetTypeCheckKind()) {
5570 case TypeCheckKind::kExactCheck:
5571 case TypeCheckKind::kArrayCheck: {
5572 if (cls.IsRegister()) {
5573 __ cmpl(temp, cls.AsRegister<Register>());
5574 } else {
5575 DCHECK(cls.IsStackSlot()) << cls;
5576 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5577 }
5578 // Jump to slow path for throwing the exception or doing a
5579 // more involved array check.
5580 __ j(kNotEqual, slow_path->GetEntryLabel());
5581 break;
5582 }
5583 case TypeCheckKind::kAbstractClassCheck: {
5584 // If the class is abstract, we eagerly fetch the super class of the
5585 // object to avoid doing a comparison we know will fail.
5586 NearLabel loop, success;
5587 __ Bind(&loop);
5588 __ movl(temp, Address(temp, super_offset));
5589 __ MaybeUnpoisonHeapReference(temp);
5590 __ testl(temp, temp);
5591 // Jump to the slow path to throw the exception.
5592 __ j(kEqual, slow_path->GetEntryLabel());
5593 if (cls.IsRegister()) {
5594 __ cmpl(temp, cls.AsRegister<Register>());
5595 } else {
5596 DCHECK(cls.IsStackSlot()) << cls;
5597 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5598 }
5599 __ j(kNotEqual, &loop);
5600 break;
5601 }
5602 case TypeCheckKind::kClassHierarchyCheck: {
5603 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005604 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005605 __ Bind(&loop);
5606 if (cls.IsRegister()) {
5607 __ cmpl(temp, cls.AsRegister<Register>());
5608 } else {
5609 DCHECK(cls.IsStackSlot()) << cls;
5610 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5611 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005612 __ j(kEqual, &done);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005613 __ movl(temp, Address(temp, super_offset));
5614 __ MaybeUnpoisonHeapReference(temp);
5615 __ testl(temp, temp);
5616 __ j(kNotEqual, &loop);
5617 // Jump to the slow path to throw the exception.
5618 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005619 break;
5620 }
5621 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005622 // Do an exact check.
5623 if (cls.IsRegister()) {
5624 __ cmpl(temp, cls.AsRegister<Register>());
5625 } else {
5626 DCHECK(cls.IsStackSlot()) << cls;
5627 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5628 }
5629 __ j(kEqual, &done);
5630 // Otherwise, we need to check that the object's class is a non primitive array.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005631 __ movl(temp, Address(temp, component_offset));
5632 __ MaybeUnpoisonHeapReference(temp);
5633 __ testl(temp, temp);
5634 __ j(kEqual, slow_path->GetEntryLabel());
5635 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
5636 __ j(kNotEqual, slow_path->GetEntryLabel());
5637 break;
5638 }
Calin Juravle98893e12015-10-02 21:05:03 +01005639 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005640 case TypeCheckKind::kInterfaceCheck:
5641 default:
5642 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
5643 instruction,
5644 instruction->GetDexPc(),
5645 nullptr);
5646 break;
5647 }
5648 __ Bind(&done);
5649
5650 if (slow_path != nullptr) {
5651 __ Bind(slow_path->GetExitLabel());
5652 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005653}
5654
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005655void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
5656 LocationSummary* locations =
5657 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5658 InvokeRuntimeCallingConvention calling_convention;
5659 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5660}
5661
5662void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005663 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
5664 : QUICK_ENTRY_POINT(pUnlockObject),
5665 instruction,
5666 instruction->GetDexPc(),
5667 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00005668}
5669
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005670void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
5671void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
5672void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
5673
5674void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5675 LocationSummary* locations =
5676 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5677 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
5678 || instruction->GetResultType() == Primitive::kPrimLong);
5679 locations->SetInAt(0, Location::RequiresRegister());
5680 locations->SetInAt(1, Location::Any());
5681 locations->SetOut(Location::SameAsFirstInput());
5682}
5683
5684void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
5685 HandleBitwiseOperation(instruction);
5686}
5687
5688void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
5689 HandleBitwiseOperation(instruction);
5690}
5691
5692void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
5693 HandleBitwiseOperation(instruction);
5694}
5695
5696void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
5697 LocationSummary* locations = instruction->GetLocations();
5698 Location first = locations->InAt(0);
5699 Location second = locations->InAt(1);
5700 DCHECK(first.Equals(locations->Out()));
5701
5702 if (instruction->GetResultType() == Primitive::kPrimInt) {
5703 if (second.IsRegister()) {
5704 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005705 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005706 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005707 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005708 } else {
5709 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005710 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005711 }
5712 } else if (second.IsConstant()) {
5713 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005714 __ andl(first.AsRegister<Register>(),
5715 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005716 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00005717 __ orl(first.AsRegister<Register>(),
5718 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005719 } else {
5720 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00005721 __ xorl(first.AsRegister<Register>(),
5722 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005723 }
5724 } else {
5725 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005726 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005727 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005728 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005729 } else {
5730 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005731 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005732 }
5733 }
5734 } else {
5735 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
5736 if (second.IsRegisterPair()) {
5737 if (instruction->IsAnd()) {
5738 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5739 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5740 } else if (instruction->IsOr()) {
5741 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5742 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5743 } else {
5744 DCHECK(instruction->IsXor());
5745 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
5746 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
5747 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005748 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005749 if (instruction->IsAnd()) {
5750 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5751 __ andl(first.AsRegisterPairHigh<Register>(),
5752 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5753 } else if (instruction->IsOr()) {
5754 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5755 __ orl(first.AsRegisterPairHigh<Register>(),
5756 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5757 } else {
5758 DCHECK(instruction->IsXor());
5759 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
5760 __ xorl(first.AsRegisterPairHigh<Register>(),
5761 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
5762 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005763 } else {
5764 DCHECK(second.IsConstant()) << second;
5765 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005766 int32_t low_value = Low32Bits(value);
5767 int32_t high_value = High32Bits(value);
5768 Immediate low(low_value);
5769 Immediate high(high_value);
5770 Register first_low = first.AsRegisterPairLow<Register>();
5771 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005772 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005773 if (low_value == 0) {
5774 __ xorl(first_low, first_low);
5775 } else if (low_value != -1) {
5776 __ andl(first_low, low);
5777 }
5778 if (high_value == 0) {
5779 __ xorl(first_high, first_high);
5780 } else if (high_value != -1) {
5781 __ andl(first_high, high);
5782 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005783 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005784 if (low_value != 0) {
5785 __ orl(first_low, low);
5786 }
5787 if (high_value != 0) {
5788 __ orl(first_high, high);
5789 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005790 } else {
5791 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005792 if (low_value != 0) {
5793 __ xorl(first_low, low);
5794 }
5795 if (high_value != 0) {
5796 __ xorl(first_high, high);
5797 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005798 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00005799 }
5800 }
5801}
5802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005803void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005804 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005805 LOG(FATAL) << "Unreachable";
5806}
5807
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005808void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00005809 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00005810 LOG(FATAL) << "Unreachable";
5811}
5812
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01005813void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
5814 DCHECK(codegen_->IsBaseline());
5815 LocationSummary* locations =
5816 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
5817 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
5818}
5819
5820void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
5821 DCHECK(codegen_->IsBaseline());
5822 // Will be generated at use site.
5823}
5824
Mark Mendellfe57faa2015-09-18 09:26:15 -04005825// Simple implementation of packed switch - generate cascaded compare/jumps.
5826void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5827 LocationSummary* locations =
5828 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5829 locations->SetInAt(0, Location::RequiresRegister());
5830}
5831
5832void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
5833 int32_t lower_bound = switch_instr->GetStartValue();
5834 int32_t num_entries = switch_instr->GetNumEntries();
5835 LocationSummary* locations = switch_instr->GetLocations();
5836 Register value_reg = locations->InAt(0).AsRegister<Register>();
5837 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5838
5839 // Create a series of compare/jumps.
5840 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
5841 for (int i = 0; i < num_entries; i++) {
5842 int32_t case_value = lower_bound + i;
5843 if (case_value == 0) {
5844 __ testl(value_reg, value_reg);
5845 } else {
5846 __ cmpl(value_reg, Immediate(case_value));
5847 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01005848 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04005849 }
5850
5851 // And the default for any other value.
5852 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
5853 __ jmp(codegen_->GetLabelOf(default_block));
5854 }
5855}
5856
Mark Mendell805b3b52015-09-18 14:10:29 -04005857void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5858 LocationSummary* locations =
5859 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
5860 locations->SetInAt(0, Location::RequiresRegister());
5861
5862 // Constant area pointer.
5863 locations->SetInAt(1, Location::RequiresRegister());
5864
5865 // And the temporary we need.
5866 locations->AddTemp(Location::RequiresRegister());
5867}
5868
5869void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
5870 int32_t lower_bound = switch_instr->GetStartValue();
5871 int32_t num_entries = switch_instr->GetNumEntries();
5872 LocationSummary* locations = switch_instr->GetLocations();
5873 Register value_reg = locations->InAt(0).AsRegister<Register>();
5874 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
5875
5876 // Optimizing has a jump area.
5877 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
5878 Register constant_area = locations->InAt(1).AsRegister<Register>();
5879
5880 // Remove the bias, if needed.
5881 if (lower_bound != 0) {
5882 __ leal(temp_reg, Address(value_reg, -lower_bound));
5883 value_reg = temp_reg;
5884 }
5885
5886 // Is the value in range?
5887 DCHECK_GE(num_entries, 1);
5888 __ cmpl(value_reg, Immediate(num_entries - 1));
5889 __ j(kAbove, codegen_->GetLabelOf(default_block));
5890
5891 // We are in the range of the table.
5892 // Load (target-constant_area) from the jump table, indexing by the value.
5893 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
5894
5895 // Compute the actual target address by adding in constant_area.
5896 __ addl(temp_reg, constant_area);
5897
5898 // And jump.
5899 __ jmp(temp_reg);
5900}
5901
Mark Mendell0616ae02015-04-17 12:49:27 -04005902void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
5903 HX86ComputeBaseMethodAddress* insn) {
5904 LocationSummary* locations =
5905 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5906 locations->SetOut(Location::RequiresRegister());
5907}
5908
5909void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
5910 HX86ComputeBaseMethodAddress* insn) {
5911 LocationSummary* locations = insn->GetLocations();
5912 Register reg = locations->Out().AsRegister<Register>();
5913
5914 // Generate call to next instruction.
5915 Label next_instruction;
5916 __ call(&next_instruction);
5917 __ Bind(&next_instruction);
5918
5919 // Remember this offset for later use with constant area.
5920 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
5921
5922 // Grab the return address off the stack.
5923 __ popl(reg);
5924}
5925
5926void LocationsBuilderX86::VisitX86LoadFromConstantTable(
5927 HX86LoadFromConstantTable* insn) {
5928 LocationSummary* locations =
5929 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
5930
5931 locations->SetInAt(0, Location::RequiresRegister());
5932 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
5933
5934 // If we don't need to be materialized, we only need the inputs to be set.
5935 if (!insn->NeedsMaterialization()) {
5936 return;
5937 }
5938
5939 switch (insn->GetType()) {
5940 case Primitive::kPrimFloat:
5941 case Primitive::kPrimDouble:
5942 locations->SetOut(Location::RequiresFpuRegister());
5943 break;
5944
5945 case Primitive::kPrimInt:
5946 locations->SetOut(Location::RequiresRegister());
5947 break;
5948
5949 default:
5950 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5951 }
5952}
5953
5954void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
5955 if (!insn->NeedsMaterialization()) {
5956 return;
5957 }
5958
5959 LocationSummary* locations = insn->GetLocations();
5960 Location out = locations->Out();
5961 Register const_area = locations->InAt(0).AsRegister<Register>();
5962 HConstant *value = insn->GetConstant();
5963
5964 switch (insn->GetType()) {
5965 case Primitive::kPrimFloat:
5966 __ movss(out.AsFpuRegister<XmmRegister>(),
5967 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
5968 break;
5969
5970 case Primitive::kPrimDouble:
5971 __ movsd(out.AsFpuRegister<XmmRegister>(),
5972 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
5973 break;
5974
5975 case Primitive::kPrimInt:
5976 __ movl(out.AsRegister<Register>(),
5977 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
5978 break;
5979
5980 default:
5981 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
5982 }
5983}
5984
Mark Mendell0616ae02015-04-17 12:49:27 -04005985/**
5986 * Class to handle late fixup of offsets into constant area.
5987 */
Vladimir Marko5233f932015-09-29 19:01:15 +01005988class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04005989 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04005990 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
5991 : codegen_(&codegen), offset_into_constant_area_(offset) {}
5992
5993 protected:
5994 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
5995
5996 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04005997
5998 private:
5999 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6000 // Patch the correct offset for the instruction. The place to patch is the
6001 // last 4 bytes of the instruction.
6002 // The value to patch is the distance from the offset in the constant area
6003 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006004 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6005 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006006
6007 // Patch in the right value.
6008 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6009 }
6010
Mark Mendell0616ae02015-04-17 12:49:27 -04006011 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006012 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006013};
6014
Mark Mendell805b3b52015-09-18 14:10:29 -04006015/**
6016 * Class to handle late fixup of offsets to a jump table that will be created in the
6017 * constant area.
6018 */
6019class JumpTableRIPFixup : public RIPFixup {
6020 public:
6021 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6022 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6023
6024 void CreateJumpTable() {
6025 X86Assembler* assembler = codegen_->GetAssembler();
6026
6027 // Ensure that the reference to the jump table has the correct offset.
6028 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6029 SetOffset(offset_in_constant_table);
6030
6031 // The label values in the jump table are computed relative to the
6032 // instruction addressing the constant area.
6033 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6034
6035 // Populate the jump table with the correct values for the jump table.
6036 int32_t num_entries = switch_instr_->GetNumEntries();
6037 HBasicBlock* block = switch_instr_->GetBlock();
6038 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6039 // The value that we want is the target offset - the position of the table.
6040 for (int32_t i = 0; i < num_entries; i++) {
6041 HBasicBlock* b = successors[i];
6042 Label* l = codegen_->GetLabelOf(b);
6043 DCHECK(l->IsBound());
6044 int32_t offset_to_block = l->Position() - relative_offset;
6045 assembler->AppendInt32(offset_to_block);
6046 }
6047 }
6048
6049 private:
6050 const HX86PackedSwitch* switch_instr_;
6051};
6052
6053void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
6054 // Generate the constant area if needed.
6055 X86Assembler* assembler = GetAssembler();
6056 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6057 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
6058 // byte values.
6059 assembler->Align(4, 0);
6060 constant_area_start_ = assembler->CodeSize();
6061
6062 // Populate any jump tables.
6063 for (auto jump_table : fixups_to_jump_tables_) {
6064 jump_table->CreateJumpTable();
6065 }
6066
6067 // And now add the constant area to the generated code.
6068 assembler->AddConstantArea();
6069 }
6070
6071 // And finish up.
6072 CodeGenerator::Finalize(allocator);
6073}
6074
Mark Mendell0616ae02015-04-17 12:49:27 -04006075Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
6076 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6077 return Address(reg, kDummy32BitOffset, fixup);
6078}
6079
6080Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
6081 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6082 return Address(reg, kDummy32BitOffset, fixup);
6083}
6084
6085Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
6086 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6087 return Address(reg, kDummy32BitOffset, fixup);
6088}
6089
6090Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
6091 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6092 return Address(reg, kDummy32BitOffset, fixup);
6093}
6094
Mark Mendell805b3b52015-09-18 14:10:29 -04006095Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
6096 Register reg,
6097 Register value) {
6098 // Create a fixup to be used to create and address the jump table.
6099 JumpTableRIPFixup* table_fixup =
6100 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6101
6102 // We have to populate the jump tables.
6103 fixups_to_jump_tables_.push_back(table_fixup);
6104
6105 // We want a scaled address, as we are extracting the correct offset from the table.
6106 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
6107}
6108
Andreas Gampe85b62f22015-09-09 13:15:38 -07006109// TODO: target as memory.
6110void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
6111 if (!target.IsValid()) {
6112 DCHECK(type == Primitive::kPrimVoid);
6113 return;
6114 }
6115
6116 DCHECK_NE(type, Primitive::kPrimVoid);
6117
6118 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
6119 if (target.Equals(return_loc)) {
6120 return;
6121 }
6122
6123 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6124 // with the else branch.
6125 if (type == Primitive::kPrimLong) {
6126 HParallelMove parallel_move(GetGraph()->GetArena());
6127 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
6128 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
6129 GetMoveResolver()->EmitNativeCode(&parallel_move);
6130 } else {
6131 // Let the parallel move resolver take care of all of this.
6132 HParallelMove parallel_move(GetGraph()->GetArena());
6133 parallel_move.AddMove(return_loc, target, type, nullptr);
6134 GetMoveResolver()->EmitNativeCode(&parallel_move);
6135 }
6136}
6137
Roland Levillain4d027112015-07-01 15:41:14 +01006138#undef __
6139
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006140} // namespace x86
6141} // namespace art