blob: 32dc636d1d62c17c984ddba6a5a7441f31e89bd7 [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
1373void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HIf* if_instr,
1374 HCondition* condition,
1375 Label* true_target,
1376 Label* false_target,
1377 Label* always_true_target) {
1378 LocationSummary* locations = condition->GetLocations();
1379 Location left = locations->InAt(0);
1380 Location right = locations->InAt(1);
1381
1382 // We don't want true_target as a nullptr.
1383 if (true_target == nullptr) {
1384 true_target = always_true_target;
1385 }
1386 bool falls_through = (false_target == nullptr);
1387
1388 // FP compares don't like null false_targets.
1389 if (false_target == nullptr) {
1390 false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1391 }
1392
1393 Primitive::Type type = condition->InputAt(0)->GetType();
1394 switch (type) {
1395 case Primitive::kPrimLong:
1396 GenerateLongComparesAndJumps(condition, true_target, false_target);
1397 break;
1398 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001399 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1400 GenerateFPJumps(condition, true_target, false_target);
1401 break;
1402 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001403 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1404 GenerateFPJumps(condition, true_target, false_target);
1405 break;
1406 default:
1407 LOG(FATAL) << "Unexpected compare type " << type;
1408 }
1409
1410 if (!falls_through) {
1411 __ jmp(false_target);
1412 }
1413}
1414
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001415void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
1416 Label* true_target,
1417 Label* false_target,
1418 Label* always_true_target) {
1419 HInstruction* cond = instruction->InputAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001420 if (cond->IsIntConstant()) {
1421 // Constant condition, statically compared against 1.
1422 int32_t cond_value = cond->AsIntConstant()->GetValue();
1423 if (cond_value == 1) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001424 if (always_true_target != nullptr) {
1425 __ jmp(always_true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001426 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001427 return;
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001428 } else {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001429 DCHECK_EQ(cond_value, 0);
1430 }
1431 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001432 HCondition* condition = cond->AsCondition();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001433 bool is_materialized =
Mark Mendellb8b97692015-05-22 16:58:19 -04001434 condition == nullptr || condition->NeedsMaterialization();
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001435 // Moves do not affect the eflags register, so if the condition is
1436 // evaluated just before the if, we don't need to evaluate it
Mark Mendellc4701932015-04-10 13:18:51 -04001437 // again. We can't use the eflags on long/FP conditions if they are
1438 // materialized due to the complex branching.
Mark Mendellb8b97692015-05-22 16:58:19 -04001439 Primitive::Type type = (condition != nullptr)
1440 ? cond->InputAt(0)->GetType()
1441 : Primitive::kPrimInt;
1442 bool eflags_set = condition != nullptr
1443 && condition->IsBeforeWhenDisregardMoves(instruction)
Roland Levillain4fa13f62015-07-06 18:11:54 +01001444 && (type != Primitive::kPrimLong && !Primitive::IsFloatingPointType(type));
Mark Mendellb8b97692015-05-22 16:58:19 -04001445 // Can we optimize the jump if we know that the next block is the true case?
1446 bool can_jump_to_false = CanReverseCondition(always_true_target, false_target, condition);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001447 if (is_materialized) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001448 if (!eflags_set) {
1449 // Materialized condition, compare against 0.
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001450 Location lhs = instruction->GetLocations()->InAt(0);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001451 if (lhs.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05001452 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001453 } else {
1454 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1455 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001456 if (can_jump_to_false) {
1457 __ j(kEqual, false_target);
1458 return;
1459 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001460 __ j(kNotEqual, true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001461 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001462 if (can_jump_to_false) {
1463 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1464 return;
1465 }
1466 __ j(X86Condition(condition->GetCondition()), true_target);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001467 }
1468 } else {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001469 // Condition has not been materialized, use its inputs as the
1470 // comparison and its condition as the branch condition.
1471
Mark Mendellc4701932015-04-10 13:18:51 -04001472 // Is this a long or FP comparison that has been folded into the HCondition?
1473 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1474 // Generate the comparison directly.
1475 GenerateCompareTestAndBranch(instruction->AsIf(),
Mark Mendellb8b97692015-05-22 16:58:19 -04001476 condition,
Mark Mendellc4701932015-04-10 13:18:51 -04001477 true_target,
1478 false_target,
1479 always_true_target);
1480 return;
1481 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001482
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001483 Location lhs = cond->GetLocations()->InAt(0);
1484 Location rhs = cond->GetLocations()->InAt(1);
1485 // LHS is guaranteed to be in a register (see
1486 // LocationsBuilderX86::VisitCondition).
1487 if (rhs.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001488 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001489 } else if (rhs.IsConstant()) {
Calin Juravleb3306642015-04-20 18:30:42 +01001490 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Mark Mendell09b84632015-02-13 17:48:38 -05001491 if (constant == 0) {
1492 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1493 } else {
1494 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1495 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001496 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001497 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001498 }
Mark Mendellb8b97692015-05-22 16:58:19 -04001499
1500 if (can_jump_to_false) {
1501 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1502 return;
1503 }
1504
1505 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001506 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001507 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001508 if (false_target != nullptr) {
1509 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001510 }
1511}
1512
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001513void LocationsBuilderX86::VisitIf(HIf* if_instr) {
1514 LocationSummary* locations =
1515 new (GetGraph()->GetArena()) LocationSummary(if_instr, LocationSummary::kNoCall);
1516 HInstruction* cond = if_instr->InputAt(0);
1517 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
1518 locations->SetInAt(0, Location::Any());
1519 }
1520}
1521
1522void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
1523 Label* true_target = codegen_->GetLabelOf(if_instr->IfTrueSuccessor());
1524 Label* false_target = codegen_->GetLabelOf(if_instr->IfFalseSuccessor());
1525 Label* always_true_target = true_target;
1526 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1527 if_instr->IfTrueSuccessor())) {
1528 always_true_target = nullptr;
1529 }
1530 if (codegen_->GoesToNextBlock(if_instr->GetBlock(),
1531 if_instr->IfFalseSuccessor())) {
1532 false_target = nullptr;
1533 }
1534 GenerateTestAndBranch(if_instr, true_target, false_target, always_true_target);
1535}
1536
1537void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1538 LocationSummary* locations = new (GetGraph()->GetArena())
1539 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
1540 HInstruction* cond = deoptimize->InputAt(0);
Aart Bikbb245d12015-10-19 11:05:03 -07001541 if (!cond->IsCondition() || cond->AsCondition()->NeedsMaterialization()) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001542 locations->SetInAt(0, Location::Any());
1543 }
1544}
1545
1546void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001547 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001548 DeoptimizationSlowPathX86(deoptimize);
1549 codegen_->AddSlowPath(slow_path);
1550 Label* slow_path_entry = slow_path->GetEntryLabel();
1551 GenerateTestAndBranch(deoptimize, slow_path_entry, nullptr, slow_path_entry);
1552}
1553
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001554void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001555 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001556}
1557
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001558void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1559 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001560}
1561
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001562void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001563 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001564}
1565
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001566void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001567 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001568}
1569
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001570void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001571 LocationSummary* locations =
1572 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 switch (store->InputAt(1)->GetType()) {
1574 case Primitive::kPrimBoolean:
1575 case Primitive::kPrimByte:
1576 case Primitive::kPrimChar:
1577 case Primitive::kPrimShort:
1578 case Primitive::kPrimInt:
1579 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001580 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001581 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1582 break;
1583
1584 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001585 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001586 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1587 break;
1588
1589 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001590 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001592}
1593
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001594void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001595}
1596
Roland Levillain0d37cd02015-05-27 16:39:19 +01001597void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001598 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001599 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001600 // Handle the long/FP comparisons made in instruction simplification.
1601 switch (cond->InputAt(0)->GetType()) {
1602 case Primitive::kPrimLong: {
1603 locations->SetInAt(0, Location::RequiresRegister());
1604 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1605 if (cond->NeedsMaterialization()) {
1606 locations->SetOut(Location::RequiresRegister());
1607 }
1608 break;
1609 }
1610 case Primitive::kPrimFloat:
1611 case Primitive::kPrimDouble: {
1612 locations->SetInAt(0, Location::RequiresFpuRegister());
1613 locations->SetInAt(1, Location::RequiresFpuRegister());
1614 if (cond->NeedsMaterialization()) {
1615 locations->SetOut(Location::RequiresRegister());
1616 }
1617 break;
1618 }
1619 default:
1620 locations->SetInAt(0, Location::RequiresRegister());
1621 locations->SetInAt(1, Location::Any());
1622 if (cond->NeedsMaterialization()) {
1623 // We need a byte register.
1624 locations->SetOut(Location::RegisterLocation(ECX));
1625 }
1626 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001627 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001628}
1629
Roland Levillain0d37cd02015-05-27 16:39:19 +01001630void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001631 if (!cond->NeedsMaterialization()) {
1632 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001633 }
Mark Mendellc4701932015-04-10 13:18:51 -04001634
1635 LocationSummary* locations = cond->GetLocations();
1636 Location lhs = locations->InAt(0);
1637 Location rhs = locations->InAt(1);
1638 Register reg = locations->Out().AsRegister<Register>();
1639 Label true_label, false_label;
1640
1641 switch (cond->InputAt(0)->GetType()) {
1642 default: {
1643 // Integer case.
1644
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001645 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001646 __ xorl(reg, reg);
1647
1648 if (rhs.IsRegister()) {
1649 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1650 } else if (rhs.IsConstant()) {
1651 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1652 if (constant == 0) {
1653 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1654 } else {
1655 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1656 }
1657 } else {
1658 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1659 }
Aart Bike9f37602015-10-09 11:15:55 -07001660 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001661 return;
1662 }
1663 case Primitive::kPrimLong:
1664 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1665 break;
1666 case Primitive::kPrimFloat:
1667 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1668 GenerateFPJumps(cond, &true_label, &false_label);
1669 break;
1670 case Primitive::kPrimDouble:
1671 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1672 GenerateFPJumps(cond, &true_label, &false_label);
1673 break;
1674 }
1675
1676 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001677 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001678
Roland Levillain4fa13f62015-07-06 18:11:54 +01001679 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001680 __ Bind(&false_label);
1681 __ xorl(reg, reg);
1682 __ jmp(&done_label);
1683
Roland Levillain4fa13f62015-07-06 18:11:54 +01001684 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001685 __ Bind(&true_label);
1686 __ movl(reg, Immediate(1));
1687 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001688}
1689
1690void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1691 VisitCondition(comp);
1692}
1693
1694void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1695 VisitCondition(comp);
1696}
1697
1698void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1699 VisitCondition(comp);
1700}
1701
1702void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1703 VisitCondition(comp);
1704}
1705
1706void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1707 VisitCondition(comp);
1708}
1709
1710void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1711 VisitCondition(comp);
1712}
1713
1714void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1715 VisitCondition(comp);
1716}
1717
1718void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1719 VisitCondition(comp);
1720}
1721
1722void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1723 VisitCondition(comp);
1724}
1725
1726void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1727 VisitCondition(comp);
1728}
1729
1730void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1731 VisitCondition(comp);
1732}
1733
1734void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1735 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001736}
1737
Aart Bike9f37602015-10-09 11:15:55 -07001738void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1739 VisitCondition(comp);
1740}
1741
1742void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1743 VisitCondition(comp);
1744}
1745
1746void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1747 VisitCondition(comp);
1748}
1749
1750void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1751 VisitCondition(comp);
1752}
1753
1754void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1755 VisitCondition(comp);
1756}
1757
1758void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1759 VisitCondition(comp);
1760}
1761
1762void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1763 VisitCondition(comp);
1764}
1765
1766void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1767 VisitCondition(comp);
1768}
1769
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001770void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001771 LocationSummary* locations =
1772 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001773 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001774}
1775
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001776void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001777 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001778}
1779
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001780void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1781 LocationSummary* locations =
1782 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1783 locations->SetOut(Location::ConstantLocation(constant));
1784}
1785
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001786void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001787 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001788}
1789
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001790void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001791 LocationSummary* locations =
1792 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001793 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001794}
1795
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001796void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001797 // Will be generated at use site.
1798}
1799
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001800void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1801 LocationSummary* locations =
1802 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1803 locations->SetOut(Location::ConstantLocation(constant));
1804}
1805
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001806void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001807 // Will be generated at use site.
1808}
1809
1810void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1811 LocationSummary* locations =
1812 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1813 locations->SetOut(Location::ConstantLocation(constant));
1814}
1815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001816void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001817 // Will be generated at use site.
1818}
1819
Calin Juravle27df7582015-04-17 19:12:31 +01001820void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1821 memory_barrier->SetLocations(nullptr);
1822}
1823
1824void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1825 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1826}
1827
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001828void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001829 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001830}
1831
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001832void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001833 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001834}
1835
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001836void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001837 LocationSummary* locations =
1838 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001839 switch (ret->InputAt(0)->GetType()) {
1840 case Primitive::kPrimBoolean:
1841 case Primitive::kPrimByte:
1842 case Primitive::kPrimChar:
1843 case Primitive::kPrimShort:
1844 case Primitive::kPrimInt:
1845 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001846 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001847 break;
1848
1849 case Primitive::kPrimLong:
1850 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001851 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001852 break;
1853
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001854 case Primitive::kPrimFloat:
1855 case Primitive::kPrimDouble:
1856 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001857 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001858 break;
1859
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001861 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001863}
1864
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001865void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001866 if (kIsDebugBuild) {
1867 switch (ret->InputAt(0)->GetType()) {
1868 case Primitive::kPrimBoolean:
1869 case Primitive::kPrimByte:
1870 case Primitive::kPrimChar:
1871 case Primitive::kPrimShort:
1872 case Primitive::kPrimInt:
1873 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001874 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 break;
1876
1877 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001878 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1879 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001880 break;
1881
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 case Primitive::kPrimFloat:
1883 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001884 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 break;
1886
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 }
1890 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001891 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001892}
1893
Calin Juravle175dc732015-08-25 15:42:32 +01001894void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1895 // The trampoline uses the same calling convention as dex calling conventions,
1896 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1897 // the method_idx.
1898 HandleInvoke(invoke);
1899}
1900
1901void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1902 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1903}
1904
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001905void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001906 // When we do not run baseline, explicit clinit checks triggered by static
1907 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1908 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001909
Mark Mendellfb8d2792015-03-31 22:16:59 -04001910 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001911 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001912 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
1913 invoke->GetLocations()->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::Any());
1914 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001915 return;
1916 }
1917
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001918 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001919
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001920 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1921 if (invoke->HasPcRelativeDexCache()) {
1922 invoke->GetLocations()->SetInAt(invoke->GetCurrentMethodInputIndex(),
1923 Location::RequiresRegister());
1924 }
1925
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001926 if (codegen_->IsBaseline()) {
1927 // Baseline does not have enough registers if the current method also
1928 // needs a register. We therefore do not require a register for it, and let
1929 // the code generation of the invoke handle it.
1930 LocationSummary* locations = invoke->GetLocations();
1931 Location location = locations->InAt(invoke->GetCurrentMethodInputIndex());
1932 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
1933 locations->SetInAt(invoke->GetCurrentMethodInputIndex(), Location::NoLocation());
1934 }
1935 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001936}
1937
Mark Mendell09ed1a32015-03-25 08:30:06 -04001938static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1939 if (invoke->GetLocations()->Intrinsified()) {
1940 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1941 intrinsic.Dispatch(invoke);
1942 return true;
1943 }
1944 return false;
1945}
1946
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001947void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001948 // When we do not run baseline, explicit clinit checks triggered by static
1949 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1950 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001951
Mark Mendell09ed1a32015-03-25 08:30:06 -04001952 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1953 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001954 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001955
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001956 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001957 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001958 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001959 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001960}
1961
1962void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
1963 HandleInvoke(invoke);
1964}
1965
1966void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001967 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001968 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001969}
1970
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001971void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001972 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1973 return;
1974 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001975
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001976 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001977 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001978 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001979}
1980
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001981void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001982 // This call to HandleInvoke allocates a temporary (core) register
1983 // which is also used to transfer the hidden argument from FP to
1984 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001985 HandleInvoke(invoke);
1986 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001987 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001988}
1989
1990void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1991 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00001992 LocationSummary* locations = invoke->GetLocations();
1993 Register temp = locations->GetTemp(0).AsRegister<Register>();
1994 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001995 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1996 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001997 Location receiver = locations->InAt(0);
1998 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1999
Roland Levillain0d5a2812015-11-13 10:07:31 +00002000 // Set the hidden argument. This is safe to do this here, as XMM7
2001 // won't be modified thereafter, before the `call` instruction.
2002 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002003 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002004 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002005
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002006 if (receiver.IsStackSlot()) {
2007 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002008 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002009 __ movl(temp, Address(temp, class_offset));
2010 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002011 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002012 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002013 }
Roland Levillain4d027112015-07-01 15:41:14 +01002014 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002015 // Instead of simply (possibly) unpoisoning `temp` here, we should
2016 // emit a read barrier for the previous class reference load.
2017 // However this is not required in practice, as this is an
2018 // intermediate/temporary reference and because the current
2019 // concurrent copying collector keeps the from-space memory
2020 // intact/accessible until the end of the marking phase (the
2021 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002022 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 // temp = temp->GetImtEntryAt(method_offset);
2024 __ movl(temp, Address(temp, method_offset));
2025 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 __ call(Address(temp,
2027 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028
2029 DCHECK(!codegen_->IsLeafMethod());
2030 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2031}
2032
Roland Levillain88cb1752014-10-20 16:36:47 +01002033void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2034 LocationSummary* locations =
2035 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2036 switch (neg->GetResultType()) {
2037 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002038 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002039 locations->SetInAt(0, Location::RequiresRegister());
2040 locations->SetOut(Location::SameAsFirstInput());
2041 break;
2042
Roland Levillain88cb1752014-10-20 16:36:47 +01002043 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002044 locations->SetInAt(0, Location::RequiresFpuRegister());
2045 locations->SetOut(Location::SameAsFirstInput());
2046 locations->AddTemp(Location::RequiresRegister());
2047 locations->AddTemp(Location::RequiresFpuRegister());
2048 break;
2049
Roland Levillain88cb1752014-10-20 16:36:47 +01002050 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002051 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002052 locations->SetOut(Location::SameAsFirstInput());
2053 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002054 break;
2055
2056 default:
2057 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2058 }
2059}
2060
2061void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2062 LocationSummary* locations = neg->GetLocations();
2063 Location out = locations->Out();
2064 Location in = locations->InAt(0);
2065 switch (neg->GetResultType()) {
2066 case Primitive::kPrimInt:
2067 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002068 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002069 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002070 break;
2071
2072 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002073 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002074 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002075 __ negl(out.AsRegisterPairLow<Register>());
2076 // Negation is similar to subtraction from zero. The least
2077 // significant byte triggers a borrow when it is different from
2078 // zero; to take it into account, add 1 to the most significant
2079 // byte if the carry flag (CF) is set to 1 after the first NEGL
2080 // operation.
2081 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2082 __ negl(out.AsRegisterPairHigh<Register>());
2083 break;
2084
Roland Levillain5368c212014-11-27 15:03:41 +00002085 case Primitive::kPrimFloat: {
2086 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 Register constant = locations->GetTemp(0).AsRegister<Register>();
2088 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002089 // Implement float negation with an exclusive or with value
2090 // 0x80000000 (mask for bit 31, representing the sign of a
2091 // single-precision floating-point number).
2092 __ movl(constant, Immediate(INT32_C(0x80000000)));
2093 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002095 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002096 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002097
Roland Levillain5368c212014-11-27 15:03:41 +00002098 case Primitive::kPrimDouble: {
2099 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002100 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002101 // Implement double negation with an exclusive or with value
2102 // 0x8000000000000000 (mask for bit 63, representing the sign of
2103 // a double-precision floating-point number).
2104 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002105 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002106 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002107 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002108
2109 default:
2110 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2111 }
2112}
2113
Roland Levillaindff1f282014-11-05 14:15:05 +00002114void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002115 Primitive::Type result_type = conversion->GetResultType();
2116 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002117 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002118
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002119 // The float-to-long and double-to-long type conversions rely on a
2120 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002121 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002122 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2123 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002124 ? LocationSummary::kCall
2125 : LocationSummary::kNoCall;
2126 LocationSummary* locations =
2127 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2128
David Brazdilb2bd1c52015-03-25 11:17:37 +00002129 // The Java language does not allow treating boolean as an integral type but
2130 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002131
Roland Levillaindff1f282014-11-05 14:15:05 +00002132 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002133 case Primitive::kPrimByte:
2134 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002135 case Primitive::kPrimBoolean:
2136 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002137 case Primitive::kPrimShort:
2138 case Primitive::kPrimInt:
2139 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002140 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002141 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2142 // Make the output overlap to please the register allocator. This greatly simplifies
2143 // the validation of the linear scan implementation
2144 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002145 break;
2146
2147 default:
2148 LOG(FATAL) << "Unexpected type conversion from " << input_type
2149 << " to " << result_type;
2150 }
2151 break;
2152
Roland Levillain01a8d712014-11-14 16:27:39 +00002153 case Primitive::kPrimShort:
2154 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002155 case Primitive::kPrimBoolean:
2156 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002157 case Primitive::kPrimByte:
2158 case Primitive::kPrimInt:
2159 case Primitive::kPrimChar:
2160 // Processing a Dex `int-to-short' instruction.
2161 locations->SetInAt(0, Location::Any());
2162 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2163 break;
2164
2165 default:
2166 LOG(FATAL) << "Unexpected type conversion from " << input_type
2167 << " to " << result_type;
2168 }
2169 break;
2170
Roland Levillain946e1432014-11-11 17:35:19 +00002171 case Primitive::kPrimInt:
2172 switch (input_type) {
2173 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002174 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002175 locations->SetInAt(0, Location::Any());
2176 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2177 break;
2178
2179 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002180 // Processing a Dex `float-to-int' instruction.
2181 locations->SetInAt(0, Location::RequiresFpuRegister());
2182 locations->SetOut(Location::RequiresRegister());
2183 locations->AddTemp(Location::RequiresFpuRegister());
2184 break;
2185
Roland Levillain946e1432014-11-11 17:35:19 +00002186 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002187 // Processing a Dex `double-to-int' instruction.
2188 locations->SetInAt(0, Location::RequiresFpuRegister());
2189 locations->SetOut(Location::RequiresRegister());
2190 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002191 break;
2192
2193 default:
2194 LOG(FATAL) << "Unexpected type conversion from " << input_type
2195 << " to " << result_type;
2196 }
2197 break;
2198
Roland Levillaindff1f282014-11-05 14:15:05 +00002199 case Primitive::kPrimLong:
2200 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002201 case Primitive::kPrimBoolean:
2202 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002203 case Primitive::kPrimByte:
2204 case Primitive::kPrimShort:
2205 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002206 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002207 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002208 locations->SetInAt(0, Location::RegisterLocation(EAX));
2209 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2210 break;
2211
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002212 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002213 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002214 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002215 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002216 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2217 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2218
Vladimir Marko949c91f2015-01-27 10:48:44 +00002219 // The runtime helper puts the result in EAX, EDX.
2220 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002221 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002222 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002223
2224 default:
2225 LOG(FATAL) << "Unexpected type conversion from " << input_type
2226 << " to " << result_type;
2227 }
2228 break;
2229
Roland Levillain981e4542014-11-14 11:47:14 +00002230 case Primitive::kPrimChar:
2231 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002232 case Primitive::kPrimBoolean:
2233 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002234 case Primitive::kPrimByte:
2235 case Primitive::kPrimShort:
2236 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002237 // Processing a Dex `int-to-char' instruction.
2238 locations->SetInAt(0, Location::Any());
2239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2240 break;
2241
2242 default:
2243 LOG(FATAL) << "Unexpected type conversion from " << input_type
2244 << " to " << result_type;
2245 }
2246 break;
2247
Roland Levillaindff1f282014-11-05 14:15:05 +00002248 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002249 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002250 case Primitive::kPrimBoolean:
2251 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002252 case Primitive::kPrimByte:
2253 case Primitive::kPrimShort:
2254 case Primitive::kPrimInt:
2255 case Primitive::kPrimChar:
2256 // Processing a Dex `int-to-float' instruction.
2257 locations->SetInAt(0, Location::RequiresRegister());
2258 locations->SetOut(Location::RequiresFpuRegister());
2259 break;
2260
2261 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002262 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002263 locations->SetInAt(0, Location::Any());
2264 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002265 break;
2266
Roland Levillaincff13742014-11-17 14:32:17 +00002267 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002268 // Processing a Dex `double-to-float' instruction.
2269 locations->SetInAt(0, Location::RequiresFpuRegister());
2270 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002271 break;
2272
2273 default:
2274 LOG(FATAL) << "Unexpected type conversion from " << input_type
2275 << " to " << result_type;
2276 };
2277 break;
2278
Roland Levillaindff1f282014-11-05 14:15:05 +00002279 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002280 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002281 case Primitive::kPrimBoolean:
2282 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002283 case Primitive::kPrimByte:
2284 case Primitive::kPrimShort:
2285 case Primitive::kPrimInt:
2286 case Primitive::kPrimChar:
2287 // Processing a Dex `int-to-double' instruction.
2288 locations->SetInAt(0, Location::RequiresRegister());
2289 locations->SetOut(Location::RequiresFpuRegister());
2290 break;
2291
2292 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002293 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002294 locations->SetInAt(0, Location::Any());
2295 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002296 break;
2297
Roland Levillaincff13742014-11-17 14:32:17 +00002298 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002299 // Processing a Dex `float-to-double' instruction.
2300 locations->SetInAt(0, Location::RequiresFpuRegister());
2301 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002302 break;
2303
2304 default:
2305 LOG(FATAL) << "Unexpected type conversion from " << input_type
2306 << " to " << result_type;
2307 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002308 break;
2309
2310 default:
2311 LOG(FATAL) << "Unexpected type conversion from " << input_type
2312 << " to " << result_type;
2313 }
2314}
2315
2316void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2317 LocationSummary* locations = conversion->GetLocations();
2318 Location out = locations->Out();
2319 Location in = locations->InAt(0);
2320 Primitive::Type result_type = conversion->GetResultType();
2321 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002322 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002323 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002324 case Primitive::kPrimByte:
2325 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002326 case Primitive::kPrimBoolean:
2327 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002328 case Primitive::kPrimShort:
2329 case Primitive::kPrimInt:
2330 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002331 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002332 if (in.IsRegister()) {
2333 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002334 } else {
2335 DCHECK(in.GetConstant()->IsIntConstant());
2336 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2337 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2338 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002339 break;
2340
2341 default:
2342 LOG(FATAL) << "Unexpected type conversion from " << input_type
2343 << " to " << result_type;
2344 }
2345 break;
2346
Roland Levillain01a8d712014-11-14 16:27:39 +00002347 case Primitive::kPrimShort:
2348 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002349 case Primitive::kPrimBoolean:
2350 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002351 case Primitive::kPrimByte:
2352 case Primitive::kPrimInt:
2353 case Primitive::kPrimChar:
2354 // Processing a Dex `int-to-short' instruction.
2355 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002356 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002357 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002358 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002359 } else {
2360 DCHECK(in.GetConstant()->IsIntConstant());
2361 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002362 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002363 }
2364 break;
2365
2366 default:
2367 LOG(FATAL) << "Unexpected type conversion from " << input_type
2368 << " to " << result_type;
2369 }
2370 break;
2371
Roland Levillain946e1432014-11-11 17:35:19 +00002372 case Primitive::kPrimInt:
2373 switch (input_type) {
2374 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002375 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002376 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002377 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002378 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002379 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002380 } else {
2381 DCHECK(in.IsConstant());
2382 DCHECK(in.GetConstant()->IsLongConstant());
2383 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002384 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002385 }
2386 break;
2387
Roland Levillain3f8f9362014-12-02 17:45:01 +00002388 case Primitive::kPrimFloat: {
2389 // Processing a Dex `float-to-int' instruction.
2390 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2391 Register output = out.AsRegister<Register>();
2392 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002393 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002394
2395 __ movl(output, Immediate(kPrimIntMax));
2396 // temp = int-to-float(output)
2397 __ cvtsi2ss(temp, output);
2398 // if input >= temp goto done
2399 __ comiss(input, temp);
2400 __ j(kAboveEqual, &done);
2401 // if input == NaN goto nan
2402 __ j(kUnordered, &nan);
2403 // output = float-to-int-truncate(input)
2404 __ cvttss2si(output, input);
2405 __ jmp(&done);
2406 __ Bind(&nan);
2407 // output = 0
2408 __ xorl(output, output);
2409 __ Bind(&done);
2410 break;
2411 }
2412
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002413 case Primitive::kPrimDouble: {
2414 // Processing a Dex `double-to-int' instruction.
2415 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2416 Register output = out.AsRegister<Register>();
2417 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002418 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002419
2420 __ movl(output, Immediate(kPrimIntMax));
2421 // temp = int-to-double(output)
2422 __ cvtsi2sd(temp, output);
2423 // if input >= temp goto done
2424 __ comisd(input, temp);
2425 __ j(kAboveEqual, &done);
2426 // if input == NaN goto nan
2427 __ j(kUnordered, &nan);
2428 // output = double-to-int-truncate(input)
2429 __ cvttsd2si(output, input);
2430 __ jmp(&done);
2431 __ Bind(&nan);
2432 // output = 0
2433 __ xorl(output, output);
2434 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002435 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002436 }
Roland Levillain946e1432014-11-11 17:35:19 +00002437
2438 default:
2439 LOG(FATAL) << "Unexpected type conversion from " << input_type
2440 << " to " << result_type;
2441 }
2442 break;
2443
Roland Levillaindff1f282014-11-05 14:15:05 +00002444 case Primitive::kPrimLong:
2445 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002448 case Primitive::kPrimByte:
2449 case Primitive::kPrimShort:
2450 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002451 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002453 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2454 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002456 __ cdq();
2457 break;
2458
2459 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002460 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002461 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2462 conversion,
2463 conversion->GetDexPc(),
2464 nullptr);
Roland Levillain624279f2014-12-04 11:54:28 +00002465 break;
2466
Roland Levillaindff1f282014-11-05 14:15:05 +00002467 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002468 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002469 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2470 conversion,
2471 conversion->GetDexPc(),
2472 nullptr);
Roland Levillaindff1f282014-11-05 14:15:05 +00002473 break;
2474
2475 default:
2476 LOG(FATAL) << "Unexpected type conversion from " << input_type
2477 << " to " << result_type;
2478 }
2479 break;
2480
Roland Levillain981e4542014-11-14 11:47:14 +00002481 case Primitive::kPrimChar:
2482 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002483 case Primitive::kPrimBoolean:
2484 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002485 case Primitive::kPrimByte:
2486 case Primitive::kPrimShort:
2487 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002488 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2489 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002490 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002491 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002492 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002493 } else {
2494 DCHECK(in.GetConstant()->IsIntConstant());
2495 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002496 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002497 }
2498 break;
2499
2500 default:
2501 LOG(FATAL) << "Unexpected type conversion from " << input_type
2502 << " to " << result_type;
2503 }
2504 break;
2505
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002507 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002508 case Primitive::kPrimBoolean:
2509 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002510 case Primitive::kPrimByte:
2511 case Primitive::kPrimShort:
2512 case Primitive::kPrimInt:
2513 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002514 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002515 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002516 break;
2517
Roland Levillain6d0e4832014-11-27 18:31:21 +00002518 case Primitive::kPrimLong: {
2519 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002520 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002521
Roland Levillain232ade02015-04-20 15:14:36 +01002522 // Create stack space for the call to
2523 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2524 // TODO: enhance register allocator to ask for stack temporaries.
2525 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2526 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2527 __ subl(ESP, Immediate(adjustment));
2528 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002529
Roland Levillain232ade02015-04-20 15:14:36 +01002530 // Load the value to the FP stack, using temporaries if needed.
2531 PushOntoFPStack(in, 0, adjustment, false, true);
2532
2533 if (out.IsStackSlot()) {
2534 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2535 } else {
2536 __ fstps(Address(ESP, 0));
2537 Location stack_temp = Location::StackSlot(0);
2538 codegen_->Move32(out, stack_temp);
2539 }
2540
2541 // Remove the temporary stack space we allocated.
2542 if (adjustment != 0) {
2543 __ addl(ESP, Immediate(adjustment));
2544 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002545 break;
2546 }
2547
Roland Levillaincff13742014-11-17 14:32:17 +00002548 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002549 // Processing a Dex `double-to-float' instruction.
2550 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002551 break;
2552
2553 default:
2554 LOG(FATAL) << "Unexpected type conversion from " << input_type
2555 << " to " << result_type;
2556 };
2557 break;
2558
Roland Levillaindff1f282014-11-05 14:15:05 +00002559 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002560 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002561 case Primitive::kPrimBoolean:
2562 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002563 case Primitive::kPrimByte:
2564 case Primitive::kPrimShort:
2565 case Primitive::kPrimInt:
2566 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002567 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002568 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002569 break;
2570
Roland Levillain647b9ed2014-11-27 12:06:00 +00002571 case Primitive::kPrimLong: {
2572 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002573 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002574
Roland Levillain232ade02015-04-20 15:14:36 +01002575 // Create stack space for the call to
2576 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2577 // TODO: enhance register allocator to ask for stack temporaries.
2578 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2579 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2580 __ subl(ESP, Immediate(adjustment));
2581 }
2582
2583 // Load the value to the FP stack, using temporaries if needed.
2584 PushOntoFPStack(in, 0, adjustment, false, true);
2585
2586 if (out.IsDoubleStackSlot()) {
2587 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2588 } else {
2589 __ fstpl(Address(ESP, 0));
2590 Location stack_temp = Location::DoubleStackSlot(0);
2591 codegen_->Move64(out, stack_temp);
2592 }
2593
2594 // Remove the temporary stack space we allocated.
2595 if (adjustment != 0) {
2596 __ addl(ESP, Immediate(adjustment));
2597 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002598 break;
2599 }
2600
Roland Levillaincff13742014-11-17 14:32:17 +00002601 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002602 // Processing a Dex `float-to-double' instruction.
2603 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002604 break;
2605
2606 default:
2607 LOG(FATAL) << "Unexpected type conversion from " << input_type
2608 << " to " << result_type;
2609 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002610 break;
2611
2612 default:
2613 LOG(FATAL) << "Unexpected type conversion from " << input_type
2614 << " to " << result_type;
2615 }
2616}
2617
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002618void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002619 LocationSummary* locations =
2620 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002621 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002622 case Primitive::kPrimInt: {
2623 locations->SetInAt(0, Location::RequiresRegister());
2624 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2625 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2626 break;
2627 }
2628
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002629 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002630 locations->SetInAt(0, Location::RequiresRegister());
2631 locations->SetInAt(1, Location::Any());
2632 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002633 break;
2634 }
2635
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002636 case Primitive::kPrimFloat:
2637 case Primitive::kPrimDouble: {
2638 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002639 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002640 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002641 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002642 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002643
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002644 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002645 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2646 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002647 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002648}
2649
2650void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2651 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002652 Location first = locations->InAt(0);
2653 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002654 Location out = locations->Out();
2655
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002656 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002657 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002658 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002659 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2660 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002661 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2662 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002663 } else {
2664 __ leal(out.AsRegister<Register>(), Address(
2665 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2666 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002667 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002668 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2669 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2670 __ addl(out.AsRegister<Register>(), Immediate(value));
2671 } else {
2672 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2673 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002674 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002675 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002676 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002677 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002678 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002679 }
2680
2681 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002682 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002683 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2684 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002685 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002686 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2687 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002688 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002689 } else {
2690 DCHECK(second.IsConstant()) << second;
2691 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2692 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2693 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002694 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002695 break;
2696 }
2697
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002698 case Primitive::kPrimFloat: {
2699 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002700 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002701 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2702 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2703 DCHECK(!const_area->NeedsMaterialization());
2704 __ addss(first.AsFpuRegister<XmmRegister>(),
2705 codegen_->LiteralFloatAddress(
2706 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2707 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2708 } else {
2709 DCHECK(second.IsStackSlot());
2710 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002711 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002712 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002713 }
2714
2715 case Primitive::kPrimDouble: {
2716 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002717 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002718 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2719 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2720 DCHECK(!const_area->NeedsMaterialization());
2721 __ addsd(first.AsFpuRegister<XmmRegister>(),
2722 codegen_->LiteralDoubleAddress(
2723 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2724 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2725 } else {
2726 DCHECK(second.IsDoubleStackSlot());
2727 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002728 }
2729 break;
2730 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002732 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002733 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002734 }
2735}
2736
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002737void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002738 LocationSummary* locations =
2739 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002740 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002741 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002742 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002743 locations->SetInAt(0, Location::RequiresRegister());
2744 locations->SetInAt(1, Location::Any());
2745 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002746 break;
2747 }
Calin Juravle11351682014-10-23 15:38:15 +01002748 case Primitive::kPrimFloat:
2749 case Primitive::kPrimDouble: {
2750 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002751 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002752 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002753 break;
Calin Juravle11351682014-10-23 15:38:15 +01002754 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002755
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002756 default:
Calin Juravle11351682014-10-23 15:38:15 +01002757 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002758 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002759}
2760
2761void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2762 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002763 Location first = locations->InAt(0);
2764 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002765 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002766 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002767 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002768 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002769 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002770 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002771 __ subl(first.AsRegister<Register>(),
2772 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002773 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002774 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002775 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002776 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002777 }
2778
2779 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002780 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002781 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2782 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002783 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002784 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002785 __ sbbl(first.AsRegisterPairHigh<Register>(),
2786 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002787 } else {
2788 DCHECK(second.IsConstant()) << second;
2789 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2790 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2791 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002792 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002793 break;
2794 }
2795
Calin Juravle11351682014-10-23 15:38:15 +01002796 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002797 if (second.IsFpuRegister()) {
2798 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2799 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2800 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2801 DCHECK(!const_area->NeedsMaterialization());
2802 __ subss(first.AsFpuRegister<XmmRegister>(),
2803 codegen_->LiteralFloatAddress(
2804 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2805 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2806 } else {
2807 DCHECK(second.IsStackSlot());
2808 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2809 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002810 break;
Calin Juravle11351682014-10-23 15:38:15 +01002811 }
2812
2813 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002814 if (second.IsFpuRegister()) {
2815 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2816 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2817 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2818 DCHECK(!const_area->NeedsMaterialization());
2819 __ subsd(first.AsFpuRegister<XmmRegister>(),
2820 codegen_->LiteralDoubleAddress(
2821 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2822 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2823 } else {
2824 DCHECK(second.IsDoubleStackSlot());
2825 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2826 }
Calin Juravle11351682014-10-23 15:38:15 +01002827 break;
2828 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002829
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002830 default:
Calin Juravle11351682014-10-23 15:38:15 +01002831 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002832 }
2833}
2834
Calin Juravle34bacdf2014-10-07 20:23:36 +01002835void LocationsBuilderX86::VisitMul(HMul* mul) {
2836 LocationSummary* locations =
2837 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2838 switch (mul->GetResultType()) {
2839 case Primitive::kPrimInt:
2840 locations->SetInAt(0, Location::RequiresRegister());
2841 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002842 if (mul->InputAt(1)->IsIntConstant()) {
2843 // Can use 3 operand multiply.
2844 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2845 } else {
2846 locations->SetOut(Location::SameAsFirstInput());
2847 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002848 break;
2849 case Primitive::kPrimLong: {
2850 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002851 locations->SetInAt(1, Location::Any());
2852 locations->SetOut(Location::SameAsFirstInput());
2853 // Needed for imul on 32bits with 64bits output.
2854 locations->AddTemp(Location::RegisterLocation(EAX));
2855 locations->AddTemp(Location::RegisterLocation(EDX));
2856 break;
2857 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002858 case Primitive::kPrimFloat:
2859 case Primitive::kPrimDouble: {
2860 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002861 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002862 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002863 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002864 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002865
2866 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002867 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002868 }
2869}
2870
2871void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2872 LocationSummary* locations = mul->GetLocations();
2873 Location first = locations->InAt(0);
2874 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002875 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002876
2877 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002878 case Primitive::kPrimInt:
2879 // The constant may have ended up in a register, so test explicitly to avoid
2880 // problems where the output may not be the same as the first operand.
2881 if (mul->InputAt(1)->IsIntConstant()) {
2882 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2883 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2884 } else if (second.IsRegister()) {
2885 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002886 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002887 } else {
2888 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002889 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002890 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002891 }
2892 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002893
2894 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002895 Register in1_hi = first.AsRegisterPairHigh<Register>();
2896 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 Register eax = locations->GetTemp(0).AsRegister<Register>();
2898 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002899
2900 DCHECK_EQ(EAX, eax);
2901 DCHECK_EQ(EDX, edx);
2902
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002903 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002904 // output: in1
2905 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2906 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2907 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002908 if (second.IsConstant()) {
2909 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002910
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002911 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2912 int32_t low_value = Low32Bits(value);
2913 int32_t high_value = High32Bits(value);
2914 Immediate low(low_value);
2915 Immediate high(high_value);
2916
2917 __ movl(eax, high);
2918 // eax <- in1.lo * in2.hi
2919 __ imull(eax, in1_lo);
2920 // in1.hi <- in1.hi * in2.lo
2921 __ imull(in1_hi, low);
2922 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2923 __ addl(in1_hi, eax);
2924 // move in2_lo to eax to prepare for double precision
2925 __ movl(eax, low);
2926 // edx:eax <- in1.lo * in2.lo
2927 __ mull(in1_lo);
2928 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2929 __ addl(in1_hi, edx);
2930 // in1.lo <- (in1.lo * in2.lo)[31:0];
2931 __ movl(in1_lo, eax);
2932 } else if (second.IsRegisterPair()) {
2933 Register in2_hi = second.AsRegisterPairHigh<Register>();
2934 Register in2_lo = second.AsRegisterPairLow<Register>();
2935
2936 __ movl(eax, in2_hi);
2937 // eax <- in1.lo * in2.hi
2938 __ imull(eax, in1_lo);
2939 // in1.hi <- in1.hi * in2.lo
2940 __ imull(in1_hi, in2_lo);
2941 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2942 __ addl(in1_hi, eax);
2943 // move in1_lo to eax to prepare for double precision
2944 __ movl(eax, in1_lo);
2945 // edx:eax <- in1.lo * in2.lo
2946 __ mull(in2_lo);
2947 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2948 __ addl(in1_hi, edx);
2949 // in1.lo <- (in1.lo * in2.lo)[31:0];
2950 __ movl(in1_lo, eax);
2951 } else {
2952 DCHECK(second.IsDoubleStackSlot()) << second;
2953 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2954 Address in2_lo(ESP, second.GetStackIndex());
2955
2956 __ movl(eax, in2_hi);
2957 // eax <- in1.lo * in2.hi
2958 __ imull(eax, in1_lo);
2959 // in1.hi <- in1.hi * in2.lo
2960 __ imull(in1_hi, in2_lo);
2961 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2962 __ addl(in1_hi, eax);
2963 // move in1_lo to eax to prepare for double precision
2964 __ movl(eax, in1_lo);
2965 // edx:eax <- in1.lo * in2.lo
2966 __ mull(in2_lo);
2967 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2968 __ addl(in1_hi, edx);
2969 // in1.lo <- (in1.lo * in2.lo)[31:0];
2970 __ movl(in1_lo, eax);
2971 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002972
2973 break;
2974 }
2975
Calin Juravleb5bfa962014-10-21 18:02:24 +01002976 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002977 DCHECK(first.Equals(locations->Out()));
2978 if (second.IsFpuRegister()) {
2979 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2980 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2981 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2982 DCHECK(!const_area->NeedsMaterialization());
2983 __ mulss(first.AsFpuRegister<XmmRegister>(),
2984 codegen_->LiteralFloatAddress(
2985 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2986 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2987 } else {
2988 DCHECK(second.IsStackSlot());
2989 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2990 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002991 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002992 }
2993
2994 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002995 DCHECK(first.Equals(locations->Out()));
2996 if (second.IsFpuRegister()) {
2997 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2998 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2999 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3000 DCHECK(!const_area->NeedsMaterialization());
3001 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3002 codegen_->LiteralDoubleAddress(
3003 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3004 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3005 } else {
3006 DCHECK(second.IsDoubleStackSlot());
3007 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3008 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003009 break;
3010 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003011
3012 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003013 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003014 }
3015}
3016
Roland Levillain232ade02015-04-20 15:14:36 +01003017void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3018 uint32_t temp_offset,
3019 uint32_t stack_adjustment,
3020 bool is_fp,
3021 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003022 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003023 DCHECK(!is_wide);
3024 if (is_fp) {
3025 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3026 } else {
3027 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3028 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003029 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003030 DCHECK(is_wide);
3031 if (is_fp) {
3032 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3033 } else {
3034 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3035 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003036 } else {
3037 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003038 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003039 Location stack_temp = Location::StackSlot(temp_offset);
3040 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003041 if (is_fp) {
3042 __ flds(Address(ESP, temp_offset));
3043 } else {
3044 __ filds(Address(ESP, temp_offset));
3045 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003046 } else {
3047 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3048 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003049 if (is_fp) {
3050 __ fldl(Address(ESP, temp_offset));
3051 } else {
3052 __ fildl(Address(ESP, temp_offset));
3053 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003054 }
3055 }
3056}
3057
3058void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3059 Primitive::Type type = rem->GetResultType();
3060 bool is_float = type == Primitive::kPrimFloat;
3061 size_t elem_size = Primitive::ComponentSize(type);
3062 LocationSummary* locations = rem->GetLocations();
3063 Location first = locations->InAt(0);
3064 Location second = locations->InAt(1);
3065 Location out = locations->Out();
3066
3067 // Create stack space for 2 elements.
3068 // TODO: enhance register allocator to ask for stack temporaries.
3069 __ subl(ESP, Immediate(2 * elem_size));
3070
3071 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003072 const bool is_wide = !is_float;
3073 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3074 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003075
3076 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003077 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003078 __ Bind(&retry);
3079 __ fprem();
3080
3081 // Move FP status to AX.
3082 __ fstsw();
3083
3084 // And see if the argument reduction is complete. This is signaled by the
3085 // C2 FPU flag bit set to 0.
3086 __ andl(EAX, Immediate(kC2ConditionMask));
3087 __ j(kNotEqual, &retry);
3088
3089 // We have settled on the final value. Retrieve it into an XMM register.
3090 // Store FP top of stack to real stack.
3091 if (is_float) {
3092 __ fsts(Address(ESP, 0));
3093 } else {
3094 __ fstl(Address(ESP, 0));
3095 }
3096
3097 // Pop the 2 items from the FP stack.
3098 __ fucompp();
3099
3100 // Load the value from the stack into an XMM register.
3101 DCHECK(out.IsFpuRegister()) << out;
3102 if (is_float) {
3103 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3104 } else {
3105 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3106 }
3107
3108 // And remove the temporary stack space we allocated.
3109 __ addl(ESP, Immediate(2 * elem_size));
3110}
3111
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003112
3113void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3114 DCHECK(instruction->IsDiv() || instruction->IsRem());
3115
3116 LocationSummary* locations = instruction->GetLocations();
3117 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003118 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003119
3120 Register out_register = locations->Out().AsRegister<Register>();
3121 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003122 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003123
3124 DCHECK(imm == 1 || imm == -1);
3125
3126 if (instruction->IsRem()) {
3127 __ xorl(out_register, out_register);
3128 } else {
3129 __ movl(out_register, input_register);
3130 if (imm == -1) {
3131 __ negl(out_register);
3132 }
3133 }
3134}
3135
3136
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003137void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003138 LocationSummary* locations = instruction->GetLocations();
3139
3140 Register out_register = locations->Out().AsRegister<Register>();
3141 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003142 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003143
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003144 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003145 Register num = locations->GetTemp(0).AsRegister<Register>();
3146
3147 __ leal(num, Address(input_register, std::abs(imm) - 1));
3148 __ testl(input_register, input_register);
3149 __ cmovl(kGreaterEqual, num, input_register);
3150 int shift = CTZ(imm);
3151 __ sarl(num, Immediate(shift));
3152
3153 if (imm < 0) {
3154 __ negl(num);
3155 }
3156
3157 __ movl(out_register, num);
3158}
3159
3160void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3161 DCHECK(instruction->IsDiv() || instruction->IsRem());
3162
3163 LocationSummary* locations = instruction->GetLocations();
3164 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3165
3166 Register eax = locations->InAt(0).AsRegister<Register>();
3167 Register out = locations->Out().AsRegister<Register>();
3168 Register num;
3169 Register edx;
3170
3171 if (instruction->IsDiv()) {
3172 edx = locations->GetTemp(0).AsRegister<Register>();
3173 num = locations->GetTemp(1).AsRegister<Register>();
3174 } else {
3175 edx = locations->Out().AsRegister<Register>();
3176 num = locations->GetTemp(0).AsRegister<Register>();
3177 }
3178
3179 DCHECK_EQ(EAX, eax);
3180 DCHECK_EQ(EDX, edx);
3181 if (instruction->IsDiv()) {
3182 DCHECK_EQ(EAX, out);
3183 } else {
3184 DCHECK_EQ(EDX, out);
3185 }
3186
3187 int64_t magic;
3188 int shift;
3189 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3190
Mark Mendell0c9497d2015-08-21 09:30:05 -04003191 NearLabel ndiv;
3192 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003193 // If numerator is 0, the result is 0, no computation needed.
3194 __ testl(eax, eax);
3195 __ j(kNotEqual, &ndiv);
3196
3197 __ xorl(out, out);
3198 __ jmp(&end);
3199
3200 __ Bind(&ndiv);
3201
3202 // Save the numerator.
3203 __ movl(num, eax);
3204
3205 // EAX = magic
3206 __ movl(eax, Immediate(magic));
3207
3208 // EDX:EAX = magic * numerator
3209 __ imull(num);
3210
3211 if (imm > 0 && magic < 0) {
3212 // EDX += num
3213 __ addl(edx, num);
3214 } else if (imm < 0 && magic > 0) {
3215 __ subl(edx, num);
3216 }
3217
3218 // Shift if needed.
3219 if (shift != 0) {
3220 __ sarl(edx, Immediate(shift));
3221 }
3222
3223 // EDX += 1 if EDX < 0
3224 __ movl(eax, edx);
3225 __ shrl(edx, Immediate(31));
3226 __ addl(edx, eax);
3227
3228 if (instruction->IsRem()) {
3229 __ movl(eax, num);
3230 __ imull(edx, Immediate(imm));
3231 __ subl(eax, edx);
3232 __ movl(edx, eax);
3233 } else {
3234 __ movl(eax, edx);
3235 }
3236 __ Bind(&end);
3237}
3238
Calin Juravlebacfec32014-11-14 15:54:36 +00003239void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3240 DCHECK(instruction->IsDiv() || instruction->IsRem());
3241
3242 LocationSummary* locations = instruction->GetLocations();
3243 Location out = locations->Out();
3244 Location first = locations->InAt(0);
3245 Location second = locations->InAt(1);
3246 bool is_div = instruction->IsDiv();
3247
3248 switch (instruction->GetResultType()) {
3249 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003250 DCHECK_EQ(EAX, first.AsRegister<Register>());
3251 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003252
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003253 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003254 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003255
3256 if (imm == 0) {
3257 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3258 } else if (imm == 1 || imm == -1) {
3259 DivRemOneOrMinusOne(instruction);
3260 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003261 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003262 } else {
3263 DCHECK(imm <= -2 || imm >= 2);
3264 GenerateDivRemWithAnyConstant(instruction);
3265 }
3266 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003267 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003268 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003269 is_div);
3270 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003271
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003272 Register second_reg = second.AsRegister<Register>();
3273 // 0x80000000/-1 triggers an arithmetic exception!
3274 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3275 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003276
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003277 __ cmpl(second_reg, Immediate(-1));
3278 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003279
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003280 // edx:eax <- sign-extended of eax
3281 __ cdq();
3282 // eax = quotient, edx = remainder
3283 __ idivl(second_reg);
3284 __ Bind(slow_path->GetExitLabel());
3285 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003286 break;
3287 }
3288
3289 case Primitive::kPrimLong: {
3290 InvokeRuntimeCallingConvention calling_convention;
3291 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3292 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3293 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3294 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3295 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3296 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3297
3298 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003299 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3300 instruction,
3301 instruction->GetDexPc(),
3302 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003303 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003304 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3305 instruction,
3306 instruction->GetDexPc(),
3307 nullptr);
Calin Juravlebacfec32014-11-14 15:54:36 +00003308 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003309 break;
3310 }
3311
3312 default:
3313 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3314 }
3315}
3316
Calin Juravle7c4954d2014-10-28 16:57:40 +00003317void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003318 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003319 ? LocationSummary::kCall
3320 : LocationSummary::kNoCall;
3321 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3322
Calin Juravle7c4954d2014-10-28 16:57:40 +00003323 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003324 case Primitive::kPrimInt: {
3325 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003326 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003327 locations->SetOut(Location::SameAsFirstInput());
3328 // Intel uses edx:eax as the dividend.
3329 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003330 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3331 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3332 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003333 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334 locations->AddTemp(Location::RequiresRegister());
3335 }
Calin Juravled0d48522014-11-04 16:40:20 +00003336 break;
3337 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003338 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003339 InvokeRuntimeCallingConvention calling_convention;
3340 locations->SetInAt(0, Location::RegisterPairLocation(
3341 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3342 locations->SetInAt(1, Location::RegisterPairLocation(
3343 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3344 // Runtime helper puts the result in EAX, EDX.
3345 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003346 break;
3347 }
3348 case Primitive::kPrimFloat:
3349 case Primitive::kPrimDouble: {
3350 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003351 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003352 locations->SetOut(Location::SameAsFirstInput());
3353 break;
3354 }
3355
3356 default:
3357 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3358 }
3359}
3360
3361void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3362 LocationSummary* locations = div->GetLocations();
3363 Location first = locations->InAt(0);
3364 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003365
3366 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003367 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003368 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003369 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003370 break;
3371 }
3372
3373 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003374 if (second.IsFpuRegister()) {
3375 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3376 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3377 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3378 DCHECK(!const_area->NeedsMaterialization());
3379 __ divss(first.AsFpuRegister<XmmRegister>(),
3380 codegen_->LiteralFloatAddress(
3381 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3382 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3383 } else {
3384 DCHECK(second.IsStackSlot());
3385 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3386 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003387 break;
3388 }
3389
3390 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003391 if (second.IsFpuRegister()) {
3392 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3393 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3394 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3395 DCHECK(!const_area->NeedsMaterialization());
3396 __ divsd(first.AsFpuRegister<XmmRegister>(),
3397 codegen_->LiteralDoubleAddress(
3398 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3399 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3400 } else {
3401 DCHECK(second.IsDoubleStackSlot());
3402 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3403 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003404 break;
3405 }
3406
3407 default:
3408 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3409 }
3410}
3411
Calin Juravlebacfec32014-11-14 15:54:36 +00003412void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003413 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003414
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003415 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3416 ? LocationSummary::kCall
3417 : LocationSummary::kNoCall;
3418 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003419
Calin Juravled2ec87d2014-12-08 14:24:46 +00003420 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003421 case Primitive::kPrimInt: {
3422 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003424 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3426 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3427 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003428 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003429 locations->AddTemp(Location::RequiresRegister());
3430 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003431 break;
3432 }
3433 case Primitive::kPrimLong: {
3434 InvokeRuntimeCallingConvention calling_convention;
3435 locations->SetInAt(0, Location::RegisterPairLocation(
3436 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3437 locations->SetInAt(1, Location::RegisterPairLocation(
3438 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3439 // Runtime helper puts the result in EAX, EDX.
3440 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3441 break;
3442 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003443 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003444 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003445 locations->SetInAt(0, Location::Any());
3446 locations->SetInAt(1, Location::Any());
3447 locations->SetOut(Location::RequiresFpuRegister());
3448 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003449 break;
3450 }
3451
3452 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003453 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003454 }
3455}
3456
3457void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3458 Primitive::Type type = rem->GetResultType();
3459 switch (type) {
3460 case Primitive::kPrimInt:
3461 case Primitive::kPrimLong: {
3462 GenerateDivRemIntegral(rem);
3463 break;
3464 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003465 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003467 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003468 break;
3469 }
3470 default:
3471 LOG(FATAL) << "Unexpected rem type " << type;
3472 }
3473}
3474
Calin Juravled0d48522014-11-04 16:40:20 +00003475void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003476 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3477 ? LocationSummary::kCallOnSlowPath
3478 : LocationSummary::kNoCall;
3479 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003480 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003481 case Primitive::kPrimByte:
3482 case Primitive::kPrimChar:
3483 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003484 case Primitive::kPrimInt: {
3485 locations->SetInAt(0, Location::Any());
3486 break;
3487 }
3488 case Primitive::kPrimLong: {
3489 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3490 if (!instruction->IsConstant()) {
3491 locations->AddTemp(Location::RequiresRegister());
3492 }
3493 break;
3494 }
3495 default:
3496 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3497 }
Calin Juravled0d48522014-11-04 16:40:20 +00003498 if (instruction->HasUses()) {
3499 locations->SetOut(Location::SameAsFirstInput());
3500 }
3501}
3502
3503void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003504 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003505 codegen_->AddSlowPath(slow_path);
3506
3507 LocationSummary* locations = instruction->GetLocations();
3508 Location value = locations->InAt(0);
3509
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003510 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003511 case Primitive::kPrimByte:
3512 case Primitive::kPrimChar:
3513 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003514 case Primitive::kPrimInt: {
3515 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003516 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003517 __ j(kEqual, slow_path->GetEntryLabel());
3518 } else if (value.IsStackSlot()) {
3519 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3520 __ j(kEqual, slow_path->GetEntryLabel());
3521 } else {
3522 DCHECK(value.IsConstant()) << value;
3523 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3524 __ jmp(slow_path->GetEntryLabel());
3525 }
3526 }
3527 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003528 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003529 case Primitive::kPrimLong: {
3530 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003531 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003532 __ movl(temp, value.AsRegisterPairLow<Register>());
3533 __ orl(temp, value.AsRegisterPairHigh<Register>());
3534 __ j(kEqual, slow_path->GetEntryLabel());
3535 } else {
3536 DCHECK(value.IsConstant()) << value;
3537 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3538 __ jmp(slow_path->GetEntryLabel());
3539 }
3540 }
3541 break;
3542 }
3543 default:
3544 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003545 }
Calin Juravled0d48522014-11-04 16:40:20 +00003546}
3547
Calin Juravle9aec02f2014-11-18 23:06:35 +00003548void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3549 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3550
3551 LocationSummary* locations =
3552 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3553
3554 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003555 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003556 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003557 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003558 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003559 // The shift count needs to be in CL or a constant.
3560 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003561 locations->SetOut(Location::SameAsFirstInput());
3562 break;
3563 }
3564 default:
3565 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3566 }
3567}
3568
3569void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3570 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3571
3572 LocationSummary* locations = op->GetLocations();
3573 Location first = locations->InAt(0);
3574 Location second = locations->InAt(1);
3575 DCHECK(first.Equals(locations->Out()));
3576
3577 switch (op->GetResultType()) {
3578 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003579 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003580 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003581 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003582 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003583 DCHECK_EQ(ECX, second_reg);
3584 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003585 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003586 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003587 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003588 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003589 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003590 }
3591 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003592 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3593 if (shift == 0) {
3594 return;
3595 }
3596 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003597 if (op->IsShl()) {
3598 __ shll(first_reg, imm);
3599 } else if (op->IsShr()) {
3600 __ sarl(first_reg, imm);
3601 } else {
3602 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003603 }
3604 }
3605 break;
3606 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003607 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003608 if (second.IsRegister()) {
3609 Register second_reg = second.AsRegister<Register>();
3610 DCHECK_EQ(ECX, second_reg);
3611 if (op->IsShl()) {
3612 GenerateShlLong(first, second_reg);
3613 } else if (op->IsShr()) {
3614 GenerateShrLong(first, second_reg);
3615 } else {
3616 GenerateUShrLong(first, second_reg);
3617 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003618 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003619 // Shift by a constant.
3620 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3621 // Nothing to do if the shift is 0, as the input is already the output.
3622 if (shift != 0) {
3623 if (op->IsShl()) {
3624 GenerateShlLong(first, shift);
3625 } else if (op->IsShr()) {
3626 GenerateShrLong(first, shift);
3627 } else {
3628 GenerateUShrLong(first, shift);
3629 }
3630 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003631 }
3632 break;
3633 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003634 default:
3635 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3636 }
3637}
3638
Mark P Mendell73945692015-04-29 14:56:17 +00003639void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3640 Register low = loc.AsRegisterPairLow<Register>();
3641 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003642 if (shift == 1) {
3643 // This is just an addition.
3644 __ addl(low, low);
3645 __ adcl(high, high);
3646 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003647 // Shift by 32 is easy. High gets low, and low gets 0.
3648 codegen_->EmitParallelMoves(
3649 loc.ToLow(),
3650 loc.ToHigh(),
3651 Primitive::kPrimInt,
3652 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3653 loc.ToLow(),
3654 Primitive::kPrimInt);
3655 } else if (shift > 32) {
3656 // Low part becomes 0. High part is low part << (shift-32).
3657 __ movl(high, low);
3658 __ shll(high, Immediate(shift - 32));
3659 __ xorl(low, low);
3660 } else {
3661 // Between 1 and 31.
3662 __ shld(high, low, Immediate(shift));
3663 __ shll(low, Immediate(shift));
3664 }
3665}
3666
Calin Juravle9aec02f2014-11-18 23:06:35 +00003667void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003668 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003669 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3670 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3671 __ testl(shifter, Immediate(32));
3672 __ j(kEqual, &done);
3673 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3674 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3675 __ Bind(&done);
3676}
3677
Mark P Mendell73945692015-04-29 14:56:17 +00003678void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3679 Register low = loc.AsRegisterPairLow<Register>();
3680 Register high = loc.AsRegisterPairHigh<Register>();
3681 if (shift == 32) {
3682 // Need to copy the sign.
3683 DCHECK_NE(low, high);
3684 __ movl(low, high);
3685 __ sarl(high, Immediate(31));
3686 } else if (shift > 32) {
3687 DCHECK_NE(low, high);
3688 // High part becomes sign. Low part is shifted by shift - 32.
3689 __ movl(low, high);
3690 __ sarl(high, Immediate(31));
3691 __ sarl(low, Immediate(shift - 32));
3692 } else {
3693 // Between 1 and 31.
3694 __ shrd(low, high, Immediate(shift));
3695 __ sarl(high, Immediate(shift));
3696 }
3697}
3698
Calin Juravle9aec02f2014-11-18 23:06:35 +00003699void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003700 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003701 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3702 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3703 __ testl(shifter, Immediate(32));
3704 __ j(kEqual, &done);
3705 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3706 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3707 __ Bind(&done);
3708}
3709
Mark P Mendell73945692015-04-29 14:56:17 +00003710void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3711 Register low = loc.AsRegisterPairLow<Register>();
3712 Register high = loc.AsRegisterPairHigh<Register>();
3713 if (shift == 32) {
3714 // Shift by 32 is easy. Low gets high, and high gets 0.
3715 codegen_->EmitParallelMoves(
3716 loc.ToHigh(),
3717 loc.ToLow(),
3718 Primitive::kPrimInt,
3719 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3720 loc.ToHigh(),
3721 Primitive::kPrimInt);
3722 } else if (shift > 32) {
3723 // Low part is high >> (shift - 32). High part becomes 0.
3724 __ movl(low, high);
3725 __ shrl(low, Immediate(shift - 32));
3726 __ xorl(high, high);
3727 } else {
3728 // Between 1 and 31.
3729 __ shrd(low, high, Immediate(shift));
3730 __ shrl(high, Immediate(shift));
3731 }
3732}
3733
Calin Juravle9aec02f2014-11-18 23:06:35 +00003734void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003735 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003736 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3737 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3738 __ testl(shifter, Immediate(32));
3739 __ j(kEqual, &done);
3740 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3741 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3742 __ Bind(&done);
3743}
3744
3745void LocationsBuilderX86::VisitShl(HShl* shl) {
3746 HandleShift(shl);
3747}
3748
3749void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3750 HandleShift(shl);
3751}
3752
3753void LocationsBuilderX86::VisitShr(HShr* shr) {
3754 HandleShift(shr);
3755}
3756
3757void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3758 HandleShift(shr);
3759}
3760
3761void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3762 HandleShift(ushr);
3763}
3764
3765void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3766 HandleShift(ushr);
3767}
3768
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003769void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003770 LocationSummary* locations =
3771 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003772 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003773 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003774 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003775 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003776}
3777
3778void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
3779 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003780 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003781 // Note: if heap poisoning is enabled, the entry point takes cares
3782 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003783 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3784 instruction,
3785 instruction->GetDexPc(),
3786 nullptr);
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003787 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003788}
3789
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003790void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3791 LocationSummary* locations =
3792 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3793 locations->SetOut(Location::RegisterLocation(EAX));
3794 InvokeRuntimeCallingConvention calling_convention;
3795 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003796 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003797 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003798}
3799
3800void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3801 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003802 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
3803
Roland Levillain4d027112015-07-01 15:41:14 +01003804 // Note: if heap poisoning is enabled, the entry point takes cares
3805 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003806 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3807 instruction,
3808 instruction->GetDexPc(),
3809 nullptr);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003810 DCHECK(!codegen_->IsLeafMethod());
3811}
3812
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003813void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003814 LocationSummary* locations =
3815 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003816 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3817 if (location.IsStackSlot()) {
3818 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3819 } else if (location.IsDoubleStackSlot()) {
3820 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003821 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003822 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003823}
3824
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003825void InstructionCodeGeneratorX86::VisitParameterValue(
3826 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3827}
3828
3829void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3830 LocationSummary* locations =
3831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3832 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3833}
3834
3835void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003836}
3837
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003838void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003839 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003840 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003841 locations->SetInAt(0, Location::RequiresRegister());
3842 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003843}
3844
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003845void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3846 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003847 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003848 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003849 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003850 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003851 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003852 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003853 break;
3854
3855 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003856 __ notl(out.AsRegisterPairLow<Register>());
3857 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003858 break;
3859
3860 default:
3861 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3862 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003863}
3864
David Brazdil66d126e2015-04-03 16:02:44 +01003865void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3866 LocationSummary* locations =
3867 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3868 locations->SetInAt(0, Location::RequiresRegister());
3869 locations->SetOut(Location::SameAsFirstInput());
3870}
3871
3872void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003873 LocationSummary* locations = bool_not->GetLocations();
3874 Location in = locations->InAt(0);
3875 Location out = locations->Out();
3876 DCHECK(in.Equals(out));
3877 __ xorl(out.AsRegister<Register>(), Immediate(1));
3878}
3879
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003880void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003881 LocationSummary* locations =
3882 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003883 switch (compare->InputAt(0)->GetType()) {
3884 case Primitive::kPrimLong: {
3885 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003886 locations->SetInAt(1, Location::Any());
3887 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3888 break;
3889 }
3890 case Primitive::kPrimFloat:
3891 case Primitive::kPrimDouble: {
3892 locations->SetInAt(0, Location::RequiresFpuRegister());
3893 locations->SetInAt(1, Location::RequiresFpuRegister());
3894 locations->SetOut(Location::RequiresRegister());
3895 break;
3896 }
3897 default:
3898 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3899 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003900}
3901
3902void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003903 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003904 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003905 Location left = locations->InAt(0);
3906 Location right = locations->InAt(1);
3907
Mark Mendell0c9497d2015-08-21 09:30:05 -04003908 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003909 switch (compare->InputAt(0)->GetType()) {
3910 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003911 Register left_low = left.AsRegisterPairLow<Register>();
3912 Register left_high = left.AsRegisterPairHigh<Register>();
3913 int32_t val_low = 0;
3914 int32_t val_high = 0;
3915 bool right_is_const = false;
3916
3917 if (right.IsConstant()) {
3918 DCHECK(right.GetConstant()->IsLongConstant());
3919 right_is_const = true;
3920 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3921 val_low = Low32Bits(val);
3922 val_high = High32Bits(val);
3923 }
3924
Calin Juravleddb7df22014-11-25 20:56:51 +00003925 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003926 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003927 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003928 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003929 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003930 DCHECK(right_is_const) << right;
3931 if (val_high == 0) {
3932 __ testl(left_high, left_high);
3933 } else {
3934 __ cmpl(left_high, Immediate(val_high));
3935 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003936 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003937 __ j(kLess, &less); // Signed compare.
3938 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003939 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003940 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003941 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003942 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003943 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003944 DCHECK(right_is_const) << right;
3945 if (val_low == 0) {
3946 __ testl(left_low, left_low);
3947 } else {
3948 __ cmpl(left_low, Immediate(val_low));
3949 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003950 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003951 break;
3952 }
3953 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003954 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003955 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3956 break;
3957 }
3958 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003959 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003960 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003961 break;
3962 }
3963 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003964 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003965 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003966 __ movl(out, Immediate(0));
3967 __ j(kEqual, &done);
3968 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3969
3970 __ Bind(&greater);
3971 __ movl(out, Immediate(1));
3972 __ jmp(&done);
3973
3974 __ Bind(&less);
3975 __ movl(out, Immediate(-1));
3976
3977 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003978}
3979
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003980void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003981 LocationSummary* locations =
3982 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003983 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
3984 locations->SetInAt(i, Location::Any());
3985 }
3986 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003987}
3988
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01003989void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01003990 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003991}
3992
Calin Juravle52c48962014-12-16 17:02:57 +00003993void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
3994 /*
3995 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
3996 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
3997 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
3998 */
3999 switch (kind) {
4000 case MemBarrierKind::kAnyAny: {
4001 __ mfence();
4002 break;
4003 }
4004 case MemBarrierKind::kAnyStore:
4005 case MemBarrierKind::kLoadAny:
4006 case MemBarrierKind::kStoreStore: {
4007 // nop
4008 break;
4009 }
4010 default:
4011 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004012 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004013}
4014
Vladimir Markodc151b22015-10-15 18:02:30 +01004015HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4016 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4017 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004018 switch (desired_dispatch_info.code_ptr_location) {
4019 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4020 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4021 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4022 // (Though the direct CALL ptr16:32 is available for consideration).
4023 return HInvokeStaticOrDirect::DispatchInfo {
4024 desired_dispatch_info.method_load_kind,
4025 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4026 desired_dispatch_info.method_load_data,
4027 0u
4028 };
4029 default:
4030 return desired_dispatch_info;
4031 }
4032}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004033
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004034Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4035 Register temp) {
4036 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
4037 Location location = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4038 if (!invoke->GetLocations()->Intrinsified()) {
4039 return location.AsRegister<Register>();
4040 }
4041 // For intrinsics we allow any location, so it may be on the stack.
4042 if (!location.IsRegister()) {
4043 __ movl(temp, Address(ESP, location.GetStackIndex()));
4044 return temp;
4045 }
4046 // For register locations, check if the register was saved. If so, get it from the stack.
4047 // Note: There is a chance that the register was saved but not overwritten, so we could
4048 // save one load. However, since this is just an intrinsic slow path we prefer this
4049 // simple and more robust approach rather that trying to determine if that's the case.
4050 SlowPathCode* slow_path = GetCurrentSlowPath();
4051 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4052 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4053 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4054 __ movl(temp, Address(ESP, stack_offset));
4055 return temp;
4056 }
4057 return location.AsRegister<Register>();
4058}
4059
Vladimir Marko58155012015-08-19 12:49:41 +00004060void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4061 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4062 switch (invoke->GetMethodLoadKind()) {
4063 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4064 // temp = thread->string_init_entrypoint
4065 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4066 break;
4067 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
4068 callee_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4069 break;
4070 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4071 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4072 break;
4073 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4074 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4075 method_patches_.emplace_back(invoke->GetTargetMethod());
4076 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4077 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004078 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4079 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4080 temp.AsRegister<Register>());
4081 uint32_t offset = invoke->GetDexCacheArrayOffset();
4082 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4083 // Add the patch entry and bind its label at the end of the instruction.
4084 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4085 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4086 break;
4087 }
Vladimir Marko58155012015-08-19 12:49:41 +00004088 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
4089 Location current_method = invoke->GetLocations()->InAt(invoke->GetCurrentMethodInputIndex());
4090 Register method_reg;
4091 Register reg = temp.AsRegister<Register>();
4092 if (current_method.IsRegister()) {
4093 method_reg = current_method.AsRegister<Register>();
4094 } else {
4095 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4096 DCHECK(!current_method.IsValid());
4097 method_reg = reg;
4098 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4099 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004100 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004101 __ movl(reg, Address(method_reg,
4102 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004103 // temp = temp[index_in_cache]
4104 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4105 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4106 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004107 }
Vladimir Marko58155012015-08-19 12:49:41 +00004108 }
4109
4110 switch (invoke->GetCodePtrLocation()) {
4111 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4112 __ call(GetFrameEntryLabel());
4113 break;
4114 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4115 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4116 Label* label = &relative_call_patches_.back().label;
4117 __ call(label); // Bind to the patch label, override at link time.
4118 __ Bind(label); // Bind the label at the end of the "call" insn.
4119 break;
4120 }
4121 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4122 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004123 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4124 LOG(FATAL) << "Unsupported";
4125 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004126 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4127 // (callee_method + offset_of_quick_compiled_code)()
4128 __ call(Address(callee_method.AsRegister<Register>(),
4129 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4130 kX86WordSize).Int32Value()));
4131 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004132 }
4133
4134 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004135}
4136
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004137void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4138 Register temp = temp_in.AsRegister<Register>();
4139 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4140 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
4141 LocationSummary* locations = invoke->GetLocations();
4142 Location receiver = locations->InAt(0);
4143 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004144 DCHECK(receiver.IsRegister());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004145 // /* HeapReference<Class> */ temp = receiver->klass_
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004146 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
4147 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004148 // Instead of simply (possibly) unpoisoning `temp` here, we should
4149 // emit a read barrier for the previous class reference load.
4150 // However this is not required in practice, as this is an
4151 // intermediate/temporary reference and because the current
4152 // concurrent copying collector keeps the from-space memory
4153 // intact/accessible until the end of the marking phase (the
4154 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004155 __ MaybeUnpoisonHeapReference(temp);
4156 // temp = temp->GetMethodAt(method_offset);
4157 __ movl(temp, Address(temp, method_offset));
4158 // call temp->GetEntryPoint();
4159 __ call(Address(
4160 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4161}
4162
Vladimir Marko58155012015-08-19 12:49:41 +00004163void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4164 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004165 size_t size =
4166 method_patches_.size() +
4167 relative_call_patches_.size() +
4168 pc_relative_dex_cache_patches_.size();
4169 linker_patches->reserve(size);
4170 // The label points to the end of the "movl" insn but the literal offset for method
4171 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4172 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004173 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004174 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004175 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4176 info.target_method.dex_file,
4177 info.target_method.dex_method_index));
4178 }
4179 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004180 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004181 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4182 info.target_method.dex_file,
4183 info.target_method.dex_method_index));
4184 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004185 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4186 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4187 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4188 &info.target_dex_file,
4189 GetMethodAddressOffset(),
4190 info.element_offset));
4191 }
Vladimir Marko58155012015-08-19 12:49:41 +00004192}
4193
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004194void CodeGeneratorX86::MarkGCCard(Register temp,
4195 Register card,
4196 Register object,
4197 Register value,
4198 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004199 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004200 if (value_can_be_null) {
4201 __ testl(value, value);
4202 __ j(kEqual, &is_null);
4203 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004204 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4205 __ movl(temp, object);
4206 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004207 __ movb(Address(temp, card, TIMES_1, 0),
4208 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004209 if (value_can_be_null) {
4210 __ Bind(&is_null);
4211 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004212}
4213
Calin Juravle52c48962014-12-16 17:02:57 +00004214void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4215 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004216
4217 bool object_field_get_with_read_barrier =
4218 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004219 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004220 new (GetGraph()->GetArena()) LocationSummary(instruction,
4221 kEmitCompilerReadBarrier ?
4222 LocationSummary::kCallOnSlowPath :
4223 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004224 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004225
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004226 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4227 locations->SetOut(Location::RequiresFpuRegister());
4228 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004229 // The output overlaps in case of long: we don't want the low move
4230 // to overwrite the object's location. Likewise, in the case of
4231 // an object field get with read barriers enabled, we do not want
4232 // the move to overwrite the object's location, as we need it to emit
4233 // the read barrier.
4234 locations->SetOut(
4235 Location::RequiresRegister(),
4236 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4237 Location::kOutputOverlap :
4238 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004239 }
Calin Juravle52c48962014-12-16 17:02:57 +00004240
4241 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4242 // Long values can be loaded atomically into an XMM using movsd.
4243 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
4244 // and then copy the XMM into the output 32bits at a time).
4245 locations->AddTemp(Location::RequiresFpuRegister());
4246 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004247}
4248
Calin Juravle52c48962014-12-16 17:02:57 +00004249void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4250 const FieldInfo& field_info) {
4251 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004252
Calin Juravle52c48962014-12-16 17:02:57 +00004253 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004254 Location base_loc = locations->InAt(0);
4255 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004256 Location out = locations->Out();
4257 bool is_volatile = field_info.IsVolatile();
4258 Primitive::Type field_type = field_info.GetFieldType();
4259 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4260
4261 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004262 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004263 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004264 break;
4265 }
4266
4267 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004268 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004269 break;
4270 }
4271
4272 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004273 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004274 break;
4275 }
4276
4277 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004278 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004279 break;
4280 }
4281
4282 case Primitive::kPrimInt:
4283 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00004284 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285 break;
4286 }
4287
4288 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004289 if (is_volatile) {
4290 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4291 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004293 __ movd(out.AsRegisterPairLow<Register>(), temp);
4294 __ psrlq(temp, Immediate(32));
4295 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4296 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004297 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004298 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004299 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004300 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4301 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004302 break;
4303 }
4304
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004305 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004306 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004307 break;
4308 }
4309
4310 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004311 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004312 break;
4313 }
4314
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004315 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004316 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004317 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004318 }
Calin Juravle52c48962014-12-16 17:02:57 +00004319
Calin Juravle77520bc2015-01-12 18:45:46 +00004320 // Longs are handled in the switch.
4321 if (field_type != Primitive::kPrimLong) {
4322 codegen_->MaybeRecordImplicitNullCheck(instruction);
4323 }
4324
Calin Juravle52c48962014-12-16 17:02:57 +00004325 if (is_volatile) {
4326 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4327 }
Roland Levillain4d027112015-07-01 15:41:14 +01004328
4329 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004330 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004331 }
Calin Juravle52c48962014-12-16 17:02:57 +00004332}
4333
4334void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4335 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4336
4337 LocationSummary* locations =
4338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4339 locations->SetInAt(0, Location::RequiresRegister());
4340 bool is_volatile = field_info.IsVolatile();
4341 Primitive::Type field_type = field_info.GetFieldType();
4342 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4343 || (field_type == Primitive::kPrimByte);
4344
4345 // The register allocator does not support multiple
4346 // inputs that die at entry with one in a specific register.
4347 if (is_byte_type) {
4348 // Ensure the value is in a byte register.
4349 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004350 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004351 if (is_volatile && field_type == Primitive::kPrimDouble) {
4352 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4353 locations->SetInAt(1, Location::RequiresFpuRegister());
4354 } else {
4355 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4356 }
4357 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4358 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004359 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004360
Calin Juravle52c48962014-12-16 17:02:57 +00004361 // 64bits value can be atomically written to an address with movsd and an XMM register.
4362 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4363 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4364 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4365 // isolated cases when we need this it isn't worth adding the extra complexity.
4366 locations->AddTemp(Location::RequiresFpuRegister());
4367 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004368 } else {
4369 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4370
4371 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4372 // Temporary registers for the write barrier.
4373 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4374 // Ensure the card is in a byte register.
4375 locations->AddTemp(Location::RegisterLocation(ECX));
4376 }
Calin Juravle52c48962014-12-16 17:02:57 +00004377 }
4378}
4379
4380void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004381 const FieldInfo& field_info,
4382 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004383 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4384
4385 LocationSummary* locations = instruction->GetLocations();
4386 Register base = locations->InAt(0).AsRegister<Register>();
4387 Location value = locations->InAt(1);
4388 bool is_volatile = field_info.IsVolatile();
4389 Primitive::Type field_type = field_info.GetFieldType();
4390 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004391 bool needs_write_barrier =
4392 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004393
4394 if (is_volatile) {
4395 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4396 }
4397
Mark Mendell81489372015-11-04 11:30:41 -05004398 bool maybe_record_implicit_null_check_done = false;
4399
Calin Juravle52c48962014-12-16 17:02:57 +00004400 switch (field_type) {
4401 case Primitive::kPrimBoolean:
4402 case Primitive::kPrimByte: {
4403 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4404 break;
4405 }
4406
4407 case Primitive::kPrimShort:
4408 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004409 if (value.IsConstant()) {
4410 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4411 __ movw(Address(base, offset), Immediate(v));
4412 } else {
4413 __ movw(Address(base, offset), value.AsRegister<Register>());
4414 }
Calin Juravle52c48962014-12-16 17:02:57 +00004415 break;
4416 }
4417
4418 case Primitive::kPrimInt:
4419 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004420 if (kPoisonHeapReferences && needs_write_barrier) {
4421 // Note that in the case where `value` is a null reference,
4422 // we do not enter this block, as the reference does not
4423 // need poisoning.
4424 DCHECK_EQ(field_type, Primitive::kPrimNot);
4425 Register temp = locations->GetTemp(0).AsRegister<Register>();
4426 __ movl(temp, value.AsRegister<Register>());
4427 __ PoisonHeapReference(temp);
4428 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004429 } else if (value.IsConstant()) {
4430 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4431 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004432 } else {
4433 __ movl(Address(base, offset), value.AsRegister<Register>());
4434 }
Calin Juravle52c48962014-12-16 17:02:57 +00004435 break;
4436 }
4437
4438 case Primitive::kPrimLong: {
4439 if (is_volatile) {
4440 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4441 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4442 __ movd(temp1, value.AsRegisterPairLow<Register>());
4443 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4444 __ punpckldq(temp1, temp2);
4445 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004446 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004447 } else if (value.IsConstant()) {
4448 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4449 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4450 codegen_->MaybeRecordImplicitNullCheck(instruction);
4451 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004452 } else {
4453 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004454 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004455 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4456 }
Mark Mendell81489372015-11-04 11:30:41 -05004457 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004458 break;
4459 }
4460
4461 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004462 if (value.IsConstant()) {
4463 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4464 __ movl(Address(base, offset), Immediate(v));
4465 } else {
4466 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4467 }
Calin Juravle52c48962014-12-16 17:02:57 +00004468 break;
4469 }
4470
4471 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004472 if (value.IsConstant()) {
4473 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4474 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4475 codegen_->MaybeRecordImplicitNullCheck(instruction);
4476 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4477 maybe_record_implicit_null_check_done = true;
4478 } else {
4479 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4480 }
Calin Juravle52c48962014-12-16 17:02:57 +00004481 break;
4482 }
4483
4484 case Primitive::kPrimVoid:
4485 LOG(FATAL) << "Unreachable type " << field_type;
4486 UNREACHABLE();
4487 }
4488
Mark Mendell81489372015-11-04 11:30:41 -05004489 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004490 codegen_->MaybeRecordImplicitNullCheck(instruction);
4491 }
4492
Roland Levillain4d027112015-07-01 15:41:14 +01004493 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004494 Register temp = locations->GetTemp(0).AsRegister<Register>();
4495 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004496 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004497 }
4498
Calin Juravle52c48962014-12-16 17:02:57 +00004499 if (is_volatile) {
4500 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4501 }
4502}
4503
4504void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4505 HandleFieldGet(instruction, instruction->GetFieldInfo());
4506}
4507
4508void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4509 HandleFieldGet(instruction, instruction->GetFieldInfo());
4510}
4511
4512void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4513 HandleFieldSet(instruction, instruction->GetFieldInfo());
4514}
4515
4516void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004517 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004518}
4519
4520void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4521 HandleFieldSet(instruction, instruction->GetFieldInfo());
4522}
4523
4524void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004525 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004526}
4527
4528void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4529 HandleFieldGet(instruction, instruction->GetFieldInfo());
4530}
4531
4532void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4533 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534}
4535
Calin Juravlee460d1d2015-09-29 04:52:17 +01004536void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4537 HUnresolvedInstanceFieldGet* instruction) {
4538 FieldAccessCallingConventionX86 calling_convention;
4539 codegen_->CreateUnresolvedFieldLocationSummary(
4540 instruction, instruction->GetFieldType(), calling_convention);
4541}
4542
4543void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4544 HUnresolvedInstanceFieldGet* instruction) {
4545 FieldAccessCallingConventionX86 calling_convention;
4546 codegen_->GenerateUnresolvedFieldAccess(instruction,
4547 instruction->GetFieldType(),
4548 instruction->GetFieldIndex(),
4549 instruction->GetDexPc(),
4550 calling_convention);
4551}
4552
4553void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4554 HUnresolvedInstanceFieldSet* instruction) {
4555 FieldAccessCallingConventionX86 calling_convention;
4556 codegen_->CreateUnresolvedFieldLocationSummary(
4557 instruction, instruction->GetFieldType(), calling_convention);
4558}
4559
4560void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4561 HUnresolvedInstanceFieldSet* instruction) {
4562 FieldAccessCallingConventionX86 calling_convention;
4563 codegen_->GenerateUnresolvedFieldAccess(instruction,
4564 instruction->GetFieldType(),
4565 instruction->GetFieldIndex(),
4566 instruction->GetDexPc(),
4567 calling_convention);
4568}
4569
4570void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4571 HUnresolvedStaticFieldGet* instruction) {
4572 FieldAccessCallingConventionX86 calling_convention;
4573 codegen_->CreateUnresolvedFieldLocationSummary(
4574 instruction, instruction->GetFieldType(), calling_convention);
4575}
4576
4577void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4578 HUnresolvedStaticFieldGet* instruction) {
4579 FieldAccessCallingConventionX86 calling_convention;
4580 codegen_->GenerateUnresolvedFieldAccess(instruction,
4581 instruction->GetFieldType(),
4582 instruction->GetFieldIndex(),
4583 instruction->GetDexPc(),
4584 calling_convention);
4585}
4586
4587void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4588 HUnresolvedStaticFieldSet* instruction) {
4589 FieldAccessCallingConventionX86 calling_convention;
4590 codegen_->CreateUnresolvedFieldLocationSummary(
4591 instruction, instruction->GetFieldType(), calling_convention);
4592}
4593
4594void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4595 HUnresolvedStaticFieldSet* instruction) {
4596 FieldAccessCallingConventionX86 calling_convention;
4597 codegen_->GenerateUnresolvedFieldAccess(instruction,
4598 instruction->GetFieldType(),
4599 instruction->GetFieldIndex(),
4600 instruction->GetDexPc(),
4601 calling_convention);
4602}
4603
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004604void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004605 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4606 ? LocationSummary::kCallOnSlowPath
4607 : LocationSummary::kNoCall;
4608 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4609 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004610 ? Location::RequiresRegister()
4611 : Location::Any();
4612 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004613 if (instruction->HasUses()) {
4614 locations->SetOut(Location::SameAsFirstInput());
4615 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004616}
4617
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004618void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004619 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4620 return;
4621 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004622 LocationSummary* locations = instruction->GetLocations();
4623 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004624
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004625 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4626 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4627}
4628
4629void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004630 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004631 codegen_->AddSlowPath(slow_path);
4632
4633 LocationSummary* locations = instruction->GetLocations();
4634 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635
4636 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004637 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004638 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004640 } else {
4641 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004642 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004643 __ jmp(slow_path->GetEntryLabel());
4644 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645 }
4646 __ j(kEqual, slow_path->GetEntryLabel());
4647}
4648
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004649void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004650 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004651 GenerateImplicitNullCheck(instruction);
4652 } else {
4653 GenerateExplicitNullCheck(instruction);
4654 }
4655}
4656
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004657void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004658 bool object_array_get_with_read_barrier =
4659 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004660 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004661 new (GetGraph()->GetArena()) LocationSummary(instruction,
4662 object_array_get_with_read_barrier ?
4663 LocationSummary::kCallOnSlowPath :
4664 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004665 locations->SetInAt(0, Location::RequiresRegister());
4666 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004667 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4668 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4669 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004670 // The output overlaps in case of long: we don't want the low move
4671 // to overwrite the array's location. Likewise, in the case of an
4672 // object array get with read barriers enabled, we do not want the
4673 // move to overwrite the array's location, as we need it to emit
4674 // the read barrier.
4675 locations->SetOut(
4676 Location::RequiresRegister(),
4677 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4678 Location::kOutputOverlap :
4679 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004680 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004681}
4682
4683void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4684 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004685 Location obj_loc = locations->InAt(0);
4686 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004687 Location index = locations->InAt(1);
4688
Calin Juravle77520bc2015-01-12 18:45:46 +00004689 Primitive::Type type = instruction->GetType();
4690 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004691 case Primitive::kPrimBoolean: {
4692 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004693 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004694 if (index.IsConstant()) {
4695 __ movzxb(out, Address(obj,
4696 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4697 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004698 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004699 }
4700 break;
4701 }
4702
4703 case Primitive::kPrimByte: {
4704 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004705 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004706 if (index.IsConstant()) {
4707 __ movsxb(out, Address(obj,
4708 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4709 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004710 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 }
4712 break;
4713 }
4714
4715 case Primitive::kPrimShort: {
4716 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004717 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004718 if (index.IsConstant()) {
4719 __ movsxw(out, Address(obj,
4720 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4721 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004722 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004723 }
4724 break;
4725 }
4726
4727 case Primitive::kPrimChar: {
4728 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004729 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004730 if (index.IsConstant()) {
4731 __ movzxw(out, Address(obj,
4732 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4733 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004734 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004735 }
4736 break;
4737 }
4738
4739 case Primitive::kPrimInt:
4740 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004741 static_assert(
4742 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4743 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004745 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004746 if (index.IsConstant()) {
4747 __ movl(out, Address(obj,
4748 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4749 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004750 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004751 }
4752 break;
4753 }
4754
4755 case Primitive::kPrimLong: {
4756 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004757 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004758 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004759 if (index.IsConstant()) {
4760 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004761 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004762 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004763 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004765 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004766 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004767 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004768 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004769 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004770 }
4771 break;
4772 }
4773
Mark Mendell7c8d0092015-01-26 11:21:33 -05004774 case Primitive::kPrimFloat: {
4775 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4776 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4777 if (index.IsConstant()) {
4778 __ movss(out, Address(obj,
4779 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4780 } else {
4781 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4782 }
4783 break;
4784 }
4785
4786 case Primitive::kPrimDouble: {
4787 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4788 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4789 if (index.IsConstant()) {
4790 __ movsd(out, Address(obj,
4791 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4792 } else {
4793 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4794 }
4795 break;
4796 }
4797
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004798 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004799 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004800 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004801 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004802
4803 if (type != Primitive::kPrimLong) {
4804 codegen_->MaybeRecordImplicitNullCheck(instruction);
4805 }
Roland Levillain4d027112015-07-01 15:41:14 +01004806
4807 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004808 static_assert(
4809 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4810 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4811 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4812 Location out = locations->Out();
4813 if (index.IsConstant()) {
4814 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4815 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4816 } else {
4817 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4818 }
Roland Levillain4d027112015-07-01 15:41:14 +01004819 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004820}
4821
4822void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004823 // This location builder might end up asking to up to four registers, which is
4824 // not currently possible for baseline. The situation in which we need four
4825 // registers cannot be met by baseline though, because it has not run any
4826 // optimization.
4827
Nicolas Geoffray39468442014-09-02 15:17:15 +01004828 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004829
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004830 bool needs_write_barrier =
4831 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004832 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4833 bool object_array_set_with_read_barrier =
4834 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004835
Nicolas Geoffray39468442014-09-02 15:17:15 +01004836 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4837 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004838 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4839 LocationSummary::kCallOnSlowPath :
4840 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004841
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004842 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4843 || (value_type == Primitive::kPrimByte);
4844 // We need the inputs to be different than the output in case of long operation.
4845 // In case of a byte operation, the register allocator does not support multiple
4846 // inputs that die at entry with one in a specific register.
4847 locations->SetInAt(0, Location::RequiresRegister());
4848 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4849 if (is_byte_type) {
4850 // Ensure the value is in a byte register.
4851 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4852 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004853 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004854 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004855 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4856 }
4857 if (needs_write_barrier) {
4858 // Temporary registers for the write barrier.
4859 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4860 // Ensure the card is in a byte register.
Roland Levillain0d5a2812015-11-13 10:07:31 +00004861 locations->AddTemp(Location::RegisterLocation(ECX)); // Possibly used for read barrier too.
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004863}
4864
4865void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4866 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004867 Location array_loc = locations->InAt(0);
4868 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004869 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004870 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004871 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004872 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4873 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4874 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004875 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004876 bool needs_write_barrier =
4877 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004878
4879 switch (value_type) {
4880 case Primitive::kPrimBoolean:
4881 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004882 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4883 Address address = index.IsConstant()
4884 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4885 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4886 if (value.IsRegister()) {
4887 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004889 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004891 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892 break;
4893 }
4894
4895 case Primitive::kPrimShort:
4896 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004897 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4898 Address address = index.IsConstant()
4899 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4900 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4901 if (value.IsRegister()) {
4902 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004903 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004904 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004906 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004907 break;
4908 }
4909
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004910 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004911 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4912 Address address = index.IsConstant()
4913 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4914 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004915
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004916 if (!value.IsRegister()) {
4917 // Just setting null.
4918 DCHECK(instruction->InputAt(2)->IsNullConstant());
4919 DCHECK(value.IsConstant()) << value;
4920 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004921 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004922 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004923 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004926
4927 DCHECK(needs_write_barrier);
4928 Register register_value = value.AsRegister<Register>();
4929 NearLabel done, not_null, do_put;
4930 SlowPathCode* slow_path = nullptr;
4931 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004932 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004933 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4934 codegen_->AddSlowPath(slow_path);
4935 if (instruction->GetValueCanBeNull()) {
4936 __ testl(register_value, register_value);
4937 __ j(kNotEqual, &not_null);
4938 __ movl(address, Immediate(0));
4939 codegen_->MaybeRecordImplicitNullCheck(instruction);
4940 __ jmp(&done);
4941 __ Bind(&not_null);
4942 }
4943
Roland Levillain0d5a2812015-11-13 10:07:31 +00004944 if (kEmitCompilerReadBarrier) {
4945 // When read barriers are enabled, the type checking
4946 // instrumentation requires two read barriers:
4947 //
4948 // __ movl(temp2, temp);
4949 // // /* HeapReference<Class> */ temp = temp->component_type_
4950 // __ movl(temp, Address(temp, component_offset));
4951 // codegen_->GenerateReadBarrier(
4952 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4953 //
4954 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4955 // __ movl(temp2, Address(register_value, class_offset));
4956 // codegen_->GenerateReadBarrier(
4957 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4958 //
4959 // __ cmpl(temp, temp2);
4960 //
4961 // However, the second read barrier may trash `temp`, as it
4962 // is a temporary register, and as such would not be saved
4963 // along with live registers before calling the runtime (nor
4964 // restored afterwards). So in this case, we bail out and
4965 // delegate the work to the array set slow path.
4966 //
4967 // TODO: Extend the register allocator to support a new
4968 // "(locally) live temp" location so as to avoid always
4969 // going into the slow path when read barriers are enabled.
4970 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004971 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004972 // /* HeapReference<Class> */ temp = array->klass_
4973 __ movl(temp, Address(array, class_offset));
4974 codegen_->MaybeRecordImplicitNullCheck(instruction);
4975 __ MaybeUnpoisonHeapReference(temp);
4976
4977 // /* HeapReference<Class> */ temp = temp->component_type_
4978 __ movl(temp, Address(temp, component_offset));
4979 // If heap poisoning is enabled, no need to unpoison `temp`
4980 // nor the object reference in `register_value->klass`, as
4981 // we are comparing two poisoned references.
4982 __ cmpl(temp, Address(register_value, class_offset));
4983
4984 if (instruction->StaticTypeOfArrayIsObjectArray()) {
4985 __ j(kEqual, &do_put);
4986 // If heap poisoning is enabled, the `temp` reference has
4987 // not been unpoisoned yet; unpoison it now.
4988 __ MaybeUnpoisonHeapReference(temp);
4989
4990 // /* HeapReference<Class> */ temp = temp->super_class_
4991 __ movl(temp, Address(temp, super_offset));
4992 // If heap poisoning is enabled, no need to unpoison
4993 // `temp`, as we are comparing against null below.
4994 __ testl(temp, temp);
4995 __ j(kNotEqual, slow_path->GetEntryLabel());
4996 __ Bind(&do_put);
4997 } else {
4998 __ j(kNotEqual, slow_path->GetEntryLabel());
4999 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005000 }
5001 }
5002
5003 if (kPoisonHeapReferences) {
5004 __ movl(temp, register_value);
5005 __ PoisonHeapReference(temp);
5006 __ movl(address, temp);
5007 } else {
5008 __ movl(address, register_value);
5009 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005010 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005011 codegen_->MaybeRecordImplicitNullCheck(instruction);
5012 }
5013
5014 Register card = locations->GetTemp(1).AsRegister<Register>();
5015 codegen_->MarkGCCard(
5016 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5017 __ Bind(&done);
5018
5019 if (slow_path != nullptr) {
5020 __ Bind(slow_path->GetExitLabel());
5021 }
5022
5023 break;
5024 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005025
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005026 case Primitive::kPrimInt: {
5027 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5028 Address address = index.IsConstant()
5029 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5030 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5031 if (value.IsRegister()) {
5032 __ movl(address, value.AsRegister<Register>());
5033 } else {
5034 DCHECK(value.IsConstant()) << value;
5035 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5036 __ movl(address, Immediate(v));
5037 }
5038 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 break;
5040 }
5041
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 case Primitive::kPrimLong: {
5043 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 if (index.IsConstant()) {
5045 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005046 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005047 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005048 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005049 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005050 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005051 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005052 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005053 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005054 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005055 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005056 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005057 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005058 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005059 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005060 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005061 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005062 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005063 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005064 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005065 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005066 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005067 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005068 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005069 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005070 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005071 Immediate(High32Bits(val)));
5072 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073 }
5074 break;
5075 }
5076
Mark Mendell7c8d0092015-01-26 11:21:33 -05005077 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005078 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5079 Address address = index.IsConstant()
5080 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5081 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005082 if (value.IsFpuRegister()) {
5083 __ movss(address, value.AsFpuRegister<XmmRegister>());
5084 } else {
5085 DCHECK(value.IsConstant());
5086 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5087 __ movl(address, Immediate(v));
5088 }
5089 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005090 break;
5091 }
5092
5093 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005094 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5095 Address address = index.IsConstant()
5096 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5097 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005098 if (value.IsFpuRegister()) {
5099 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5100 } else {
5101 DCHECK(value.IsConstant());
5102 Address address_hi = index.IsConstant() ?
5103 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5104 offset + kX86WordSize) :
5105 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5106 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5107 __ movl(address, Immediate(Low32Bits(v)));
5108 codegen_->MaybeRecordImplicitNullCheck(instruction);
5109 __ movl(address_hi, Immediate(High32Bits(v)));
5110 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005111 break;
5112 }
5113
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 case Primitive::kPrimVoid:
5115 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005116 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005117 }
5118}
5119
5120void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5121 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005122 locations->SetInAt(0, Location::RequiresRegister());
5123 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124}
5125
5126void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5127 LocationSummary* locations = instruction->GetLocations();
5128 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005129 Register obj = locations->InAt(0).AsRegister<Register>();
5130 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005132 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133}
5134
5135void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005136 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5137 ? LocationSummary::kCallOnSlowPath
5138 : LocationSummary::kNoCall;
5139 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005140 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005141 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005142 if (instruction->HasUses()) {
5143 locations->SetOut(Location::SameAsFirstInput());
5144 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005145}
5146
5147void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5148 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005149 Location index_loc = locations->InAt(0);
5150 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005151 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005152 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153
Mark Mendell99dbd682015-04-22 16:18:52 -04005154 if (length_loc.IsConstant()) {
5155 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5156 if (index_loc.IsConstant()) {
5157 // BCE will remove the bounds check if we are guarenteed to pass.
5158 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5159 if (index < 0 || index >= length) {
5160 codegen_->AddSlowPath(slow_path);
5161 __ jmp(slow_path->GetEntryLabel());
5162 } else {
5163 // Some optimization after BCE may have generated this, and we should not
5164 // generate a bounds check if it is a valid range.
5165 }
5166 return;
5167 }
5168
5169 // We have to reverse the jump condition because the length is the constant.
5170 Register index_reg = index_loc.AsRegister<Register>();
5171 __ cmpl(index_reg, Immediate(length));
5172 codegen_->AddSlowPath(slow_path);
5173 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005174 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005175 Register length = length_loc.AsRegister<Register>();
5176 if (index_loc.IsConstant()) {
5177 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5178 __ cmpl(length, Immediate(value));
5179 } else {
5180 __ cmpl(length, index_loc.AsRegister<Register>());
5181 }
5182 codegen_->AddSlowPath(slow_path);
5183 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005184 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185}
5186
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005187void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5188 temp->SetLocations(nullptr);
5189}
5190
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005191void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005192 // Nothing to do, this is driven by the code generator.
5193}
5194
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005195void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005196 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005197}
5198
5199void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005200 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5201}
5202
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005203void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5204 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5205}
5206
5207void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005208 HBasicBlock* block = instruction->GetBlock();
5209 if (block->GetLoopInformation() != nullptr) {
5210 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5211 // The back edge will generate the suspend check.
5212 return;
5213 }
5214 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5215 // The goto will generate the suspend check.
5216 return;
5217 }
5218 GenerateSuspendCheck(instruction, nullptr);
5219}
5220
5221void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5222 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005223 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005224 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5225 if (slow_path == nullptr) {
5226 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5227 instruction->SetSlowPath(slow_path);
5228 codegen_->AddSlowPath(slow_path);
5229 if (successor != nullptr) {
5230 DCHECK(successor->IsLoopHeader());
5231 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5232 }
5233 } else {
5234 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5235 }
5236
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005237 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005238 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005239 if (successor == nullptr) {
5240 __ j(kNotEqual, slow_path->GetEntryLabel());
5241 __ Bind(slow_path->GetReturnLabel());
5242 } else {
5243 __ j(kEqual, codegen_->GetLabelOf(successor));
5244 __ jmp(slow_path->GetEntryLabel());
5245 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005246}
5247
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005248X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5249 return codegen_->GetAssembler();
5250}
5251
Mark Mendell7c8d0092015-01-26 11:21:33 -05005252void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005253 ScratchRegisterScope ensure_scratch(
5254 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5255 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5256 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5257 __ movl(temp_reg, Address(ESP, src + stack_offset));
5258 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005259}
5260
5261void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005262 ScratchRegisterScope ensure_scratch(
5263 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5264 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5265 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5266 __ movl(temp_reg, Address(ESP, src + stack_offset));
5267 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5268 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5269 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005270}
5271
5272void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005273 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005274 Location source = move->GetSource();
5275 Location destination = move->GetDestination();
5276
5277 if (source.IsRegister()) {
5278 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005279 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005280 } else {
5281 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005282 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005283 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005284 } else if (source.IsFpuRegister()) {
5285 if (destination.IsFpuRegister()) {
5286 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5287 } else if (destination.IsStackSlot()) {
5288 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5289 } else {
5290 DCHECK(destination.IsDoubleStackSlot());
5291 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5292 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005293 } else if (source.IsStackSlot()) {
5294 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005295 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005296 } else if (destination.IsFpuRegister()) {
5297 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005298 } else {
5299 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005300 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5301 }
5302 } else if (source.IsDoubleStackSlot()) {
5303 if (destination.IsFpuRegister()) {
5304 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5305 } else {
5306 DCHECK(destination.IsDoubleStackSlot()) << destination;
5307 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005308 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005309 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005310 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005311 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005312 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005313 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005314 if (value == 0) {
5315 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5316 } else {
5317 __ movl(destination.AsRegister<Register>(), Immediate(value));
5318 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005319 } else {
5320 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005321 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005322 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005323 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005324 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005325 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005326 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005327 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005328 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5329 if (value == 0) {
5330 // Easy handling of 0.0.
5331 __ xorps(dest, dest);
5332 } else {
5333 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005334 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5335 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5336 __ movl(temp, Immediate(value));
5337 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005338 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005339 } else {
5340 DCHECK(destination.IsStackSlot()) << destination;
5341 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5342 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005343 } else if (constant->IsLongConstant()) {
5344 int64_t value = constant->AsLongConstant()->GetValue();
5345 int32_t low_value = Low32Bits(value);
5346 int32_t high_value = High32Bits(value);
5347 Immediate low(low_value);
5348 Immediate high(high_value);
5349 if (destination.IsDoubleStackSlot()) {
5350 __ movl(Address(ESP, destination.GetStackIndex()), low);
5351 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5352 } else {
5353 __ movl(destination.AsRegisterPairLow<Register>(), low);
5354 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5355 }
5356 } else {
5357 DCHECK(constant->IsDoubleConstant());
5358 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005359 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005360 int32_t low_value = Low32Bits(value);
5361 int32_t high_value = High32Bits(value);
5362 Immediate low(low_value);
5363 Immediate high(high_value);
5364 if (destination.IsFpuRegister()) {
5365 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5366 if (value == 0) {
5367 // Easy handling of 0.0.
5368 __ xorpd(dest, dest);
5369 } else {
5370 __ pushl(high);
5371 __ pushl(low);
5372 __ movsd(dest, Address(ESP, 0));
5373 __ addl(ESP, Immediate(8));
5374 }
5375 } else {
5376 DCHECK(destination.IsDoubleStackSlot()) << destination;
5377 __ movl(Address(ESP, destination.GetStackIndex()), low);
5378 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5379 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005380 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005381 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005382 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005383 }
5384}
5385
Mark Mendella5c19ce2015-04-01 12:51:05 -04005386void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005387 Register suggested_scratch = reg == EAX ? EBX : EAX;
5388 ScratchRegisterScope ensure_scratch(
5389 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5390
5391 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5392 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5393 __ movl(Address(ESP, mem + stack_offset), reg);
5394 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005395}
5396
Mark Mendell7c8d0092015-01-26 11:21:33 -05005397void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005398 ScratchRegisterScope ensure_scratch(
5399 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5400
5401 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5402 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5403 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5404 __ movss(Address(ESP, mem + stack_offset), reg);
5405 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005406}
5407
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005408void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005409 ScratchRegisterScope ensure_scratch1(
5410 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005411
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005412 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5413 ScratchRegisterScope ensure_scratch2(
5414 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005415
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005416 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5417 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5418 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5419 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5420 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5421 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005422}
5423
5424void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005425 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005426 Location source = move->GetSource();
5427 Location destination = move->GetDestination();
5428
5429 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005430 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5431 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5432 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5433 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5434 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005435 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005436 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005437 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005438 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005439 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5440 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005441 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5442 // Use XOR Swap algorithm to avoid a temporary.
5443 DCHECK_NE(source.reg(), destination.reg());
5444 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5445 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5446 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5447 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5448 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5449 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5450 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005451 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5452 // Take advantage of the 16 bytes in the XMM register.
5453 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5454 Address stack(ESP, destination.GetStackIndex());
5455 // Load the double into the high doubleword.
5456 __ movhpd(reg, stack);
5457
5458 // Store the low double into the destination.
5459 __ movsd(stack, reg);
5460
5461 // Move the high double to the low double.
5462 __ psrldq(reg, Immediate(8));
5463 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5464 // Take advantage of the 16 bytes in the XMM register.
5465 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5466 Address stack(ESP, source.GetStackIndex());
5467 // Load the double into the high doubleword.
5468 __ movhpd(reg, stack);
5469
5470 // Store the low double into the destination.
5471 __ movsd(stack, reg);
5472
5473 // Move the high double to the low double.
5474 __ psrldq(reg, Immediate(8));
5475 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5476 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5477 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005478 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005479 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005480 }
5481}
5482
5483void ParallelMoveResolverX86::SpillScratch(int reg) {
5484 __ pushl(static_cast<Register>(reg));
5485}
5486
5487void ParallelMoveResolverX86::RestoreScratch(int reg) {
5488 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005489}
5490
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005491void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005492 InvokeRuntimeCallingConvention calling_convention;
5493 CodeGenerator::CreateLoadClassLocationSummary(
5494 cls,
5495 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005496 Location::RegisterLocation(EAX),
5497 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005498}
5499
5500void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005501 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005502 if (cls->NeedsAccessCheck()) {
5503 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5504 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5505 cls,
5506 cls->GetDexPc(),
5507 nullptr);
Calin Juravle580b6092015-10-06 17:35:58 +01005508 return;
5509 }
5510
Roland Levillain0d5a2812015-11-13 10:07:31 +00005511 Location out_loc = locations->Out();
5512 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005513 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005514
Calin Juravle580b6092015-10-06 17:35:58 +01005515 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005516 DCHECK(!cls->CanCallRuntime());
5517 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005518 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5519 if (kEmitCompilerReadBarrier) {
5520 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5521 __ leal(out, Address(current_method, declaring_class_offset));
5522 // /* mirror::Class* */ out = out->Read()
5523 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5524 } else {
5525 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5526 __ movl(out, Address(current_method, declaring_class_offset));
5527 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005528 } else {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005529 DCHECK(cls->CanCallRuntime());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005530 // /* GcRoot<mirror::Class>[] */ out =
5531 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5532 __ movl(out, Address(current_method,
5533 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5534
5535 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5536 if (kEmitCompilerReadBarrier) {
5537 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5538 __ leal(out, Address(out, cache_offset));
5539 // /* mirror::Class* */ out = out->Read()
5540 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5541 } else {
5542 // /* GcRoot<mirror::Class> */ out = out[type_index]
5543 __ movl(out, Address(out, cache_offset));
5544 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005545
Andreas Gampe85b62f22015-09-09 13:15:38 -07005546 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005547 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5548 codegen_->AddSlowPath(slow_path);
5549 __ testl(out, out);
5550 __ j(kEqual, slow_path->GetEntryLabel());
5551 if (cls->MustGenerateClinitCheck()) {
5552 GenerateClassInitializationCheck(slow_path, out);
5553 } else {
5554 __ Bind(slow_path->GetExitLabel());
5555 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005556 }
5557}
5558
5559void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5560 LocationSummary* locations =
5561 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5562 locations->SetInAt(0, Location::RequiresRegister());
5563 if (check->HasUses()) {
5564 locations->SetOut(Location::SameAsFirstInput());
5565 }
5566}
5567
5568void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005569 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005570 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005571 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005572 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005573 GenerateClassInitializationCheck(slow_path,
5574 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005575}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005576
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005577void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005578 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005579 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5580 Immediate(mirror::Class::kStatusInitialized));
5581 __ j(kLess, slow_path->GetEntryLabel());
5582 __ Bind(slow_path->GetExitLabel());
5583 // No need for memory fence, thanks to the X86 memory model.
5584}
5585
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005586void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5587 LocationSummary* locations =
5588 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005589 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005590 locations->SetOut(Location::RequiresRegister());
5591}
5592
5593void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005594 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005595 codegen_->AddSlowPath(slow_path);
5596
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005597 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005598 Location out_loc = locations->Out();
5599 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005600 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005601
5602 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5603 if (kEmitCompilerReadBarrier) {
5604 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5605 __ leal(out, Address(current_method, declaring_class_offset));
5606 // /* mirror::Class* */ out = out->Read()
5607 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5608 } else {
5609 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5610 __ movl(out, Address(current_method, declaring_class_offset));
5611 }
5612
5613 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005614 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005615
5616 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5617 if (kEmitCompilerReadBarrier) {
5618 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5619 __ leal(out, Address(out, cache_offset));
5620 // /* mirror::String* */ out = out->Read()
5621 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5622 } else {
5623 // /* GcRoot<mirror::String> */ out = out[string_index]
5624 __ movl(out, Address(out, cache_offset));
5625 }
5626
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005627 __ testl(out, out);
5628 __ j(kEqual, slow_path->GetEntryLabel());
5629 __ Bind(slow_path->GetExitLabel());
5630}
5631
David Brazdilcb1c0552015-08-04 16:22:25 +01005632static Address GetExceptionTlsAddress() {
5633 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5634}
5635
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005636void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5637 LocationSummary* locations =
5638 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5639 locations->SetOut(Location::RequiresRegister());
5640}
5641
5642void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005643 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5644}
5645
5646void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5647 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5648}
5649
5650void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5651 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005652}
5653
5654void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5655 LocationSummary* locations =
5656 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5657 InvokeRuntimeCallingConvention calling_convention;
5658 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5659}
5660
5661void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005662 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5663 instruction,
5664 instruction->GetDexPc(),
5665 nullptr);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005666}
5667
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005668void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005669 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005670 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5671 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005672 case TypeCheckKind::kExactCheck:
5673 case TypeCheckKind::kAbstractClassCheck:
5674 case TypeCheckKind::kClassHierarchyCheck:
5675 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005676 call_kind =
5677 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005678 break;
5679 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005680 case TypeCheckKind::kUnresolvedCheck:
5681 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005682 call_kind = LocationSummary::kCallOnSlowPath;
5683 break;
5684 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005685
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005686 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005687 locations->SetInAt(0, Location::RequiresRegister());
5688 locations->SetInAt(1, Location::Any());
5689 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5690 locations->SetOut(Location::RequiresRegister());
5691 // When read barriers are enabled, we need a temporary register for
5692 // some cases.
5693 if (kEmitCompilerReadBarrier &&
5694 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5695 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5696 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5697 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005698 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005699}
5700
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005701void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005702 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005703 Location obj_loc = locations->InAt(0);
5704 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005705 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005706 Location out_loc = locations->Out();
5707 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005708 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005709 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5710 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5711 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005712 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005714
5715 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005716 // Avoid null check if we know obj is not null.
5717 if (instruction->MustDoNullCheck()) {
5718 __ testl(obj, obj);
5719 __ j(kEqual, &zero);
5720 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005721
Roland Levillain0d5a2812015-11-13 10:07:31 +00005722 // /* HeapReference<Class> */ out = obj->klass_
5723 __ movl(out, Address(obj, class_offset));
5724 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005725
5726 switch (instruction->GetTypeCheckKind()) {
5727 case TypeCheckKind::kExactCheck: {
5728 if (cls.IsRegister()) {
5729 __ cmpl(out, cls.AsRegister<Register>());
5730 } else {
5731 DCHECK(cls.IsStackSlot()) << cls;
5732 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5733 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005734
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005735 // Classes must be equal for the instanceof to succeed.
5736 __ j(kNotEqual, &zero);
5737 __ movl(out, Immediate(1));
5738 __ jmp(&done);
5739 break;
5740 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005741
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 case TypeCheckKind::kAbstractClassCheck: {
5743 // If the class is abstract, we eagerly fetch the super class of the
5744 // object to avoid doing a comparison we know will fail.
5745 NearLabel loop;
5746 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005747 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5748 if (kEmitCompilerReadBarrier) {
5749 // Save the value of `out` into `temp` before overwriting it
5750 // in the following move operation, as we will need it for the
5751 // read barrier below.
5752 Register temp = temp_loc.AsRegister<Register>();
5753 __ movl(temp, out);
5754 }
5755 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005756 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005757 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005758 __ testl(out, out);
5759 // If `out` is null, we use it for the result, and jump to `done`.
5760 __ j(kEqual, &done);
5761 if (cls.IsRegister()) {
5762 __ cmpl(out, cls.AsRegister<Register>());
5763 } else {
5764 DCHECK(cls.IsStackSlot()) << cls;
5765 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5766 }
5767 __ j(kNotEqual, &loop);
5768 __ movl(out, Immediate(1));
5769 if (zero.IsLinked()) {
5770 __ jmp(&done);
5771 }
5772 break;
5773 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005774
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005775 case TypeCheckKind::kClassHierarchyCheck: {
5776 // Walk over the class hierarchy to find a match.
5777 NearLabel loop, success;
5778 __ Bind(&loop);
5779 if (cls.IsRegister()) {
5780 __ cmpl(out, cls.AsRegister<Register>());
5781 } else {
5782 DCHECK(cls.IsStackSlot()) << cls;
5783 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5784 }
5785 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5787 if (kEmitCompilerReadBarrier) {
5788 // Save the value of `out` into `temp` before overwriting it
5789 // in the following move operation, as we will need it for the
5790 // read barrier below.
5791 Register temp = temp_loc.AsRegister<Register>();
5792 __ movl(temp, out);
5793 }
5794 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005795 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005796 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005797 __ testl(out, out);
5798 __ j(kNotEqual, &loop);
5799 // If `out` is null, we use it for the result, and jump to `done`.
5800 __ jmp(&done);
5801 __ Bind(&success);
5802 __ movl(out, Immediate(1));
5803 if (zero.IsLinked()) {
5804 __ jmp(&done);
5805 }
5806 break;
5807 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005808
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005809 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005810 // Do an exact check.
5811 NearLabel exact_check;
5812 if (cls.IsRegister()) {
5813 __ cmpl(out, cls.AsRegister<Register>());
5814 } else {
5815 DCHECK(cls.IsStackSlot()) << cls;
5816 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5817 }
5818 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819 // Otherwise, we need to check that the object's class is a non-primitive array.
5820 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5821 if (kEmitCompilerReadBarrier) {
5822 // Save the value of `out` into `temp` before overwriting it
5823 // in the following move operation, as we will need it for the
5824 // read barrier below.
5825 Register temp = temp_loc.AsRegister<Register>();
5826 __ movl(temp, out);
5827 }
5828 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005829 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005830 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005831 __ testl(out, out);
5832 // If `out` is null, we use it for the result, and jump to `done`.
5833 __ j(kEqual, &done);
5834 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5835 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005836 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005837 __ movl(out, Immediate(1));
5838 __ jmp(&done);
5839 break;
5840 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005841
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005842 case TypeCheckKind::kArrayCheck: {
5843 if (cls.IsRegister()) {
5844 __ cmpl(out, cls.AsRegister<Register>());
5845 } else {
5846 DCHECK(cls.IsStackSlot()) << cls;
5847 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5848 }
5849 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005850 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5851 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005852 codegen_->AddSlowPath(slow_path);
5853 __ j(kNotEqual, slow_path->GetEntryLabel());
5854 __ movl(out, Immediate(1));
5855 if (zero.IsLinked()) {
5856 __ jmp(&done);
5857 }
5858 break;
5859 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005860
Calin Juravle98893e12015-10-02 21:05:03 +01005861 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005862 case TypeCheckKind::kInterfaceCheck: {
5863 // Note that we indeed only call on slow path, but we always go
5864 // into the slow path for the unresolved & interface check
5865 // cases.
5866 //
5867 // We cannot directly call the InstanceofNonTrivial runtime
5868 // entry point without resorting to a type checking slow path
5869 // here (i.e. by calling InvokeRuntime directly), as it would
5870 // require to assign fixed registers for the inputs of this
5871 // HInstanceOf instruction (following the runtime calling
5872 // convention), which might be cluttered by the potential first
5873 // read barrier emission at the beginning of this method.
5874 DCHECK(locations->OnlyCallsOnSlowPath());
5875 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5876 /* is_fatal */ false);
5877 codegen_->AddSlowPath(slow_path);
5878 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005879 if (zero.IsLinked()) {
5880 __ jmp(&done);
5881 }
5882 break;
5883 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005884 }
5885
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005887 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005888 __ xorl(out, out);
5889 }
5890
5891 if (done.IsLinked()) {
5892 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005893 }
5894
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005895 if (slow_path != nullptr) {
5896 __ Bind(slow_path->GetExitLabel());
5897 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005898}
5899
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005900void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005901 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5902 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5904 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 case TypeCheckKind::kExactCheck:
5906 case TypeCheckKind::kAbstractClassCheck:
5907 case TypeCheckKind::kClassHierarchyCheck:
5908 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5910 LocationSummary::kCallOnSlowPath :
5911 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005912 break;
5913 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005914 case TypeCheckKind::kUnresolvedCheck:
5915 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005916 call_kind = LocationSummary::kCallOnSlowPath;
5917 break;
5918 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5920 locations->SetInAt(0, Location::RequiresRegister());
5921 locations->SetInAt(1, Location::Any());
5922 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
5923 locations->AddTemp(Location::RequiresRegister());
5924 // When read barriers are enabled, we need an additional temporary
5925 // register for some cases.
5926 if (kEmitCompilerReadBarrier &&
5927 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5928 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5929 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005932}
5933
5934void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5935 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005936 Location obj_loc = locations->InAt(0);
5937 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005938 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005939 Location temp_loc = locations->GetTemp(0);
5940 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5942 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5943 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5944 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5947 bool is_type_check_slow_path_fatal =
5948 (type_check_kind == TypeCheckKind::kExactCheck ||
5949 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5950 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5951 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5952 !instruction->CanThrowIntoCatchBlock();
5953 SlowPathCode* type_check_slow_path =
5954 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5955 is_type_check_slow_path_fatal);
5956 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957
Roland Levillain0d5a2812015-11-13 10:07:31 +00005958 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005959 // Avoid null check if we know obj is not null.
5960 if (instruction->MustDoNullCheck()) {
5961 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005962 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005963 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005964
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 // /* HeapReference<Class> */ temp = obj->klass_
5966 __ movl(temp, Address(obj, class_offset));
5967 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968
Roland Levillain0d5a2812015-11-13 10:07:31 +00005969 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 case TypeCheckKind::kExactCheck:
5971 case TypeCheckKind::kArrayCheck: {
5972 if (cls.IsRegister()) {
5973 __ cmpl(temp, cls.AsRegister<Register>());
5974 } else {
5975 DCHECK(cls.IsStackSlot()) << cls;
5976 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
5977 }
5978 // Jump to slow path for throwing the exception or doing a
5979 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005980 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981 break;
5982 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005983
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005984 case TypeCheckKind::kAbstractClassCheck: {
5985 // If the class is abstract, we eagerly fetch the super class of the
5986 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005987 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005989 Location temp2_loc =
5990 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
5991 if (kEmitCompilerReadBarrier) {
5992 // Save the value of `temp` into `temp2` before overwriting it
5993 // in the following move operation, as we will need it for the
5994 // read barrier below.
5995 Register temp2 = temp2_loc.AsRegister<Register>();
5996 __ movl(temp2, temp);
5997 }
5998 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006000 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6001
6002 // If the class reference currently in `temp` is not null, jump
6003 // to the `compare_classes` label to compare it with the checked
6004 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006005 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006006 __ j(kNotEqual, &compare_classes);
6007 // Otherwise, jump to the slow path to throw the exception.
6008 //
6009 // But before, move back the object's class into `temp` before
6010 // going into the slow path, as it has been overwritten in the
6011 // meantime.
6012 // /* HeapReference<Class> */ temp = obj->klass_
6013 __ movl(temp, Address(obj, class_offset));
6014 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6015 __ jmp(type_check_slow_path->GetEntryLabel());
6016
6017 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 if (cls.IsRegister()) {
6019 __ cmpl(temp, cls.AsRegister<Register>());
6020 } else {
6021 DCHECK(cls.IsStackSlot()) << cls;
6022 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6023 }
6024 __ j(kNotEqual, &loop);
6025 break;
6026 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006027
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028 case TypeCheckKind::kClassHierarchyCheck: {
6029 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006030 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006031 __ Bind(&loop);
6032 if (cls.IsRegister()) {
6033 __ cmpl(temp, cls.AsRegister<Register>());
6034 } else {
6035 DCHECK(cls.IsStackSlot()) << cls;
6036 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6037 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006038 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006039
6040 Location temp2_loc =
6041 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6042 if (kEmitCompilerReadBarrier) {
6043 // Save the value of `temp` into `temp2` before overwriting it
6044 // in the following move operation, as we will need it for the
6045 // read barrier below.
6046 Register temp2 = temp2_loc.AsRegister<Register>();
6047 __ movl(temp2, temp);
6048 }
6049 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006050 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006051 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6052
6053 // If the class reference currently in `temp` is not null, jump
6054 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006055 __ testl(temp, temp);
6056 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006057 // Otherwise, jump to the slow path to throw the exception.
6058 //
6059 // But before, move back the object's class into `temp` before
6060 // going into the slow path, as it has been overwritten in the
6061 // meantime.
6062 // /* HeapReference<Class> */ temp = obj->klass_
6063 __ movl(temp, Address(obj, class_offset));
6064 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6065 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006066 break;
6067 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006069 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006070 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006071 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006072 if (cls.IsRegister()) {
6073 __ cmpl(temp, cls.AsRegister<Register>());
6074 } else {
6075 DCHECK(cls.IsStackSlot()) << cls;
6076 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6077 }
6078 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079
6080 // Otherwise, we need to check that the object's class is a non-primitive array.
6081 Location temp2_loc =
6082 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6083 if (kEmitCompilerReadBarrier) {
6084 // Save the value of `temp` into `temp2` before overwriting it
6085 // in the following move operation, as we will need it for the
6086 // read barrier below.
6087 Register temp2 = temp2_loc.AsRegister<Register>();
6088 __ movl(temp2, temp);
6089 }
6090 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006091 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006092 codegen_->MaybeGenerateReadBarrier(
6093 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
6094
6095 // If the component type is not null (i.e. the object is indeed
6096 // an array), jump to label `check_non_primitive_component_type`
6097 // to further check that this component type is not a primitive
6098 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006099 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006100 __ j(kNotEqual, &check_non_primitive_component_type);
6101 // Otherwise, jump to the slow path to throw the exception.
6102 //
6103 // But before, move back the object's class into `temp` before
6104 // going into the slow path, as it has been overwritten in the
6105 // meantime.
6106 // /* HeapReference<Class> */ temp = obj->klass_
6107 __ movl(temp, Address(obj, class_offset));
6108 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6109 __ jmp(type_check_slow_path->GetEntryLabel());
6110
6111 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006112 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006113 __ j(kEqual, &done);
6114 // Same comment as above regarding `temp` and the slow path.
6115 // /* HeapReference<Class> */ temp = obj->klass_
6116 __ movl(temp, Address(obj, class_offset));
6117 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6118 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006119 break;
6120 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121
Calin Juravle98893e12015-10-02 21:05:03 +01006122 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006123 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006124 // We always go into the type check slow path for the unresolved &
6125 // interface check cases.
6126 //
6127 // We cannot directly call the CheckCast runtime entry point
6128 // without resorting to a type checking slow path here (i.e. by
6129 // calling InvokeRuntime directly), as it would require to
6130 // assign fixed registers for the inputs of this HInstanceOf
6131 // instruction (following the runtime calling convention), which
6132 // might be cluttered by the potential first read barrier
6133 // emission at the beginning of this method.
6134 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006135 break;
6136 }
6137 __ Bind(&done);
6138
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006140}
6141
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006142void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6143 LocationSummary* locations =
6144 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6145 InvokeRuntimeCallingConvention calling_convention;
6146 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6147}
6148
6149void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006150 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6151 : QUICK_ENTRY_POINT(pUnlockObject),
6152 instruction,
6153 instruction->GetDexPc(),
6154 nullptr);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006155}
6156
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006157void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6158void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6159void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6160
6161void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6162 LocationSummary* locations =
6163 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6164 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6165 || instruction->GetResultType() == Primitive::kPrimLong);
6166 locations->SetInAt(0, Location::RequiresRegister());
6167 locations->SetInAt(1, Location::Any());
6168 locations->SetOut(Location::SameAsFirstInput());
6169}
6170
6171void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6172 HandleBitwiseOperation(instruction);
6173}
6174
6175void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6176 HandleBitwiseOperation(instruction);
6177}
6178
6179void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6180 HandleBitwiseOperation(instruction);
6181}
6182
6183void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6184 LocationSummary* locations = instruction->GetLocations();
6185 Location first = locations->InAt(0);
6186 Location second = locations->InAt(1);
6187 DCHECK(first.Equals(locations->Out()));
6188
6189 if (instruction->GetResultType() == Primitive::kPrimInt) {
6190 if (second.IsRegister()) {
6191 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006192 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006193 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006194 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006195 } else {
6196 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006197 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006198 }
6199 } else if (second.IsConstant()) {
6200 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006201 __ andl(first.AsRegister<Register>(),
6202 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006203 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006204 __ orl(first.AsRegister<Register>(),
6205 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006206 } else {
6207 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006208 __ xorl(first.AsRegister<Register>(),
6209 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006210 }
6211 } else {
6212 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006213 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006214 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006215 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006216 } else {
6217 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006218 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006219 }
6220 }
6221 } else {
6222 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6223 if (second.IsRegisterPair()) {
6224 if (instruction->IsAnd()) {
6225 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6226 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6227 } else if (instruction->IsOr()) {
6228 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6229 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6230 } else {
6231 DCHECK(instruction->IsXor());
6232 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6233 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6234 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006235 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006236 if (instruction->IsAnd()) {
6237 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6238 __ andl(first.AsRegisterPairHigh<Register>(),
6239 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6240 } else if (instruction->IsOr()) {
6241 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6242 __ orl(first.AsRegisterPairHigh<Register>(),
6243 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6244 } else {
6245 DCHECK(instruction->IsXor());
6246 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6247 __ xorl(first.AsRegisterPairHigh<Register>(),
6248 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6249 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006250 } else {
6251 DCHECK(second.IsConstant()) << second;
6252 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006253 int32_t low_value = Low32Bits(value);
6254 int32_t high_value = High32Bits(value);
6255 Immediate low(low_value);
6256 Immediate high(high_value);
6257 Register first_low = first.AsRegisterPairLow<Register>();
6258 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006259 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006260 if (low_value == 0) {
6261 __ xorl(first_low, first_low);
6262 } else if (low_value != -1) {
6263 __ andl(first_low, low);
6264 }
6265 if (high_value == 0) {
6266 __ xorl(first_high, first_high);
6267 } else if (high_value != -1) {
6268 __ andl(first_high, high);
6269 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006270 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006271 if (low_value != 0) {
6272 __ orl(first_low, low);
6273 }
6274 if (high_value != 0) {
6275 __ orl(first_high, high);
6276 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006277 } else {
6278 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006279 if (low_value != 0) {
6280 __ xorl(first_low, low);
6281 }
6282 if (high_value != 0) {
6283 __ xorl(first_high, high);
6284 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006285 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006286 }
6287 }
6288}
6289
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290void CodeGeneratorX86::GenerateReadBarrier(HInstruction* instruction,
6291 Location out,
6292 Location ref,
6293 Location obj,
6294 uint32_t offset,
6295 Location index) {
6296 DCHECK(kEmitCompilerReadBarrier);
6297
6298 // If heap poisoning is enabled, the unpoisoning of the loaded
6299 // reference will be carried out by the runtime within the slow
6300 // path.
6301 //
6302 // Note that `ref` currently does not get unpoisoned (when heap
6303 // poisoning is enabled), which is alright as the `ref` argument is
6304 // not used by the artReadBarrierSlow entry point.
6305 //
6306 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6307 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6308 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6309 AddSlowPath(slow_path);
6310
6311 // TODO: When read barrier has a fast path, add it here.
6312 /* Currently the read barrier call is inserted after the original load.
6313 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
6314 * original load. This load-load ordering is required by the read barrier.
6315 * The fast path/slow path (for Baker's algorithm) should look like:
6316 *
6317 * bool isGray = obj.LockWord & kReadBarrierMask;
6318 * lfence; // load fence or artificial data dependence to prevent load-load reordering
6319 * ref = obj.field; // this is the original load
6320 * if (isGray) {
6321 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
6322 * }
6323 */
6324
6325 __ jmp(slow_path->GetEntryLabel());
6326 __ Bind(slow_path->GetExitLabel());
6327}
6328
6329void CodeGeneratorX86::MaybeGenerateReadBarrier(HInstruction* instruction,
6330 Location out,
6331 Location ref,
6332 Location obj,
6333 uint32_t offset,
6334 Location index) {
6335 if (kEmitCompilerReadBarrier) {
6336 // If heap poisoning is enabled, unpoisoning will be taken care of
6337 // by the runtime within the slow path.
6338 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
6339 } else if (kPoisonHeapReferences) {
6340 __ UnpoisonHeapReference(out.AsRegister<Register>());
6341 }
6342}
6343
6344void CodeGeneratorX86::GenerateReadBarrierForRoot(HInstruction* instruction,
6345 Location out,
6346 Location root) {
6347 DCHECK(kEmitCompilerReadBarrier);
6348
6349 // Note that GC roots are not affected by heap poisoning, so we do
6350 // not need to do anything special for this here.
6351 SlowPathCode* slow_path =
6352 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6353 AddSlowPath(slow_path);
6354
6355 // TODO: Implement a fast path for ReadBarrierForRoot, performing
6356 // the following operation (for Baker's algorithm):
6357 //
6358 // if (thread.tls32_.is_gc_marking) {
6359 // root = Mark(root);
6360 // }
6361
6362 __ jmp(slow_path->GetEntryLabel());
6363 __ Bind(slow_path->GetExitLabel());
6364}
6365
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006366void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006367 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006368 LOG(FATAL) << "Unreachable";
6369}
6370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006371void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006372 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006373 LOG(FATAL) << "Unreachable";
6374}
6375
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006376void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6377 DCHECK(codegen_->IsBaseline());
6378 LocationSummary* locations =
6379 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6380 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6381}
6382
6383void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6384 DCHECK(codegen_->IsBaseline());
6385 // Will be generated at use site.
6386}
6387
Mark Mendellfe57faa2015-09-18 09:26:15 -04006388// Simple implementation of packed switch - generate cascaded compare/jumps.
6389void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6390 LocationSummary* locations =
6391 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6392 locations->SetInAt(0, Location::RequiresRegister());
6393}
6394
6395void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6396 int32_t lower_bound = switch_instr->GetStartValue();
6397 int32_t num_entries = switch_instr->GetNumEntries();
6398 LocationSummary* locations = switch_instr->GetLocations();
6399 Register value_reg = locations->InAt(0).AsRegister<Register>();
6400 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6401
6402 // Create a series of compare/jumps.
6403 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6404 for (int i = 0; i < num_entries; i++) {
6405 int32_t case_value = lower_bound + i;
6406 if (case_value == 0) {
6407 __ testl(value_reg, value_reg);
6408 } else {
6409 __ cmpl(value_reg, Immediate(case_value));
6410 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006411 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006412 }
6413
6414 // And the default for any other value.
6415 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6416 __ jmp(codegen_->GetLabelOf(default_block));
6417 }
6418}
6419
Mark Mendell805b3b52015-09-18 14:10:29 -04006420void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6421 LocationSummary* locations =
6422 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6423 locations->SetInAt(0, Location::RequiresRegister());
6424
6425 // Constant area pointer.
6426 locations->SetInAt(1, Location::RequiresRegister());
6427
6428 // And the temporary we need.
6429 locations->AddTemp(Location::RequiresRegister());
6430}
6431
6432void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6433 int32_t lower_bound = switch_instr->GetStartValue();
6434 int32_t num_entries = switch_instr->GetNumEntries();
6435 LocationSummary* locations = switch_instr->GetLocations();
6436 Register value_reg = locations->InAt(0).AsRegister<Register>();
6437 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6438
6439 // Optimizing has a jump area.
6440 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6441 Register constant_area = locations->InAt(1).AsRegister<Register>();
6442
6443 // Remove the bias, if needed.
6444 if (lower_bound != 0) {
6445 __ leal(temp_reg, Address(value_reg, -lower_bound));
6446 value_reg = temp_reg;
6447 }
6448
6449 // Is the value in range?
6450 DCHECK_GE(num_entries, 1);
6451 __ cmpl(value_reg, Immediate(num_entries - 1));
6452 __ j(kAbove, codegen_->GetLabelOf(default_block));
6453
6454 // We are in the range of the table.
6455 // Load (target-constant_area) from the jump table, indexing by the value.
6456 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6457
6458 // Compute the actual target address by adding in constant_area.
6459 __ addl(temp_reg, constant_area);
6460
6461 // And jump.
6462 __ jmp(temp_reg);
6463}
6464
Mark Mendell0616ae02015-04-17 12:49:27 -04006465void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6466 HX86ComputeBaseMethodAddress* insn) {
6467 LocationSummary* locations =
6468 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6469 locations->SetOut(Location::RequiresRegister());
6470}
6471
6472void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6473 HX86ComputeBaseMethodAddress* insn) {
6474 LocationSummary* locations = insn->GetLocations();
6475 Register reg = locations->Out().AsRegister<Register>();
6476
6477 // Generate call to next instruction.
6478 Label next_instruction;
6479 __ call(&next_instruction);
6480 __ Bind(&next_instruction);
6481
6482 // Remember this offset for later use with constant area.
6483 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6484
6485 // Grab the return address off the stack.
6486 __ popl(reg);
6487}
6488
6489void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6490 HX86LoadFromConstantTable* insn) {
6491 LocationSummary* locations =
6492 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6493
6494 locations->SetInAt(0, Location::RequiresRegister());
6495 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6496
6497 // If we don't need to be materialized, we only need the inputs to be set.
6498 if (!insn->NeedsMaterialization()) {
6499 return;
6500 }
6501
6502 switch (insn->GetType()) {
6503 case Primitive::kPrimFloat:
6504 case Primitive::kPrimDouble:
6505 locations->SetOut(Location::RequiresFpuRegister());
6506 break;
6507
6508 case Primitive::kPrimInt:
6509 locations->SetOut(Location::RequiresRegister());
6510 break;
6511
6512 default:
6513 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6514 }
6515}
6516
6517void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6518 if (!insn->NeedsMaterialization()) {
6519 return;
6520 }
6521
6522 LocationSummary* locations = insn->GetLocations();
6523 Location out = locations->Out();
6524 Register const_area = locations->InAt(0).AsRegister<Register>();
6525 HConstant *value = insn->GetConstant();
6526
6527 switch (insn->GetType()) {
6528 case Primitive::kPrimFloat:
6529 __ movss(out.AsFpuRegister<XmmRegister>(),
6530 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6531 break;
6532
6533 case Primitive::kPrimDouble:
6534 __ movsd(out.AsFpuRegister<XmmRegister>(),
6535 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6536 break;
6537
6538 case Primitive::kPrimInt:
6539 __ movl(out.AsRegister<Register>(),
6540 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6541 break;
6542
6543 default:
6544 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6545 }
6546}
6547
Mark Mendell0616ae02015-04-17 12:49:27 -04006548/**
6549 * Class to handle late fixup of offsets into constant area.
6550 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006551class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006552 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006553 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6554 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6555
6556 protected:
6557 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6558
6559 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006560
6561 private:
6562 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6563 // Patch the correct offset for the instruction. The place to patch is the
6564 // last 4 bytes of the instruction.
6565 // The value to patch is the distance from the offset in the constant area
6566 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006567 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6568 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006569
6570 // Patch in the right value.
6571 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6572 }
6573
Mark Mendell0616ae02015-04-17 12:49:27 -04006574 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006575 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006576};
6577
Mark Mendell805b3b52015-09-18 14:10:29 -04006578/**
6579 * Class to handle late fixup of offsets to a jump table that will be created in the
6580 * constant area.
6581 */
6582class JumpTableRIPFixup : public RIPFixup {
6583 public:
6584 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6585 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6586
6587 void CreateJumpTable() {
6588 X86Assembler* assembler = codegen_->GetAssembler();
6589
6590 // Ensure that the reference to the jump table has the correct offset.
6591 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6592 SetOffset(offset_in_constant_table);
6593
6594 // The label values in the jump table are computed relative to the
6595 // instruction addressing the constant area.
6596 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6597
6598 // Populate the jump table with the correct values for the jump table.
6599 int32_t num_entries = switch_instr_->GetNumEntries();
6600 HBasicBlock* block = switch_instr_->GetBlock();
6601 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6602 // The value that we want is the target offset - the position of the table.
6603 for (int32_t i = 0; i < num_entries; i++) {
6604 HBasicBlock* b = successors[i];
6605 Label* l = codegen_->GetLabelOf(b);
6606 DCHECK(l->IsBound());
6607 int32_t offset_to_block = l->Position() - relative_offset;
6608 assembler->AppendInt32(offset_to_block);
6609 }
6610 }
6611
6612 private:
6613 const HX86PackedSwitch* switch_instr_;
6614};
6615
6616void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
6617 // Generate the constant area if needed.
6618 X86Assembler* assembler = GetAssembler();
6619 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6620 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
6621 // byte values.
6622 assembler->Align(4, 0);
6623 constant_area_start_ = assembler->CodeSize();
6624
6625 // Populate any jump tables.
6626 for (auto jump_table : fixups_to_jump_tables_) {
6627 jump_table->CreateJumpTable();
6628 }
6629
6630 // And now add the constant area to the generated code.
6631 assembler->AddConstantArea();
6632 }
6633
6634 // And finish up.
6635 CodeGenerator::Finalize(allocator);
6636}
6637
Mark Mendell0616ae02015-04-17 12:49:27 -04006638Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
6639 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6640 return Address(reg, kDummy32BitOffset, fixup);
6641}
6642
6643Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
6644 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6645 return Address(reg, kDummy32BitOffset, fixup);
6646}
6647
6648Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
6649 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6650 return Address(reg, kDummy32BitOffset, fixup);
6651}
6652
6653Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
6654 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6655 return Address(reg, kDummy32BitOffset, fixup);
6656}
6657
Mark Mendell805b3b52015-09-18 14:10:29 -04006658Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
6659 Register reg,
6660 Register value) {
6661 // Create a fixup to be used to create and address the jump table.
6662 JumpTableRIPFixup* table_fixup =
6663 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6664
6665 // We have to populate the jump tables.
6666 fixups_to_jump_tables_.push_back(table_fixup);
6667
6668 // We want a scaled address, as we are extracting the correct offset from the table.
6669 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
6670}
6671
Andreas Gampe85b62f22015-09-09 13:15:38 -07006672// TODO: target as memory.
6673void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
6674 if (!target.IsValid()) {
6675 DCHECK(type == Primitive::kPrimVoid);
6676 return;
6677 }
6678
6679 DCHECK_NE(type, Primitive::kPrimVoid);
6680
6681 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
6682 if (target.Equals(return_loc)) {
6683 return;
6684 }
6685
6686 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6687 // with the else branch.
6688 if (type == Primitive::kPrimLong) {
6689 HParallelMove parallel_move(GetGraph()->GetArena());
6690 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
6691 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
6692 GetMoveResolver()->EmitNativeCode(&parallel_move);
6693 } else {
6694 // Let the parallel move resolver take care of all of this.
6695 HParallelMove parallel_move(GetGraph()->GetArena());
6696 parallel_move.AddMove(return_loc, target, type, nullptr);
6697 GetMoveResolver()->EmitNativeCode(&parallel_move);
6698 }
6699}
6700
Roland Levillain4d027112015-07-01 15:41:14 +01006701#undef __
6702
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006703} // namespace x86
6704} // namespace art