blob: 999306c34bd94a2613a84bc9224bf2e8a844e9b8 [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
Roland Levillain0d5a2812015-11-13 10:07:31 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace x86 {
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045
Mark Mendell5f874182015-03-04 15:42:45 -050046static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047
Mark Mendell24f2dfa2015-01-14 19:51:45 -050048static constexpr int kC2ConditionMask = 0x400;
49
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000051
Roland Levillain62a46b22015-06-01 18:24:13 +010052#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010053#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Andreas Gampe85b62f22015-09-09 13:15:38 -070055class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010057 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058
Alexandre Rames2ed20af2015-03-06 13:55:35 +000059 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010060 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000062 if (instruction_->CanThrowIntoCatchBlock()) {
63 // Live registers will be restored in the catch block if caught.
64 SaveLiveRegisters(codegen, instruction_->GetLocations());
65 }
Alexandre Rames8158f282015-08-07 10:26:17 +010066 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
67 instruction_,
68 instruction_->GetDexPc(),
69 this);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010077 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
79};
80
Andreas Gampe85b62f22015-09-09 13:15:38 -070081class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000082 public:
83 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
84
Alexandre Rames2ed20af2015-03-06 13:55:35 +000085 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000087 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000088 if (instruction_->CanThrowIntoCatchBlock()) {
89 // Live registers will be restored in the catch block if caught.
90 SaveLiveRegisters(codegen, instruction_->GetLocations());
91 }
Alexandre Rames8158f282015-08-07 10:26:17 +010092 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
93 instruction_,
94 instruction_->GetDexPc(),
95 this);
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
103 HDivZeroCheck* const instruction_;
104 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
105};
106
Andreas Gampe85b62f22015-09-09 13:15:38 -0700107class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000108 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100109 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000144 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100147 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100148 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100149 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
150 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100151 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
152 instruction_,
153 instruction_->GetDexPc(),
154 this);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100155 }
156
Alexandre Rames8158f282015-08-07 10:26:17 +0100157 bool IsFatal() const OVERRIDE { return true; }
158
Alexandre Rames9931f312015-06-19 14:47:01 +0100159 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
160
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100161 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100162 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163
164 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
165};
166
Andreas Gampe85b62f22015-09-09 13:15:38 -0700167class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000168 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000169 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100170 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000172 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100173 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000175 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100176 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
177 instruction_,
178 instruction_->GetDexPc(),
179 this);
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000180 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100181 if (successor_ == nullptr) {
182 __ jmp(GetReturnLabel());
183 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100184 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 }
187
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100188 Label* GetReturnLabel() {
189 DCHECK(successor_ == nullptr);
190 return &return_label_;
191 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100193 HBasicBlock* GetSuccessor() const {
194 return successor_;
195 }
196
Alexandre Rames9931f312015-06-19 14:47:01 +0100197 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
198
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 private:
200 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100201 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 Label return_label_;
203
204 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
205};
206
Andreas Gampe85b62f22015-09-09 13:15:38 -0700207class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000208 public:
209 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
210
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000211 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000212 LocationSummary* locations = instruction_->GetLocations();
213 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
214
215 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
216 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000217 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000218
219 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800220 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100221 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
222 instruction_,
223 instruction_->GetDexPc(),
224 this);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000225 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000226 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000227
228 __ jmp(GetExitLabel());
229 }
230
Alexandre Rames9931f312015-06-19 14:47:01 +0100231 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
232
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000233 private:
234 HLoadString* const instruction_;
235
236 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
237};
238
Andreas Gampe85b62f22015-09-09 13:15:38 -0700239class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000240 public:
241 LoadClassSlowPathX86(HLoadClass* cls,
242 HInstruction* at,
243 uint32_t dex_pc,
244 bool do_clinit)
245 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
246 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
247 }
248
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000249 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000250 LocationSummary* locations = at_->GetLocations();
251 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
252 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000253 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254
255 InvokeRuntimeCallingConvention calling_convention;
256 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100257 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
258 : QUICK_ENTRY_POINT(pInitializeType),
259 at_, dex_pc_, this);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000260
261 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000262 Location out = locations->Out();
263 if (out.IsValid()) {
264 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
265 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000267
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000268 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269 __ jmp(GetExitLabel());
270 }
271
Alexandre Rames9931f312015-06-19 14:47:01 +0100272 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
273
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 private:
275 // The class this slow path will load.
276 HLoadClass* const cls_;
277
278 // The instruction where this slow path is happening.
279 // (Might be the load class or an initialization check).
280 HInstruction* const at_;
281
282 // The dex PC of `at_`.
283 const uint32_t dex_pc_;
284
285 // Whether to initialize the class.
286 const bool do_clinit_;
287
288 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
289};
290
Andreas Gampe85b62f22015-09-09 13:15:38 -0700291class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000292 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000293 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
294 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000295
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000296 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000297 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100298 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
299 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000300 DCHECK(instruction_->IsCheckCast()
301 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302
303 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
304 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000305
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000306 if (!is_fatal_) {
307 SaveLiveRegisters(codegen, locations);
308 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309
310 // We're moving two locations to locations that could overlap, so we need a parallel
311 // move resolver.
312 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000313 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100314 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000315 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100316 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100317 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100318 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
319 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000320
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000321 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100322 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
323 instruction_,
324 instruction_->GetDexPc(),
325 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000326 CheckEntrypointTypes<
327 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000328 } else {
329 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100330 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
331 instruction_,
332 instruction_->GetDexPc(),
333 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000334 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000335 }
336
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000337 if (!is_fatal_) {
338 if (instruction_->IsInstanceOf()) {
339 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
340 }
341 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000342
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000343 __ jmp(GetExitLabel());
344 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000345 }
346
Alexandre Rames9931f312015-06-19 14:47:01 +0100347 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000348 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100349
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000350 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000351 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000352 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353
354 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
355};
356
Andreas Gampe85b62f22015-09-09 13:15:38 -0700357class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700358 public:
359 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
360 : instruction_(instruction) {}
361
362 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100363 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100364 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 __ Bind(GetEntryLabel());
366 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100367 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
368 instruction_,
369 instruction_->GetDexPc(),
370 this);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 }
372
Alexandre Rames9931f312015-06-19 14:47:01 +0100373 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
374
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 private:
376 HInstruction* const instruction_;
377 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
378};
379
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100380class ArraySetSlowPathX86 : public SlowPathCode {
381 public:
382 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
383
384 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
385 LocationSummary* locations = instruction_->GetLocations();
386 __ Bind(GetEntryLabel());
387 SaveLiveRegisters(codegen, locations);
388
389 InvokeRuntimeCallingConvention calling_convention;
390 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
391 parallel_move.AddMove(
392 locations->InAt(0),
393 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
394 Primitive::kPrimNot,
395 nullptr);
396 parallel_move.AddMove(
397 locations->InAt(1),
398 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
399 Primitive::kPrimInt,
400 nullptr);
401 parallel_move.AddMove(
402 locations->InAt(2),
403 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
404 Primitive::kPrimNot,
405 nullptr);
406 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
407
408 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
409 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
410 instruction_,
411 instruction_->GetDexPc(),
412 this);
413 RestoreLiveRegisters(codegen, locations);
414 __ jmp(GetExitLabel());
415 }
416
417 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
418
419 private:
420 HInstruction* const instruction_;
421
422 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
423};
424
Roland Levillain0d5a2812015-11-13 10:07:31 +0000425// Slow path generating a read barrier for a heap reference.
426class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
427 public:
428 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
429 Location out,
430 Location ref,
431 Location obj,
432 uint32_t offset,
433 Location index)
434 : instruction_(instruction),
435 out_(out),
436 ref_(ref),
437 obj_(obj),
438 offset_(offset),
439 index_(index) {
440 DCHECK(kEmitCompilerReadBarrier);
441 // If `obj` is equal to `out` or `ref`, it means the initial object
442 // has been overwritten by (or after) the heap object reference load
443 // to be instrumented, e.g.:
444 //
445 // __ movl(out, Address(out, offset));
446 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
447 //
448 // In that case, we have lost the information about the original
449 // object, and the emitted read barrier cannot work properly.
450 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
451 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
452 }
453
454 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
455 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
456 LocationSummary* locations = instruction_->GetLocations();
457 Register reg_out = out_.AsRegister<Register>();
458 DCHECK(locations->CanCall());
459 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
460 DCHECK(!instruction_->IsInvoke() ||
461 (instruction_->IsInvokeStaticOrDirect() &&
462 instruction_->GetLocations()->Intrinsified()));
463
464 __ Bind(GetEntryLabel());
465 SaveLiveRegisters(codegen, locations);
466
467 // We may have to change the index's value, but as `index_` is a
468 // constant member (like other "inputs" of this slow path),
469 // introduce a copy of it, `index`.
470 Location index = index_;
471 if (index_.IsValid()) {
472 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
473 if (instruction_->IsArrayGet()) {
474 // Compute the actual memory offset and store it in `index`.
475 Register index_reg = index_.AsRegister<Register>();
476 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
477 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
478 // We are about to change the value of `index_reg` (see the
479 // calls to art::x86::X86Assembler::shll and
480 // art::x86::X86Assembler::AddImmediate below), but it has
481 // not been saved by the previous call to
482 // art::SlowPathCode::SaveLiveRegisters, as it is a
483 // callee-save register --
484 // art::SlowPathCode::SaveLiveRegisters does not consider
485 // callee-save registers, as it has been designed with the
486 // assumption that callee-save registers are supposed to be
487 // handled by the called function. So, as a callee-save
488 // register, `index_reg` _would_ eventually be saved onto
489 // the stack, but it would be too late: we would have
490 // changed its value earlier. Therefore, we manually save
491 // it here into another freely available register,
492 // `free_reg`, chosen of course among the caller-save
493 // registers (as a callee-save `free_reg` register would
494 // exhibit the same problem).
495 //
496 // Note we could have requested a temporary register from
497 // the register allocator instead; but we prefer not to, as
498 // this is a slow path, and we know we can find a
499 // caller-save register that is available.
500 Register free_reg = FindAvailableCallerSaveRegister(codegen);
501 __ movl(free_reg, index_reg);
502 index_reg = free_reg;
503 index = Location::RegisterLocation(index_reg);
504 } else {
505 // The initial register stored in `index_` has already been
506 // saved in the call to art::SlowPathCode::SaveLiveRegisters
507 // (as it is not a callee-save register), so we can freely
508 // use it.
509 }
510 // Shifting the index value contained in `index_reg` by the scale
511 // factor (2) cannot overflow in practice, as the runtime is
512 // unable to allocate object arrays with a size larger than
513 // 2^26 - 1 (that is, 2^28 - 4 bytes).
514 __ shll(index_reg, Immediate(TIMES_4));
515 static_assert(
516 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
517 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
518 __ AddImmediate(index_reg, Immediate(offset_));
519 } else {
520 DCHECK(instruction_->IsInvoke());
521 DCHECK(instruction_->GetLocations()->Intrinsified());
522 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
523 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
524 << instruction_->AsInvoke()->GetIntrinsic();
525 DCHECK_EQ(offset_, 0U);
526 DCHECK(index_.IsRegisterPair());
527 // UnsafeGet's offset location is a register pair, the low
528 // part contains the correct offset.
529 index = index_.ToLow();
530 }
531 }
532
533 // We're moving two or three locations to locations that could
534 // overlap, so we need a parallel move resolver.
535 InvokeRuntimeCallingConvention calling_convention;
536 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
537 parallel_move.AddMove(ref_,
538 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
539 Primitive::kPrimNot,
540 nullptr);
541 parallel_move.AddMove(obj_,
542 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
543 Primitive::kPrimNot,
544 nullptr);
545 if (index.IsValid()) {
546 parallel_move.AddMove(index,
547 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
548 Primitive::kPrimInt,
549 nullptr);
550 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
551 } else {
552 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
553 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
554 }
555 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
556 instruction_,
557 instruction_->GetDexPc(),
558 this);
559 CheckEntrypointTypes<
560 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
561 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
562
563 RestoreLiveRegisters(codegen, locations);
564 __ jmp(GetExitLabel());
565 }
566
567 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
568
569 private:
570 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
571 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
572 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
573 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
574 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
575 return static_cast<Register>(i);
576 }
577 }
578 // We shall never fail to find a free caller-save register, as
579 // there are more than two core caller-save registers on x86
580 // (meaning it is possible to find one which is different from
581 // `ref` and `obj`).
582 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
583 LOG(FATAL) << "Could not find a free caller-save register";
584 UNREACHABLE();
585 }
586
587 HInstruction* const instruction_;
588 const Location out_;
589 const Location ref_;
590 const Location obj_;
591 const uint32_t offset_;
592 // An additional location containing an index to an array.
593 // Only used for HArrayGet and the UnsafeGetObject &
594 // UnsafeGetObjectVolatile intrinsics.
595 const Location index_;
596
597 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
598};
599
600// Slow path generating a read barrier for a GC root.
601class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
602 public:
603 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
604 : instruction_(instruction), out_(out), root_(root) {}
605
606 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
607 LocationSummary* locations = instruction_->GetLocations();
608 Register reg_out = out_.AsRegister<Register>();
609 DCHECK(locations->CanCall());
610 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
611 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
612
613 __ Bind(GetEntryLabel());
614 SaveLiveRegisters(codegen, locations);
615
616 InvokeRuntimeCallingConvention calling_convention;
617 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
618 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
619 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
620 instruction_,
621 instruction_->GetDexPc(),
622 this);
623 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
624 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
625
626 RestoreLiveRegisters(codegen, locations);
627 __ jmp(GetExitLabel());
628 }
629
630 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
631
632 private:
633 HInstruction* const instruction_;
634 const Location out_;
635 const Location root_;
636
637 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
638};
639
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100640#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100641#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100642
Aart Bike9f37602015-10-09 11:15:55 -0700643inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700644 switch (cond) {
645 case kCondEQ: return kEqual;
646 case kCondNE: return kNotEqual;
647 case kCondLT: return kLess;
648 case kCondLE: return kLessEqual;
649 case kCondGT: return kGreater;
650 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700651 case kCondB: return kBelow;
652 case kCondBE: return kBelowEqual;
653 case kCondA: return kAbove;
654 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700655 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100656 LOG(FATAL) << "Unreachable";
657 UNREACHABLE();
658}
659
Aart Bike9f37602015-10-09 11:15:55 -0700660// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100661inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
662 switch (cond) {
663 case kCondEQ: return kEqual;
664 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700665 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100666 case kCondLT: return kBelow;
667 case kCondLE: return kBelowEqual;
668 case kCondGT: return kAbove;
669 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700670 // Unsigned remain unchanged.
671 case kCondB: return kBelow;
672 case kCondBE: return kBelowEqual;
673 case kCondA: return kAbove;
674 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100675 }
676 LOG(FATAL) << "Unreachable";
677 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700678}
679
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100680void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100681 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100682}
683
684void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100685 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100686}
687
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100688size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
689 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
690 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100691}
692
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100693size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
694 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
695 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100696}
697
Mark Mendell7c8d0092015-01-26 11:21:33 -0500698size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
699 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
700 return GetFloatingPointSpillSlotSize();
701}
702
703size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
704 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
705 return GetFloatingPointSpillSlotSize();
706}
707
Calin Juravle175dc732015-08-25 15:42:32 +0100708void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
709 HInstruction* instruction,
710 uint32_t dex_pc,
711 SlowPathCode* slow_path) {
712 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
713 instruction,
714 dex_pc,
715 slow_path);
716}
717
718void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100719 HInstruction* instruction,
720 uint32_t dex_pc,
721 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100722 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100723 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100724 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100725}
726
Mark Mendellfb8d2792015-03-31 22:16:59 -0400727CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000728 const X86InstructionSetFeatures& isa_features,
729 const CompilerOptions& compiler_options,
730 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500731 : CodeGenerator(graph,
732 kNumberOfCpuRegisters,
733 kNumberOfXmmRegisters,
734 kNumberOfRegisterPairs,
735 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
736 arraysize(kCoreCalleeSaves))
737 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100738 0,
739 compiler_options,
740 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100741 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100742 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100743 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400744 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000745 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100746 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400747 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000748 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400749 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000750 // Use a fake return address register to mimic Quick.
751 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100752}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100753
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100754Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100755 switch (type) {
756 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100757 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100758 X86ManagedRegister pair =
759 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100760 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
761 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100762 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
763 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100764 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100765 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100766 }
767
768 case Primitive::kPrimByte:
769 case Primitive::kPrimBoolean:
770 case Primitive::kPrimChar:
771 case Primitive::kPrimShort:
772 case Primitive::kPrimInt:
773 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100774 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100775 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100776 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100777 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
778 X86ManagedRegister current =
779 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
780 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100781 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100782 }
783 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100784 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100785 }
786
787 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100788 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100789 return Location::FpuRegisterLocation(
790 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100791 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100792
793 case Primitive::kPrimVoid:
794 LOG(FATAL) << "Unreachable type " << type;
795 }
796
Roland Levillain0d5a2812015-11-13 10:07:31 +0000797 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100798}
799
Mark Mendell5f874182015-03-04 15:42:45 -0500800void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100801 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100802 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100803
804 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100805 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100806
Mark Mendell5f874182015-03-04 15:42:45 -0500807 if (is_baseline) {
808 blocked_core_registers_[EBP] = true;
809 blocked_core_registers_[ESI] = true;
810 blocked_core_registers_[EDI] = true;
811 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100812
813 UpdateBlockedPairRegisters();
814}
815
816void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
817 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
818 X86ManagedRegister current =
819 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
820 if (blocked_core_registers_[current.AsRegisterPairLow()]
821 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
822 blocked_register_pairs_[i] = true;
823 }
824 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825}
826
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100827InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
828 : HGraphVisitor(graph),
829 assembler_(codegen->GetAssembler()),
830 codegen_(codegen) {}
831
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100832static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100833 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100834}
835
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000836void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100837 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000838 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000839 bool skip_overflow_check =
840 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000841 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000842
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000843 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100844 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100845 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100846 }
847
Mark Mendell5f874182015-03-04 15:42:45 -0500848 if (HasEmptyFrame()) {
849 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000850 }
Mark Mendell5f874182015-03-04 15:42:45 -0500851
852 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
853 Register reg = kCoreCalleeSaves[i];
854 if (allocated_registers_.ContainsCoreRegister(reg)) {
855 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100856 __ cfi().AdjustCFAOffset(kX86WordSize);
857 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500858 }
859 }
860
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100861 int adjust = GetFrameSize() - FrameEntrySpillSize();
862 __ subl(ESP, Immediate(adjust));
863 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100864 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000865}
866
867void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100868 __ cfi().RememberState();
869 if (!HasEmptyFrame()) {
870 int adjust = GetFrameSize() - FrameEntrySpillSize();
871 __ addl(ESP, Immediate(adjust));
872 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500873
David Srbeckyc34dc932015-04-12 09:27:43 +0100874 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
875 Register reg = kCoreCalleeSaves[i];
876 if (allocated_registers_.ContainsCoreRegister(reg)) {
877 __ popl(reg);
878 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
879 __ cfi().Restore(DWARFReg(reg));
880 }
Mark Mendell5f874182015-03-04 15:42:45 -0500881 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000882 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100883 __ ret();
884 __ cfi().RestoreState();
885 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000886}
887
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100888void CodeGeneratorX86::Bind(HBasicBlock* block) {
889 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000890}
891
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100892Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
893 switch (load->GetType()) {
894 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100895 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100896 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100897
898 case Primitive::kPrimInt:
899 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100900 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100901 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100902
903 case Primitive::kPrimBoolean:
904 case Primitive::kPrimByte:
905 case Primitive::kPrimChar:
906 case Primitive::kPrimShort:
907 case Primitive::kPrimVoid:
908 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700909 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100910 }
911
912 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700913 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914}
915
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100916Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
917 switch (type) {
918 case Primitive::kPrimBoolean:
919 case Primitive::kPrimByte:
920 case Primitive::kPrimChar:
921 case Primitive::kPrimShort:
922 case Primitive::kPrimInt:
923 case Primitive::kPrimNot:
924 return Location::RegisterLocation(EAX);
925
926 case Primitive::kPrimLong:
927 return Location::RegisterPairLocation(EAX, EDX);
928
929 case Primitive::kPrimVoid:
930 return Location::NoLocation();
931
932 case Primitive::kPrimDouble:
933 case Primitive::kPrimFloat:
934 return Location::FpuRegisterLocation(XMM0);
935 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100936
937 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100938}
939
940Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
941 return Location::RegisterLocation(kMethodRegisterArgument);
942}
943
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100944Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100945 switch (type) {
946 case Primitive::kPrimBoolean:
947 case Primitive::kPrimByte:
948 case Primitive::kPrimChar:
949 case Primitive::kPrimShort:
950 case Primitive::kPrimInt:
951 case Primitive::kPrimNot: {
952 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000953 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100954 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100955 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100956 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000957 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100958 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100959 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100960
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000961 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100962 uint32_t index = gp_index_;
963 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100965 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100966 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
967 calling_convention.GetRegisterPairAt(index));
968 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100969 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000970 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
971 }
972 }
973
974 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100975 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000976 stack_index_++;
977 if (index < calling_convention.GetNumberOfFpuRegisters()) {
978 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
979 } else {
980 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
981 }
982 }
983
984 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100985 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000986 stack_index_ += 2;
987 if (index < calling_convention.GetNumberOfFpuRegisters()) {
988 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
989 } else {
990 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100991 }
992 }
993
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100994 case Primitive::kPrimVoid:
995 LOG(FATAL) << "Unexpected parameter type " << type;
996 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100997 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000998 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100999}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001000
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001void CodeGeneratorX86::Move32(Location destination, Location source) {
1002 if (source.Equals(destination)) {
1003 return;
1004 }
1005 if (destination.IsRegister()) {
1006 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001007 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001008 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001009 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 } else {
1011 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001012 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001014 } else if (destination.IsFpuRegister()) {
1015 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001016 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001017 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001018 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019 } else {
1020 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001021 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001023 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001024 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001025 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001027 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001029 } else if (source.IsConstant()) {
1030 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001031 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001032 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001033 } else {
1034 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001035 __ pushl(Address(ESP, source.GetStackIndex()));
1036 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 }
1038 }
1039}
1040
1041void CodeGeneratorX86::Move64(Location destination, Location source) {
1042 if (source.Equals(destination)) {
1043 return;
1044 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001045 if (destination.IsRegisterPair()) {
1046 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001047 EmitParallelMoves(
1048 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1049 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001050 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001051 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001052 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1053 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001054 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001055 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1056 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1057 __ psrlq(src_reg, Immediate(32));
1058 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001059 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001060 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001061 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001062 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1063 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001064 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1065 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001066 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001067 if (source.IsFpuRegister()) {
1068 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1069 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001070 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001071 } else if (source.IsRegisterPair()) {
1072 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1073 // Create stack space for 2 elements.
1074 __ subl(ESP, Immediate(2 * elem_size));
1075 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1076 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1077 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1078 // And remove the temporary stack space we allocated.
1079 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 } else {
1081 LOG(FATAL) << "Unimplemented";
1082 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001083 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001084 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001085 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001086 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001087 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001089 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001091 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001092 } else if (source.IsConstant()) {
1093 HConstant* constant = source.GetConstant();
1094 int64_t value;
1095 if (constant->IsLongConstant()) {
1096 value = constant->AsLongConstant()->GetValue();
1097 } else {
1098 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001099 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001100 }
1101 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1102 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001103 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001104 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001105 EmitParallelMoves(
1106 Location::StackSlot(source.GetStackIndex()),
1107 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001108 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001109 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001110 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1111 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001112 }
1113 }
1114}
1115
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001116void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001117 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001118 if (instruction->IsCurrentMethod()) {
1119 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1120 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001121 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001122 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001123 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001124 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1125 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001126 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001127 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001128 } else if (location.IsStackSlot()) {
1129 __ movl(Address(ESP, location.GetStackIndex()), imm);
1130 } else {
1131 DCHECK(location.IsConstant());
1132 DCHECK_EQ(location.GetConstant(), const_to_move);
1133 }
1134 } else if (const_to_move->IsLongConstant()) {
1135 int64_t value = const_to_move->AsLongConstant()->GetValue();
1136 if (location.IsRegisterPair()) {
1137 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1138 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1139 } else if (location.IsDoubleStackSlot()) {
1140 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001141 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1142 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001143 } else {
1144 DCHECK(location.IsConstant());
1145 DCHECK_EQ(location.GetConstant(), instruction);
1146 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001147 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001148 } else if (instruction->IsTemporary()) {
1149 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001150 if (temp_location.IsStackSlot()) {
1151 Move32(location, temp_location);
1152 } else {
1153 DCHECK(temp_location.IsDoubleStackSlot());
1154 Move64(location, temp_location);
1155 }
Roland Levillain476df552014-10-09 17:51:36 +01001156 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 switch (instruction->GetType()) {
1159 case Primitive::kPrimBoolean:
1160 case Primitive::kPrimByte:
1161 case Primitive::kPrimChar:
1162 case Primitive::kPrimShort:
1163 case Primitive::kPrimInt:
1164 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001165 case Primitive::kPrimFloat:
1166 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001167 break;
1168
1169 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001170 case Primitive::kPrimDouble:
1171 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 break;
1173
1174 default:
1175 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1176 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001177 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001178 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 switch (instruction->GetType()) {
1180 case Primitive::kPrimBoolean:
1181 case Primitive::kPrimByte:
1182 case Primitive::kPrimChar:
1183 case Primitive::kPrimShort:
1184 case Primitive::kPrimInt:
1185 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001186 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001187 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188 break;
1189
1190 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001191 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001192 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 break;
1194
1195 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001196 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198 }
1199}
1200
Calin Juravle175dc732015-08-25 15:42:32 +01001201void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1202 DCHECK(location.IsRegister());
1203 __ movl(location.AsRegister<Register>(), Immediate(value));
1204}
1205
Calin Juravlee460d1d2015-09-29 04:52:17 +01001206void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1207 if (Primitive::Is64BitType(dst_type)) {
1208 Move64(dst, src);
1209 } else {
1210 Move32(dst, src);
1211 }
1212}
1213
1214void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1215 if (location.IsRegister()) {
1216 locations->AddTemp(location);
1217 } else if (location.IsRegisterPair()) {
1218 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1219 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1220 } else {
1221 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1222 }
1223}
1224
David Brazdilfc6a86a2015-06-26 10:33:45 +00001225void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001226 DCHECK(!successor->IsExitBlock());
1227
1228 HBasicBlock* block = got->GetBlock();
1229 HInstruction* previous = got->GetPrevious();
1230
1231 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001232 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001233 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1234 return;
1235 }
1236
1237 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1238 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1239 }
1240 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001241 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001242 }
1243}
1244
David Brazdilfc6a86a2015-06-26 10:33:45 +00001245void LocationsBuilderX86::VisitGoto(HGoto* got) {
1246 got->SetLocations(nullptr);
1247}
1248
1249void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1250 HandleGoto(got, got->GetSuccessor());
1251}
1252
1253void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1254 try_boundary->SetLocations(nullptr);
1255}
1256
1257void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1258 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1259 if (!successor->IsExitBlock()) {
1260 HandleGoto(try_boundary, successor);
1261 }
1262}
1263
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001264void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001265 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001266}
1267
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001268void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001269}
1270
Mark Mendellc4701932015-04-10 13:18:51 -04001271void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1272 Label* true_label,
1273 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001274 if (cond->IsFPConditionTrueIfNaN()) {
1275 __ j(kUnordered, true_label);
1276 } else if (cond->IsFPConditionFalseIfNaN()) {
1277 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001278 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001279 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001280}
1281
1282void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1283 Label* true_label,
1284 Label* false_label) {
1285 LocationSummary* locations = cond->GetLocations();
1286 Location left = locations->InAt(0);
1287 Location right = locations->InAt(1);
1288 IfCondition if_cond = cond->GetCondition();
1289
Mark Mendellc4701932015-04-10 13:18:51 -04001290 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001291 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001292 IfCondition true_high_cond = if_cond;
1293 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001294 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001295
1296 // Set the conditions for the test, remembering that == needs to be
1297 // decided using the low words.
1298 switch (if_cond) {
1299 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001300 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001301 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001302 break;
1303 case kCondLT:
1304 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001305 break;
1306 case kCondLE:
1307 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001308 break;
1309 case kCondGT:
1310 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001311 break;
1312 case kCondGE:
1313 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001314 break;
Aart Bike9f37602015-10-09 11:15:55 -07001315 case kCondB:
1316 false_high_cond = kCondA;
1317 break;
1318 case kCondBE:
1319 true_high_cond = kCondB;
1320 break;
1321 case kCondA:
1322 false_high_cond = kCondB;
1323 break;
1324 case kCondAE:
1325 true_high_cond = kCondA;
1326 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001327 }
1328
1329 if (right.IsConstant()) {
1330 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001331 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001332 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001333
1334 if (val_high == 0) {
1335 __ testl(left_high, left_high);
1336 } else {
1337 __ cmpl(left_high, Immediate(val_high));
1338 }
1339 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001340 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001341 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001342 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001343 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001344 __ j(X86Condition(true_high_cond), true_label);
1345 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001346 }
1347 // Must be equal high, so compare the lows.
1348 if (val_low == 0) {
1349 __ testl(left_low, left_low);
1350 } else {
1351 __ cmpl(left_low, Immediate(val_low));
1352 }
1353 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001354 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001355 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001356
1357 __ cmpl(left_high, right_high);
1358 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001359 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001360 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001361 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001362 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001363 __ j(X86Condition(true_high_cond), true_label);
1364 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001365 }
1366 // Must be equal high, so compare the lows.
1367 __ cmpl(left_low, right_low);
1368 }
1369 // The last comparison might be unsigned.
1370 __ j(final_condition, true_label);
1371}
1372
David Brazdil0debae72015-11-12 18:37:00 +00001373void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
1374 Label* true_target_in,
1375 Label* false_target_in) {
1376 // Generated branching requires both targets to be explicit. If either of the
1377 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1378 Label fallthrough_target;
1379 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1380 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1381
Mark Mendellc4701932015-04-10 13:18:51 -04001382 LocationSummary* locations = condition->GetLocations();
1383 Location left = locations->InAt(0);
1384 Location right = locations->InAt(1);
1385
Mark Mendellc4701932015-04-10 13:18:51 -04001386 Primitive::Type type = condition->InputAt(0)->GetType();
1387 switch (type) {
1388 case Primitive::kPrimLong:
1389 GenerateLongComparesAndJumps(condition, true_target, false_target);
1390 break;
1391 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001392 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1393 GenerateFPJumps(condition, true_target, false_target);
1394 break;
1395 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001396 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1397 GenerateFPJumps(condition, true_target, false_target);
1398 break;
1399 default:
1400 LOG(FATAL) << "Unexpected compare type " << type;
1401 }
1402
David Brazdil0debae72015-11-12 18:37:00 +00001403 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001404 __ jmp(false_target);
1405 }
David Brazdil0debae72015-11-12 18:37:00 +00001406
1407 if (fallthrough_target.IsLinked()) {
1408 __ Bind(&fallthrough_target);
1409 }
Mark Mendellc4701932015-04-10 13:18:51 -04001410}
1411
David Brazdil0debae72015-11-12 18:37:00 +00001412static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1413 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1414 // are set only strictly before `branch`. We can't use the eflags on long/FP
1415 // conditions if they are materialized due to the complex branching.
1416 return cond->IsCondition() &&
1417 cond->GetNext() == branch &&
1418 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1419 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1420}
1421
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001422void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001423 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001424 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001425 Label* false_target) {
1426 HInstruction* cond = instruction->InputAt(condition_input_index);
1427
1428 if (true_target == nullptr && false_target == nullptr) {
1429 // Nothing to do. The code always falls through.
1430 return;
1431 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001432 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001433 if (cond->AsIntConstant()->IsOne()) {
1434 if (true_target != nullptr) {
1435 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001436 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001437 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001438 DCHECK(cond->AsIntConstant()->IsZero());
1439 if (false_target != nullptr) {
1440 __ jmp(false_target);
1441 }
1442 }
1443 return;
1444 }
1445
1446 // The following code generates these patterns:
1447 // (1) true_target == nullptr && false_target != nullptr
1448 // - opposite condition true => branch to false_target
1449 // (2) true_target != nullptr && false_target == nullptr
1450 // - condition true => branch to true_target
1451 // (3) true_target != nullptr && false_target != nullptr
1452 // - condition true => branch to true_target
1453 // - branch to false_target
1454 if (IsBooleanValueOrMaterializedCondition(cond)) {
1455 if (AreEflagsSetFrom(cond, instruction)) {
1456 if (true_target == nullptr) {
1457 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1458 } else {
1459 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1460 }
1461 } else {
1462 // Materialized condition, compare against 0.
1463 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1464 if (lhs.IsRegister()) {
1465 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1466 } else {
1467 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1468 }
1469 if (true_target == nullptr) {
1470 __ j(kEqual, false_target);
1471 } else {
1472 __ j(kNotEqual, true_target);
1473 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001474 }
1475 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001476 // Condition has not been materialized, use its inputs as the comparison and
1477 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001478 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001479
1480 // If this is a long or FP comparison that has been folded into
1481 // the HCondition, generate the comparison directly.
1482 Primitive::Type type = condition->InputAt(0)->GetType();
1483 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1484 GenerateCompareTestAndBranch(condition, true_target, false_target);
1485 return;
1486 }
1487
1488 Location lhs = condition->GetLocations()->InAt(0);
1489 Location rhs = condition->GetLocations()->InAt(1);
1490 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
1491 if (rhs.IsRegister()) {
1492 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1493 } else if (rhs.IsConstant()) {
1494 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1495 if (constant == 0) {
1496 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001497 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001498 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001499 }
1500 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001501 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1502 }
1503 if (true_target == nullptr) {
1504 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1505 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001506 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001507 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001508 }
David Brazdil0debae72015-11-12 18:37:00 +00001509
1510 // If neither branch falls through (case 3), the conditional branch to `true_target`
1511 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1512 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001513 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001514 }
1515}
1516
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001517void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001518 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1519 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001520 locations->SetInAt(0, Location::Any());
1521 }
1522}
1523
1524void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001525 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1526 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1527 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1528 nullptr : codegen_->GetLabelOf(true_successor);
1529 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1530 nullptr : codegen_->GetLabelOf(false_successor);
1531 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001532}
1533
1534void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1535 LocationSummary* locations = new (GetGraph()->GetArena())
1536 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001537 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001538 locations->SetInAt(0, Location::Any());
1539 }
1540}
1541
1542void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001543 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001544 DeoptimizationSlowPathX86(deoptimize);
1545 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001546 GenerateTestAndBranch(deoptimize,
1547 /* condition_input_index */ 0,
1548 slow_path->GetEntryLabel(),
1549 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001550}
1551
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001552void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001553 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001554}
1555
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001556void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1557 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001558}
1559
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001560void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001561 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001562}
1563
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001564void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001565 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001566}
1567
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001568void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001569 LocationSummary* locations =
1570 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001571 switch (store->InputAt(1)->GetType()) {
1572 case Primitive::kPrimBoolean:
1573 case Primitive::kPrimByte:
1574 case Primitive::kPrimChar:
1575 case Primitive::kPrimShort:
1576 case Primitive::kPrimInt:
1577 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001578 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001579 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1580 break;
1581
1582 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001583 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001584 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1585 break;
1586
1587 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001588 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001589 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001590}
1591
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001592void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001593}
1594
Roland Levillain0d37cd02015-05-27 16:39:19 +01001595void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001596 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001597 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001598 // Handle the long/FP comparisons made in instruction simplification.
1599 switch (cond->InputAt(0)->GetType()) {
1600 case Primitive::kPrimLong: {
1601 locations->SetInAt(0, Location::RequiresRegister());
1602 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1603 if (cond->NeedsMaterialization()) {
1604 locations->SetOut(Location::RequiresRegister());
1605 }
1606 break;
1607 }
1608 case Primitive::kPrimFloat:
1609 case Primitive::kPrimDouble: {
1610 locations->SetInAt(0, Location::RequiresFpuRegister());
1611 locations->SetInAt(1, Location::RequiresFpuRegister());
1612 if (cond->NeedsMaterialization()) {
1613 locations->SetOut(Location::RequiresRegister());
1614 }
1615 break;
1616 }
1617 default:
1618 locations->SetInAt(0, Location::RequiresRegister());
1619 locations->SetInAt(1, Location::Any());
1620 if (cond->NeedsMaterialization()) {
1621 // We need a byte register.
1622 locations->SetOut(Location::RegisterLocation(ECX));
1623 }
1624 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001625 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001626}
1627
Roland Levillain0d37cd02015-05-27 16:39:19 +01001628void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001629 if (!cond->NeedsMaterialization()) {
1630 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001631 }
Mark Mendellc4701932015-04-10 13:18:51 -04001632
1633 LocationSummary* locations = cond->GetLocations();
1634 Location lhs = locations->InAt(0);
1635 Location rhs = locations->InAt(1);
1636 Register reg = locations->Out().AsRegister<Register>();
1637 Label true_label, false_label;
1638
1639 switch (cond->InputAt(0)->GetType()) {
1640 default: {
1641 // Integer case.
1642
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001643 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001644 __ xorl(reg, reg);
1645
1646 if (rhs.IsRegister()) {
1647 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1648 } else if (rhs.IsConstant()) {
1649 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1650 if (constant == 0) {
1651 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1652 } else {
1653 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1654 }
1655 } else {
1656 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1657 }
Aart Bike9f37602015-10-09 11:15:55 -07001658 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001659 return;
1660 }
1661 case Primitive::kPrimLong:
1662 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1663 break;
1664 case Primitive::kPrimFloat:
1665 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1666 GenerateFPJumps(cond, &true_label, &false_label);
1667 break;
1668 case Primitive::kPrimDouble:
1669 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1670 GenerateFPJumps(cond, &true_label, &false_label);
1671 break;
1672 }
1673
1674 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001675 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001676
Roland Levillain4fa13f62015-07-06 18:11:54 +01001677 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001678 __ Bind(&false_label);
1679 __ xorl(reg, reg);
1680 __ jmp(&done_label);
1681
Roland Levillain4fa13f62015-07-06 18:11:54 +01001682 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001683 __ Bind(&true_label);
1684 __ movl(reg, Immediate(1));
1685 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001686}
1687
1688void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1689 VisitCondition(comp);
1690}
1691
1692void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1693 VisitCondition(comp);
1694}
1695
1696void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1697 VisitCondition(comp);
1698}
1699
1700void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1701 VisitCondition(comp);
1702}
1703
1704void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1705 VisitCondition(comp);
1706}
1707
1708void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1709 VisitCondition(comp);
1710}
1711
1712void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1713 VisitCondition(comp);
1714}
1715
1716void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1717 VisitCondition(comp);
1718}
1719
1720void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1721 VisitCondition(comp);
1722}
1723
1724void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1725 VisitCondition(comp);
1726}
1727
1728void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1729 VisitCondition(comp);
1730}
1731
1732void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1733 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001734}
1735
Aart Bike9f37602015-10-09 11:15:55 -07001736void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1737 VisitCondition(comp);
1738}
1739
1740void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1741 VisitCondition(comp);
1742}
1743
1744void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1745 VisitCondition(comp);
1746}
1747
1748void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1749 VisitCondition(comp);
1750}
1751
1752void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1753 VisitCondition(comp);
1754}
1755
1756void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1757 VisitCondition(comp);
1758}
1759
1760void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1761 VisitCondition(comp);
1762}
1763
1764void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1765 VisitCondition(comp);
1766}
1767
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001768void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001769 LocationSummary* locations =
1770 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001771 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001772}
1773
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001774void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001775 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001776}
1777
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001778void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1779 LocationSummary* locations =
1780 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1781 locations->SetOut(Location::ConstantLocation(constant));
1782}
1783
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001784void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001785 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001786}
1787
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001788void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001789 LocationSummary* locations =
1790 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001791 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001792}
1793
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001794void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001795 // Will be generated at use site.
1796}
1797
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001798void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1799 LocationSummary* locations =
1800 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1801 locations->SetOut(Location::ConstantLocation(constant));
1802}
1803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001804void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001805 // Will be generated at use site.
1806}
1807
1808void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1809 LocationSummary* locations =
1810 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1811 locations->SetOut(Location::ConstantLocation(constant));
1812}
1813
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001814void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001815 // Will be generated at use site.
1816}
1817
Calin Juravle27df7582015-04-17 19:12:31 +01001818void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1819 memory_barrier->SetLocations(nullptr);
1820}
1821
1822void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1823 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1824}
1825
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001826void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001827 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001828}
1829
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001830void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001831 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001832}
1833
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001834void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001835 LocationSummary* locations =
1836 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001837 switch (ret->InputAt(0)->GetType()) {
1838 case Primitive::kPrimBoolean:
1839 case Primitive::kPrimByte:
1840 case Primitive::kPrimChar:
1841 case Primitive::kPrimShort:
1842 case Primitive::kPrimInt:
1843 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001844 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001845 break;
1846
1847 case Primitive::kPrimLong:
1848 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001849 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001850 break;
1851
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 case Primitive::kPrimFloat:
1853 case Primitive::kPrimDouble:
1854 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001855 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001856 break;
1857
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001858 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001859 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001861}
1862
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001863void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001864 if (kIsDebugBuild) {
1865 switch (ret->InputAt(0)->GetType()) {
1866 case Primitive::kPrimBoolean:
1867 case Primitive::kPrimByte:
1868 case Primitive::kPrimChar:
1869 case Primitive::kPrimShort:
1870 case Primitive::kPrimInt:
1871 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001872 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 break;
1874
1875 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001876 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1877 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
1879
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 case Primitive::kPrimFloat:
1881 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001882 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001883 break;
1884
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 }
1888 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001889 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001890}
1891
Calin Juravle175dc732015-08-25 15:42:32 +01001892void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1893 // The trampoline uses the same calling convention as dex calling conventions,
1894 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1895 // the method_idx.
1896 HandleInvoke(invoke);
1897}
1898
1899void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1900 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1901}
1902
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001903void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001904 // When we do not run baseline, explicit clinit checks triggered by static
1905 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1906 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001907
Mark Mendellfb8d2792015-03-31 22:16:59 -04001908 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001909 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001910 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1911 invoke->GetLocations()->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::Any());
1912 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001913 return;
1914 }
1915
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001916 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001917
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001918 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1919 if (invoke->HasPcRelativeDexCache()) {
1920 invoke->GetLocations()->SetInAt(invoke->GetCurrentMethodInputIndex(),
1921 Location::RequiresRegister());
1922 }
1923
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001924 if (codegen_->IsBaseline()) {
1925 // Baseline does not have enough registers if the current method also
1926 // needs a register. We therefore do not require a register for it, and let
1927 // the code generation of the invoke handle it.
1928 LocationSummary* locations = invoke->GetLocations();
1929 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1930 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1931 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1932 }
1933 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001934}
1935
Mark Mendell09ed1a32015-03-25 08:30:06 -04001936static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1937 if (invoke->GetLocations()->Intrinsified()) {
1938 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1939 intrinsic.Dispatch(invoke);
1940 return true;
1941 }
1942 return false;
1943}
1944
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001945void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001946 // When we do not run baseline, explicit clinit checks triggered by static
1947 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1948 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001949
Mark Mendell09ed1a32015-03-25 08:30:06 -04001950 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1951 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001952 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001953
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001954 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001955 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001956 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001957 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001958}
1959
1960void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1961 HandleInvoke(invoke);
1962}
1963
1964void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001965 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001966 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001967}
1968
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001969void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001970 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1971 return;
1972 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001973
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001974 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001975 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001976 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001977}
1978
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001979void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001980 // This call to HandleInvoke allocates a temporary (core) register
1981 // which is also used to transfer the hidden argument from FP to
1982 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001983 HandleInvoke(invoke);
1984 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001985 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001986}
1987
1988void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1989 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00001990 LocationSummary* locations = invoke->GetLocations();
1991 Register temp = locations->GetTemp(0).AsRegister<Register>();
1992 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001993 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1994 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001995 Location receiver = locations->InAt(0);
1996 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1997
Roland Levillain0d5a2812015-11-13 10:07:31 +00001998 // Set the hidden argument. This is safe to do this here, as XMM7
1999 // won't be modified thereafter, before the `call` instruction.
2000 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002001 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002002 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002003
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002004 if (receiver.IsStackSlot()) {
2005 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002006 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002007 __ movl(temp, Address(temp, class_offset));
2008 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002009 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002010 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002011 }
Roland Levillain4d027112015-07-01 15:41:14 +01002012 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002013 // Instead of simply (possibly) unpoisoning `temp` here, we should
2014 // emit a read barrier for the previous class reference load.
2015 // However this is not required in practice, as this is an
2016 // intermediate/temporary reference and because the current
2017 // concurrent copying collector keeps the from-space memory
2018 // intact/accessible until the end of the marking phase (the
2019 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002020 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002021 // temp = temp->GetImtEntryAt(method_offset);
2022 __ movl(temp, Address(temp, method_offset));
2023 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002024 __ call(Address(temp,
2025 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002026
2027 DCHECK(!codegen_->IsLeafMethod());
2028 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2029}
2030
Roland Levillain88cb1752014-10-20 16:36:47 +01002031void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2032 LocationSummary* locations =
2033 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2034 switch (neg->GetResultType()) {
2035 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002036 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002037 locations->SetInAt(0, Location::RequiresRegister());
2038 locations->SetOut(Location::SameAsFirstInput());
2039 break;
2040
Roland Levillain88cb1752014-10-20 16:36:47 +01002041 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002042 locations->SetInAt(0, Location::RequiresFpuRegister());
2043 locations->SetOut(Location::SameAsFirstInput());
2044 locations->AddTemp(Location::RequiresRegister());
2045 locations->AddTemp(Location::RequiresFpuRegister());
2046 break;
2047
Roland Levillain88cb1752014-10-20 16:36:47 +01002048 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002049 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002050 locations->SetOut(Location::SameAsFirstInput());
2051 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002052 break;
2053
2054 default:
2055 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2056 }
2057}
2058
2059void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2060 LocationSummary* locations = neg->GetLocations();
2061 Location out = locations->Out();
2062 Location in = locations->InAt(0);
2063 switch (neg->GetResultType()) {
2064 case Primitive::kPrimInt:
2065 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002066 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002067 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002068 break;
2069
2070 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002071 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002072 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002073 __ negl(out.AsRegisterPairLow<Register>());
2074 // Negation is similar to subtraction from zero. The least
2075 // significant byte triggers a borrow when it is different from
2076 // zero; to take it into account, add 1 to the most significant
2077 // byte if the carry flag (CF) is set to 1 after the first NEGL
2078 // operation.
2079 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2080 __ negl(out.AsRegisterPairHigh<Register>());
2081 break;
2082
Roland Levillain5368c212014-11-27 15:03:41 +00002083 case Primitive::kPrimFloat: {
2084 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002085 Register constant = locations->GetTemp(0).AsRegister<Register>();
2086 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002087 // Implement float negation with an exclusive or with value
2088 // 0x80000000 (mask for bit 31, representing the sign of a
2089 // single-precision floating-point number).
2090 __ movl(constant, Immediate(INT32_C(0x80000000)));
2091 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002093 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002094 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002095
Roland Levillain5368c212014-11-27 15:03:41 +00002096 case Primitive::kPrimDouble: {
2097 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002098 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002099 // Implement double negation with an exclusive or with value
2100 // 0x8000000000000000 (mask for bit 63, representing the sign of
2101 // a double-precision floating-point number).
2102 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002103 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002104 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002105 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002106
2107 default:
2108 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2109 }
2110}
2111
Roland Levillaindff1f282014-11-05 14:15:05 +00002112void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002113 Primitive::Type result_type = conversion->GetResultType();
2114 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002115 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002116
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002117 // The float-to-long and double-to-long type conversions rely on a
2118 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002119 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002120 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2121 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002122 ? LocationSummary::kCall
2123 : LocationSummary::kNoCall;
2124 LocationSummary* locations =
2125 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2126
David Brazdilb2bd1c52015-03-25 11:17:37 +00002127 // The Java language does not allow treating boolean as an integral type but
2128 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002129
Roland Levillaindff1f282014-11-05 14:15:05 +00002130 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002131 case Primitive::kPrimByte:
2132 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002133 case Primitive::kPrimBoolean:
2134 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002135 case Primitive::kPrimShort:
2136 case Primitive::kPrimInt:
2137 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002138 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002139 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2140 // Make the output overlap to please the register allocator. This greatly simplifies
2141 // the validation of the linear scan implementation
2142 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002143 break;
2144
2145 default:
2146 LOG(FATAL) << "Unexpected type conversion from " << input_type
2147 << " to " << result_type;
2148 }
2149 break;
2150
Roland Levillain01a8d712014-11-14 16:27:39 +00002151 case Primitive::kPrimShort:
2152 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002153 case Primitive::kPrimBoolean:
2154 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002155 case Primitive::kPrimByte:
2156 case Primitive::kPrimInt:
2157 case Primitive::kPrimChar:
2158 // Processing a Dex `int-to-short' instruction.
2159 locations->SetInAt(0, Location::Any());
2160 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2161 break;
2162
2163 default:
2164 LOG(FATAL) << "Unexpected type conversion from " << input_type
2165 << " to " << result_type;
2166 }
2167 break;
2168
Roland Levillain946e1432014-11-11 17:35:19 +00002169 case Primitive::kPrimInt:
2170 switch (input_type) {
2171 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002172 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002173 locations->SetInAt(0, Location::Any());
2174 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2175 break;
2176
2177 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002178 // Processing a Dex `float-to-int' instruction.
2179 locations->SetInAt(0, Location::RequiresFpuRegister());
2180 locations->SetOut(Location::RequiresRegister());
2181 locations->AddTemp(Location::RequiresFpuRegister());
2182 break;
2183
Roland Levillain946e1432014-11-11 17:35:19 +00002184 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002185 // Processing a Dex `double-to-int' instruction.
2186 locations->SetInAt(0, Location::RequiresFpuRegister());
2187 locations->SetOut(Location::RequiresRegister());
2188 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002189 break;
2190
2191 default:
2192 LOG(FATAL) << "Unexpected type conversion from " << input_type
2193 << " to " << result_type;
2194 }
2195 break;
2196
Roland Levillaindff1f282014-11-05 14:15:05 +00002197 case Primitive::kPrimLong:
2198 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002199 case Primitive::kPrimBoolean:
2200 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002201 case Primitive::kPrimByte:
2202 case Primitive::kPrimShort:
2203 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002204 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002205 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002206 locations->SetInAt(0, Location::RegisterLocation(EAX));
2207 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2208 break;
2209
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002210 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002211 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002212 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002213 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002214 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2215 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2216
Vladimir Marko949c91f2015-01-27 10:48:44 +00002217 // The runtime helper puts the result in EAX, EDX.
2218 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002219 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002220 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002221
2222 default:
2223 LOG(FATAL) << "Unexpected type conversion from " << input_type
2224 << " to " << result_type;
2225 }
2226 break;
2227
Roland Levillain981e4542014-11-14 11:47:14 +00002228 case Primitive::kPrimChar:
2229 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002230 case Primitive::kPrimBoolean:
2231 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002232 case Primitive::kPrimByte:
2233 case Primitive::kPrimShort:
2234 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002235 // Processing a Dex `int-to-char' instruction.
2236 locations->SetInAt(0, Location::Any());
2237 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2238 break;
2239
2240 default:
2241 LOG(FATAL) << "Unexpected type conversion from " << input_type
2242 << " to " << result_type;
2243 }
2244 break;
2245
Roland Levillaindff1f282014-11-05 14:15:05 +00002246 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002247 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002248 case Primitive::kPrimBoolean:
2249 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002250 case Primitive::kPrimByte:
2251 case Primitive::kPrimShort:
2252 case Primitive::kPrimInt:
2253 case Primitive::kPrimChar:
2254 // Processing a Dex `int-to-float' instruction.
2255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetOut(Location::RequiresFpuRegister());
2257 break;
2258
2259 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002260 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002261 locations->SetInAt(0, Location::Any());
2262 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002263 break;
2264
Roland Levillaincff13742014-11-17 14:32:17 +00002265 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002266 // Processing a Dex `double-to-float' instruction.
2267 locations->SetInAt(0, Location::RequiresFpuRegister());
2268 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002269 break;
2270
2271 default:
2272 LOG(FATAL) << "Unexpected type conversion from " << input_type
2273 << " to " << result_type;
2274 };
2275 break;
2276
Roland Levillaindff1f282014-11-05 14:15:05 +00002277 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002278 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002279 case Primitive::kPrimBoolean:
2280 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002281 case Primitive::kPrimByte:
2282 case Primitive::kPrimShort:
2283 case Primitive::kPrimInt:
2284 case Primitive::kPrimChar:
2285 // Processing a Dex `int-to-double' instruction.
2286 locations->SetInAt(0, Location::RequiresRegister());
2287 locations->SetOut(Location::RequiresFpuRegister());
2288 break;
2289
2290 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002291 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002292 locations->SetInAt(0, Location::Any());
2293 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002294 break;
2295
Roland Levillaincff13742014-11-17 14:32:17 +00002296 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002297 // Processing a Dex `float-to-double' instruction.
2298 locations->SetInAt(0, Location::RequiresFpuRegister());
2299 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002300 break;
2301
2302 default:
2303 LOG(FATAL) << "Unexpected type conversion from " << input_type
2304 << " to " << result_type;
2305 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002306 break;
2307
2308 default:
2309 LOG(FATAL) << "Unexpected type conversion from " << input_type
2310 << " to " << result_type;
2311 }
2312}
2313
2314void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2315 LocationSummary* locations = conversion->GetLocations();
2316 Location out = locations->Out();
2317 Location in = locations->InAt(0);
2318 Primitive::Type result_type = conversion->GetResultType();
2319 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002320 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002322 case Primitive::kPrimByte:
2323 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002324 case Primitive::kPrimBoolean:
2325 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002326 case Primitive::kPrimShort:
2327 case Primitive::kPrimInt:
2328 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002329 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002330 if (in.IsRegister()) {
2331 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002332 } else {
2333 DCHECK(in.GetConstant()->IsIntConstant());
2334 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2335 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2336 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002337 break;
2338
2339 default:
2340 LOG(FATAL) << "Unexpected type conversion from " << input_type
2341 << " to " << result_type;
2342 }
2343 break;
2344
Roland Levillain01a8d712014-11-14 16:27:39 +00002345 case Primitive::kPrimShort:
2346 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002347 case Primitive::kPrimBoolean:
2348 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002349 case Primitive::kPrimByte:
2350 case Primitive::kPrimInt:
2351 case Primitive::kPrimChar:
2352 // Processing a Dex `int-to-short' instruction.
2353 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002354 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002355 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002357 } else {
2358 DCHECK(in.GetConstant()->IsIntConstant());
2359 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002360 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002361 }
2362 break;
2363
2364 default:
2365 LOG(FATAL) << "Unexpected type conversion from " << input_type
2366 << " to " << result_type;
2367 }
2368 break;
2369
Roland Levillain946e1432014-11-11 17:35:19 +00002370 case Primitive::kPrimInt:
2371 switch (input_type) {
2372 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002373 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002374 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002376 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002377 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002378 } else {
2379 DCHECK(in.IsConstant());
2380 DCHECK(in.GetConstant()->IsLongConstant());
2381 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002382 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002383 }
2384 break;
2385
Roland Levillain3f8f9362014-12-02 17:45:01 +00002386 case Primitive::kPrimFloat: {
2387 // Processing a Dex `float-to-int' instruction.
2388 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2389 Register output = out.AsRegister<Register>();
2390 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002391 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002392
2393 __ movl(output, Immediate(kPrimIntMax));
2394 // temp = int-to-float(output)
2395 __ cvtsi2ss(temp, output);
2396 // if input >= temp goto done
2397 __ comiss(input, temp);
2398 __ j(kAboveEqual, &done);
2399 // if input == NaN goto nan
2400 __ j(kUnordered, &nan);
2401 // output = float-to-int-truncate(input)
2402 __ cvttss2si(output, input);
2403 __ jmp(&done);
2404 __ Bind(&nan);
2405 // output = 0
2406 __ xorl(output, output);
2407 __ Bind(&done);
2408 break;
2409 }
2410
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002411 case Primitive::kPrimDouble: {
2412 // Processing a Dex `double-to-int' instruction.
2413 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2414 Register output = out.AsRegister<Register>();
2415 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002416 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002417
2418 __ movl(output, Immediate(kPrimIntMax));
2419 // temp = int-to-double(output)
2420 __ cvtsi2sd(temp, output);
2421 // if input >= temp goto done
2422 __ comisd(input, temp);
2423 __ j(kAboveEqual, &done);
2424 // if input == NaN goto nan
2425 __ j(kUnordered, &nan);
2426 // output = double-to-int-truncate(input)
2427 __ cvttsd2si(output, input);
2428 __ jmp(&done);
2429 __ Bind(&nan);
2430 // output = 0
2431 __ xorl(output, output);
2432 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002433 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002434 }
Roland Levillain946e1432014-11-11 17:35:19 +00002435
2436 default:
2437 LOG(FATAL) << "Unexpected type conversion from " << input_type
2438 << " to " << result_type;
2439 }
2440 break;
2441
Roland Levillaindff1f282014-11-05 14:15:05 +00002442 case Primitive::kPrimLong:
2443 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002444 case Primitive::kPrimBoolean:
2445 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002446 case Primitive::kPrimByte:
2447 case Primitive::kPrimShort:
2448 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002449 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002450 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002451 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2452 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002454 __ cdq();
2455 break;
2456
2457 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002458 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002459 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2460 conversion,
2461 conversion->GetDexPc(),
2462 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002463 break;
2464
Roland Levillaindff1f282014-11-05 14:15:05 +00002465 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002466 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002467 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2468 conversion,
2469 conversion->GetDexPc(),
2470 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002471 break;
2472
2473 default:
2474 LOG(FATAL) << "Unexpected type conversion from " << input_type
2475 << " to " << result_type;
2476 }
2477 break;
2478
Roland Levillain981e4542014-11-14 11:47:14 +00002479 case Primitive::kPrimChar:
2480 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002481 case Primitive::kPrimBoolean:
2482 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002483 case Primitive::kPrimByte:
2484 case Primitive::kPrimShort:
2485 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002486 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2487 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002488 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002489 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002491 } else {
2492 DCHECK(in.GetConstant()->IsIntConstant());
2493 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002494 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002495 }
2496 break;
2497
2498 default:
2499 LOG(FATAL) << "Unexpected type conversion from " << input_type
2500 << " to " << result_type;
2501 }
2502 break;
2503
Roland Levillaindff1f282014-11-05 14:15:05 +00002504 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002505 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002506 case Primitive::kPrimBoolean:
2507 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002508 case Primitive::kPrimByte:
2509 case Primitive::kPrimShort:
2510 case Primitive::kPrimInt:
2511 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002512 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002513 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002514 break;
2515
Roland Levillain6d0e4832014-11-27 18:31:21 +00002516 case Primitive::kPrimLong: {
2517 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002518 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002519
Roland Levillain232ade02015-04-20 15:14:36 +01002520 // Create stack space for the call to
2521 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2522 // TODO: enhance register allocator to ask for stack temporaries.
2523 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2524 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2525 __ subl(ESP, Immediate(adjustment));
2526 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002527
Roland Levillain232ade02015-04-20 15:14:36 +01002528 // Load the value to the FP stack, using temporaries if needed.
2529 PushOntoFPStack(in, 0, adjustment, false, true);
2530
2531 if (out.IsStackSlot()) {
2532 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2533 } else {
2534 __ fstps(Address(ESP, 0));
2535 Location stack_temp = Location::StackSlot(0);
2536 codegen_->Move32(out, stack_temp);
2537 }
2538
2539 // Remove the temporary stack space we allocated.
2540 if (adjustment != 0) {
2541 __ addl(ESP, Immediate(adjustment));
2542 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002543 break;
2544 }
2545
Roland Levillaincff13742014-11-17 14:32:17 +00002546 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002547 // Processing a Dex `double-to-float' instruction.
2548 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002549 break;
2550
2551 default:
2552 LOG(FATAL) << "Unexpected type conversion from " << input_type
2553 << " to " << result_type;
2554 };
2555 break;
2556
Roland Levillaindff1f282014-11-05 14:15:05 +00002557 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002558 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002559 case Primitive::kPrimBoolean:
2560 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002561 case Primitive::kPrimByte:
2562 case Primitive::kPrimShort:
2563 case Primitive::kPrimInt:
2564 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002565 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002566 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002567 break;
2568
Roland Levillain647b9ed2014-11-27 12:06:00 +00002569 case Primitive::kPrimLong: {
2570 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002571 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002572
Roland Levillain232ade02015-04-20 15:14:36 +01002573 // Create stack space for the call to
2574 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2575 // TODO: enhance register allocator to ask for stack temporaries.
2576 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2577 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2578 __ subl(ESP, Immediate(adjustment));
2579 }
2580
2581 // Load the value to the FP stack, using temporaries if needed.
2582 PushOntoFPStack(in, 0, adjustment, false, true);
2583
2584 if (out.IsDoubleStackSlot()) {
2585 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2586 } else {
2587 __ fstpl(Address(ESP, 0));
2588 Location stack_temp = Location::DoubleStackSlot(0);
2589 codegen_->Move64(out, stack_temp);
2590 }
2591
2592 // Remove the temporary stack space we allocated.
2593 if (adjustment != 0) {
2594 __ addl(ESP, Immediate(adjustment));
2595 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002596 break;
2597 }
2598
Roland Levillaincff13742014-11-17 14:32:17 +00002599 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002600 // Processing a Dex `float-to-double' instruction.
2601 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002602 break;
2603
2604 default:
2605 LOG(FATAL) << "Unexpected type conversion from " << input_type
2606 << " to " << result_type;
2607 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002608 break;
2609
2610 default:
2611 LOG(FATAL) << "Unexpected type conversion from " << input_type
2612 << " to " << result_type;
2613 }
2614}
2615
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002616void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002617 LocationSummary* locations =
2618 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002619 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002620 case Primitive::kPrimInt: {
2621 locations->SetInAt(0, Location::RequiresRegister());
2622 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2623 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2624 break;
2625 }
2626
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002627 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002628 locations->SetInAt(0, Location::RequiresRegister());
2629 locations->SetInAt(1, Location::Any());
2630 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002631 break;
2632 }
2633
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002634 case Primitive::kPrimFloat:
2635 case Primitive::kPrimDouble: {
2636 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002637 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002638 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002639 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002640 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002641
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002642 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002643 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2644 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002645 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002646}
2647
2648void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2649 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002650 Location first = locations->InAt(0);
2651 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002652 Location out = locations->Out();
2653
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002654 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002655 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002656 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002657 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2658 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002659 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2660 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002661 } else {
2662 __ leal(out.AsRegister<Register>(), Address(
2663 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2664 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002665 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002666 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2667 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2668 __ addl(out.AsRegister<Register>(), Immediate(value));
2669 } else {
2670 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2671 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002672 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002673 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002674 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002675 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002676 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002677 }
2678
2679 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002680 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002681 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2682 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002683 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002684 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2685 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002686 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002687 } else {
2688 DCHECK(second.IsConstant()) << second;
2689 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2690 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2691 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002692 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002693 break;
2694 }
2695
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002696 case Primitive::kPrimFloat: {
2697 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002698 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002699 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2700 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2701 DCHECK(!const_area->NeedsMaterialization());
2702 __ addss(first.AsFpuRegister<XmmRegister>(),
2703 codegen_->LiteralFloatAddress(
2704 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2705 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2706 } else {
2707 DCHECK(second.IsStackSlot());
2708 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002709 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002710 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002711 }
2712
2713 case Primitive::kPrimDouble: {
2714 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002716 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2717 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2718 DCHECK(!const_area->NeedsMaterialization());
2719 __ addsd(first.AsFpuRegister<XmmRegister>(),
2720 codegen_->LiteralDoubleAddress(
2721 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2722 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2723 } else {
2724 DCHECK(second.IsDoubleStackSlot());
2725 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002726 }
2727 break;
2728 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002729
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002730 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002731 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002732 }
2733}
2734
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002735void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002736 LocationSummary* locations =
2737 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002738 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002739 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002740 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002741 locations->SetInAt(0, Location::RequiresRegister());
2742 locations->SetInAt(1, Location::Any());
2743 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002744 break;
2745 }
Calin Juravle11351682014-10-23 15:38:15 +01002746 case Primitive::kPrimFloat:
2747 case Primitive::kPrimDouble: {
2748 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002749 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002750 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002751 break;
Calin Juravle11351682014-10-23 15:38:15 +01002752 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002753
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002754 default:
Calin Juravle11351682014-10-23 15:38:15 +01002755 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002756 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002757}
2758
2759void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2760 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002761 Location first = locations->InAt(0);
2762 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002763 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002764 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002766 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002767 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002768 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002769 __ subl(first.AsRegister<Register>(),
2770 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002771 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002773 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002774 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002775 }
2776
2777 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002778 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002779 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2780 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002781 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002782 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002783 __ sbbl(first.AsRegisterPairHigh<Register>(),
2784 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002785 } else {
2786 DCHECK(second.IsConstant()) << second;
2787 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2788 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2789 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002790 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002791 break;
2792 }
2793
Calin Juravle11351682014-10-23 15:38:15 +01002794 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002795 if (second.IsFpuRegister()) {
2796 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2797 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2798 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2799 DCHECK(!const_area->NeedsMaterialization());
2800 __ subss(first.AsFpuRegister<XmmRegister>(),
2801 codegen_->LiteralFloatAddress(
2802 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2803 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2804 } else {
2805 DCHECK(second.IsStackSlot());
2806 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2807 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002808 break;
Calin Juravle11351682014-10-23 15:38:15 +01002809 }
2810
2811 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002812 if (second.IsFpuRegister()) {
2813 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2814 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2815 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2816 DCHECK(!const_area->NeedsMaterialization());
2817 __ subsd(first.AsFpuRegister<XmmRegister>(),
2818 codegen_->LiteralDoubleAddress(
2819 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2820 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2821 } else {
2822 DCHECK(second.IsDoubleStackSlot());
2823 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2824 }
Calin Juravle11351682014-10-23 15:38:15 +01002825 break;
2826 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002827
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002828 default:
Calin Juravle11351682014-10-23 15:38:15 +01002829 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002830 }
2831}
2832
Calin Juravle34bacdf2014-10-07 20:23:36 +01002833void LocationsBuilderX86::VisitMul(HMul* mul) {
2834 LocationSummary* locations =
2835 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2836 switch (mul->GetResultType()) {
2837 case Primitive::kPrimInt:
2838 locations->SetInAt(0, Location::RequiresRegister());
2839 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002840 if (mul->InputAt(1)->IsIntConstant()) {
2841 // Can use 3 operand multiply.
2842 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2843 } else {
2844 locations->SetOut(Location::SameAsFirstInput());
2845 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002846 break;
2847 case Primitive::kPrimLong: {
2848 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002849 locations->SetInAt(1, Location::Any());
2850 locations->SetOut(Location::SameAsFirstInput());
2851 // Needed for imul on 32bits with 64bits output.
2852 locations->AddTemp(Location::RegisterLocation(EAX));
2853 locations->AddTemp(Location::RegisterLocation(EDX));
2854 break;
2855 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002856 case Primitive::kPrimFloat:
2857 case Primitive::kPrimDouble: {
2858 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002859 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002860 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002861 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002862 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002863
2864 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002865 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002866 }
2867}
2868
2869void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2870 LocationSummary* locations = mul->GetLocations();
2871 Location first = locations->InAt(0);
2872 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002873 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002874
2875 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002876 case Primitive::kPrimInt:
2877 // The constant may have ended up in a register, so test explicitly to avoid
2878 // problems where the output may not be the same as the first operand.
2879 if (mul->InputAt(1)->IsIntConstant()) {
2880 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2881 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2882 } else if (second.IsRegister()) {
2883 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002885 } else {
2886 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002887 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002889 }
2890 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002891
2892 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002893 Register in1_hi = first.AsRegisterPairHigh<Register>();
2894 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002895 Register eax = locations->GetTemp(0).AsRegister<Register>();
2896 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002897
2898 DCHECK_EQ(EAX, eax);
2899 DCHECK_EQ(EDX, edx);
2900
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002901 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002902 // output: in1
2903 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2904 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2905 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002906 if (second.IsConstant()) {
2907 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002908
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002909 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2910 int32_t low_value = Low32Bits(value);
2911 int32_t high_value = High32Bits(value);
2912 Immediate low(low_value);
2913 Immediate high(high_value);
2914
2915 __ movl(eax, high);
2916 // eax <- in1.lo * in2.hi
2917 __ imull(eax, in1_lo);
2918 // in1.hi <- in1.hi * in2.lo
2919 __ imull(in1_hi, low);
2920 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2921 __ addl(in1_hi, eax);
2922 // move in2_lo to eax to prepare for double precision
2923 __ movl(eax, low);
2924 // edx:eax <- in1.lo * in2.lo
2925 __ mull(in1_lo);
2926 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2927 __ addl(in1_hi, edx);
2928 // in1.lo <- (in1.lo * in2.lo)[31:0];
2929 __ movl(in1_lo, eax);
2930 } else if (second.IsRegisterPair()) {
2931 Register in2_hi = second.AsRegisterPairHigh<Register>();
2932 Register in2_lo = second.AsRegisterPairLow<Register>();
2933
2934 __ movl(eax, in2_hi);
2935 // eax <- in1.lo * in2.hi
2936 __ imull(eax, in1_lo);
2937 // in1.hi <- in1.hi * in2.lo
2938 __ imull(in1_hi, in2_lo);
2939 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2940 __ addl(in1_hi, eax);
2941 // move in1_lo to eax to prepare for double precision
2942 __ movl(eax, in1_lo);
2943 // edx:eax <- in1.lo * in2.lo
2944 __ mull(in2_lo);
2945 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2946 __ addl(in1_hi, edx);
2947 // in1.lo <- (in1.lo * in2.lo)[31:0];
2948 __ movl(in1_lo, eax);
2949 } else {
2950 DCHECK(second.IsDoubleStackSlot()) << second;
2951 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2952 Address in2_lo(ESP, second.GetStackIndex());
2953
2954 __ movl(eax, in2_hi);
2955 // eax <- in1.lo * in2.hi
2956 __ imull(eax, in1_lo);
2957 // in1.hi <- in1.hi * in2.lo
2958 __ imull(in1_hi, in2_lo);
2959 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2960 __ addl(in1_hi, eax);
2961 // move in1_lo to eax to prepare for double precision
2962 __ movl(eax, in1_lo);
2963 // edx:eax <- in1.lo * in2.lo
2964 __ mull(in2_lo);
2965 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2966 __ addl(in1_hi, edx);
2967 // in1.lo <- (in1.lo * in2.lo)[31:0];
2968 __ movl(in1_lo, eax);
2969 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002970
2971 break;
2972 }
2973
Calin Juravleb5bfa962014-10-21 18:02:24 +01002974 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002975 DCHECK(first.Equals(locations->Out()));
2976 if (second.IsFpuRegister()) {
2977 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2978 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2979 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2980 DCHECK(!const_area->NeedsMaterialization());
2981 __ mulss(first.AsFpuRegister<XmmRegister>(),
2982 codegen_->LiteralFloatAddress(
2983 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2984 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2985 } else {
2986 DCHECK(second.IsStackSlot());
2987 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2988 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002989 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002990 }
2991
2992 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002993 DCHECK(first.Equals(locations->Out()));
2994 if (second.IsFpuRegister()) {
2995 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2996 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2997 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2998 DCHECK(!const_area->NeedsMaterialization());
2999 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3000 codegen_->LiteralDoubleAddress(
3001 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3002 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3003 } else {
3004 DCHECK(second.IsDoubleStackSlot());
3005 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3006 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003007 break;
3008 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003009
3010 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003011 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 }
3013}
3014
Roland Levillain232ade02015-04-20 15:14:36 +01003015void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3016 uint32_t temp_offset,
3017 uint32_t stack_adjustment,
3018 bool is_fp,
3019 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003020 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003021 DCHECK(!is_wide);
3022 if (is_fp) {
3023 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3024 } else {
3025 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3026 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003027 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003028 DCHECK(is_wide);
3029 if (is_fp) {
3030 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3031 } else {
3032 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3033 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003034 } else {
3035 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003036 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003037 Location stack_temp = Location::StackSlot(temp_offset);
3038 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003039 if (is_fp) {
3040 __ flds(Address(ESP, temp_offset));
3041 } else {
3042 __ filds(Address(ESP, temp_offset));
3043 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003044 } else {
3045 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3046 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003047 if (is_fp) {
3048 __ fldl(Address(ESP, temp_offset));
3049 } else {
3050 __ fildl(Address(ESP, temp_offset));
3051 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003052 }
3053 }
3054}
3055
3056void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3057 Primitive::Type type = rem->GetResultType();
3058 bool is_float = type == Primitive::kPrimFloat;
3059 size_t elem_size = Primitive::ComponentSize(type);
3060 LocationSummary* locations = rem->GetLocations();
3061 Location first = locations->InAt(0);
3062 Location second = locations->InAt(1);
3063 Location out = locations->Out();
3064
3065 // Create stack space for 2 elements.
3066 // TODO: enhance register allocator to ask for stack temporaries.
3067 __ subl(ESP, Immediate(2 * elem_size));
3068
3069 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003070 const bool is_wide = !is_float;
3071 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3072 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003073
3074 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003075 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003076 __ Bind(&retry);
3077 __ fprem();
3078
3079 // Move FP status to AX.
3080 __ fstsw();
3081
3082 // And see if the argument reduction is complete. This is signaled by the
3083 // C2 FPU flag bit set to 0.
3084 __ andl(EAX, Immediate(kC2ConditionMask));
3085 __ j(kNotEqual, &retry);
3086
3087 // We have settled on the final value. Retrieve it into an XMM register.
3088 // Store FP top of stack to real stack.
3089 if (is_float) {
3090 __ fsts(Address(ESP, 0));
3091 } else {
3092 __ fstl(Address(ESP, 0));
3093 }
3094
3095 // Pop the 2 items from the FP stack.
3096 __ fucompp();
3097
3098 // Load the value from the stack into an XMM register.
3099 DCHECK(out.IsFpuRegister()) << out;
3100 if (is_float) {
3101 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3102 } else {
3103 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3104 }
3105
3106 // And remove the temporary stack space we allocated.
3107 __ addl(ESP, Immediate(2 * elem_size));
3108}
3109
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003110
3111void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3112 DCHECK(instruction->IsDiv() || instruction->IsRem());
3113
3114 LocationSummary* locations = instruction->GetLocations();
3115 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003116 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003117
3118 Register out_register = locations->Out().AsRegister<Register>();
3119 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003120 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003121
3122 DCHECK(imm == 1 || imm == -1);
3123
3124 if (instruction->IsRem()) {
3125 __ xorl(out_register, out_register);
3126 } else {
3127 __ movl(out_register, input_register);
3128 if (imm == -1) {
3129 __ negl(out_register);
3130 }
3131 }
3132}
3133
3134
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003135void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003136 LocationSummary* locations = instruction->GetLocations();
3137
3138 Register out_register = locations->Out().AsRegister<Register>();
3139 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003140 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003141
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003142 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003143 Register num = locations->GetTemp(0).AsRegister<Register>();
3144
3145 __ leal(num, Address(input_register, std::abs(imm) - 1));
3146 __ testl(input_register, input_register);
3147 __ cmovl(kGreaterEqual, num, input_register);
3148 int shift = CTZ(imm);
3149 __ sarl(num, Immediate(shift));
3150
3151 if (imm < 0) {
3152 __ negl(num);
3153 }
3154
3155 __ movl(out_register, num);
3156}
3157
3158void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3159 DCHECK(instruction->IsDiv() || instruction->IsRem());
3160
3161 LocationSummary* locations = instruction->GetLocations();
3162 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3163
3164 Register eax = locations->InAt(0).AsRegister<Register>();
3165 Register out = locations->Out().AsRegister<Register>();
3166 Register num;
3167 Register edx;
3168
3169 if (instruction->IsDiv()) {
3170 edx = locations->GetTemp(0).AsRegister<Register>();
3171 num = locations->GetTemp(1).AsRegister<Register>();
3172 } else {
3173 edx = locations->Out().AsRegister<Register>();
3174 num = locations->GetTemp(0).AsRegister<Register>();
3175 }
3176
3177 DCHECK_EQ(EAX, eax);
3178 DCHECK_EQ(EDX, edx);
3179 if (instruction->IsDiv()) {
3180 DCHECK_EQ(EAX, out);
3181 } else {
3182 DCHECK_EQ(EDX, out);
3183 }
3184
3185 int64_t magic;
3186 int shift;
3187 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3188
Mark Mendell0c9497d2015-08-21 09:30:05 -04003189 NearLabel ndiv;
3190 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003191 // If numerator is 0, the result is 0, no computation needed.
3192 __ testl(eax, eax);
3193 __ j(kNotEqual, &ndiv);
3194
3195 __ xorl(out, out);
3196 __ jmp(&end);
3197
3198 __ Bind(&ndiv);
3199
3200 // Save the numerator.
3201 __ movl(num, eax);
3202
3203 // EAX = magic
3204 __ movl(eax, Immediate(magic));
3205
3206 // EDX:EAX = magic * numerator
3207 __ imull(num);
3208
3209 if (imm > 0 && magic < 0) {
3210 // EDX += num
3211 __ addl(edx, num);
3212 } else if (imm < 0 && magic > 0) {
3213 __ subl(edx, num);
3214 }
3215
3216 // Shift if needed.
3217 if (shift != 0) {
3218 __ sarl(edx, Immediate(shift));
3219 }
3220
3221 // EDX += 1 if EDX < 0
3222 __ movl(eax, edx);
3223 __ shrl(edx, Immediate(31));
3224 __ addl(edx, eax);
3225
3226 if (instruction->IsRem()) {
3227 __ movl(eax, num);
3228 __ imull(edx, Immediate(imm));
3229 __ subl(eax, edx);
3230 __ movl(edx, eax);
3231 } else {
3232 __ movl(eax, edx);
3233 }
3234 __ Bind(&end);
3235}
3236
Calin Juravlebacfec32014-11-14 15:54:36 +00003237void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3238 DCHECK(instruction->IsDiv() || instruction->IsRem());
3239
3240 LocationSummary* locations = instruction->GetLocations();
3241 Location out = locations->Out();
3242 Location first = locations->InAt(0);
3243 Location second = locations->InAt(1);
3244 bool is_div = instruction->IsDiv();
3245
3246 switch (instruction->GetResultType()) {
3247 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003248 DCHECK_EQ(EAX, first.AsRegister<Register>());
3249 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003250
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003251 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003252 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003253
3254 if (imm == 0) {
3255 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3256 } else if (imm == 1 || imm == -1) {
3257 DivRemOneOrMinusOne(instruction);
3258 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003259 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003260 } else {
3261 DCHECK(imm <= -2 || imm >= 2);
3262 GenerateDivRemWithAnyConstant(instruction);
3263 }
3264 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003265 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003266 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003267 is_div);
3268 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003269
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003270 Register second_reg = second.AsRegister<Register>();
3271 // 0x80000000/-1 triggers an arithmetic exception!
3272 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3273 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003274
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003275 __ cmpl(second_reg, Immediate(-1));
3276 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003277
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003278 // edx:eax <- sign-extended of eax
3279 __ cdq();
3280 // eax = quotient, edx = remainder
3281 __ idivl(second_reg);
3282 __ Bind(slow_path->GetExitLabel());
3283 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003284 break;
3285 }
3286
3287 case Primitive::kPrimLong: {
3288 InvokeRuntimeCallingConvention calling_convention;
3289 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3290 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3291 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3292 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3293 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3294 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3295
3296 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003297 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3298 instruction,
3299 instruction->GetDexPc(),
3300 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003301 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003302 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3303 instruction,
3304 instruction->GetDexPc(),
3305 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003306 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003307 break;
3308 }
3309
3310 default:
3311 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3312 }
3313}
3314
Calin Juravle7c4954d2014-10-28 16:57:40 +00003315void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003316 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003317 ? LocationSummary::kCall
3318 : LocationSummary::kNoCall;
3319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3320
Calin Juravle7c4954d2014-10-28 16:57:40 +00003321 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003322 case Primitive::kPrimInt: {
3323 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003324 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003325 locations->SetOut(Location::SameAsFirstInput());
3326 // Intel uses edx:eax as the dividend.
3327 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003328 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3329 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3330 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003331 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003332 locations->AddTemp(Location::RequiresRegister());
3333 }
Calin Juravled0d48522014-11-04 16:40:20 +00003334 break;
3335 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003336 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003337 InvokeRuntimeCallingConvention calling_convention;
3338 locations->SetInAt(0, Location::RegisterPairLocation(
3339 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3340 locations->SetInAt(1, Location::RegisterPairLocation(
3341 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3342 // Runtime helper puts the result in EAX, EDX.
3343 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003344 break;
3345 }
3346 case Primitive::kPrimFloat:
3347 case Primitive::kPrimDouble: {
3348 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003349 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003350 locations->SetOut(Location::SameAsFirstInput());
3351 break;
3352 }
3353
3354 default:
3355 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3356 }
3357}
3358
3359void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3360 LocationSummary* locations = div->GetLocations();
3361 Location first = locations->InAt(0);
3362 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003363
3364 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003365 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003366 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003367 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003368 break;
3369 }
3370
3371 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003372 if (second.IsFpuRegister()) {
3373 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3374 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3375 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3376 DCHECK(!const_area->NeedsMaterialization());
3377 __ divss(first.AsFpuRegister<XmmRegister>(),
3378 codegen_->LiteralFloatAddress(
3379 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3380 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3381 } else {
3382 DCHECK(second.IsStackSlot());
3383 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3384 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003385 break;
3386 }
3387
3388 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003389 if (second.IsFpuRegister()) {
3390 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3391 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3392 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3393 DCHECK(!const_area->NeedsMaterialization());
3394 __ divsd(first.AsFpuRegister<XmmRegister>(),
3395 codegen_->LiteralDoubleAddress(
3396 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3397 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3398 } else {
3399 DCHECK(second.IsDoubleStackSlot());
3400 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3401 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003402 break;
3403 }
3404
3405 default:
3406 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3407 }
3408}
3409
Calin Juravlebacfec32014-11-14 15:54:36 +00003410void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003411 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003412
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003413 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3414 ? LocationSummary::kCall
3415 : LocationSummary::kNoCall;
3416 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003417
Calin Juravled2ec87d2014-12-08 14:24:46 +00003418 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003419 case Primitive::kPrimInt: {
3420 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003421 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003422 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3424 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3425 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003426 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003427 locations->AddTemp(Location::RequiresRegister());
3428 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003429 break;
3430 }
3431 case Primitive::kPrimLong: {
3432 InvokeRuntimeCallingConvention calling_convention;
3433 locations->SetInAt(0, Location::RegisterPairLocation(
3434 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3435 locations->SetInAt(1, Location::RegisterPairLocation(
3436 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3437 // Runtime helper puts the result in EAX, EDX.
3438 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3439 break;
3440 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003441 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003442 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003443 locations->SetInAt(0, Location::Any());
3444 locations->SetInAt(1, Location::Any());
3445 locations->SetOut(Location::RequiresFpuRegister());
3446 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003447 break;
3448 }
3449
3450 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003451 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003452 }
3453}
3454
3455void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3456 Primitive::Type type = rem->GetResultType();
3457 switch (type) {
3458 case Primitive::kPrimInt:
3459 case Primitive::kPrimLong: {
3460 GenerateDivRemIntegral(rem);
3461 break;
3462 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003463 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003464 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003465 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 break;
3467 }
3468 default:
3469 LOG(FATAL) << "Unexpected rem type " << type;
3470 }
3471}
3472
Calin Juravled0d48522014-11-04 16:40:20 +00003473void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003474 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3475 ? LocationSummary::kCallOnSlowPath
3476 : LocationSummary::kNoCall;
3477 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003478 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003479 case Primitive::kPrimByte:
3480 case Primitive::kPrimChar:
3481 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003482 case Primitive::kPrimInt: {
3483 locations->SetInAt(0, Location::Any());
3484 break;
3485 }
3486 case Primitive::kPrimLong: {
3487 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3488 if (!instruction->IsConstant()) {
3489 locations->AddTemp(Location::RequiresRegister());
3490 }
3491 break;
3492 }
3493 default:
3494 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3495 }
Calin Juravled0d48522014-11-04 16:40:20 +00003496 if (instruction->HasUses()) {
3497 locations->SetOut(Location::SameAsFirstInput());
3498 }
3499}
3500
3501void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003502 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003503 codegen_->AddSlowPath(slow_path);
3504
3505 LocationSummary* locations = instruction->GetLocations();
3506 Location value = locations->InAt(0);
3507
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003508 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003509 case Primitive::kPrimByte:
3510 case Primitive::kPrimChar:
3511 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003512 case Primitive::kPrimInt: {
3513 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003514 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003515 __ j(kEqual, slow_path->GetEntryLabel());
3516 } else if (value.IsStackSlot()) {
3517 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3518 __ j(kEqual, slow_path->GetEntryLabel());
3519 } else {
3520 DCHECK(value.IsConstant()) << value;
3521 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3522 __ jmp(slow_path->GetEntryLabel());
3523 }
3524 }
3525 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003526 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003527 case Primitive::kPrimLong: {
3528 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003529 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003530 __ movl(temp, value.AsRegisterPairLow<Register>());
3531 __ orl(temp, value.AsRegisterPairHigh<Register>());
3532 __ j(kEqual, slow_path->GetEntryLabel());
3533 } else {
3534 DCHECK(value.IsConstant()) << value;
3535 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3536 __ jmp(slow_path->GetEntryLabel());
3537 }
3538 }
3539 break;
3540 }
3541 default:
3542 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003543 }
Calin Juravled0d48522014-11-04 16:40:20 +00003544}
3545
Calin Juravle9aec02f2014-11-18 23:06:35 +00003546void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3547 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3548
3549 LocationSummary* locations =
3550 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3551
3552 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003553 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003554 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003555 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003556 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003557 // The shift count needs to be in CL or a constant.
3558 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003559 locations->SetOut(Location::SameAsFirstInput());
3560 break;
3561 }
3562 default:
3563 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3564 }
3565}
3566
3567void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3568 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3569
3570 LocationSummary* locations = op->GetLocations();
3571 Location first = locations->InAt(0);
3572 Location second = locations->InAt(1);
3573 DCHECK(first.Equals(locations->Out()));
3574
3575 switch (op->GetResultType()) {
3576 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003577 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003578 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003579 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003580 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003581 DCHECK_EQ(ECX, second_reg);
3582 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003583 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003584 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003585 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003586 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003587 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003588 }
3589 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003590 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3591 if (shift == 0) {
3592 return;
3593 }
3594 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003595 if (op->IsShl()) {
3596 __ shll(first_reg, imm);
3597 } else if (op->IsShr()) {
3598 __ sarl(first_reg, imm);
3599 } else {
3600 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003601 }
3602 }
3603 break;
3604 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003605 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003606 if (second.IsRegister()) {
3607 Register second_reg = second.AsRegister<Register>();
3608 DCHECK_EQ(ECX, second_reg);
3609 if (op->IsShl()) {
3610 GenerateShlLong(first, second_reg);
3611 } else if (op->IsShr()) {
3612 GenerateShrLong(first, second_reg);
3613 } else {
3614 GenerateUShrLong(first, second_reg);
3615 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003616 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003617 // Shift by a constant.
3618 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3619 // Nothing to do if the shift is 0, as the input is already the output.
3620 if (shift != 0) {
3621 if (op->IsShl()) {
3622 GenerateShlLong(first, shift);
3623 } else if (op->IsShr()) {
3624 GenerateShrLong(first, shift);
3625 } else {
3626 GenerateUShrLong(first, shift);
3627 }
3628 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003629 }
3630 break;
3631 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003632 default:
3633 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3634 }
3635}
3636
Mark P Mendell73945692015-04-29 14:56:17 +00003637void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3638 Register low = loc.AsRegisterPairLow<Register>();
3639 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003640 if (shift == 1) {
3641 // This is just an addition.
3642 __ addl(low, low);
3643 __ adcl(high, high);
3644 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003645 // Shift by 32 is easy. High gets low, and low gets 0.
3646 codegen_->EmitParallelMoves(
3647 loc.ToLow(),
3648 loc.ToHigh(),
3649 Primitive::kPrimInt,
3650 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3651 loc.ToLow(),
3652 Primitive::kPrimInt);
3653 } else if (shift > 32) {
3654 // Low part becomes 0. High part is low part << (shift-32).
3655 __ movl(high, low);
3656 __ shll(high, Immediate(shift - 32));
3657 __ xorl(low, low);
3658 } else {
3659 // Between 1 and 31.
3660 __ shld(high, low, Immediate(shift));
3661 __ shll(low, Immediate(shift));
3662 }
3663}
3664
Calin Juravle9aec02f2014-11-18 23:06:35 +00003665void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003666 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003667 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3668 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3669 __ testl(shifter, Immediate(32));
3670 __ j(kEqual, &done);
3671 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3672 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3673 __ Bind(&done);
3674}
3675
Mark P Mendell73945692015-04-29 14:56:17 +00003676void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3677 Register low = loc.AsRegisterPairLow<Register>();
3678 Register high = loc.AsRegisterPairHigh<Register>();
3679 if (shift == 32) {
3680 // Need to copy the sign.
3681 DCHECK_NE(low, high);
3682 __ movl(low, high);
3683 __ sarl(high, Immediate(31));
3684 } else if (shift > 32) {
3685 DCHECK_NE(low, high);
3686 // High part becomes sign. Low part is shifted by shift - 32.
3687 __ movl(low, high);
3688 __ sarl(high, Immediate(31));
3689 __ sarl(low, Immediate(shift - 32));
3690 } else {
3691 // Between 1 and 31.
3692 __ shrd(low, high, Immediate(shift));
3693 __ sarl(high, Immediate(shift));
3694 }
3695}
3696
Calin Juravle9aec02f2014-11-18 23:06:35 +00003697void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003698 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003699 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3700 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3701 __ testl(shifter, Immediate(32));
3702 __ j(kEqual, &done);
3703 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3704 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3705 __ Bind(&done);
3706}
3707
Mark P Mendell73945692015-04-29 14:56:17 +00003708void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3709 Register low = loc.AsRegisterPairLow<Register>();
3710 Register high = loc.AsRegisterPairHigh<Register>();
3711 if (shift == 32) {
3712 // Shift by 32 is easy. Low gets high, and high gets 0.
3713 codegen_->EmitParallelMoves(
3714 loc.ToHigh(),
3715 loc.ToLow(),
3716 Primitive::kPrimInt,
3717 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3718 loc.ToHigh(),
3719 Primitive::kPrimInt);
3720 } else if (shift > 32) {
3721 // Low part is high >> (shift - 32). High part becomes 0.
3722 __ movl(low, high);
3723 __ shrl(low, Immediate(shift - 32));
3724 __ xorl(high, high);
3725 } else {
3726 // Between 1 and 31.
3727 __ shrd(low, high, Immediate(shift));
3728 __ shrl(high, Immediate(shift));
3729 }
3730}
3731
Calin Juravle9aec02f2014-11-18 23:06:35 +00003732void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003733 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003734 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3735 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3736 __ testl(shifter, Immediate(32));
3737 __ j(kEqual, &done);
3738 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3739 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3740 __ Bind(&done);
3741}
3742
3743void LocationsBuilderX86::VisitShl(HShl* shl) {
3744 HandleShift(shl);
3745}
3746
3747void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3748 HandleShift(shl);
3749}
3750
3751void LocationsBuilderX86::VisitShr(HShr* shr) {
3752 HandleShift(shr);
3753}
3754
3755void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3756 HandleShift(shr);
3757}
3758
3759void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3760 HandleShift(ushr);
3761}
3762
3763void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3764 HandleShift(ushr);
3765}
3766
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003767void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003768 LocationSummary* locations =
3769 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003770 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003771 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003772 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003773 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003774}
3775
3776void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3777 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003778 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003779 // Note: if heap poisoning is enabled, the entry point takes cares
3780 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003781 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3782 instruction,
3783 instruction->GetDexPc(),
3784 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003785 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003786}
3787
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003788void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3789 LocationSummary* locations =
3790 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3791 locations->SetOut(Location::RegisterLocation(EAX));
3792 InvokeRuntimeCallingConvention calling_convention;
3793 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003794 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003795 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003796}
3797
3798void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3799 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003800 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3801
Roland Levillain4d027112015-07-01 15:41:14 +01003802 // Note: if heap poisoning is enabled, the entry point takes cares
3803 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003804 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3805 instruction,
3806 instruction->GetDexPc(),
3807 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003808 DCHECK(!codegen_->IsLeafMethod());
3809}
3810
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003811void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003812 LocationSummary* locations =
3813 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003814 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3815 if (location.IsStackSlot()) {
3816 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3817 } else if (location.IsDoubleStackSlot()) {
3818 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003819 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003820 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003821}
3822
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003823void InstructionCodeGeneratorX86::VisitParameterValue(
3824 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3825}
3826
3827void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3828 LocationSummary* locations =
3829 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3830 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3831}
3832
3833void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003834}
3835
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003836void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003837 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003838 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003839 locations->SetInAt(0, Location::RequiresRegister());
3840 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003841}
3842
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003843void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3844 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003845 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003846 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003847 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003848 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003849 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003850 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003851 break;
3852
3853 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003854 __ notl(out.AsRegisterPairLow<Register>());
3855 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003856 break;
3857
3858 default:
3859 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3860 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003861}
3862
David Brazdil66d126e2015-04-03 16:02:44 +01003863void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3864 LocationSummary* locations =
3865 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3866 locations->SetInAt(0, Location::RequiresRegister());
3867 locations->SetOut(Location::SameAsFirstInput());
3868}
3869
3870void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003871 LocationSummary* locations = bool_not->GetLocations();
3872 Location in = locations->InAt(0);
3873 Location out = locations->Out();
3874 DCHECK(in.Equals(out));
3875 __ xorl(out.AsRegister<Register>(), Immediate(1));
3876}
3877
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003878void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003879 LocationSummary* locations =
3880 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003881 switch (compare->InputAt(0)->GetType()) {
3882 case Primitive::kPrimLong: {
3883 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003884 locations->SetInAt(1, Location::Any());
3885 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3886 break;
3887 }
3888 case Primitive::kPrimFloat:
3889 case Primitive::kPrimDouble: {
3890 locations->SetInAt(0, Location::RequiresFpuRegister());
3891 locations->SetInAt(1, Location::RequiresFpuRegister());
3892 locations->SetOut(Location::RequiresRegister());
3893 break;
3894 }
3895 default:
3896 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3897 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003898}
3899
3900void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003901 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003902 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003903 Location left = locations->InAt(0);
3904 Location right = locations->InAt(1);
3905
Mark Mendell0c9497d2015-08-21 09:30:05 -04003906 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003907 switch (compare->InputAt(0)->GetType()) {
3908 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003909 Register left_low = left.AsRegisterPairLow<Register>();
3910 Register left_high = left.AsRegisterPairHigh<Register>();
3911 int32_t val_low = 0;
3912 int32_t val_high = 0;
3913 bool right_is_const = false;
3914
3915 if (right.IsConstant()) {
3916 DCHECK(right.GetConstant()->IsLongConstant());
3917 right_is_const = true;
3918 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3919 val_low = Low32Bits(val);
3920 val_high = High32Bits(val);
3921 }
3922
Calin Juravleddb7df22014-11-25 20:56:51 +00003923 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003924 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003925 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003926 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003927 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003928 DCHECK(right_is_const) << right;
3929 if (val_high == 0) {
3930 __ testl(left_high, left_high);
3931 } else {
3932 __ cmpl(left_high, Immediate(val_high));
3933 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003934 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003935 __ j(kLess, &less); // Signed compare.
3936 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003937 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003938 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003939 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003940 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003941 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003942 DCHECK(right_is_const) << right;
3943 if (val_low == 0) {
3944 __ testl(left_low, left_low);
3945 } else {
3946 __ cmpl(left_low, Immediate(val_low));
3947 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003948 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003949 break;
3950 }
3951 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003952 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003953 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3954 break;
3955 }
3956 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003957 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003958 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003959 break;
3960 }
3961 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003962 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003963 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003964 __ movl(out, Immediate(0));
3965 __ j(kEqual, &done);
3966 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3967
3968 __ Bind(&greater);
3969 __ movl(out, Immediate(1));
3970 __ jmp(&done);
3971
3972 __ Bind(&less);
3973 __ movl(out, Immediate(-1));
3974
3975 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003976}
3977
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003978void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003979 LocationSummary* locations =
3980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003981 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3982 locations->SetInAt(i, Location::Any());
3983 }
3984 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003985}
3986
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003987void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003988 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003989}
3990
Calin Juravle52c48962014-12-16 17:02:57 +00003991void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3992 /*
3993 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3994 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3995 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3996 */
3997 switch (kind) {
3998 case MemBarrierKind::kAnyAny: {
3999 __ mfence();
4000 break;
4001 }
4002 case MemBarrierKind::kAnyStore:
4003 case MemBarrierKind::kLoadAny:
4004 case MemBarrierKind::kStoreStore: {
4005 // nop
4006 break;
4007 }
4008 default:
4009 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004010 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004011}
4012
Vladimir Markodc151b22015-10-15 18:02:30 +01004013HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4014 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4015 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004016 switch (desired_dispatch_info.code_ptr_location) {
4017 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4018 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4019 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4020 // (Though the direct CALL ptr16:32 is available for consideration).
4021 return HInvokeStaticOrDirect::DispatchInfo {
4022 desired_dispatch_info.method_load_kind,
4023 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4024 desired_dispatch_info.method_load_data,
4025 0u
4026 };
4027 default:
4028 return desired_dispatch_info;
4029 }
4030}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004031
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004032Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4033 Register temp) {
4034 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4035 Location location = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4036 if (!invoke->GetLocations()->Intrinsified()) {
4037 return location.AsRegister<Register>();
4038 }
4039 // For intrinsics we allow any location, so it may be on the stack.
4040 if (!location.IsRegister()) {
4041 __ movl(temp, Address(ESP, location.GetStackIndex()));
4042 return temp;
4043 }
4044 // For register locations, check if the register was saved. If so, get it from the stack.
4045 // Note: There is a chance that the register was saved but not overwritten, so we could
4046 // save one load. However, since this is just an intrinsic slow path we prefer this
4047 // simple and more robust approach rather that trying to determine if that's the case.
4048 SlowPathCode* slow_path = GetCurrentSlowPath();
4049 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4050 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4051 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4052 __ movl(temp, Address(ESP, stack_offset));
4053 return temp;
4054 }
4055 return location.AsRegister<Register>();
4056}
4057
Vladimir Marko58155012015-08-19 12:49:41 +00004058void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4059 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4060 switch (invoke->GetMethodLoadKind()) {
4061 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4062 // temp = thread->string_init_entrypoint
4063 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4064 break;
4065 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
4066 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4067 break;
4068 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4069 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4070 break;
4071 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4072 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4073 method_patches_.emplace_back(invoke->GetTargetMethod());
4074 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4075 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004076 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4077 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4078 temp.AsRegister<Register>());
4079 uint32_t offset = invoke->GetDexCacheArrayOffset();
4080 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4081 // Add the patch entry and bind its label at the end of the instruction.
4082 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4083 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4084 break;
4085 }
Vladimir Marko58155012015-08-19 12:49:41 +00004086 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
4087 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4088 Register method_reg;
4089 Register reg = temp.AsRegister<Register>();
4090 if (current_method.IsRegister()) {
4091 method_reg = current_method.AsRegister<Register>();
4092 } else {
4093 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4094 DCHECK(!current_method.IsValid());
4095 method_reg = reg;
4096 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4097 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004098 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004099 __ movl(reg, Address(method_reg,
4100 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004101 // temp = temp[index_in_cache]
4102 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4103 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4104 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004105 }
Vladimir Marko58155012015-08-19 12:49:41 +00004106 }
4107
4108 switch (invoke->GetCodePtrLocation()) {
4109 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4110 __ call(GetFrameEntryLabel());
4111 break;
4112 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4113 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4114 Label* label = &relative_call_patches_.back().label;
4115 __ call(label); // Bind to the patch label, override at link time.
4116 __ Bind(label); // Bind the label at the end of the "call" insn.
4117 break;
4118 }
4119 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4120 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004121 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4122 LOG(FATAL) << "Unsupported";
4123 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004124 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4125 // (callee_method + offset_of_quick_compiled_code)()
4126 __ call(Address(callee_method.AsRegister<Register>(),
4127 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4128 kX86WordSize).Int32Value()));
4129 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004130 }
4131
4132 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004133}
4134
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004135void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4136 Register temp = temp_in.AsRegister<Register>();
4137 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4138 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
4139 LocationSummary* locations = invoke->GetLocations();
4140 Location receiver = locations->InAt(0);
4141 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004142 DCHECK(receiver.IsRegister());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004143 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004144 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
4145 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004146 // Instead of simply (possibly) unpoisoning `temp` here, we should
4147 // emit a read barrier for the previous class reference load.
4148 // However this is not required in practice, as this is an
4149 // intermediate/temporary reference and because the current
4150 // concurrent copying collector keeps the from-space memory
4151 // intact/accessible until the end of the marking phase (the
4152 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004153 __ MaybeUnpoisonHeapReference(temp);
4154 // temp = temp->GetMethodAt(method_offset);
4155 __ movl(temp, Address(temp, method_offset));
4156 // call temp->GetEntryPoint();
4157 __ call(Address(
4158 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4159}
4160
Vladimir Marko58155012015-08-19 12:49:41 +00004161void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4162 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004163 size_t size =
4164 method_patches_.size() +
4165 relative_call_patches_.size() +
4166 pc_relative_dex_cache_patches_.size();
4167 linker_patches->reserve(size);
4168 // The label points to the end of the "movl" insn but the literal offset for method
4169 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4170 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004171 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004172 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004173 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4174 info.target_method.dex_file,
4175 info.target_method.dex_method_index));
4176 }
4177 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004178 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004179 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4180 info.target_method.dex_file,
4181 info.target_method.dex_method_index));
4182 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004183 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4184 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4185 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4186 &info.target_dex_file,
4187 GetMethodAddressOffset(),
4188 info.element_offset));
4189 }
Vladimir Marko58155012015-08-19 12:49:41 +00004190}
4191
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004192void CodeGeneratorX86::MarkGCCard(Register temp,
4193 Register card,
4194 Register object,
4195 Register value,
4196 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004197 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004198 if (value_can_be_null) {
4199 __ testl(value, value);
4200 __ j(kEqual, &is_null);
4201 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004202 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4203 __ movl(temp, object);
4204 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004205 __ movb(Address(temp, card, TIMES_1, 0),
4206 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004207 if (value_can_be_null) {
4208 __ Bind(&is_null);
4209 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004210}
4211
Calin Juravle52c48962014-12-16 17:02:57 +00004212void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4213 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004214
4215 bool object_field_get_with_read_barrier =
4216 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004217 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004218 new (GetGraph()->GetArena()) LocationSummary(instruction,
4219 kEmitCompilerReadBarrier ?
4220 LocationSummary::kCallOnSlowPath :
4221 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004222 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004223
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004224 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4225 locations->SetOut(Location::RequiresFpuRegister());
4226 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004227 // The output overlaps in case of long: we don't want the low move
4228 // to overwrite the object's location. Likewise, in the case of
4229 // an object field get with read barriers enabled, we do not want
4230 // the move to overwrite the object's location, as we need it to emit
4231 // the read barrier.
4232 locations->SetOut(
4233 Location::RequiresRegister(),
4234 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4235 Location::kOutputOverlap :
4236 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004237 }
Calin Juravle52c48962014-12-16 17:02:57 +00004238
4239 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4240 // Long values can be loaded atomically into an XMM using movsd.
4241 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
4242 // and then copy the XMM into the output 32bits at a time).
4243 locations->AddTemp(Location::RequiresFpuRegister());
4244 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004245}
4246
Calin Juravle52c48962014-12-16 17:02:57 +00004247void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4248 const FieldInfo& field_info) {
4249 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004250
Calin Juravle52c48962014-12-16 17:02:57 +00004251 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004252 Location base_loc = locations->InAt(0);
4253 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004254 Location out = locations->Out();
4255 bool is_volatile = field_info.IsVolatile();
4256 Primitive::Type field_type = field_info.GetFieldType();
4257 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4258
4259 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004260 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004261 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262 break;
4263 }
4264
4265 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004266 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267 break;
4268 }
4269
4270 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004271 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272 break;
4273 }
4274
4275 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004276 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004277 break;
4278 }
4279
4280 case Primitive::kPrimInt:
4281 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00004282 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283 break;
4284 }
4285
4286 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004287 if (is_volatile) {
4288 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4289 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004291 __ movd(out.AsRegisterPairLow<Register>(), temp);
4292 __ psrlq(temp, Immediate(32));
4293 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4294 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004295 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004296 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004297 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004298 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4299 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 break;
4301 }
4302
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004303 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004304 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004305 break;
4306 }
4307
4308 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004309 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004310 break;
4311 }
4312
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004313 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004314 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004315 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004316 }
Calin Juravle52c48962014-12-16 17:02:57 +00004317
Calin Juravle77520bc2015-01-12 18:45:46 +00004318 // Longs are handled in the switch.
4319 if (field_type != Primitive::kPrimLong) {
4320 codegen_->MaybeRecordImplicitNullCheck(instruction);
4321 }
4322
Calin Juravle52c48962014-12-16 17:02:57 +00004323 if (is_volatile) {
4324 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4325 }
Roland Levillain4d027112015-07-01 15:41:14 +01004326
4327 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004328 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004329 }
Calin Juravle52c48962014-12-16 17:02:57 +00004330}
4331
4332void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4333 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4334
4335 LocationSummary* locations =
4336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4337 locations->SetInAt(0, Location::RequiresRegister());
4338 bool is_volatile = field_info.IsVolatile();
4339 Primitive::Type field_type = field_info.GetFieldType();
4340 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4341 || (field_type == Primitive::kPrimByte);
4342
4343 // The register allocator does not support multiple
4344 // inputs that die at entry with one in a specific register.
4345 if (is_byte_type) {
4346 // Ensure the value is in a byte register.
4347 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004348 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004349 if (is_volatile && field_type == Primitive::kPrimDouble) {
4350 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4351 locations->SetInAt(1, Location::RequiresFpuRegister());
4352 } else {
4353 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4354 }
4355 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4356 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004357 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004358
Calin Juravle52c48962014-12-16 17:02:57 +00004359 // 64bits value can be atomically written to an address with movsd and an XMM register.
4360 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4361 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4362 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4363 // isolated cases when we need this it isn't worth adding the extra complexity.
4364 locations->AddTemp(Location::RequiresFpuRegister());
4365 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004366 } else {
4367 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4368
4369 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4370 // Temporary registers for the write barrier.
4371 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4372 // Ensure the card is in a byte register.
4373 locations->AddTemp(Location::RegisterLocation(ECX));
4374 }
Calin Juravle52c48962014-12-16 17:02:57 +00004375 }
4376}
4377
4378void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004379 const FieldInfo& field_info,
4380 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004381 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4382
4383 LocationSummary* locations = instruction->GetLocations();
4384 Register base = locations->InAt(0).AsRegister<Register>();
4385 Location value = locations->InAt(1);
4386 bool is_volatile = field_info.IsVolatile();
4387 Primitive::Type field_type = field_info.GetFieldType();
4388 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004389 bool needs_write_barrier =
4390 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004391
4392 if (is_volatile) {
4393 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4394 }
4395
Mark Mendell81489372015-11-04 11:30:41 -05004396 bool maybe_record_implicit_null_check_done = false;
4397
Calin Juravle52c48962014-12-16 17:02:57 +00004398 switch (field_type) {
4399 case Primitive::kPrimBoolean:
4400 case Primitive::kPrimByte: {
4401 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4402 break;
4403 }
4404
4405 case Primitive::kPrimShort:
4406 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004407 if (value.IsConstant()) {
4408 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4409 __ movw(Address(base, offset), Immediate(v));
4410 } else {
4411 __ movw(Address(base, offset), value.AsRegister<Register>());
4412 }
Calin Juravle52c48962014-12-16 17:02:57 +00004413 break;
4414 }
4415
4416 case Primitive::kPrimInt:
4417 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004418 if (kPoisonHeapReferences && needs_write_barrier) {
4419 // Note that in the case where `value` is a null reference,
4420 // we do not enter this block, as the reference does not
4421 // need poisoning.
4422 DCHECK_EQ(field_type, Primitive::kPrimNot);
4423 Register temp = locations->GetTemp(0).AsRegister<Register>();
4424 __ movl(temp, value.AsRegister<Register>());
4425 __ PoisonHeapReference(temp);
4426 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004427 } else if (value.IsConstant()) {
4428 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4429 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004430 } else {
4431 __ movl(Address(base, offset), value.AsRegister<Register>());
4432 }
Calin Juravle52c48962014-12-16 17:02:57 +00004433 break;
4434 }
4435
4436 case Primitive::kPrimLong: {
4437 if (is_volatile) {
4438 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4439 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4440 __ movd(temp1, value.AsRegisterPairLow<Register>());
4441 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4442 __ punpckldq(temp1, temp2);
4443 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004444 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004445 } else if (value.IsConstant()) {
4446 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4447 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4448 codegen_->MaybeRecordImplicitNullCheck(instruction);
4449 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004450 } else {
4451 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004452 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004453 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4454 }
Mark Mendell81489372015-11-04 11:30:41 -05004455 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004456 break;
4457 }
4458
4459 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004460 if (value.IsConstant()) {
4461 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4462 __ movl(Address(base, offset), Immediate(v));
4463 } else {
4464 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4465 }
Calin Juravle52c48962014-12-16 17:02:57 +00004466 break;
4467 }
4468
4469 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004470 if (value.IsConstant()) {
4471 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4472 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4473 codegen_->MaybeRecordImplicitNullCheck(instruction);
4474 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4475 maybe_record_implicit_null_check_done = true;
4476 } else {
4477 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4478 }
Calin Juravle52c48962014-12-16 17:02:57 +00004479 break;
4480 }
4481
4482 case Primitive::kPrimVoid:
4483 LOG(FATAL) << "Unreachable type " << field_type;
4484 UNREACHABLE();
4485 }
4486
Mark Mendell81489372015-11-04 11:30:41 -05004487 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004488 codegen_->MaybeRecordImplicitNullCheck(instruction);
4489 }
4490
Roland Levillain4d027112015-07-01 15:41:14 +01004491 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004492 Register temp = locations->GetTemp(0).AsRegister<Register>();
4493 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004494 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004495 }
4496
Calin Juravle52c48962014-12-16 17:02:57 +00004497 if (is_volatile) {
4498 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4499 }
4500}
4501
4502void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4503 HandleFieldGet(instruction, instruction->GetFieldInfo());
4504}
4505
4506void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4507 HandleFieldGet(instruction, instruction->GetFieldInfo());
4508}
4509
4510void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4511 HandleFieldSet(instruction, instruction->GetFieldInfo());
4512}
4513
4514void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004515 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004516}
4517
4518void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4519 HandleFieldSet(instruction, instruction->GetFieldInfo());
4520}
4521
4522void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004523 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004524}
4525
4526void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4527 HandleFieldGet(instruction, instruction->GetFieldInfo());
4528}
4529
4530void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4531 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004532}
4533
Calin Juravlee460d1d2015-09-29 04:52:17 +01004534void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4535 HUnresolvedInstanceFieldGet* instruction) {
4536 FieldAccessCallingConventionX86 calling_convention;
4537 codegen_->CreateUnresolvedFieldLocationSummary(
4538 instruction, instruction->GetFieldType(), calling_convention);
4539}
4540
4541void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4542 HUnresolvedInstanceFieldGet* instruction) {
4543 FieldAccessCallingConventionX86 calling_convention;
4544 codegen_->GenerateUnresolvedFieldAccess(instruction,
4545 instruction->GetFieldType(),
4546 instruction->GetFieldIndex(),
4547 instruction->GetDexPc(),
4548 calling_convention);
4549}
4550
4551void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4552 HUnresolvedInstanceFieldSet* instruction) {
4553 FieldAccessCallingConventionX86 calling_convention;
4554 codegen_->CreateUnresolvedFieldLocationSummary(
4555 instruction, instruction->GetFieldType(), calling_convention);
4556}
4557
4558void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4559 HUnresolvedInstanceFieldSet* instruction) {
4560 FieldAccessCallingConventionX86 calling_convention;
4561 codegen_->GenerateUnresolvedFieldAccess(instruction,
4562 instruction->GetFieldType(),
4563 instruction->GetFieldIndex(),
4564 instruction->GetDexPc(),
4565 calling_convention);
4566}
4567
4568void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4569 HUnresolvedStaticFieldGet* instruction) {
4570 FieldAccessCallingConventionX86 calling_convention;
4571 codegen_->CreateUnresolvedFieldLocationSummary(
4572 instruction, instruction->GetFieldType(), calling_convention);
4573}
4574
4575void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4576 HUnresolvedStaticFieldGet* instruction) {
4577 FieldAccessCallingConventionX86 calling_convention;
4578 codegen_->GenerateUnresolvedFieldAccess(instruction,
4579 instruction->GetFieldType(),
4580 instruction->GetFieldIndex(),
4581 instruction->GetDexPc(),
4582 calling_convention);
4583}
4584
4585void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4586 HUnresolvedStaticFieldSet* instruction) {
4587 FieldAccessCallingConventionX86 calling_convention;
4588 codegen_->CreateUnresolvedFieldLocationSummary(
4589 instruction, instruction->GetFieldType(), calling_convention);
4590}
4591
4592void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4593 HUnresolvedStaticFieldSet* instruction) {
4594 FieldAccessCallingConventionX86 calling_convention;
4595 codegen_->GenerateUnresolvedFieldAccess(instruction,
4596 instruction->GetFieldType(),
4597 instruction->GetFieldIndex(),
4598 instruction->GetDexPc(),
4599 calling_convention);
4600}
4601
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004602void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004603 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4604 ? LocationSummary::kCallOnSlowPath
4605 : LocationSummary::kNoCall;
4606 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4607 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004608 ? Location::RequiresRegister()
4609 : Location::Any();
4610 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004611 if (instruction->HasUses()) {
4612 locations->SetOut(Location::SameAsFirstInput());
4613 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614}
4615
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004616void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004617 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4618 return;
4619 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004620 LocationSummary* locations = instruction->GetLocations();
4621 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004622
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004623 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4624 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4625}
4626
4627void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004628 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004629 codegen_->AddSlowPath(slow_path);
4630
4631 LocationSummary* locations = instruction->GetLocations();
4632 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004633
4634 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004635 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004636 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004637 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004638 } else {
4639 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004640 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004641 __ jmp(slow_path->GetEntryLabel());
4642 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004643 }
4644 __ j(kEqual, slow_path->GetEntryLabel());
4645}
4646
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004647void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004648 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004649 GenerateImplicitNullCheck(instruction);
4650 } else {
4651 GenerateExplicitNullCheck(instruction);
4652 }
4653}
4654
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004655void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004656 bool object_array_get_with_read_barrier =
4657 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004658 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004659 new (GetGraph()->GetArena()) LocationSummary(instruction,
4660 object_array_get_with_read_barrier ?
4661 LocationSummary::kCallOnSlowPath :
4662 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004663 locations->SetInAt(0, Location::RequiresRegister());
4664 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004665 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4666 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4667 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004668 // The output overlaps in case of long: we don't want the low move
4669 // to overwrite the array's location. Likewise, in the case of an
4670 // object array get with read barriers enabled, we do not want the
4671 // move to overwrite the array's location, as we need it to emit
4672 // the read barrier.
4673 locations->SetOut(
4674 Location::RequiresRegister(),
4675 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4676 Location::kOutputOverlap :
4677 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004678 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004679}
4680
4681void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4682 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004683 Location obj_loc = locations->InAt(0);
4684 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004685 Location index = locations->InAt(1);
4686
Calin Juravle77520bc2015-01-12 18:45:46 +00004687 Primitive::Type type = instruction->GetType();
4688 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004689 case Primitive::kPrimBoolean: {
4690 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004691 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004692 if (index.IsConstant()) {
4693 __ movzxb(out, Address(obj,
4694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4695 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004696 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004697 }
4698 break;
4699 }
4700
4701 case Primitive::kPrimByte: {
4702 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004703 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004704 if (index.IsConstant()) {
4705 __ movsxb(out, Address(obj,
4706 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4707 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004708 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004709 }
4710 break;
4711 }
4712
4713 case Primitive::kPrimShort: {
4714 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004715 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004716 if (index.IsConstant()) {
4717 __ movsxw(out, Address(obj,
4718 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4719 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004720 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 }
4722 break;
4723 }
4724
4725 case Primitive::kPrimChar: {
4726 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004727 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004728 if (index.IsConstant()) {
4729 __ movzxw(out, Address(obj,
4730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4731 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004732 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004733 }
4734 break;
4735 }
4736
4737 case Primitive::kPrimInt:
4738 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004739 static_assert(
4740 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4741 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004742 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004743 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744 if (index.IsConstant()) {
4745 __ movl(out, Address(obj,
4746 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4747 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004748 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004749 }
4750 break;
4751 }
4752
4753 case Primitive::kPrimLong: {
4754 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004755 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004756 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004757 if (index.IsConstant()) {
4758 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004759 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004760 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004761 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004762 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004763 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004764 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004765 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004766 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004767 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004768 }
4769 break;
4770 }
4771
Mark Mendell7c8d0092015-01-26 11:21:33 -05004772 case Primitive::kPrimFloat: {
4773 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4774 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4775 if (index.IsConstant()) {
4776 __ movss(out, Address(obj,
4777 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4778 } else {
4779 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4780 }
4781 break;
4782 }
4783
4784 case Primitive::kPrimDouble: {
4785 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4786 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4787 if (index.IsConstant()) {
4788 __ movsd(out, Address(obj,
4789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4790 } else {
4791 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4792 }
4793 break;
4794 }
4795
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004796 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004797 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004798 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004799 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004800
4801 if (type != Primitive::kPrimLong) {
4802 codegen_->MaybeRecordImplicitNullCheck(instruction);
4803 }
Roland Levillain4d027112015-07-01 15:41:14 +01004804
4805 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004806 static_assert(
4807 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4808 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4809 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4810 Location out = locations->Out();
4811 if (index.IsConstant()) {
4812 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4813 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4814 } else {
4815 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4816 }
Roland Levillain4d027112015-07-01 15:41:14 +01004817 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818}
4819
4820void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004821 // This location builder might end up asking to up to four registers, which is
4822 // not currently possible for baseline. The situation in which we need four
4823 // registers cannot be met by baseline though, because it has not run any
4824 // optimization.
4825
Nicolas Geoffray39468442014-09-02 15:17:15 +01004826 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004827
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004828 bool needs_write_barrier =
4829 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004830 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4831 bool object_array_set_with_read_barrier =
4832 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004833
Nicolas Geoffray39468442014-09-02 15:17:15 +01004834 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4835 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004836 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4837 LocationSummary::kCallOnSlowPath :
4838 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004839
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004840 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4841 || (value_type == Primitive::kPrimByte);
4842 // We need the inputs to be different than the output in case of long operation.
4843 // In case of a byte operation, the register allocator does not support multiple
4844 // inputs that die at entry with one in a specific register.
4845 locations->SetInAt(0, Location::RequiresRegister());
4846 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4847 if (is_byte_type) {
4848 // Ensure the value is in a byte register.
4849 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4850 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004851 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004852 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004853 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4854 }
4855 if (needs_write_barrier) {
4856 // Temporary registers for the write barrier.
4857 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4858 // Ensure the card is in a byte register.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004859 locations->AddTemp(Location::RegisterLocation(ECX)); // Possibly used for read barrier too.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004860 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004861}
4862
4863void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4864 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004865 Location array_loc = locations->InAt(0);
4866 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004867 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004868 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004869 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004870 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4871 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4872 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004873 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004874 bool needs_write_barrier =
4875 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004876
4877 switch (value_type) {
4878 case Primitive::kPrimBoolean:
4879 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004880 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4881 Address address = index.IsConstant()
4882 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4883 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4884 if (value.IsRegister()) {
4885 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004887 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004889 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 break;
4891 }
4892
4893 case Primitive::kPrimShort:
4894 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004895 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4896 Address address = index.IsConstant()
4897 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4898 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4899 if (value.IsRegister()) {
4900 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004901 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004902 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004903 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004904 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 break;
4906 }
4907
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004908 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004909 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4910 Address address = index.IsConstant()
4911 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4912 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004913
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004914 if (!value.IsRegister()) {
4915 // Just setting null.
4916 DCHECK(instruction->InputAt(2)->IsNullConstant());
4917 DCHECK(value.IsConstant()) << value;
4918 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004919 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004920 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004921 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004922 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924
4925 DCHECK(needs_write_barrier);
4926 Register register_value = value.AsRegister<Register>();
4927 NearLabel done, not_null, do_put;
4928 SlowPathCode* slow_path = nullptr;
4929 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004930 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004931 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4932 codegen_->AddSlowPath(slow_path);
4933 if (instruction->GetValueCanBeNull()) {
4934 __ testl(register_value, register_value);
4935 __ j(kNotEqual, &not_null);
4936 __ movl(address, Immediate(0));
4937 codegen_->MaybeRecordImplicitNullCheck(instruction);
4938 __ jmp(&done);
4939 __ Bind(&not_null);
4940 }
4941
Roland Levillain0d5a2812015-11-13 10:07:31 +00004942 if (kEmitCompilerReadBarrier) {
4943 // When read barriers are enabled, the type checking
4944 // instrumentation requires two read barriers:
4945 //
4946 // __ movl(temp2, temp);
4947 // // /* HeapReference<Class> */ temp = temp->component_type_
4948 // __ movl(temp, Address(temp, component_offset));
4949 // codegen_->GenerateReadBarrier(
4950 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4951 //
4952 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4953 // __ movl(temp2, Address(register_value, class_offset));
4954 // codegen_->GenerateReadBarrier(
4955 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4956 //
4957 // __ cmpl(temp, temp2);
4958 //
4959 // However, the second read barrier may trash `temp`, as it
4960 // is a temporary register, and as such would not be saved
4961 // along with live registers before calling the runtime (nor
4962 // restored afterwards). So in this case, we bail out and
4963 // delegate the work to the array set slow path.
4964 //
4965 // TODO: Extend the register allocator to support a new
4966 // "(locally) live temp" location so as to avoid always
4967 // going into the slow path when read barriers are enabled.
4968 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004969 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004970 // /* HeapReference<Class> */ temp = array->klass_
4971 __ movl(temp, Address(array, class_offset));
4972 codegen_->MaybeRecordImplicitNullCheck(instruction);
4973 __ MaybeUnpoisonHeapReference(temp);
4974
4975 // /* HeapReference<Class> */ temp = temp->component_type_
4976 __ movl(temp, Address(temp, component_offset));
4977 // If heap poisoning is enabled, no need to unpoison `temp`
4978 // nor the object reference in `register_value->klass`, as
4979 // we are comparing two poisoned references.
4980 __ cmpl(temp, Address(register_value, class_offset));
4981
4982 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4983 __ j(kEqual, &do_put);
4984 // If heap poisoning is enabled, the `temp` reference has
4985 // not been unpoisoned yet; unpoison it now.
4986 __ MaybeUnpoisonHeapReference(temp);
4987
4988 // /* HeapReference<Class> */ temp = temp->super_class_
4989 __ movl(temp, Address(temp, super_offset));
4990 // If heap poisoning is enabled, no need to unpoison
4991 // `temp`, as we are comparing against null below.
4992 __ testl(temp, temp);
4993 __ j(kNotEqual, slow_path->GetEntryLabel());
4994 __ Bind(&do_put);
4995 } else {
4996 __ j(kNotEqual, slow_path->GetEntryLabel());
4997 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004998 }
4999 }
5000
5001 if (kPoisonHeapReferences) {
5002 __ movl(temp, register_value);
5003 __ PoisonHeapReference(temp);
5004 __ movl(address, temp);
5005 } else {
5006 __ movl(address, register_value);
5007 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005008 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005009 codegen_->MaybeRecordImplicitNullCheck(instruction);
5010 }
5011
5012 Register card = locations->GetTemp(1).AsRegister<Register>();
5013 codegen_->MarkGCCard(
5014 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5015 __ Bind(&done);
5016
5017 if (slow_path != nullptr) {
5018 __ Bind(slow_path->GetExitLabel());
5019 }
5020
5021 break;
5022 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005023
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005024 case Primitive::kPrimInt: {
5025 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5026 Address address = index.IsConstant()
5027 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5028 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5029 if (value.IsRegister()) {
5030 __ movl(address, value.AsRegister<Register>());
5031 } else {
5032 DCHECK(value.IsConstant()) << value;
5033 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5034 __ movl(address, Immediate(v));
5035 }
5036 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 break;
5038 }
5039
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 case Primitive::kPrimLong: {
5041 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 if (index.IsConstant()) {
5043 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005044 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005045 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005046 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005047 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005048 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005049 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005050 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005051 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005052 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005053 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005056 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005057 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005058 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005059 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005060 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005061 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005062 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005063 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005064 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005065 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005066 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005067 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005068 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005069 Immediate(High32Bits(val)));
5070 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 }
5072 break;
5073 }
5074
Mark Mendell7c8d0092015-01-26 11:21:33 -05005075 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005076 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5077 Address address = index.IsConstant()
5078 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5079 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005080 if (value.IsFpuRegister()) {
5081 __ movss(address, value.AsFpuRegister<XmmRegister>());
5082 } else {
5083 DCHECK(value.IsConstant());
5084 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5085 __ movl(address, Immediate(v));
5086 }
5087 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005088 break;
5089 }
5090
5091 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005092 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5093 Address address = index.IsConstant()
5094 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5095 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005096 if (value.IsFpuRegister()) {
5097 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5098 } else {
5099 DCHECK(value.IsConstant());
5100 Address address_hi = index.IsConstant() ?
5101 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5102 offset + kX86WordSize) :
5103 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5104 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5105 __ movl(address, Immediate(Low32Bits(v)));
5106 codegen_->MaybeRecordImplicitNullCheck(instruction);
5107 __ movl(address_hi, Immediate(High32Bits(v)));
5108 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005109 break;
5110 }
5111
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 case Primitive::kPrimVoid:
5113 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005114 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 }
5116}
5117
5118void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5119 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005120 locations->SetInAt(0, Location::RequiresRegister());
5121 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122}
5123
5124void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5125 LocationSummary* locations = instruction->GetLocations();
5126 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005127 Register obj = locations->InAt(0).AsRegister<Register>();
5128 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005129 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005130 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131}
5132
5133void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005134 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5135 ? LocationSummary::kCallOnSlowPath
5136 : LocationSummary::kNoCall;
5137 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005138 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005139 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005140 if (instruction->HasUses()) {
5141 locations->SetOut(Location::SameAsFirstInput());
5142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143}
5144
5145void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5146 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005147 Location index_loc = locations->InAt(0);
5148 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005149 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005150 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151
Mark Mendell99dbd682015-04-22 16:18:52 -04005152 if (length_loc.IsConstant()) {
5153 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5154 if (index_loc.IsConstant()) {
5155 // BCE will remove the bounds check if we are guarenteed to pass.
5156 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5157 if (index < 0 || index >= length) {
5158 codegen_->AddSlowPath(slow_path);
5159 __ jmp(slow_path->GetEntryLabel());
5160 } else {
5161 // Some optimization after BCE may have generated this, and we should not
5162 // generate a bounds check if it is a valid range.
5163 }
5164 return;
5165 }
5166
5167 // We have to reverse the jump condition because the length is the constant.
5168 Register index_reg = index_loc.AsRegister<Register>();
5169 __ cmpl(index_reg, Immediate(length));
5170 codegen_->AddSlowPath(slow_path);
5171 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005172 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005173 Register length = length_loc.AsRegister<Register>();
5174 if (index_loc.IsConstant()) {
5175 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5176 __ cmpl(length, Immediate(value));
5177 } else {
5178 __ cmpl(length, index_loc.AsRegister<Register>());
5179 }
5180 codegen_->AddSlowPath(slow_path);
5181 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005182 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005183}
5184
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5186 temp->SetLocations(nullptr);
5187}
5188
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005189void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005190 // Nothing to do, this is driven by the code generator.
5191}
5192
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005193void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005194 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005195}
5196
5197void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005198 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5199}
5200
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005201void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5202 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5203}
5204
5205void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005206 HBasicBlock* block = instruction->GetBlock();
5207 if (block->GetLoopInformation() != nullptr) {
5208 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5209 // The back edge will generate the suspend check.
5210 return;
5211 }
5212 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5213 // The goto will generate the suspend check.
5214 return;
5215 }
5216 GenerateSuspendCheck(instruction, nullptr);
5217}
5218
5219void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5220 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005221 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005222 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5223 if (slow_path == nullptr) {
5224 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5225 instruction->SetSlowPath(slow_path);
5226 codegen_->AddSlowPath(slow_path);
5227 if (successor != nullptr) {
5228 DCHECK(successor->IsLoopHeader());
5229 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5230 }
5231 } else {
5232 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5233 }
5234
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005235 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005236 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005237 if (successor == nullptr) {
5238 __ j(kNotEqual, slow_path->GetEntryLabel());
5239 __ Bind(slow_path->GetReturnLabel());
5240 } else {
5241 __ j(kEqual, codegen_->GetLabelOf(successor));
5242 __ jmp(slow_path->GetEntryLabel());
5243 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005244}
5245
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005246X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5247 return codegen_->GetAssembler();
5248}
5249
Mark Mendell7c8d0092015-01-26 11:21:33 -05005250void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005251 ScratchRegisterScope ensure_scratch(
5252 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5253 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5254 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5255 __ movl(temp_reg, Address(ESP, src + stack_offset));
5256 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005257}
5258
5259void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005260 ScratchRegisterScope ensure_scratch(
5261 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5262 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5263 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5264 __ movl(temp_reg, Address(ESP, src + stack_offset));
5265 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5266 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5267 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005268}
5269
5270void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005271 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005272 Location source = move->GetSource();
5273 Location destination = move->GetDestination();
5274
5275 if (source.IsRegister()) {
5276 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005277 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005278 } else {
5279 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005280 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005281 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005282 } else if (source.IsFpuRegister()) {
5283 if (destination.IsFpuRegister()) {
5284 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5285 } else if (destination.IsStackSlot()) {
5286 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5287 } else {
5288 DCHECK(destination.IsDoubleStackSlot());
5289 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5290 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005291 } else if (source.IsStackSlot()) {
5292 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005293 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005294 } else if (destination.IsFpuRegister()) {
5295 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005296 } else {
5297 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005298 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5299 }
5300 } else if (source.IsDoubleStackSlot()) {
5301 if (destination.IsFpuRegister()) {
5302 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5303 } else {
5304 DCHECK(destination.IsDoubleStackSlot()) << destination;
5305 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005306 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005307 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005308 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005309 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005310 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005311 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005312 if (value == 0) {
5313 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5314 } else {
5315 __ movl(destination.AsRegister<Register>(), Immediate(value));
5316 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005317 } else {
5318 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005319 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005320 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005321 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005322 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005323 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005324 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005325 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005326 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5327 if (value == 0) {
5328 // Easy handling of 0.0.
5329 __ xorps(dest, dest);
5330 } else {
5331 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005332 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5333 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5334 __ movl(temp, Immediate(value));
5335 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005336 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005337 } else {
5338 DCHECK(destination.IsStackSlot()) << destination;
5339 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5340 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005341 } else if (constant->IsLongConstant()) {
5342 int64_t value = constant->AsLongConstant()->GetValue();
5343 int32_t low_value = Low32Bits(value);
5344 int32_t high_value = High32Bits(value);
5345 Immediate low(low_value);
5346 Immediate high(high_value);
5347 if (destination.IsDoubleStackSlot()) {
5348 __ movl(Address(ESP, destination.GetStackIndex()), low);
5349 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5350 } else {
5351 __ movl(destination.AsRegisterPairLow<Register>(), low);
5352 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5353 }
5354 } else {
5355 DCHECK(constant->IsDoubleConstant());
5356 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005357 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005358 int32_t low_value = Low32Bits(value);
5359 int32_t high_value = High32Bits(value);
5360 Immediate low(low_value);
5361 Immediate high(high_value);
5362 if (destination.IsFpuRegister()) {
5363 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5364 if (value == 0) {
5365 // Easy handling of 0.0.
5366 __ xorpd(dest, dest);
5367 } else {
5368 __ pushl(high);
5369 __ pushl(low);
5370 __ movsd(dest, Address(ESP, 0));
5371 __ addl(ESP, Immediate(8));
5372 }
5373 } else {
5374 DCHECK(destination.IsDoubleStackSlot()) << destination;
5375 __ movl(Address(ESP, destination.GetStackIndex()), low);
5376 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5377 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005378 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005379 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005380 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005381 }
5382}
5383
Mark Mendella5c19ce2015-04-01 12:51:05 -04005384void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005385 Register suggested_scratch = reg == EAX ? EBX : EAX;
5386 ScratchRegisterScope ensure_scratch(
5387 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5388
5389 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5390 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5391 __ movl(Address(ESP, mem + stack_offset), reg);
5392 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005393}
5394
Mark Mendell7c8d0092015-01-26 11:21:33 -05005395void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005396 ScratchRegisterScope ensure_scratch(
5397 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5398
5399 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5400 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5401 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5402 __ movss(Address(ESP, mem + stack_offset), reg);
5403 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005404}
5405
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005406void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005407 ScratchRegisterScope ensure_scratch1(
5408 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005409
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005410 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5411 ScratchRegisterScope ensure_scratch2(
5412 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005413
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005414 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5415 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5416 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5417 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5418 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5419 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005420}
5421
5422void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005423 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005424 Location source = move->GetSource();
5425 Location destination = move->GetDestination();
5426
5427 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005428 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5429 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5430 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5431 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5432 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005433 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005434 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005435 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005436 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005437 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5438 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005439 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5440 // Use XOR Swap algorithm to avoid a temporary.
5441 DCHECK_NE(source.reg(), destination.reg());
5442 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5443 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5444 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5445 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5446 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5447 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5448 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005449 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5450 // Take advantage of the 16 bytes in the XMM register.
5451 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5452 Address stack(ESP, destination.GetStackIndex());
5453 // Load the double into the high doubleword.
5454 __ movhpd(reg, stack);
5455
5456 // Store the low double into the destination.
5457 __ movsd(stack, reg);
5458
5459 // Move the high double to the low double.
5460 __ psrldq(reg, Immediate(8));
5461 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5462 // Take advantage of the 16 bytes in the XMM register.
5463 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5464 Address stack(ESP, source.GetStackIndex());
5465 // Load the double into the high doubleword.
5466 __ movhpd(reg, stack);
5467
5468 // Store the low double into the destination.
5469 __ movsd(stack, reg);
5470
5471 // Move the high double to the low double.
5472 __ psrldq(reg, Immediate(8));
5473 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5474 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5475 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005476 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005477 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005478 }
5479}
5480
5481void ParallelMoveResolverX86::SpillScratch(int reg) {
5482 __ pushl(static_cast<Register>(reg));
5483}
5484
5485void ParallelMoveResolverX86::RestoreScratch(int reg) {
5486 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005487}
5488
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005489void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005490 InvokeRuntimeCallingConvention calling_convention;
5491 CodeGenerator::CreateLoadClassLocationSummary(
5492 cls,
5493 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005494 Location::RegisterLocation(EAX),
5495 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005496}
5497
5498void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005499 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005500 if (cls->NeedsAccessCheck()) {
5501 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5502 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5503 cls,
5504 cls->GetDexPc(),
5505 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005506 return;
5507 }
5508
Roland Levillain0d5a2812015-11-13 10:07:31 +00005509 Location out_loc = locations->Out();
5510 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005511 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005512
Calin Juravle580b6092015-10-06 17:35:58 +01005513 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005514 DCHECK(!cls->CanCallRuntime());
5515 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005516 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5517 if (kEmitCompilerReadBarrier) {
5518 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5519 __ leal(out, Address(current_method, declaring_class_offset));
5520 // /* mirror::Class* */ out = out->Read()
5521 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5522 } else {
5523 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5524 __ movl(out, Address(current_method, declaring_class_offset));
5525 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005526 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005527 DCHECK(cls->CanCallRuntime());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005528 // /* GcRoot<mirror::Class>[] */ out =
5529 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5530 __ movl(out, Address(current_method,
5531 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5532
5533 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5534 if (kEmitCompilerReadBarrier) {
5535 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5536 __ leal(out, Address(out, cache_offset));
5537 // /* mirror::Class* */ out = out->Read()
5538 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5539 } else {
5540 // /* GcRoot<mirror::Class> */ out = out[type_index]
5541 __ movl(out, Address(out, cache_offset));
5542 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005543
Andreas Gampe85b62f22015-09-09 13:15:38 -07005544 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005545 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5546 codegen_->AddSlowPath(slow_path);
5547 __ testl(out, out);
5548 __ j(kEqual, slow_path->GetEntryLabel());
5549 if (cls->MustGenerateClinitCheck()) {
5550 GenerateClassInitializationCheck(slow_path, out);
5551 } else {
5552 __ Bind(slow_path->GetExitLabel());
5553 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005554 }
5555}
5556
5557void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5558 LocationSummary* locations =
5559 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5560 locations->SetInAt(0, Location::RequiresRegister());
5561 if (check->HasUses()) {
5562 locations->SetOut(Location::SameAsFirstInput());
5563 }
5564}
5565
5566void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005567 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005568 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005569 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005570 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005571 GenerateClassInitializationCheck(slow_path,
5572 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005573}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005574
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005575void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005576 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005577 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5578 Immediate(mirror::Class::kStatusInitialized));
5579 __ j(kLess, slow_path->GetEntryLabel());
5580 __ Bind(slow_path->GetExitLabel());
5581 // No need for memory fence, thanks to the X86 memory model.
5582}
5583
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005584void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5585 LocationSummary* locations =
5586 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005587 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005588 locations->SetOut(Location::RequiresRegister());
5589}
5590
5591void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005592 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005593 codegen_->AddSlowPath(slow_path);
5594
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005595 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005596 Location out_loc = locations->Out();
5597 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005598 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005599
5600 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5601 if (kEmitCompilerReadBarrier) {
5602 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5603 __ leal(out, Address(current_method, declaring_class_offset));
5604 // /* mirror::Class* */ out = out->Read()
5605 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5606 } else {
5607 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5608 __ movl(out, Address(current_method, declaring_class_offset));
5609 }
5610
5611 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005612 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005613
5614 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5615 if (kEmitCompilerReadBarrier) {
5616 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5617 __ leal(out, Address(out, cache_offset));
5618 // /* mirror::String* */ out = out->Read()
5619 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5620 } else {
5621 // /* GcRoot<mirror::String> */ out = out[string_index]
5622 __ movl(out, Address(out, cache_offset));
5623 }
5624
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005625 __ testl(out, out);
5626 __ j(kEqual, slow_path->GetEntryLabel());
5627 __ Bind(slow_path->GetExitLabel());
5628}
5629
David Brazdilcb1c0552015-08-04 16:22:25 +01005630static Address GetExceptionTlsAddress() {
5631 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5632}
5633
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005634void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5635 LocationSummary* locations =
5636 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5637 locations->SetOut(Location::RequiresRegister());
5638}
5639
5640void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005641 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5642}
5643
5644void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5645 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5646}
5647
5648void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5649 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005650}
5651
5652void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5653 LocationSummary* locations =
5654 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5655 InvokeRuntimeCallingConvention calling_convention;
5656 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5657}
5658
5659void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005660 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5661 instruction,
5662 instruction->GetDexPc(),
5663 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005664}
5665
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005666void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005667 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005668 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5669 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005670 case TypeCheckKind::kExactCheck:
5671 case TypeCheckKind::kAbstractClassCheck:
5672 case TypeCheckKind::kClassHierarchyCheck:
5673 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005674 call_kind =
5675 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005676 break;
5677 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005678 case TypeCheckKind::kUnresolvedCheck:
5679 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005680 call_kind = LocationSummary::kCallOnSlowPath;
5681 break;
5682 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005683
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005684 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005685 locations->SetInAt(0, Location::RequiresRegister());
5686 locations->SetInAt(1, Location::Any());
5687 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5688 locations->SetOut(Location::RequiresRegister());
5689 // When read barriers are enabled, we need a temporary register for
5690 // some cases.
5691 if (kEmitCompilerReadBarrier &&
5692 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5693 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5694 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5695 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005696 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005697}
5698
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005699void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005700 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005701 Location obj_loc = locations->InAt(0);
5702 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005703 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005704 Location out_loc = locations->Out();
5705 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005706 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005707 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5708 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5709 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005710 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005711 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005712
5713 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005714 // Avoid null check if we know obj is not null.
5715 if (instruction->MustDoNullCheck()) {
5716 __ testl(obj, obj);
5717 __ j(kEqual, &zero);
5718 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005719
Roland Levillain0d5a2812015-11-13 10:07:31 +00005720 // /* HeapReference<Class> */ out = obj->klass_
5721 __ movl(out, Address(obj, class_offset));
5722 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005723
5724 switch (instruction->GetTypeCheckKind()) {
5725 case TypeCheckKind::kExactCheck: {
5726 if (cls.IsRegister()) {
5727 __ cmpl(out, cls.AsRegister<Register>());
5728 } else {
5729 DCHECK(cls.IsStackSlot()) << cls;
5730 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5731 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005732
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005733 // Classes must be equal for the instanceof to succeed.
5734 __ j(kNotEqual, &zero);
5735 __ movl(out, Immediate(1));
5736 __ jmp(&done);
5737 break;
5738 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005739
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 case TypeCheckKind::kAbstractClassCheck: {
5741 // If the class is abstract, we eagerly fetch the super class of the
5742 // object to avoid doing a comparison we know will fail.
5743 NearLabel loop;
5744 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005745 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5746 if (kEmitCompilerReadBarrier) {
5747 // Save the value of `out` into `temp` before overwriting it
5748 // in the following move operation, as we will need it for the
5749 // read barrier below.
5750 Register temp = temp_loc.AsRegister<Register>();
5751 __ movl(temp, out);
5752 }
5753 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005756 __ testl(out, out);
5757 // If `out` is null, we use it for the result, and jump to `done`.
5758 __ j(kEqual, &done);
5759 if (cls.IsRegister()) {
5760 __ cmpl(out, cls.AsRegister<Register>());
5761 } else {
5762 DCHECK(cls.IsStackSlot()) << cls;
5763 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5764 }
5765 __ j(kNotEqual, &loop);
5766 __ movl(out, Immediate(1));
5767 if (zero.IsLinked()) {
5768 __ jmp(&done);
5769 }
5770 break;
5771 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 case TypeCheckKind::kClassHierarchyCheck: {
5774 // Walk over the class hierarchy to find a match.
5775 NearLabel loop, success;
5776 __ Bind(&loop);
5777 if (cls.IsRegister()) {
5778 __ cmpl(out, cls.AsRegister<Register>());
5779 } else {
5780 DCHECK(cls.IsStackSlot()) << cls;
5781 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5782 }
5783 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005784 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5785 if (kEmitCompilerReadBarrier) {
5786 // Save the value of `out` into `temp` before overwriting it
5787 // in the following move operation, as we will need it for the
5788 // read barrier below.
5789 Register temp = temp_loc.AsRegister<Register>();
5790 __ movl(temp, out);
5791 }
5792 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005793 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005794 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 __ testl(out, out);
5796 __ j(kNotEqual, &loop);
5797 // If `out` is null, we use it for the result, and jump to `done`.
5798 __ jmp(&done);
5799 __ Bind(&success);
5800 __ movl(out, Immediate(1));
5801 if (zero.IsLinked()) {
5802 __ jmp(&done);
5803 }
5804 break;
5805 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005806
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005807 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005808 // Do an exact check.
5809 NearLabel exact_check;
5810 if (cls.IsRegister()) {
5811 __ cmpl(out, cls.AsRegister<Register>());
5812 } else {
5813 DCHECK(cls.IsStackSlot()) << cls;
5814 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5815 }
5816 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 // Otherwise, we need to check that the object's class is a non-primitive array.
5818 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5819 if (kEmitCompilerReadBarrier) {
5820 // Save the value of `out` into `temp` before overwriting it
5821 // in the following move operation, as we will need it for the
5822 // read barrier below.
5823 Register temp = temp_loc.AsRegister<Register>();
5824 __ movl(temp, out);
5825 }
5826 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005827 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005828 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 __ testl(out, out);
5830 // If `out` is null, we use it for the result, and jump to `done`.
5831 __ j(kEqual, &done);
5832 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5833 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005834 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005835 __ movl(out, Immediate(1));
5836 __ jmp(&done);
5837 break;
5838 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840 case TypeCheckKind::kArrayCheck: {
5841 if (cls.IsRegister()) {
5842 __ cmpl(out, cls.AsRegister<Register>());
5843 } else {
5844 DCHECK(cls.IsStackSlot()) << cls;
5845 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5846 }
5847 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005848 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5849 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005850 codegen_->AddSlowPath(slow_path);
5851 __ j(kNotEqual, slow_path->GetEntryLabel());
5852 __ movl(out, Immediate(1));
5853 if (zero.IsLinked()) {
5854 __ jmp(&done);
5855 }
5856 break;
5857 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858
Calin Juravle98893e12015-10-02 21:05:03 +01005859 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005860 case TypeCheckKind::kInterfaceCheck: {
5861 // Note that we indeed only call on slow path, but we always go
5862 // into the slow path for the unresolved & interface check
5863 // cases.
5864 //
5865 // We cannot directly call the InstanceofNonTrivial runtime
5866 // entry point without resorting to a type checking slow path
5867 // here (i.e. by calling InvokeRuntime directly), as it would
5868 // require to assign fixed registers for the inputs of this
5869 // HInstanceOf instruction (following the runtime calling
5870 // convention), which might be cluttered by the potential first
5871 // read barrier emission at the beginning of this method.
5872 DCHECK(locations->OnlyCallsOnSlowPath());
5873 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5874 /* is_fatal */ false);
5875 codegen_->AddSlowPath(slow_path);
5876 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877 if (zero.IsLinked()) {
5878 __ jmp(&done);
5879 }
5880 break;
5881 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005882 }
5883
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005885 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 __ xorl(out, out);
5887 }
5888
5889 if (done.IsLinked()) {
5890 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005891 }
5892
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005893 if (slow_path != nullptr) {
5894 __ Bind(slow_path->GetExitLabel());
5895 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005896}
5897
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005898void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005899 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5900 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005901 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5902 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005903 case TypeCheckKind::kExactCheck:
5904 case TypeCheckKind::kAbstractClassCheck:
5905 case TypeCheckKind::kClassHierarchyCheck:
5906 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005907 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5908 LocationSummary::kCallOnSlowPath :
5909 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 break;
5911 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912 case TypeCheckKind::kUnresolvedCheck:
5913 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 call_kind = LocationSummary::kCallOnSlowPath;
5915 break;
5916 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005917 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5918 locations->SetInAt(0, Location::RequiresRegister());
5919 locations->SetInAt(1, Location::Any());
5920 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
5921 locations->AddTemp(Location::RequiresRegister());
5922 // When read barriers are enabled, we need an additional temporary
5923 // register for some cases.
5924 if (kEmitCompilerReadBarrier &&
5925 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5926 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5927 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005928 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005930}
5931
5932void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5933 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005934 Location obj_loc = locations->InAt(0);
5935 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005936 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005937 Location temp_loc = locations->GetTemp(0);
5938 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005939 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5940 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5941 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5942 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943
Roland Levillain0d5a2812015-11-13 10:07:31 +00005944 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5945 bool is_type_check_slow_path_fatal =
5946 (type_check_kind == TypeCheckKind::kExactCheck ||
5947 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5948 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5949 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5950 !instruction->CanThrowIntoCatchBlock();
5951 SlowPathCode* type_check_slow_path =
5952 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5953 is_type_check_slow_path_fatal);
5954 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955
Roland Levillain0d5a2812015-11-13 10:07:31 +00005956 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005957 // Avoid null check if we know obj is not null.
5958 if (instruction->MustDoNullCheck()) {
5959 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005960 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005961 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005962
Roland Levillain0d5a2812015-11-13 10:07:31 +00005963 // /* HeapReference<Class> */ temp = obj->klass_
5964 __ movl(temp, Address(obj, class_offset));
5965 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005966
Roland Levillain0d5a2812015-11-13 10:07:31 +00005967 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 case TypeCheckKind::kExactCheck:
5969 case TypeCheckKind::kArrayCheck: {
5970 if (cls.IsRegister()) {
5971 __ cmpl(temp, cls.AsRegister<Register>());
5972 } else {
5973 DCHECK(cls.IsStackSlot()) << cls;
5974 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5975 }
5976 // Jump to slow path for throwing the exception or doing a
5977 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005979 break;
5980 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005981
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005982 case TypeCheckKind::kAbstractClassCheck: {
5983 // If the class is abstract, we eagerly fetch the super class of the
5984 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005985 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005986 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005987 Location temp2_loc =
5988 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5989 if (kEmitCompilerReadBarrier) {
5990 // Save the value of `temp` into `temp2` before overwriting it
5991 // in the following move operation, as we will need it for the
5992 // read barrier below.
5993 Register temp2 = temp2_loc.AsRegister<Register>();
5994 __ movl(temp2, temp);
5995 }
5996 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005997 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
5999
6000 // If the class reference currently in `temp` is not null, jump
6001 // to the `compare_classes` label to compare it with the checked
6002 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006003 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006004 __ j(kNotEqual, &compare_classes);
6005 // Otherwise, jump to the slow path to throw the exception.
6006 //
6007 // But before, move back the object's class into `temp` before
6008 // going into the slow path, as it has been overwritten in the
6009 // meantime.
6010 // /* HeapReference<Class> */ temp = obj->klass_
6011 __ movl(temp, Address(obj, class_offset));
6012 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6013 __ jmp(type_check_slow_path->GetEntryLabel());
6014
6015 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006016 if (cls.IsRegister()) {
6017 __ cmpl(temp, cls.AsRegister<Register>());
6018 } else {
6019 DCHECK(cls.IsStackSlot()) << cls;
6020 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6021 }
6022 __ j(kNotEqual, &loop);
6023 break;
6024 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006025
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006026 case TypeCheckKind::kClassHierarchyCheck: {
6027 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006028 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006029 __ Bind(&loop);
6030 if (cls.IsRegister()) {
6031 __ cmpl(temp, cls.AsRegister<Register>());
6032 } else {
6033 DCHECK(cls.IsStackSlot()) << cls;
6034 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6035 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006036 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037
6038 Location temp2_loc =
6039 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6040 if (kEmitCompilerReadBarrier) {
6041 // Save the value of `temp` into `temp2` before overwriting it
6042 // in the following move operation, as we will need it for the
6043 // read barrier below.
6044 Register temp2 = temp2_loc.AsRegister<Register>();
6045 __ movl(temp2, temp);
6046 }
6047 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006049 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6050
6051 // If the class reference currently in `temp` is not null, jump
6052 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006053 __ testl(temp, temp);
6054 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006055 // Otherwise, jump to the slow path to throw the exception.
6056 //
6057 // But before, move back the object's class into `temp` before
6058 // going into the slow path, as it has been overwritten in the
6059 // meantime.
6060 // /* HeapReference<Class> */ temp = obj->klass_
6061 __ movl(temp, Address(obj, class_offset));
6062 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6063 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006064 break;
6065 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006066
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006067 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006068 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006070 if (cls.IsRegister()) {
6071 __ cmpl(temp, cls.AsRegister<Register>());
6072 } else {
6073 DCHECK(cls.IsStackSlot()) << cls;
6074 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6075 }
6076 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077
6078 // Otherwise, we need to check that the object's class is a non-primitive array.
6079 Location temp2_loc =
6080 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6081 if (kEmitCompilerReadBarrier) {
6082 // Save the value of `temp` into `temp2` before overwriting it
6083 // in the following move operation, as we will need it for the
6084 // read barrier below.
6085 Register temp2 = temp2_loc.AsRegister<Register>();
6086 __ movl(temp2, temp);
6087 }
6088 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006090 codegen_->MaybeGenerateReadBarrier(
6091 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
6092
6093 // If the component type is not null (i.e. the object is indeed
6094 // an array), jump to label `check_non_primitive_component_type`
6095 // to further check that this component type is not a primitive
6096 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006097 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006098 __ j(kNotEqual, &check_non_primitive_component_type);
6099 // Otherwise, jump to the slow path to throw the exception.
6100 //
6101 // But before, move back the object's class into `temp` before
6102 // going into the slow path, as it has been overwritten in the
6103 // meantime.
6104 // /* HeapReference<Class> */ temp = obj->klass_
6105 __ movl(temp, Address(obj, class_offset));
6106 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6107 __ jmp(type_check_slow_path->GetEntryLabel());
6108
6109 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006110 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006111 __ j(kEqual, &done);
6112 // Same comment as above regarding `temp` and the slow path.
6113 // /* HeapReference<Class> */ temp = obj->klass_
6114 __ movl(temp, Address(obj, class_offset));
6115 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6116 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006117 break;
6118 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006119
Calin Juravle98893e12015-10-02 21:05:03 +01006120 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006121 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 // We always go into the type check slow path for the unresolved &
6123 // interface check cases.
6124 //
6125 // We cannot directly call the CheckCast runtime entry point
6126 // without resorting to a type checking slow path here (i.e. by
6127 // calling InvokeRuntime directly), as it would require to
6128 // assign fixed registers for the inputs of this HInstanceOf
6129 // instruction (following the runtime calling convention), which
6130 // might be cluttered by the potential first read barrier
6131 // emission at the beginning of this method.
6132 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 break;
6134 }
6135 __ Bind(&done);
6136
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006138}
6139
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006140void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6141 LocationSummary* locations =
6142 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6143 InvokeRuntimeCallingConvention calling_convention;
6144 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6145}
6146
6147void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006148 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6149 : QUICK_ENTRY_POINT(pUnlockObject),
6150 instruction,
6151 instruction->GetDexPc(),
6152 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006153}
6154
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006155void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6156void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6157void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6158
6159void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6160 LocationSummary* locations =
6161 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6162 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6163 || instruction->GetResultType() == Primitive::kPrimLong);
6164 locations->SetInAt(0, Location::RequiresRegister());
6165 locations->SetInAt(1, Location::Any());
6166 locations->SetOut(Location::SameAsFirstInput());
6167}
6168
6169void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6170 HandleBitwiseOperation(instruction);
6171}
6172
6173void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6174 HandleBitwiseOperation(instruction);
6175}
6176
6177void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6178 HandleBitwiseOperation(instruction);
6179}
6180
6181void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6182 LocationSummary* locations = instruction->GetLocations();
6183 Location first = locations->InAt(0);
6184 Location second = locations->InAt(1);
6185 DCHECK(first.Equals(locations->Out()));
6186
6187 if (instruction->GetResultType() == Primitive::kPrimInt) {
6188 if (second.IsRegister()) {
6189 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006190 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006191 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006192 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006193 } else {
6194 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006195 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006196 }
6197 } else if (second.IsConstant()) {
6198 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006199 __ andl(first.AsRegister<Register>(),
6200 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006201 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006202 __ orl(first.AsRegister<Register>(),
6203 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006204 } else {
6205 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006206 __ xorl(first.AsRegister<Register>(),
6207 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006208 }
6209 } else {
6210 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006211 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006212 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006213 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006214 } else {
6215 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006216 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006217 }
6218 }
6219 } else {
6220 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6221 if (second.IsRegisterPair()) {
6222 if (instruction->IsAnd()) {
6223 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6224 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6225 } else if (instruction->IsOr()) {
6226 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6227 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6228 } else {
6229 DCHECK(instruction->IsXor());
6230 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6231 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6232 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006233 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006234 if (instruction->IsAnd()) {
6235 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6236 __ andl(first.AsRegisterPairHigh<Register>(),
6237 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6238 } else if (instruction->IsOr()) {
6239 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6240 __ orl(first.AsRegisterPairHigh<Register>(),
6241 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6242 } else {
6243 DCHECK(instruction->IsXor());
6244 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6245 __ xorl(first.AsRegisterPairHigh<Register>(),
6246 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6247 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006248 } else {
6249 DCHECK(second.IsConstant()) << second;
6250 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006251 int32_t low_value = Low32Bits(value);
6252 int32_t high_value = High32Bits(value);
6253 Immediate low(low_value);
6254 Immediate high(high_value);
6255 Register first_low = first.AsRegisterPairLow<Register>();
6256 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006257 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006258 if (low_value == 0) {
6259 __ xorl(first_low, first_low);
6260 } else if (low_value != -1) {
6261 __ andl(first_low, low);
6262 }
6263 if (high_value == 0) {
6264 __ xorl(first_high, first_high);
6265 } else if (high_value != -1) {
6266 __ andl(first_high, high);
6267 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006268 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006269 if (low_value != 0) {
6270 __ orl(first_low, low);
6271 }
6272 if (high_value != 0) {
6273 __ orl(first_high, high);
6274 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006275 } else {
6276 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006277 if (low_value != 0) {
6278 __ xorl(first_low, low);
6279 }
6280 if (high_value != 0) {
6281 __ xorl(first_high, high);
6282 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006283 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006284 }
6285 }
6286}
6287
Roland Levillain0d5a2812015-11-13 10:07:31 +00006288void CodeGeneratorX86::GenerateReadBarrier(HInstruction* instruction,
6289 Location out,
6290 Location ref,
6291 Location obj,
6292 uint32_t offset,
6293 Location index) {
6294 DCHECK(kEmitCompilerReadBarrier);
6295
6296 // If heap poisoning is enabled, the unpoisoning of the loaded
6297 // reference will be carried out by the runtime within the slow
6298 // path.
6299 //
6300 // Note that `ref` currently does not get unpoisoned (when heap
6301 // poisoning is enabled), which is alright as the `ref` argument is
6302 // not used by the artReadBarrierSlow entry point.
6303 //
6304 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6305 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6306 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6307 AddSlowPath(slow_path);
6308
6309 // TODO: When read barrier has a fast path, add it here.
6310 /* Currently the read barrier call is inserted after the original load.
6311 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
6312 * original load. This load-load ordering is required by the read barrier.
6313 * The fast path/slow path (for Baker's algorithm) should look like:
6314 *
6315 * bool isGray = obj.LockWord & kReadBarrierMask;
6316 * lfence; // load fence or artificial data dependence to prevent load-load reordering
6317 * ref = obj.field; // this is the original load
6318 * if (isGray) {
6319 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
6320 * }
6321 */
6322
6323 __ jmp(slow_path->GetEntryLabel());
6324 __ Bind(slow_path->GetExitLabel());
6325}
6326
6327void CodeGeneratorX86::MaybeGenerateReadBarrier(HInstruction* instruction,
6328 Location out,
6329 Location ref,
6330 Location obj,
6331 uint32_t offset,
6332 Location index) {
6333 if (kEmitCompilerReadBarrier) {
6334 // If heap poisoning is enabled, unpoisoning will be taken care of
6335 // by the runtime within the slow path.
6336 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
6337 } else if (kPoisonHeapReferences) {
6338 __ UnpoisonHeapReference(out.AsRegister<Register>());
6339 }
6340}
6341
6342void CodeGeneratorX86::GenerateReadBarrierForRoot(HInstruction* instruction,
6343 Location out,
6344 Location root) {
6345 DCHECK(kEmitCompilerReadBarrier);
6346
6347 // Note that GC roots are not affected by heap poisoning, so we do
6348 // not need to do anything special for this here.
6349 SlowPathCode* slow_path =
6350 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6351 AddSlowPath(slow_path);
6352
6353 // TODO: Implement a fast path for ReadBarrierForRoot, performing
6354 // the following operation (for Baker's algorithm):
6355 //
6356 // if (thread.tls32_.is_gc_marking) {
6357 // root = Mark(root);
6358 // }
6359
6360 __ jmp(slow_path->GetEntryLabel());
6361 __ Bind(slow_path->GetExitLabel());
6362}
6363
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006364void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006365 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006366 LOG(FATAL) << "Unreachable";
6367}
6368
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006369void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006370 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006371 LOG(FATAL) << "Unreachable";
6372}
6373
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006374void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6375 DCHECK(codegen_->IsBaseline());
6376 LocationSummary* locations =
6377 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6378 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6379}
6380
6381void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6382 DCHECK(codegen_->IsBaseline());
6383 // Will be generated at use site.
6384}
6385
Mark Mendellfe57faa2015-09-18 09:26:15 -04006386// Simple implementation of packed switch - generate cascaded compare/jumps.
6387void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6388 LocationSummary* locations =
6389 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6390 locations->SetInAt(0, Location::RequiresRegister());
6391}
6392
6393void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6394 int32_t lower_bound = switch_instr->GetStartValue();
6395 int32_t num_entries = switch_instr->GetNumEntries();
6396 LocationSummary* locations = switch_instr->GetLocations();
6397 Register value_reg = locations->InAt(0).AsRegister<Register>();
6398 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6399
6400 // Create a series of compare/jumps.
6401 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6402 for (int i = 0; i < num_entries; i++) {
6403 int32_t case_value = lower_bound + i;
6404 if (case_value == 0) {
6405 __ testl(value_reg, value_reg);
6406 } else {
6407 __ cmpl(value_reg, Immediate(case_value));
6408 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006409 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006410 }
6411
6412 // And the default for any other value.
6413 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6414 __ jmp(codegen_->GetLabelOf(default_block));
6415 }
6416}
6417
Mark Mendell805b3b52015-09-18 14:10:29 -04006418void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6419 LocationSummary* locations =
6420 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6421 locations->SetInAt(0, Location::RequiresRegister());
6422
6423 // Constant area pointer.
6424 locations->SetInAt(1, Location::RequiresRegister());
6425
6426 // And the temporary we need.
6427 locations->AddTemp(Location::RequiresRegister());
6428}
6429
6430void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6431 int32_t lower_bound = switch_instr->GetStartValue();
6432 int32_t num_entries = switch_instr->GetNumEntries();
6433 LocationSummary* locations = switch_instr->GetLocations();
6434 Register value_reg = locations->InAt(0).AsRegister<Register>();
6435 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6436
6437 // Optimizing has a jump area.
6438 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6439 Register constant_area = locations->InAt(1).AsRegister<Register>();
6440
6441 // Remove the bias, if needed.
6442 if (lower_bound != 0) {
6443 __ leal(temp_reg, Address(value_reg, -lower_bound));
6444 value_reg = temp_reg;
6445 }
6446
6447 // Is the value in range?
6448 DCHECK_GE(num_entries, 1);
6449 __ cmpl(value_reg, Immediate(num_entries - 1));
6450 __ j(kAbove, codegen_->GetLabelOf(default_block));
6451
6452 // We are in the range of the table.
6453 // Load (target-constant_area) from the jump table, indexing by the value.
6454 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6455
6456 // Compute the actual target address by adding in constant_area.
6457 __ addl(temp_reg, constant_area);
6458
6459 // And jump.
6460 __ jmp(temp_reg);
6461}
6462
Mark Mendell0616ae02015-04-17 12:49:27 -04006463void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6464 HX86ComputeBaseMethodAddress* insn) {
6465 LocationSummary* locations =
6466 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6467 locations->SetOut(Location::RequiresRegister());
6468}
6469
6470void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6471 HX86ComputeBaseMethodAddress* insn) {
6472 LocationSummary* locations = insn->GetLocations();
6473 Register reg = locations->Out().AsRegister<Register>();
6474
6475 // Generate call to next instruction.
6476 Label next_instruction;
6477 __ call(&next_instruction);
6478 __ Bind(&next_instruction);
6479
6480 // Remember this offset for later use with constant area.
6481 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6482
6483 // Grab the return address off the stack.
6484 __ popl(reg);
6485}
6486
6487void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6488 HX86LoadFromConstantTable* insn) {
6489 LocationSummary* locations =
6490 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6491
6492 locations->SetInAt(0, Location::RequiresRegister());
6493 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6494
6495 // If we don't need to be materialized, we only need the inputs to be set.
6496 if (!insn->NeedsMaterialization()) {
6497 return;
6498 }
6499
6500 switch (insn->GetType()) {
6501 case Primitive::kPrimFloat:
6502 case Primitive::kPrimDouble:
6503 locations->SetOut(Location::RequiresFpuRegister());
6504 break;
6505
6506 case Primitive::kPrimInt:
6507 locations->SetOut(Location::RequiresRegister());
6508 break;
6509
6510 default:
6511 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6512 }
6513}
6514
6515void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6516 if (!insn->NeedsMaterialization()) {
6517 return;
6518 }
6519
6520 LocationSummary* locations = insn->GetLocations();
6521 Location out = locations->Out();
6522 Register const_area = locations->InAt(0).AsRegister<Register>();
6523 HConstant *value = insn->GetConstant();
6524
6525 switch (insn->GetType()) {
6526 case Primitive::kPrimFloat:
6527 __ movss(out.AsFpuRegister<XmmRegister>(),
6528 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6529 break;
6530
6531 case Primitive::kPrimDouble:
6532 __ movsd(out.AsFpuRegister<XmmRegister>(),
6533 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6534 break;
6535
6536 case Primitive::kPrimInt:
6537 __ movl(out.AsRegister<Register>(),
6538 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6539 break;
6540
6541 default:
6542 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6543 }
6544}
6545
Mark Mendell0616ae02015-04-17 12:49:27 -04006546/**
6547 * Class to handle late fixup of offsets into constant area.
6548 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006549class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006550 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006551 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6552 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6553
6554 protected:
6555 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6556
6557 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006558
6559 private:
6560 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6561 // Patch the correct offset for the instruction. The place to patch is the
6562 // last 4 bytes of the instruction.
6563 // The value to patch is the distance from the offset in the constant area
6564 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006565 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6566 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006567
6568 // Patch in the right value.
6569 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6570 }
6571
Mark Mendell0616ae02015-04-17 12:49:27 -04006572 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006573 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006574};
6575
Mark Mendell805b3b52015-09-18 14:10:29 -04006576/**
6577 * Class to handle late fixup of offsets to a jump table that will be created in the
6578 * constant area.
6579 */
6580class JumpTableRIPFixup : public RIPFixup {
6581 public:
6582 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6583 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6584
6585 void CreateJumpTable() {
6586 X86Assembler* assembler = codegen_->GetAssembler();
6587
6588 // Ensure that the reference to the jump table has the correct offset.
6589 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6590 SetOffset(offset_in_constant_table);
6591
6592 // The label values in the jump table are computed relative to the
6593 // instruction addressing the constant area.
6594 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6595
6596 // Populate the jump table with the correct values for the jump table.
6597 int32_t num_entries = switch_instr_->GetNumEntries();
6598 HBasicBlock* block = switch_instr_->GetBlock();
6599 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6600 // The value that we want is the target offset - the position of the table.
6601 for (int32_t i = 0; i < num_entries; i++) {
6602 HBasicBlock* b = successors[i];
6603 Label* l = codegen_->GetLabelOf(b);
6604 DCHECK(l->IsBound());
6605 int32_t offset_to_block = l->Position() - relative_offset;
6606 assembler->AppendInt32(offset_to_block);
6607 }
6608 }
6609
6610 private:
6611 const HX86PackedSwitch* switch_instr_;
6612};
6613
6614void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
6615 // Generate the constant area if needed.
6616 X86Assembler* assembler = GetAssembler();
6617 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6618 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
6619 // byte values.
6620 assembler->Align(4, 0);
6621 constant_area_start_ = assembler->CodeSize();
6622
6623 // Populate any jump tables.
6624 for (auto jump_table : fixups_to_jump_tables_) {
6625 jump_table->CreateJumpTable();
6626 }
6627
6628 // And now add the constant area to the generated code.
6629 assembler->AddConstantArea();
6630 }
6631
6632 // And finish up.
6633 CodeGenerator::Finalize(allocator);
6634}
6635
Mark Mendell0616ae02015-04-17 12:49:27 -04006636Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
6637 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6638 return Address(reg, kDummy32BitOffset, fixup);
6639}
6640
6641Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
6642 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6643 return Address(reg, kDummy32BitOffset, fixup);
6644}
6645
6646Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
6647 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6648 return Address(reg, kDummy32BitOffset, fixup);
6649}
6650
6651Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
6652 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6653 return Address(reg, kDummy32BitOffset, fixup);
6654}
6655
Mark Mendell805b3b52015-09-18 14:10:29 -04006656Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
6657 Register reg,
6658 Register value) {
6659 // Create a fixup to be used to create and address the jump table.
6660 JumpTableRIPFixup* table_fixup =
6661 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6662
6663 // We have to populate the jump tables.
6664 fixups_to_jump_tables_.push_back(table_fixup);
6665
6666 // We want a scaled address, as we are extracting the correct offset from the table.
6667 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
6668}
6669
Andreas Gampe85b62f22015-09-09 13:15:38 -07006670// TODO: target as memory.
6671void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
6672 if (!target.IsValid()) {
6673 DCHECK(type == Primitive::kPrimVoid);
6674 return;
6675 }
6676
6677 DCHECK_NE(type, Primitive::kPrimVoid);
6678
6679 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
6680 if (target.Equals(return_loc)) {
6681 return;
6682 }
6683
6684 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6685 // with the else branch.
6686 if (type == Primitive::kPrimLong) {
6687 HParallelMove parallel_move(GetGraph()->GetArena());
6688 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
6689 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
6690 GetMoveResolver()->EmitNativeCode(&parallel_move);
6691 } else {
6692 // Let the parallel move resolver take care of all of this.
6693 HParallelMove parallel_move(GetGraph()->GetArena());
6694 parallel_move.AddMove(return_loc, target, type, nullptr);
6695 GetMoveResolver()->EmitNativeCode(&parallel_move);
6696 }
6697}
6698
Roland Levillain4d027112015-07-01 15:41:14 +01006699#undef __
6700
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006701} // namespace x86
6702} // namespace art