blob: bc3256ec8c6b084983b06c7891eb92303a07ad47 [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 Geoffrayb4c13762015-12-16 12:06:39 +000045
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);
Roland Levillain888d0672015-11-23 18:53:50 +000070 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010071 }
72
Alexandre Rames8158f282015-08-07 10:26:17 +010073 bool IsFatal() const OVERRIDE { return true; }
74
Alexandre Rames9931f312015-06-19 14:47:01 +010075 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
76
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010078 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010079 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
80};
81
Andreas Gampe85b62f22015-09-09 13:15:38 -070082class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000083 public:
84 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
85
Alexandre Rames2ed20af2015-03-06 13:55:35 +000086 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010087 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000088 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000089 if (instruction_->CanThrowIntoCatchBlock()) {
90 // Live registers will be restored in the catch block if caught.
91 SaveLiveRegisters(codegen, instruction_->GetLocations());
92 }
Alexandre Rames8158f282015-08-07 10:26:17 +010093 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
94 instruction_,
95 instruction_->GetDexPc(),
96 this);
Roland Levillain888d0672015-11-23 18:53:50 +000097 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000098 }
99
Alexandre Rames8158f282015-08-07 10:26:17 +0100100 bool IsFatal() const OVERRIDE { return true; }
101
Alexandre Rames9931f312015-06-19 14:47:01 +0100102 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
103
Calin Juravled0d48522014-11-04 16:40:20 +0000104 private:
105 HDivZeroCheck* const instruction_;
106 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
107};
108
Andreas Gampe85b62f22015-09-09 13:15:38 -0700109class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000110 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100111 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000112
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000113 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000114 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000115 if (is_div_) {
116 __ negl(reg_);
117 } else {
118 __ movl(reg_, Immediate(0));
119 }
Calin Juravled0d48522014-11-04 16:40:20 +0000120 __ jmp(GetExitLabel());
121 }
122
Alexandre Rames9931f312015-06-19 14:47:01 +0100123 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
124
Calin Juravled0d48522014-11-04 16:40:20 +0000125 private:
126 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000127 bool is_div_;
128 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000129};
130
Andreas Gampe85b62f22015-09-09 13:15:38 -0700131class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100133 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100134
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000135 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100136 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100137 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100138 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000139 // We're moving two locations to locations that could overlap, so we need a parallel
140 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000141 if (instruction_->CanThrowIntoCatchBlock()) {
142 // Live registers will be restored in the catch block if caught.
143 SaveLiveRegisters(codegen, instruction_->GetLocations());
144 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100145 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100147 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000148 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100149 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100150 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100151 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
152 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100153 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
154 instruction_,
155 instruction_->GetDexPc(),
156 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000157 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100158 }
159
Alexandre Rames8158f282015-08-07 10:26:17 +0100160 bool IsFatal() const OVERRIDE { return true; }
161
Alexandre Rames9931f312015-06-19 14:47:01 +0100162 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
163
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100164 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100165 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166
167 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
168};
169
Andreas Gampe85b62f22015-09-09 13:15:38 -0700170class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100173 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100176 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000178 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
180 instruction_,
181 instruction_->GetDexPc(),
182 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000183 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 if (successor_ == nullptr) {
186 __ jmp(GetReturnLabel());
187 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 }
191
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 Label* GetReturnLabel() {
193 DCHECK(successor_ == nullptr);
194 return &return_label_;
195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100197 HBasicBlock* GetSuccessor() const {
198 return successor_;
199 }
200
Alexandre Rames9931f312015-06-19 14:47:01 +0100201 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
202
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000203 private:
204 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100205 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000206 Label return_label_;
207
208 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
209};
210
Andreas Gampe85b62f22015-09-09 13:15:38 -0700211class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000212 public:
213 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
214
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000215 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000216 LocationSummary* locations = instruction_->GetLocations();
217 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
218
219 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
220 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000221 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000222
223 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800224 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100225 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
226 instruction_,
227 instruction_->GetDexPc(),
228 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000229 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232
233 __ jmp(GetExitLabel());
234 }
235
Alexandre Rames9931f312015-06-19 14:47:01 +0100236 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
237
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238 private:
239 HLoadString* const instruction_;
240
241 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
242};
243
Andreas Gampe85b62f22015-09-09 13:15:38 -0700244class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 public:
246 LoadClassSlowPathX86(HLoadClass* cls,
247 HInstruction* at,
248 uint32_t dex_pc,
249 bool do_clinit)
250 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
251 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
252 }
253
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000254 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000255 LocationSummary* locations = at_->GetLocations();
256 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
257 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000258 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000259
260 InvokeRuntimeCallingConvention calling_convention;
261 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100262 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
263 : QUICK_ENTRY_POINT(pInitializeType),
264 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000265 if (do_clinit_) {
266 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
267 } else {
268 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
269 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270
271 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000272 Location out = locations->Out();
273 if (out.IsValid()) {
274 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
275 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000276 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000277
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000278 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000279 __ jmp(GetExitLabel());
280 }
281
Alexandre Rames9931f312015-06-19 14:47:01 +0100282 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
283
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000284 private:
285 // The class this slow path will load.
286 HLoadClass* const cls_;
287
288 // The instruction where this slow path is happening.
289 // (Might be the load class or an initialization check).
290 HInstruction* const at_;
291
292 // The dex PC of `at_`.
293 const uint32_t dex_pc_;
294
295 // Whether to initialize the class.
296 const bool do_clinit_;
297
298 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
299};
300
Andreas Gampe85b62f22015-09-09 13:15:38 -0700301class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000302 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000303 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
304 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000306 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000307 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100308 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
309 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000310 DCHECK(instruction_->IsCheckCast()
311 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
313 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
314 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000315
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000316 if (!is_fatal_) {
317 SaveLiveRegisters(codegen, locations);
318 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000319
320 // We're moving two locations to locations that could overlap, so we need a parallel
321 // move resolver.
322 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000323 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100324 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000325 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100326 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100327 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100328 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
329 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000330
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000331 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100332 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
333 instruction_,
334 instruction_->GetDexPc(),
335 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000336 CheckEntrypointTypes<
337 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000338 } else {
339 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100340 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
341 instruction_,
342 instruction_->GetDexPc(),
343 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000344 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000345 }
346
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000347 if (!is_fatal_) {
348 if (instruction_->IsInstanceOf()) {
349 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
350 }
351 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000352
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000353 __ jmp(GetExitLabel());
354 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000355 }
356
Alexandre Rames9931f312015-06-19 14:47:01 +0100357 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000358 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100359
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000360 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000361 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000362 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000363
364 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
365};
366
Andreas Gampe85b62f22015-09-09 13:15:38 -0700367class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368 public:
369 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
370 : instruction_(instruction) {}
371
372 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100373 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100374 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 __ Bind(GetEntryLabel());
376 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100377 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
378 instruction_,
379 instruction_->GetDexPc(),
380 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000381 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 }
383
Alexandre Rames9931f312015-06-19 14:47:01 +0100384 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
385
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 private:
387 HInstruction* const instruction_;
388 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
389};
390
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100391class ArraySetSlowPathX86 : public SlowPathCode {
392 public:
393 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
394
395 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
396 LocationSummary* locations = instruction_->GetLocations();
397 __ Bind(GetEntryLabel());
398 SaveLiveRegisters(codegen, locations);
399
400 InvokeRuntimeCallingConvention calling_convention;
401 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
402 parallel_move.AddMove(
403 locations->InAt(0),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
405 Primitive::kPrimNot,
406 nullptr);
407 parallel_move.AddMove(
408 locations->InAt(1),
409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
410 Primitive::kPrimInt,
411 nullptr);
412 parallel_move.AddMove(
413 locations->InAt(2),
414 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
415 Primitive::kPrimNot,
416 nullptr);
417 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
418
419 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
420 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
421 instruction_,
422 instruction_->GetDexPc(),
423 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000424 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100425 RestoreLiveRegisters(codegen, locations);
426 __ jmp(GetExitLabel());
427 }
428
429 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
430
431 private:
432 HInstruction* const instruction_;
433
434 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
435};
436
Roland Levillain7c1559a2015-12-15 10:55:36 +0000437// Slow path marking an object during a read barrier.
438class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
439 public:
440 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
441 : instruction_(instruction), out_(out), obj_(obj) {
442 DCHECK(kEmitCompilerReadBarrier);
443 }
444
445 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
446
447 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
448 LocationSummary* locations = instruction_->GetLocations();
449 Register reg_out = out_.AsRegister<Register>();
450 DCHECK(locations->CanCall());
451 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
452 DCHECK(instruction_->IsInstanceFieldGet() ||
453 instruction_->IsStaticFieldGet() ||
454 instruction_->IsArrayGet() ||
455 instruction_->IsLoadClass() ||
456 instruction_->IsLoadString() ||
457 instruction_->IsInstanceOf() ||
458 instruction_->IsCheckCast())
459 << "Unexpected instruction in read barrier marking slow path: "
460 << instruction_->DebugName();
461
462 __ Bind(GetEntryLabel());
463 SaveLiveRegisters(codegen, locations);
464
465 InvokeRuntimeCallingConvention calling_convention;
466 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
467 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
468 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
469 instruction_,
470 instruction_->GetDexPc(),
471 this);
472 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
473 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
474
475 RestoreLiveRegisters(codegen, locations);
476 __ jmp(GetExitLabel());
477 }
478
479 private:
480 HInstruction* const instruction_;
481 const Location out_;
482 const Location obj_;
483
484 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
485};
486
Roland Levillain0d5a2812015-11-13 10:07:31 +0000487// Slow path generating a read barrier for a heap reference.
488class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
489 public:
490 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
491 Location out,
492 Location ref,
493 Location obj,
494 uint32_t offset,
495 Location index)
496 : instruction_(instruction),
497 out_(out),
498 ref_(ref),
499 obj_(obj),
500 offset_(offset),
501 index_(index) {
502 DCHECK(kEmitCompilerReadBarrier);
503 // If `obj` is equal to `out` or `ref`, it means the initial object
504 // has been overwritten by (or after) the heap object reference load
505 // to be instrumented, e.g.:
506 //
507 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000508 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000509 //
510 // In that case, we have lost the information about the original
511 // object, and the emitted read barrier cannot work properly.
512 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
513 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
514 }
515
516 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
517 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
518 LocationSummary* locations = instruction_->GetLocations();
519 Register reg_out = out_.AsRegister<Register>();
520 DCHECK(locations->CanCall());
521 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
522 DCHECK(!instruction_->IsInvoke() ||
523 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000524 instruction_->GetLocations()->Intrinsified()))
525 << "Unexpected instruction in read barrier for heap reference slow path: "
526 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000527
528 __ Bind(GetEntryLabel());
529 SaveLiveRegisters(codegen, locations);
530
531 // We may have to change the index's value, but as `index_` is a
532 // constant member (like other "inputs" of this slow path),
533 // introduce a copy of it, `index`.
534 Location index = index_;
535 if (index_.IsValid()) {
536 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
537 if (instruction_->IsArrayGet()) {
538 // Compute the actual memory offset and store it in `index`.
539 Register index_reg = index_.AsRegister<Register>();
540 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
541 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
542 // We are about to change the value of `index_reg` (see the
543 // calls to art::x86::X86Assembler::shll and
544 // art::x86::X86Assembler::AddImmediate below), but it has
545 // not been saved by the previous call to
546 // art::SlowPathCode::SaveLiveRegisters, as it is a
547 // callee-save register --
548 // art::SlowPathCode::SaveLiveRegisters does not consider
549 // callee-save registers, as it has been designed with the
550 // assumption that callee-save registers are supposed to be
551 // handled by the called function. So, as a callee-save
552 // register, `index_reg` _would_ eventually be saved onto
553 // the stack, but it would be too late: we would have
554 // changed its value earlier. Therefore, we manually save
555 // it here into another freely available register,
556 // `free_reg`, chosen of course among the caller-save
557 // registers (as a callee-save `free_reg` register would
558 // exhibit the same problem).
559 //
560 // Note we could have requested a temporary register from
561 // the register allocator instead; but we prefer not to, as
562 // this is a slow path, and we know we can find a
563 // caller-save register that is available.
564 Register free_reg = FindAvailableCallerSaveRegister(codegen);
565 __ movl(free_reg, index_reg);
566 index_reg = free_reg;
567 index = Location::RegisterLocation(index_reg);
568 } else {
569 // The initial register stored in `index_` has already been
570 // saved in the call to art::SlowPathCode::SaveLiveRegisters
571 // (as it is not a callee-save register), so we can freely
572 // use it.
573 }
574 // Shifting the index value contained in `index_reg` by the scale
575 // factor (2) cannot overflow in practice, as the runtime is
576 // unable to allocate object arrays with a size larger than
577 // 2^26 - 1 (that is, 2^28 - 4 bytes).
578 __ shll(index_reg, Immediate(TIMES_4));
579 static_assert(
580 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
581 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
582 __ AddImmediate(index_reg, Immediate(offset_));
583 } else {
584 DCHECK(instruction_->IsInvoke());
585 DCHECK(instruction_->GetLocations()->Intrinsified());
586 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
587 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
588 << instruction_->AsInvoke()->GetIntrinsic();
589 DCHECK_EQ(offset_, 0U);
590 DCHECK(index_.IsRegisterPair());
591 // UnsafeGet's offset location is a register pair, the low
592 // part contains the correct offset.
593 index = index_.ToLow();
594 }
595 }
596
597 // We're moving two or three locations to locations that could
598 // overlap, so we need a parallel move resolver.
599 InvokeRuntimeCallingConvention calling_convention;
600 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
601 parallel_move.AddMove(ref_,
602 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
603 Primitive::kPrimNot,
604 nullptr);
605 parallel_move.AddMove(obj_,
606 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
607 Primitive::kPrimNot,
608 nullptr);
609 if (index.IsValid()) {
610 parallel_move.AddMove(index,
611 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
612 Primitive::kPrimInt,
613 nullptr);
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 } else {
616 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
617 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
618 }
619 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
620 instruction_,
621 instruction_->GetDexPc(),
622 this);
623 CheckEntrypointTypes<
624 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
625 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
626
627 RestoreLiveRegisters(codegen, locations);
628 __ jmp(GetExitLabel());
629 }
630
631 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
632
633 private:
634 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
635 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
636 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
637 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
638 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
639 return static_cast<Register>(i);
640 }
641 }
642 // We shall never fail to find a free caller-save register, as
643 // there are more than two core caller-save registers on x86
644 // (meaning it is possible to find one which is different from
645 // `ref` and `obj`).
646 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
647 LOG(FATAL) << "Could not find a free caller-save register";
648 UNREACHABLE();
649 }
650
651 HInstruction* const instruction_;
652 const Location out_;
653 const Location ref_;
654 const Location obj_;
655 const uint32_t offset_;
656 // An additional location containing an index to an array.
657 // Only used for HArrayGet and the UnsafeGetObject &
658 // UnsafeGetObjectVolatile intrinsics.
659 const Location index_;
660
661 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
662};
663
664// Slow path generating a read barrier for a GC root.
665class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
666 public:
667 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000668 : instruction_(instruction), out_(out), root_(root) {
669 DCHECK(kEmitCompilerReadBarrier);
670 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000671
672 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
673 LocationSummary* locations = instruction_->GetLocations();
674 Register reg_out = out_.AsRegister<Register>();
675 DCHECK(locations->CanCall());
676 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000677 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
678 << "Unexpected instruction in read barrier for GC root slow path: "
679 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000680
681 __ Bind(GetEntryLabel());
682 SaveLiveRegisters(codegen, locations);
683
684 InvokeRuntimeCallingConvention calling_convention;
685 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
686 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
687 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
688 instruction_,
689 instruction_->GetDexPc(),
690 this);
691 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
692 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
693
694 RestoreLiveRegisters(codegen, locations);
695 __ jmp(GetExitLabel());
696 }
697
698 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
699
700 private:
701 HInstruction* const instruction_;
702 const Location out_;
703 const Location root_;
704
705 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
706};
707
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100709#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100710
Aart Bike9f37602015-10-09 11:15:55 -0700711inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700712 switch (cond) {
713 case kCondEQ: return kEqual;
714 case kCondNE: return kNotEqual;
715 case kCondLT: return kLess;
716 case kCondLE: return kLessEqual;
717 case kCondGT: return kGreater;
718 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700719 case kCondB: return kBelow;
720 case kCondBE: return kBelowEqual;
721 case kCondA: return kAbove;
722 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700723 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100724 LOG(FATAL) << "Unreachable";
725 UNREACHABLE();
726}
727
Aart Bike9f37602015-10-09 11:15:55 -0700728// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100729inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
730 switch (cond) {
731 case kCondEQ: return kEqual;
732 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700733 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100734 case kCondLT: return kBelow;
735 case kCondLE: return kBelowEqual;
736 case kCondGT: return kAbove;
737 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700738 // Unsigned remain unchanged.
739 case kCondB: return kBelow;
740 case kCondBE: return kBelowEqual;
741 case kCondA: return kAbove;
742 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100743 }
744 LOG(FATAL) << "Unreachable";
745 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700746}
747
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100748void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100749 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100750}
751
752void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100753 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100754}
755
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100756size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
757 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
758 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100759}
760
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100761size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
762 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
763 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100764}
765
Mark Mendell7c8d0092015-01-26 11:21:33 -0500766size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
767 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
768 return GetFloatingPointSpillSlotSize();
769}
770
771size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
772 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
773 return GetFloatingPointSpillSlotSize();
774}
775
Calin Juravle175dc732015-08-25 15:42:32 +0100776void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
777 HInstruction* instruction,
778 uint32_t dex_pc,
779 SlowPathCode* slow_path) {
780 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
781 instruction,
782 dex_pc,
783 slow_path);
784}
785
786void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100787 HInstruction* instruction,
788 uint32_t dex_pc,
789 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100790 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100791 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100792 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100793}
794
Mark Mendellfb8d2792015-03-31 22:16:59 -0400795CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000796 const X86InstructionSetFeatures& isa_features,
797 const CompilerOptions& compiler_options,
798 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500799 : CodeGenerator(graph,
800 kNumberOfCpuRegisters,
801 kNumberOfXmmRegisters,
802 kNumberOfRegisterPairs,
803 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
804 arraysize(kCoreCalleeSaves))
805 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100806 0,
807 compiler_options,
808 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100809 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100810 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100811 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400812 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000813 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100814 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400815 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000816 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400817 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000818 // Use a fake return address register to mimic Quick.
819 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100820}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100822Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100823 switch (type) {
824 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100825 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100826 X86ManagedRegister pair =
827 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100828 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
829 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100830 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
831 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100832 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100833 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100834 }
835
836 case Primitive::kPrimByte:
837 case Primitive::kPrimBoolean:
838 case Primitive::kPrimChar:
839 case Primitive::kPrimShort:
840 case Primitive::kPrimInt:
841 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100842 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100843 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100844 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100845 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
846 X86ManagedRegister current =
847 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
848 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100849 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100850 }
851 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100852 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100853 }
854
855 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100856 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100857 return Location::FpuRegisterLocation(
858 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100859 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100860
861 case Primitive::kPrimVoid:
862 LOG(FATAL) << "Unreachable type " << type;
863 }
864
Roland Levillain0d5a2812015-11-13 10:07:31 +0000865 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100866}
867
Mark Mendell5f874182015-03-04 15:42:45 -0500868void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100869 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100870 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100871
872 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100873 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100874
Mark Mendell5f874182015-03-04 15:42:45 -0500875 if (is_baseline) {
876 blocked_core_registers_[EBP] = true;
877 blocked_core_registers_[ESI] = true;
878 blocked_core_registers_[EDI] = true;
879 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100880
881 UpdateBlockedPairRegisters();
882}
883
884void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
885 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
886 X86ManagedRegister current =
887 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
888 if (blocked_core_registers_[current.AsRegisterPairLow()]
889 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
890 blocked_register_pairs_[i] = true;
891 }
892 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100893}
894
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100895InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
896 : HGraphVisitor(graph),
897 assembler_(codegen->GetAssembler()),
898 codegen_(codegen) {}
899
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100900static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100901 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100902}
903
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000904void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100905 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000906 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000907 bool skip_overflow_check =
908 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000909 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000910
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000911 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100912 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100913 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100914 }
915
Mark Mendell5f874182015-03-04 15:42:45 -0500916 if (HasEmptyFrame()) {
917 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000918 }
Mark Mendell5f874182015-03-04 15:42:45 -0500919
920 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
921 Register reg = kCoreCalleeSaves[i];
922 if (allocated_registers_.ContainsCoreRegister(reg)) {
923 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100924 __ cfi().AdjustCFAOffset(kX86WordSize);
925 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500926 }
927 }
928
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100929 int adjust = GetFrameSize() - FrameEntrySpillSize();
930 __ subl(ESP, Immediate(adjust));
931 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100932 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000933}
934
935void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100936 __ cfi().RememberState();
937 if (!HasEmptyFrame()) {
938 int adjust = GetFrameSize() - FrameEntrySpillSize();
939 __ addl(ESP, Immediate(adjust));
940 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500941
David Srbeckyc34dc932015-04-12 09:27:43 +0100942 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
943 Register reg = kCoreCalleeSaves[i];
944 if (allocated_registers_.ContainsCoreRegister(reg)) {
945 __ popl(reg);
946 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
947 __ cfi().Restore(DWARFReg(reg));
948 }
Mark Mendell5f874182015-03-04 15:42:45 -0500949 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000950 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100951 __ ret();
952 __ cfi().RestoreState();
953 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000954}
955
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100956void CodeGeneratorX86::Bind(HBasicBlock* block) {
957 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000958}
959
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100960Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
961 switch (load->GetType()) {
962 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100963 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100965
966 case Primitive::kPrimInt:
967 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100968 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100969 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100970
971 case Primitive::kPrimBoolean:
972 case Primitive::kPrimByte:
973 case Primitive::kPrimChar:
974 case Primitive::kPrimShort:
975 case Primitive::kPrimVoid:
976 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700977 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100978 }
979
980 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700981 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100982}
983
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100984Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
985 switch (type) {
986 case Primitive::kPrimBoolean:
987 case Primitive::kPrimByte:
988 case Primitive::kPrimChar:
989 case Primitive::kPrimShort:
990 case Primitive::kPrimInt:
991 case Primitive::kPrimNot:
992 return Location::RegisterLocation(EAX);
993
994 case Primitive::kPrimLong:
995 return Location::RegisterPairLocation(EAX, EDX);
996
997 case Primitive::kPrimVoid:
998 return Location::NoLocation();
999
1000 case Primitive::kPrimDouble:
1001 case Primitive::kPrimFloat:
1002 return Location::FpuRegisterLocation(XMM0);
1003 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001004
1005 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001006}
1007
1008Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1009 return Location::RegisterLocation(kMethodRegisterArgument);
1010}
1011
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001012Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013 switch (type) {
1014 case Primitive::kPrimBoolean:
1015 case Primitive::kPrimByte:
1016 case Primitive::kPrimChar:
1017 case Primitive::kPrimShort:
1018 case Primitive::kPrimInt:
1019 case Primitive::kPrimNot: {
1020 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001021 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001022 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001023 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001024 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001025 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001026 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001027 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001028
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001029 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001030 uint32_t index = gp_index_;
1031 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001032 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001033 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001034 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1035 calling_convention.GetRegisterPairAt(index));
1036 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001037 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001038 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1039 }
1040 }
1041
1042 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001043 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001044 stack_index_++;
1045 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1046 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1047 } else {
1048 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1049 }
1050 }
1051
1052 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001053 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001054 stack_index_ += 2;
1055 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1056 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1057 } else {
1058 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001059 }
1060 }
1061
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001062 case Primitive::kPrimVoid:
1063 LOG(FATAL) << "Unexpected parameter type " << type;
1064 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001065 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001066 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001067}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001068
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001069void CodeGeneratorX86::Move32(Location destination, Location source) {
1070 if (source.Equals(destination)) {
1071 return;
1072 }
1073 if (destination.IsRegister()) {
1074 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001075 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001076 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001077 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 } else {
1079 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001080 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001081 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001082 } else if (destination.IsFpuRegister()) {
1083 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001084 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001085 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001086 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001087 } else {
1088 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001089 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001090 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001092 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001093 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001094 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001095 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001096 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001097 } else if (source.IsConstant()) {
1098 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001099 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001100 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001101 } else {
1102 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001103 __ pushl(Address(ESP, source.GetStackIndex()));
1104 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001105 }
1106 }
1107}
1108
1109void CodeGeneratorX86::Move64(Location destination, Location source) {
1110 if (source.Equals(destination)) {
1111 return;
1112 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001113 if (destination.IsRegisterPair()) {
1114 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001115 EmitParallelMoves(
1116 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1117 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001118 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001119 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001120 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1121 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001122 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001123 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1124 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1125 __ psrlq(src_reg, Immediate(32));
1126 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001127 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001128 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001129 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001130 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1131 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001132 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1133 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001134 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001135 if (source.IsFpuRegister()) {
1136 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1137 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001139 } else if (source.IsRegisterPair()) {
1140 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1141 // Create stack space for 2 elements.
1142 __ subl(ESP, Immediate(2 * elem_size));
1143 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1144 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1145 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1146 // And remove the temporary stack space we allocated.
1147 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001148 } else {
1149 LOG(FATAL) << "Unimplemented";
1150 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001151 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001152 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001154 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001155 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001156 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001157 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001158 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001159 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001160 } else if (source.IsConstant()) {
1161 HConstant* constant = source.GetConstant();
1162 int64_t value;
1163 if (constant->IsLongConstant()) {
1164 value = constant->AsLongConstant()->GetValue();
1165 } else {
1166 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001167 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001168 }
1169 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1170 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001171 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001172 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001173 EmitParallelMoves(
1174 Location::StackSlot(source.GetStackIndex()),
1175 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001176 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001177 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001178 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1179 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001180 }
1181 }
1182}
1183
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001184void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001185 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001186 if (instruction->IsCurrentMethod()) {
1187 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1188 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001189 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001190 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001191 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001192 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1193 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001194 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001195 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001196 } else if (location.IsStackSlot()) {
1197 __ movl(Address(ESP, location.GetStackIndex()), imm);
1198 } else {
1199 DCHECK(location.IsConstant());
1200 DCHECK_EQ(location.GetConstant(), const_to_move);
1201 }
1202 } else if (const_to_move->IsLongConstant()) {
1203 int64_t value = const_to_move->AsLongConstant()->GetValue();
1204 if (location.IsRegisterPair()) {
1205 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1206 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1207 } else if (location.IsDoubleStackSlot()) {
1208 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001209 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1210 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001211 } else {
1212 DCHECK(location.IsConstant());
1213 DCHECK_EQ(location.GetConstant(), instruction);
1214 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001215 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001216 } else if (instruction->IsTemporary()) {
1217 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001218 if (temp_location.IsStackSlot()) {
1219 Move32(location, temp_location);
1220 } else {
1221 DCHECK(temp_location.IsDoubleStackSlot());
1222 Move64(location, temp_location);
1223 }
Roland Levillain476df552014-10-09 17:51:36 +01001224 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001225 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001226 switch (instruction->GetType()) {
1227 case Primitive::kPrimBoolean:
1228 case Primitive::kPrimByte:
1229 case Primitive::kPrimChar:
1230 case Primitive::kPrimShort:
1231 case Primitive::kPrimInt:
1232 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001233 case Primitive::kPrimFloat:
1234 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001235 break;
1236
1237 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001238 case Primitive::kPrimDouble:
1239 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001240 break;
1241
1242 default:
1243 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1244 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001245 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001246 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 switch (instruction->GetType()) {
1248 case Primitive::kPrimBoolean:
1249 case Primitive::kPrimByte:
1250 case Primitive::kPrimChar:
1251 case Primitive::kPrimShort:
1252 case Primitive::kPrimInt:
1253 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001254 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001255 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001256 break;
1257
1258 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001259 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001260 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001261 break;
1262
1263 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001264 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001265 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001266 }
1267}
1268
Calin Juravle175dc732015-08-25 15:42:32 +01001269void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1270 DCHECK(location.IsRegister());
1271 __ movl(location.AsRegister<Register>(), Immediate(value));
1272}
1273
Calin Juravlee460d1d2015-09-29 04:52:17 +01001274void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1275 if (Primitive::Is64BitType(dst_type)) {
1276 Move64(dst, src);
1277 } else {
1278 Move32(dst, src);
1279 }
1280}
1281
1282void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1283 if (location.IsRegister()) {
1284 locations->AddTemp(location);
1285 } else if (location.IsRegisterPair()) {
1286 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1287 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1288 } else {
1289 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1290 }
1291}
1292
David Brazdilfc6a86a2015-06-26 10:33:45 +00001293void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001294 DCHECK(!successor->IsExitBlock());
1295
1296 HBasicBlock* block = got->GetBlock();
1297 HInstruction* previous = got->GetPrevious();
1298
1299 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001300 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001301 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1302 return;
1303 }
1304
1305 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1306 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1307 }
1308 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001309 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001310 }
1311}
1312
David Brazdilfc6a86a2015-06-26 10:33:45 +00001313void LocationsBuilderX86::VisitGoto(HGoto* got) {
1314 got->SetLocations(nullptr);
1315}
1316
1317void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1318 HandleGoto(got, got->GetSuccessor());
1319}
1320
1321void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1322 try_boundary->SetLocations(nullptr);
1323}
1324
1325void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1326 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1327 if (!successor->IsExitBlock()) {
1328 HandleGoto(try_boundary, successor);
1329 }
1330}
1331
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001332void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001333 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001334}
1335
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001336void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001337}
1338
Mark Mendellc4701932015-04-10 13:18:51 -04001339void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1340 Label* true_label,
1341 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001342 if (cond->IsFPConditionTrueIfNaN()) {
1343 __ j(kUnordered, true_label);
1344 } else if (cond->IsFPConditionFalseIfNaN()) {
1345 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001346 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001347 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001348}
1349
1350void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1351 Label* true_label,
1352 Label* false_label) {
1353 LocationSummary* locations = cond->GetLocations();
1354 Location left = locations->InAt(0);
1355 Location right = locations->InAt(1);
1356 IfCondition if_cond = cond->GetCondition();
1357
Mark Mendellc4701932015-04-10 13:18:51 -04001358 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001359 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001360 IfCondition true_high_cond = if_cond;
1361 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001362 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001363
1364 // Set the conditions for the test, remembering that == needs to be
1365 // decided using the low words.
1366 switch (if_cond) {
1367 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001368 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001369 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001370 break;
1371 case kCondLT:
1372 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001373 break;
1374 case kCondLE:
1375 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001376 break;
1377 case kCondGT:
1378 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001379 break;
1380 case kCondGE:
1381 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001382 break;
Aart Bike9f37602015-10-09 11:15:55 -07001383 case kCondB:
1384 false_high_cond = kCondA;
1385 break;
1386 case kCondBE:
1387 true_high_cond = kCondB;
1388 break;
1389 case kCondA:
1390 false_high_cond = kCondB;
1391 break;
1392 case kCondAE:
1393 true_high_cond = kCondA;
1394 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001395 }
1396
1397 if (right.IsConstant()) {
1398 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001399 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001400 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001401
1402 if (val_high == 0) {
1403 __ testl(left_high, left_high);
1404 } else {
1405 __ cmpl(left_high, Immediate(val_high));
1406 }
1407 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001408 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001409 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001410 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001411 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001412 __ j(X86Condition(true_high_cond), true_label);
1413 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001414 }
1415 // Must be equal high, so compare the lows.
1416 if (val_low == 0) {
1417 __ testl(left_low, left_low);
1418 } else {
1419 __ cmpl(left_low, Immediate(val_low));
1420 }
1421 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001422 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001423 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001424
1425 __ cmpl(left_high, right_high);
1426 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001427 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001428 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001429 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001430 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001431 __ j(X86Condition(true_high_cond), true_label);
1432 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001433 }
1434 // Must be equal high, so compare the lows.
1435 __ cmpl(left_low, right_low);
1436 }
1437 // The last comparison might be unsigned.
1438 __ j(final_condition, true_label);
1439}
1440
David Brazdil0debae72015-11-12 18:37:00 +00001441void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
1442 Label* true_target_in,
1443 Label* false_target_in) {
1444 // Generated branching requires both targets to be explicit. If either of the
1445 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1446 Label fallthrough_target;
1447 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1448 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1449
Mark Mendellc4701932015-04-10 13:18:51 -04001450 LocationSummary* locations = condition->GetLocations();
1451 Location left = locations->InAt(0);
1452 Location right = locations->InAt(1);
1453
Mark Mendellc4701932015-04-10 13:18:51 -04001454 Primitive::Type type = condition->InputAt(0)->GetType();
1455 switch (type) {
1456 case Primitive::kPrimLong:
1457 GenerateLongComparesAndJumps(condition, true_target, false_target);
1458 break;
1459 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001460 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1461 GenerateFPJumps(condition, true_target, false_target);
1462 break;
1463 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001464 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1465 GenerateFPJumps(condition, true_target, false_target);
1466 break;
1467 default:
1468 LOG(FATAL) << "Unexpected compare type " << type;
1469 }
1470
David Brazdil0debae72015-11-12 18:37:00 +00001471 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001472 __ jmp(false_target);
1473 }
David Brazdil0debae72015-11-12 18:37:00 +00001474
1475 if (fallthrough_target.IsLinked()) {
1476 __ Bind(&fallthrough_target);
1477 }
Mark Mendellc4701932015-04-10 13:18:51 -04001478}
1479
David Brazdil0debae72015-11-12 18:37:00 +00001480static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1481 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1482 // are set only strictly before `branch`. We can't use the eflags on long/FP
1483 // conditions if they are materialized due to the complex branching.
1484 return cond->IsCondition() &&
1485 cond->GetNext() == branch &&
1486 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1487 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1488}
1489
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001490void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001491 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001492 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001493 Label* false_target) {
1494 HInstruction* cond = instruction->InputAt(condition_input_index);
1495
1496 if (true_target == nullptr && false_target == nullptr) {
1497 // Nothing to do. The code always falls through.
1498 return;
1499 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001500 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001501 if (cond->AsIntConstant()->IsOne()) {
1502 if (true_target != nullptr) {
1503 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001504 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001505 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001506 DCHECK(cond->AsIntConstant()->IsZero());
1507 if (false_target != nullptr) {
1508 __ jmp(false_target);
1509 }
1510 }
1511 return;
1512 }
1513
1514 // The following code generates these patterns:
1515 // (1) true_target == nullptr && false_target != nullptr
1516 // - opposite condition true => branch to false_target
1517 // (2) true_target != nullptr && false_target == nullptr
1518 // - condition true => branch to true_target
1519 // (3) true_target != nullptr && false_target != nullptr
1520 // - condition true => branch to true_target
1521 // - branch to false_target
1522 if (IsBooleanValueOrMaterializedCondition(cond)) {
1523 if (AreEflagsSetFrom(cond, instruction)) {
1524 if (true_target == nullptr) {
1525 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1526 } else {
1527 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1528 }
1529 } else {
1530 // Materialized condition, compare against 0.
1531 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1532 if (lhs.IsRegister()) {
1533 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1534 } else {
1535 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1536 }
1537 if (true_target == nullptr) {
1538 __ j(kEqual, false_target);
1539 } else {
1540 __ j(kNotEqual, true_target);
1541 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001542 }
1543 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001544 // Condition has not been materialized, use its inputs as the comparison and
1545 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001546 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001547
1548 // If this is a long or FP comparison that has been folded into
1549 // the HCondition, generate the comparison directly.
1550 Primitive::Type type = condition->InputAt(0)->GetType();
1551 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1552 GenerateCompareTestAndBranch(condition, true_target, false_target);
1553 return;
1554 }
1555
1556 Location lhs = condition->GetLocations()->InAt(0);
1557 Location rhs = condition->GetLocations()->InAt(1);
1558 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
1559 if (rhs.IsRegister()) {
1560 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1561 } else if (rhs.IsConstant()) {
1562 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1563 if (constant == 0) {
1564 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001565 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001566 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001567 }
1568 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001569 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1570 }
1571 if (true_target == nullptr) {
1572 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1573 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001574 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001575 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001576 }
David Brazdil0debae72015-11-12 18:37:00 +00001577
1578 // If neither branch falls through (case 3), the conditional branch to `true_target`
1579 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1580 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001581 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001582 }
1583}
1584
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001585void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001586 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1587 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001588 locations->SetInAt(0, Location::Any());
1589 }
1590}
1591
1592void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001593 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1594 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1595 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1596 nullptr : codegen_->GetLabelOf(true_successor);
1597 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1598 nullptr : codegen_->GetLabelOf(false_successor);
1599 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001600}
1601
1602void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1603 LocationSummary* locations = new (GetGraph()->GetArena())
1604 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001605 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001606 locations->SetInAt(0, Location::Any());
1607 }
1608}
1609
1610void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001611 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001612 DeoptimizationSlowPathX86(deoptimize);
1613 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001614 GenerateTestAndBranch(deoptimize,
1615 /* condition_input_index */ 0,
1616 slow_path->GetEntryLabel(),
1617 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001618}
1619
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001620void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001621 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001622}
1623
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001624void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1625 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001626}
1627
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001628void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001629 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001630}
1631
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001632void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001633 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001634}
1635
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001636void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001637 LocationSummary* locations =
1638 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001639 switch (store->InputAt(1)->GetType()) {
1640 case Primitive::kPrimBoolean:
1641 case Primitive::kPrimByte:
1642 case Primitive::kPrimChar:
1643 case Primitive::kPrimShort:
1644 case Primitive::kPrimInt:
1645 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001646 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001647 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1648 break;
1649
1650 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001651 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001652 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1653 break;
1654
1655 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001656 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001657 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001658}
1659
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001660void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001661}
1662
Roland Levillain0d37cd02015-05-27 16:39:19 +01001663void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001664 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001665 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001666 // Handle the long/FP comparisons made in instruction simplification.
1667 switch (cond->InputAt(0)->GetType()) {
1668 case Primitive::kPrimLong: {
1669 locations->SetInAt(0, Location::RequiresRegister());
1670 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1671 if (cond->NeedsMaterialization()) {
1672 locations->SetOut(Location::RequiresRegister());
1673 }
1674 break;
1675 }
1676 case Primitive::kPrimFloat:
1677 case Primitive::kPrimDouble: {
1678 locations->SetInAt(0, Location::RequiresFpuRegister());
1679 locations->SetInAt(1, Location::RequiresFpuRegister());
1680 if (cond->NeedsMaterialization()) {
1681 locations->SetOut(Location::RequiresRegister());
1682 }
1683 break;
1684 }
1685 default:
1686 locations->SetInAt(0, Location::RequiresRegister());
1687 locations->SetInAt(1, Location::Any());
1688 if (cond->NeedsMaterialization()) {
1689 // We need a byte register.
1690 locations->SetOut(Location::RegisterLocation(ECX));
1691 }
1692 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001693 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001694}
1695
Roland Levillain0d37cd02015-05-27 16:39:19 +01001696void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001697 if (!cond->NeedsMaterialization()) {
1698 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001699 }
Mark Mendellc4701932015-04-10 13:18:51 -04001700
1701 LocationSummary* locations = cond->GetLocations();
1702 Location lhs = locations->InAt(0);
1703 Location rhs = locations->InAt(1);
1704 Register reg = locations->Out().AsRegister<Register>();
1705 Label true_label, false_label;
1706
1707 switch (cond->InputAt(0)->GetType()) {
1708 default: {
1709 // Integer case.
1710
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001711 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001712 __ xorl(reg, reg);
1713
1714 if (rhs.IsRegister()) {
1715 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1716 } else if (rhs.IsConstant()) {
1717 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1718 if (constant == 0) {
1719 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1720 } else {
1721 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1722 }
1723 } else {
1724 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1725 }
Aart Bike9f37602015-10-09 11:15:55 -07001726 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001727 return;
1728 }
1729 case Primitive::kPrimLong:
1730 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1731 break;
1732 case Primitive::kPrimFloat:
1733 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1734 GenerateFPJumps(cond, &true_label, &false_label);
1735 break;
1736 case Primitive::kPrimDouble:
1737 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1738 GenerateFPJumps(cond, &true_label, &false_label);
1739 break;
1740 }
1741
1742 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001743 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001744
Roland Levillain4fa13f62015-07-06 18:11:54 +01001745 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001746 __ Bind(&false_label);
1747 __ xorl(reg, reg);
1748 __ jmp(&done_label);
1749
Roland Levillain4fa13f62015-07-06 18:11:54 +01001750 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001751 __ Bind(&true_label);
1752 __ movl(reg, Immediate(1));
1753 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1757 VisitCondition(comp);
1758}
1759
1760void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1761 VisitCondition(comp);
1762}
1763
1764void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1765 VisitCondition(comp);
1766}
1767
1768void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1769 VisitCondition(comp);
1770}
1771
1772void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1773 VisitCondition(comp);
1774}
1775
1776void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1777 VisitCondition(comp);
1778}
1779
1780void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1781 VisitCondition(comp);
1782}
1783
1784void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1785 VisitCondition(comp);
1786}
1787
1788void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1789 VisitCondition(comp);
1790}
1791
1792void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1793 VisitCondition(comp);
1794}
1795
1796void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1797 VisitCondition(comp);
1798}
1799
1800void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1801 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001802}
1803
Aart Bike9f37602015-10-09 11:15:55 -07001804void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1805 VisitCondition(comp);
1806}
1807
1808void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1809 VisitCondition(comp);
1810}
1811
1812void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1813 VisitCondition(comp);
1814}
1815
1816void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1817 VisitCondition(comp);
1818}
1819
1820void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1821 VisitCondition(comp);
1822}
1823
1824void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1825 VisitCondition(comp);
1826}
1827
1828void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1829 VisitCondition(comp);
1830}
1831
1832void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1833 VisitCondition(comp);
1834}
1835
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001836void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001837 LocationSummary* locations =
1838 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001839 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001840}
1841
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001842void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001843 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001844}
1845
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001846void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1847 LocationSummary* locations =
1848 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1849 locations->SetOut(Location::ConstantLocation(constant));
1850}
1851
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001852void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001853 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001854}
1855
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001856void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001857 LocationSummary* locations =
1858 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001859 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860}
1861
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001862void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863 // Will be generated at use site.
1864}
1865
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001866void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1867 LocationSummary* locations =
1868 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1869 locations->SetOut(Location::ConstantLocation(constant));
1870}
1871
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001872void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001873 // Will be generated at use site.
1874}
1875
1876void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1877 LocationSummary* locations =
1878 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1879 locations->SetOut(Location::ConstantLocation(constant));
1880}
1881
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001882void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001883 // Will be generated at use site.
1884}
1885
Calin Juravle27df7582015-04-17 19:12:31 +01001886void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1887 memory_barrier->SetLocations(nullptr);
1888}
1889
1890void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001891 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001892}
1893
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001894void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001895 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001896}
1897
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001898void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001899 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001900}
1901
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001902void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001903 LocationSummary* locations =
1904 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001905 switch (ret->InputAt(0)->GetType()) {
1906 case Primitive::kPrimBoolean:
1907 case Primitive::kPrimByte:
1908 case Primitive::kPrimChar:
1909 case Primitive::kPrimShort:
1910 case Primitive::kPrimInt:
1911 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001912 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 break;
1914
1915 case Primitive::kPrimLong:
1916 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001917 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 break;
1919
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001920 case Primitive::kPrimFloat:
1921 case Primitive::kPrimDouble:
1922 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001923 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001924 break;
1925
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001929}
1930
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001931void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001932 if (kIsDebugBuild) {
1933 switch (ret->InputAt(0)->GetType()) {
1934 case Primitive::kPrimBoolean:
1935 case Primitive::kPrimByte:
1936 case Primitive::kPrimChar:
1937 case Primitive::kPrimShort:
1938 case Primitive::kPrimInt:
1939 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001940 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 break;
1942
1943 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001944 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1945 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001946 break;
1947
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001948 case Primitive::kPrimFloat:
1949 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001950 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001951 break;
1952
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001953 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001954 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001955 }
1956 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001957 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001958}
1959
Calin Juravle175dc732015-08-25 15:42:32 +01001960void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1961 // The trampoline uses the same calling convention as dex calling conventions,
1962 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1963 // the method_idx.
1964 HandleInvoke(invoke);
1965}
1966
1967void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1968 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1969}
1970
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001971void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001972 // When we do not run baseline, explicit clinit checks triggered by static
1973 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1974 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001975
Mark Mendellfb8d2792015-03-31 22:16:59 -04001976 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001977 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001978 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001979 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001980 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001981 return;
1982 }
1983
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001984 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001985
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001986 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1987 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001988 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001989 }
1990
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001991 if (codegen_->IsBaseline()) {
1992 // Baseline does not have enough registers if the current method also
1993 // needs a register. We therefore do not require a register for it, and let
1994 // the code generation of the invoke handle it.
1995 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00001996 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001997 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001998 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001999 }
2000 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002001}
2002
Mark Mendell09ed1a32015-03-25 08:30:06 -04002003static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2004 if (invoke->GetLocations()->Intrinsified()) {
2005 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2006 intrinsic.Dispatch(invoke);
2007 return true;
2008 }
2009 return false;
2010}
2011
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002012void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002013 // When we do not run baseline, explicit clinit checks triggered by static
2014 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2015 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002016
Mark Mendell09ed1a32015-03-25 08:30:06 -04002017 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2018 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002019 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002020
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002021 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002022 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002023 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002024 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002025}
2026
2027void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002028 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2029 if (intrinsic.TryDispatch(invoke)) {
2030 return;
2031 }
2032
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002033 HandleInvoke(invoke);
2034}
2035
2036void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002037 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002038 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002039}
2040
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002041void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002042 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2043 return;
2044 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002045
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002046 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002047 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002048 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002049}
2050
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002052 // This call to HandleInvoke allocates a temporary (core) register
2053 // which is also used to transfer the hidden argument from FP to
2054 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055 HandleInvoke(invoke);
2056 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002057 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002058}
2059
2060void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2061 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002062 LocationSummary* locations = invoke->GetLocations();
2063 Register temp = locations->GetTemp(0).AsRegister<Register>();
2064 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002065 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2066 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002067 Location receiver = locations->InAt(0);
2068 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2069
Roland Levillain0d5a2812015-11-13 10:07:31 +00002070 // Set the hidden argument. This is safe to do this here, as XMM7
2071 // won't be modified thereafter, before the `call` instruction.
2072 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002074 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002075
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002076 if (receiver.IsStackSlot()) {
2077 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002078 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002079 __ movl(temp, Address(temp, class_offset));
2080 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002081 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083 }
Roland Levillain4d027112015-07-01 15:41:14 +01002084 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002085 // Instead of simply (possibly) unpoisoning `temp` here, we should
2086 // emit a read barrier for the previous class reference load.
2087 // However this is not required in practice, as this is an
2088 // intermediate/temporary reference and because the current
2089 // concurrent copying collector keeps the from-space memory
2090 // intact/accessible until the end of the marking phase (the
2091 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002092 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002093 // temp = temp->GetImtEntryAt(method_offset);
2094 __ movl(temp, Address(temp, method_offset));
2095 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002096 __ call(Address(temp,
2097 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098
2099 DCHECK(!codegen_->IsLeafMethod());
2100 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2101}
2102
Roland Levillain88cb1752014-10-20 16:36:47 +01002103void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2104 LocationSummary* locations =
2105 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2106 switch (neg->GetResultType()) {
2107 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002108 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002109 locations->SetInAt(0, Location::RequiresRegister());
2110 locations->SetOut(Location::SameAsFirstInput());
2111 break;
2112
Roland Levillain88cb1752014-10-20 16:36:47 +01002113 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002114 locations->SetInAt(0, Location::RequiresFpuRegister());
2115 locations->SetOut(Location::SameAsFirstInput());
2116 locations->AddTemp(Location::RequiresRegister());
2117 locations->AddTemp(Location::RequiresFpuRegister());
2118 break;
2119
Roland Levillain88cb1752014-10-20 16:36:47 +01002120 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002121 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002122 locations->SetOut(Location::SameAsFirstInput());
2123 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 break;
2125
2126 default:
2127 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2128 }
2129}
2130
2131void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2132 LocationSummary* locations = neg->GetLocations();
2133 Location out = locations->Out();
2134 Location in = locations->InAt(0);
2135 switch (neg->GetResultType()) {
2136 case Primitive::kPrimInt:
2137 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002138 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002139 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002140 break;
2141
2142 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002143 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002144 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002145 __ negl(out.AsRegisterPairLow<Register>());
2146 // Negation is similar to subtraction from zero. The least
2147 // significant byte triggers a borrow when it is different from
2148 // zero; to take it into account, add 1 to the most significant
2149 // byte if the carry flag (CF) is set to 1 after the first NEGL
2150 // operation.
2151 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2152 __ negl(out.AsRegisterPairHigh<Register>());
2153 break;
2154
Roland Levillain5368c212014-11-27 15:03:41 +00002155 case Primitive::kPrimFloat: {
2156 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002157 Register constant = locations->GetTemp(0).AsRegister<Register>();
2158 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002159 // Implement float negation with an exclusive or with value
2160 // 0x80000000 (mask for bit 31, representing the sign of a
2161 // single-precision floating-point number).
2162 __ movl(constant, Immediate(INT32_C(0x80000000)));
2163 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002164 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002165 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002166 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002167
Roland Levillain5368c212014-11-27 15:03:41 +00002168 case Primitive::kPrimDouble: {
2169 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002170 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002171 // Implement double negation with an exclusive or with value
2172 // 0x8000000000000000 (mask for bit 63, representing the sign of
2173 // a double-precision floating-point number).
2174 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002175 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002176 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002177 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002178
2179 default:
2180 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2181 }
2182}
2183
Roland Levillaindff1f282014-11-05 14:15:05 +00002184void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002185 Primitive::Type result_type = conversion->GetResultType();
2186 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002187 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002188
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002189 // The float-to-long and double-to-long type conversions rely on a
2190 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002191 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002192 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2193 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002194 ? LocationSummary::kCall
2195 : LocationSummary::kNoCall;
2196 LocationSummary* locations =
2197 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2198
David Brazdilb2bd1c52015-03-25 11:17:37 +00002199 // The Java language does not allow treating boolean as an integral type but
2200 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002201
Roland Levillaindff1f282014-11-05 14:15:05 +00002202 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002203 case Primitive::kPrimByte:
2204 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002205 case Primitive::kPrimBoolean:
2206 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002207 case Primitive::kPrimShort:
2208 case Primitive::kPrimInt:
2209 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002210 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002211 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2212 // Make the output overlap to please the register allocator. This greatly simplifies
2213 // the validation of the linear scan implementation
2214 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002215 break;
2216
2217 default:
2218 LOG(FATAL) << "Unexpected type conversion from " << input_type
2219 << " to " << result_type;
2220 }
2221 break;
2222
Roland Levillain01a8d712014-11-14 16:27:39 +00002223 case Primitive::kPrimShort:
2224 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002225 case Primitive::kPrimBoolean:
2226 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002227 case Primitive::kPrimByte:
2228 case Primitive::kPrimInt:
2229 case Primitive::kPrimChar:
2230 // Processing a Dex `int-to-short' instruction.
2231 locations->SetInAt(0, Location::Any());
2232 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2233 break;
2234
2235 default:
2236 LOG(FATAL) << "Unexpected type conversion from " << input_type
2237 << " to " << result_type;
2238 }
2239 break;
2240
Roland Levillain946e1432014-11-11 17:35:19 +00002241 case Primitive::kPrimInt:
2242 switch (input_type) {
2243 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002244 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002245 locations->SetInAt(0, Location::Any());
2246 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2247 break;
2248
2249 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002250 // Processing a Dex `float-to-int' instruction.
2251 locations->SetInAt(0, Location::RequiresFpuRegister());
2252 locations->SetOut(Location::RequiresRegister());
2253 locations->AddTemp(Location::RequiresFpuRegister());
2254 break;
2255
Roland Levillain946e1432014-11-11 17:35:19 +00002256 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002257 // Processing a Dex `double-to-int' instruction.
2258 locations->SetInAt(0, Location::RequiresFpuRegister());
2259 locations->SetOut(Location::RequiresRegister());
2260 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002261 break;
2262
2263 default:
2264 LOG(FATAL) << "Unexpected type conversion from " << input_type
2265 << " to " << result_type;
2266 }
2267 break;
2268
Roland Levillaindff1f282014-11-05 14:15:05 +00002269 case Primitive::kPrimLong:
2270 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002271 case Primitive::kPrimBoolean:
2272 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002273 case Primitive::kPrimByte:
2274 case Primitive::kPrimShort:
2275 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002276 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002277 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002278 locations->SetInAt(0, Location::RegisterLocation(EAX));
2279 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2280 break;
2281
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002282 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002283 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002284 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002285 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002286 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2287 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2288
Vladimir Marko949c91f2015-01-27 10:48:44 +00002289 // The runtime helper puts the result in EAX, EDX.
2290 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002291 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002292 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002293
2294 default:
2295 LOG(FATAL) << "Unexpected type conversion from " << input_type
2296 << " to " << result_type;
2297 }
2298 break;
2299
Roland Levillain981e4542014-11-14 11:47:14 +00002300 case Primitive::kPrimChar:
2301 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002302 case Primitive::kPrimBoolean:
2303 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002304 case Primitive::kPrimByte:
2305 case Primitive::kPrimShort:
2306 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002307 // Processing a Dex `int-to-char' instruction.
2308 locations->SetInAt(0, Location::Any());
2309 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2310 break;
2311
2312 default:
2313 LOG(FATAL) << "Unexpected type conversion from " << input_type
2314 << " to " << result_type;
2315 }
2316 break;
2317
Roland Levillaindff1f282014-11-05 14:15:05 +00002318 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002319 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002320 case Primitive::kPrimBoolean:
2321 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002322 case Primitive::kPrimByte:
2323 case Primitive::kPrimShort:
2324 case Primitive::kPrimInt:
2325 case Primitive::kPrimChar:
2326 // Processing a Dex `int-to-float' instruction.
2327 locations->SetInAt(0, Location::RequiresRegister());
2328 locations->SetOut(Location::RequiresFpuRegister());
2329 break;
2330
2331 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002332 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002333 locations->SetInAt(0, Location::Any());
2334 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002335 break;
2336
Roland Levillaincff13742014-11-17 14:32:17 +00002337 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002338 // Processing a Dex `double-to-float' instruction.
2339 locations->SetInAt(0, Location::RequiresFpuRegister());
2340 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002341 break;
2342
2343 default:
2344 LOG(FATAL) << "Unexpected type conversion from " << input_type
2345 << " to " << result_type;
2346 };
2347 break;
2348
Roland Levillaindff1f282014-11-05 14:15:05 +00002349 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002350 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002351 case Primitive::kPrimBoolean:
2352 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002353 case Primitive::kPrimByte:
2354 case Primitive::kPrimShort:
2355 case Primitive::kPrimInt:
2356 case Primitive::kPrimChar:
2357 // Processing a Dex `int-to-double' instruction.
2358 locations->SetInAt(0, Location::RequiresRegister());
2359 locations->SetOut(Location::RequiresFpuRegister());
2360 break;
2361
2362 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002363 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002364 locations->SetInAt(0, Location::Any());
2365 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002366 break;
2367
Roland Levillaincff13742014-11-17 14:32:17 +00002368 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002369 // Processing a Dex `float-to-double' instruction.
2370 locations->SetInAt(0, Location::RequiresFpuRegister());
2371 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002372 break;
2373
2374 default:
2375 LOG(FATAL) << "Unexpected type conversion from " << input_type
2376 << " to " << result_type;
2377 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002378 break;
2379
2380 default:
2381 LOG(FATAL) << "Unexpected type conversion from " << input_type
2382 << " to " << result_type;
2383 }
2384}
2385
2386void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2387 LocationSummary* locations = conversion->GetLocations();
2388 Location out = locations->Out();
2389 Location in = locations->InAt(0);
2390 Primitive::Type result_type = conversion->GetResultType();
2391 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002392 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002393 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002394 case Primitive::kPrimByte:
2395 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002396 case Primitive::kPrimBoolean:
2397 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002398 case Primitive::kPrimShort:
2399 case Primitive::kPrimInt:
2400 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002401 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002402 if (in.IsRegister()) {
2403 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002404 } else {
2405 DCHECK(in.GetConstant()->IsIntConstant());
2406 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2407 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2408 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002409 break;
2410
2411 default:
2412 LOG(FATAL) << "Unexpected type conversion from " << input_type
2413 << " to " << result_type;
2414 }
2415 break;
2416
Roland Levillain01a8d712014-11-14 16:27:39 +00002417 case Primitive::kPrimShort:
2418 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002419 case Primitive::kPrimBoolean:
2420 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002421 case Primitive::kPrimByte:
2422 case Primitive::kPrimInt:
2423 case Primitive::kPrimChar:
2424 // Processing a Dex `int-to-short' instruction.
2425 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002426 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002427 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002428 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002429 } else {
2430 DCHECK(in.GetConstant()->IsIntConstant());
2431 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002432 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002433 }
2434 break;
2435
2436 default:
2437 LOG(FATAL) << "Unexpected type conversion from " << input_type
2438 << " to " << result_type;
2439 }
2440 break;
2441
Roland Levillain946e1432014-11-11 17:35:19 +00002442 case Primitive::kPrimInt:
2443 switch (input_type) {
2444 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002445 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002446 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002447 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002448 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002450 } else {
2451 DCHECK(in.IsConstant());
2452 DCHECK(in.GetConstant()->IsLongConstant());
2453 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002454 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002455 }
2456 break;
2457
Roland Levillain3f8f9362014-12-02 17:45:01 +00002458 case Primitive::kPrimFloat: {
2459 // Processing a Dex `float-to-int' instruction.
2460 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2461 Register output = out.AsRegister<Register>();
2462 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002463 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002464
2465 __ movl(output, Immediate(kPrimIntMax));
2466 // temp = int-to-float(output)
2467 __ cvtsi2ss(temp, output);
2468 // if input >= temp goto done
2469 __ comiss(input, temp);
2470 __ j(kAboveEqual, &done);
2471 // if input == NaN goto nan
2472 __ j(kUnordered, &nan);
2473 // output = float-to-int-truncate(input)
2474 __ cvttss2si(output, input);
2475 __ jmp(&done);
2476 __ Bind(&nan);
2477 // output = 0
2478 __ xorl(output, output);
2479 __ Bind(&done);
2480 break;
2481 }
2482
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002483 case Primitive::kPrimDouble: {
2484 // Processing a Dex `double-to-int' instruction.
2485 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2486 Register output = out.AsRegister<Register>();
2487 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002488 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002489
2490 __ movl(output, Immediate(kPrimIntMax));
2491 // temp = int-to-double(output)
2492 __ cvtsi2sd(temp, output);
2493 // if input >= temp goto done
2494 __ comisd(input, temp);
2495 __ j(kAboveEqual, &done);
2496 // if input == NaN goto nan
2497 __ j(kUnordered, &nan);
2498 // output = double-to-int-truncate(input)
2499 __ cvttsd2si(output, input);
2500 __ jmp(&done);
2501 __ Bind(&nan);
2502 // output = 0
2503 __ xorl(output, output);
2504 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002505 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002506 }
Roland Levillain946e1432014-11-11 17:35:19 +00002507
2508 default:
2509 LOG(FATAL) << "Unexpected type conversion from " << input_type
2510 << " to " << result_type;
2511 }
2512 break;
2513
Roland Levillaindff1f282014-11-05 14:15:05 +00002514 case Primitive::kPrimLong:
2515 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002516 case Primitive::kPrimBoolean:
2517 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002518 case Primitive::kPrimByte:
2519 case Primitive::kPrimShort:
2520 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002521 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002522 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002523 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2524 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002525 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002526 __ cdq();
2527 break;
2528
2529 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002530 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002531 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2532 conversion,
2533 conversion->GetDexPc(),
2534 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002535 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002536 break;
2537
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002539 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002540 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2541 conversion,
2542 conversion->GetDexPc(),
2543 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002544 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002545 break;
2546
2547 default:
2548 LOG(FATAL) << "Unexpected type conversion from " << input_type
2549 << " to " << result_type;
2550 }
2551 break;
2552
Roland Levillain981e4542014-11-14 11:47:14 +00002553 case Primitive::kPrimChar:
2554 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002555 case Primitive::kPrimBoolean:
2556 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002557 case Primitive::kPrimByte:
2558 case Primitive::kPrimShort:
2559 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002560 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2561 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002562 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002563 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002564 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002565 } else {
2566 DCHECK(in.GetConstant()->IsIntConstant());
2567 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002568 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002569 }
2570 break;
2571
2572 default:
2573 LOG(FATAL) << "Unexpected type conversion from " << input_type
2574 << " to " << result_type;
2575 }
2576 break;
2577
Roland Levillaindff1f282014-11-05 14:15:05 +00002578 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002579 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002580 case Primitive::kPrimBoolean:
2581 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002582 case Primitive::kPrimByte:
2583 case Primitive::kPrimShort:
2584 case Primitive::kPrimInt:
2585 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002586 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002587 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002588 break;
2589
Roland Levillain6d0e4832014-11-27 18:31:21 +00002590 case Primitive::kPrimLong: {
2591 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002592 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002593
Roland Levillain232ade02015-04-20 15:14:36 +01002594 // Create stack space for the call to
2595 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2596 // TODO: enhance register allocator to ask for stack temporaries.
2597 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2598 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2599 __ subl(ESP, Immediate(adjustment));
2600 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002601
Roland Levillain232ade02015-04-20 15:14:36 +01002602 // Load the value to the FP stack, using temporaries if needed.
2603 PushOntoFPStack(in, 0, adjustment, false, true);
2604
2605 if (out.IsStackSlot()) {
2606 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2607 } else {
2608 __ fstps(Address(ESP, 0));
2609 Location stack_temp = Location::StackSlot(0);
2610 codegen_->Move32(out, stack_temp);
2611 }
2612
2613 // Remove the temporary stack space we allocated.
2614 if (adjustment != 0) {
2615 __ addl(ESP, Immediate(adjustment));
2616 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002617 break;
2618 }
2619
Roland Levillaincff13742014-11-17 14:32:17 +00002620 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002621 // Processing a Dex `double-to-float' instruction.
2622 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002623 break;
2624
2625 default:
2626 LOG(FATAL) << "Unexpected type conversion from " << input_type
2627 << " to " << result_type;
2628 };
2629 break;
2630
Roland Levillaindff1f282014-11-05 14:15:05 +00002631 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002632 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002633 case Primitive::kPrimBoolean:
2634 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002635 case Primitive::kPrimByte:
2636 case Primitive::kPrimShort:
2637 case Primitive::kPrimInt:
2638 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002639 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002640 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002641 break;
2642
Roland Levillain647b9ed2014-11-27 12:06:00 +00002643 case Primitive::kPrimLong: {
2644 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002645 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002646
Roland Levillain232ade02015-04-20 15:14:36 +01002647 // Create stack space for the call to
2648 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2649 // TODO: enhance register allocator to ask for stack temporaries.
2650 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2651 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2652 __ subl(ESP, Immediate(adjustment));
2653 }
2654
2655 // Load the value to the FP stack, using temporaries if needed.
2656 PushOntoFPStack(in, 0, adjustment, false, true);
2657
2658 if (out.IsDoubleStackSlot()) {
2659 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2660 } else {
2661 __ fstpl(Address(ESP, 0));
2662 Location stack_temp = Location::DoubleStackSlot(0);
2663 codegen_->Move64(out, stack_temp);
2664 }
2665
2666 // Remove the temporary stack space we allocated.
2667 if (adjustment != 0) {
2668 __ addl(ESP, Immediate(adjustment));
2669 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002670 break;
2671 }
2672
Roland Levillaincff13742014-11-17 14:32:17 +00002673 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002674 // Processing a Dex `float-to-double' instruction.
2675 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002676 break;
2677
2678 default:
2679 LOG(FATAL) << "Unexpected type conversion from " << input_type
2680 << " to " << result_type;
2681 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002682 break;
2683
2684 default:
2685 LOG(FATAL) << "Unexpected type conversion from " << input_type
2686 << " to " << result_type;
2687 }
2688}
2689
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002690void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002691 LocationSummary* locations =
2692 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002693 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002694 case Primitive::kPrimInt: {
2695 locations->SetInAt(0, Location::RequiresRegister());
2696 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2697 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2698 break;
2699 }
2700
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002701 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002702 locations->SetInAt(0, Location::RequiresRegister());
2703 locations->SetInAt(1, Location::Any());
2704 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002705 break;
2706 }
2707
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002708 case Primitive::kPrimFloat:
2709 case Primitive::kPrimDouble: {
2710 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002711 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002712 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002713 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002714 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002715
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002716 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002717 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2718 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002719 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002720}
2721
2722void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2723 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002724 Location first = locations->InAt(0);
2725 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002726 Location out = locations->Out();
2727
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002728 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002729 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002730 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002731 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2732 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002733 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2734 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002735 } else {
2736 __ leal(out.AsRegister<Register>(), Address(
2737 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2738 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002739 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002740 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2741 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2742 __ addl(out.AsRegister<Register>(), Immediate(value));
2743 } else {
2744 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2745 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002746 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002747 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002748 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002749 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002750 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002751 }
2752
2753 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002754 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002755 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2756 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002757 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002758 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2759 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002760 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002761 } else {
2762 DCHECK(second.IsConstant()) << second;
2763 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2764 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2765 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002766 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002767 break;
2768 }
2769
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002770 case Primitive::kPrimFloat: {
2771 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002772 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002773 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2774 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2775 DCHECK(!const_area->NeedsMaterialization());
2776 __ addss(first.AsFpuRegister<XmmRegister>(),
2777 codegen_->LiteralFloatAddress(
2778 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2779 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2780 } else {
2781 DCHECK(second.IsStackSlot());
2782 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002783 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002784 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002785 }
2786
2787 case Primitive::kPrimDouble: {
2788 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002790 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2791 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2792 DCHECK(!const_area->NeedsMaterialization());
2793 __ addsd(first.AsFpuRegister<XmmRegister>(),
2794 codegen_->LiteralDoubleAddress(
2795 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2796 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2797 } else {
2798 DCHECK(second.IsDoubleStackSlot());
2799 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002800 }
2801 break;
2802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002803
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002804 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002805 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002806 }
2807}
2808
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002809void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002810 LocationSummary* locations =
2811 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002812 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002813 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002814 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002815 locations->SetInAt(0, Location::RequiresRegister());
2816 locations->SetInAt(1, Location::Any());
2817 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002818 break;
2819 }
Calin Juravle11351682014-10-23 15:38:15 +01002820 case Primitive::kPrimFloat:
2821 case Primitive::kPrimDouble: {
2822 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002823 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002824 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002825 break;
Calin Juravle11351682014-10-23 15:38:15 +01002826 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002827
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002828 default:
Calin Juravle11351682014-10-23 15:38:15 +01002829 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002830 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002831}
2832
2833void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2834 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002835 Location first = locations->InAt(0);
2836 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002837 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002838 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002839 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002840 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002841 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002842 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002843 __ subl(first.AsRegister<Register>(),
2844 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002845 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002846 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002847 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002848 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002849 }
2850
2851 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002852 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002853 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2854 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002855 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002856 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002857 __ sbbl(first.AsRegisterPairHigh<Register>(),
2858 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002859 } else {
2860 DCHECK(second.IsConstant()) << second;
2861 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2862 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2863 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002864 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002865 break;
2866 }
2867
Calin Juravle11351682014-10-23 15:38:15 +01002868 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002869 if (second.IsFpuRegister()) {
2870 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2871 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2872 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2873 DCHECK(!const_area->NeedsMaterialization());
2874 __ subss(first.AsFpuRegister<XmmRegister>(),
2875 codegen_->LiteralFloatAddress(
2876 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2877 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2878 } else {
2879 DCHECK(second.IsStackSlot());
2880 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2881 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002882 break;
Calin Juravle11351682014-10-23 15:38:15 +01002883 }
2884
2885 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002886 if (second.IsFpuRegister()) {
2887 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2888 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2889 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2890 DCHECK(!const_area->NeedsMaterialization());
2891 __ subsd(first.AsFpuRegister<XmmRegister>(),
2892 codegen_->LiteralDoubleAddress(
2893 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2894 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2895 } else {
2896 DCHECK(second.IsDoubleStackSlot());
2897 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2898 }
Calin Juravle11351682014-10-23 15:38:15 +01002899 break;
2900 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002901
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002902 default:
Calin Juravle11351682014-10-23 15:38:15 +01002903 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002904 }
2905}
2906
Calin Juravle34bacdf2014-10-07 20:23:36 +01002907void LocationsBuilderX86::VisitMul(HMul* mul) {
2908 LocationSummary* locations =
2909 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2910 switch (mul->GetResultType()) {
2911 case Primitive::kPrimInt:
2912 locations->SetInAt(0, Location::RequiresRegister());
2913 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002914 if (mul->InputAt(1)->IsIntConstant()) {
2915 // Can use 3 operand multiply.
2916 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2917 } else {
2918 locations->SetOut(Location::SameAsFirstInput());
2919 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002920 break;
2921 case Primitive::kPrimLong: {
2922 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002923 locations->SetInAt(1, Location::Any());
2924 locations->SetOut(Location::SameAsFirstInput());
2925 // Needed for imul on 32bits with 64bits output.
2926 locations->AddTemp(Location::RegisterLocation(EAX));
2927 locations->AddTemp(Location::RegisterLocation(EDX));
2928 break;
2929 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002930 case Primitive::kPrimFloat:
2931 case Primitive::kPrimDouble: {
2932 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002933 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002934 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002935 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002936 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002937
2938 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002939 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002940 }
2941}
2942
2943void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2944 LocationSummary* locations = mul->GetLocations();
2945 Location first = locations->InAt(0);
2946 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002947 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002948
2949 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002950 case Primitive::kPrimInt:
2951 // The constant may have ended up in a register, so test explicitly to avoid
2952 // problems where the output may not be the same as the first operand.
2953 if (mul->InputAt(1)->IsIntConstant()) {
2954 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2955 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2956 } else if (second.IsRegister()) {
2957 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002958 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002959 } else {
2960 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002961 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002962 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002963 }
2964 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002965
2966 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002967 Register in1_hi = first.AsRegisterPairHigh<Register>();
2968 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 Register eax = locations->GetTemp(0).AsRegister<Register>();
2970 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002971
2972 DCHECK_EQ(EAX, eax);
2973 DCHECK_EQ(EDX, edx);
2974
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002975 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002976 // output: in1
2977 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2978 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2979 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002980 if (second.IsConstant()) {
2981 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002982
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002983 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2984 int32_t low_value = Low32Bits(value);
2985 int32_t high_value = High32Bits(value);
2986 Immediate low(low_value);
2987 Immediate high(high_value);
2988
2989 __ movl(eax, high);
2990 // eax <- in1.lo * in2.hi
2991 __ imull(eax, in1_lo);
2992 // in1.hi <- in1.hi * in2.lo
2993 __ imull(in1_hi, low);
2994 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2995 __ addl(in1_hi, eax);
2996 // move in2_lo to eax to prepare for double precision
2997 __ movl(eax, low);
2998 // edx:eax <- in1.lo * in2.lo
2999 __ mull(in1_lo);
3000 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3001 __ addl(in1_hi, edx);
3002 // in1.lo <- (in1.lo * in2.lo)[31:0];
3003 __ movl(in1_lo, eax);
3004 } else if (second.IsRegisterPair()) {
3005 Register in2_hi = second.AsRegisterPairHigh<Register>();
3006 Register in2_lo = second.AsRegisterPairLow<Register>();
3007
3008 __ movl(eax, in2_hi);
3009 // eax <- in1.lo * in2.hi
3010 __ imull(eax, in1_lo);
3011 // in1.hi <- in1.hi * in2.lo
3012 __ imull(in1_hi, in2_lo);
3013 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3014 __ addl(in1_hi, eax);
3015 // move in1_lo to eax to prepare for double precision
3016 __ movl(eax, in1_lo);
3017 // edx:eax <- in1.lo * in2.lo
3018 __ mull(in2_lo);
3019 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3020 __ addl(in1_hi, edx);
3021 // in1.lo <- (in1.lo * in2.lo)[31:0];
3022 __ movl(in1_lo, eax);
3023 } else {
3024 DCHECK(second.IsDoubleStackSlot()) << second;
3025 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3026 Address in2_lo(ESP, second.GetStackIndex());
3027
3028 __ movl(eax, in2_hi);
3029 // eax <- in1.lo * in2.hi
3030 __ imull(eax, in1_lo);
3031 // in1.hi <- in1.hi * in2.lo
3032 __ imull(in1_hi, in2_lo);
3033 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3034 __ addl(in1_hi, eax);
3035 // move in1_lo to eax to prepare for double precision
3036 __ movl(eax, in1_lo);
3037 // edx:eax <- in1.lo * in2.lo
3038 __ mull(in2_lo);
3039 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3040 __ addl(in1_hi, edx);
3041 // in1.lo <- (in1.lo * in2.lo)[31:0];
3042 __ movl(in1_lo, eax);
3043 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044
3045 break;
3046 }
3047
Calin Juravleb5bfa962014-10-21 18:02:24 +01003048 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003049 DCHECK(first.Equals(locations->Out()));
3050 if (second.IsFpuRegister()) {
3051 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3052 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3053 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3054 DCHECK(!const_area->NeedsMaterialization());
3055 __ mulss(first.AsFpuRegister<XmmRegister>(),
3056 codegen_->LiteralFloatAddress(
3057 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3058 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3059 } else {
3060 DCHECK(second.IsStackSlot());
3061 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3062 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003063 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003064 }
3065
3066 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003067 DCHECK(first.Equals(locations->Out()));
3068 if (second.IsFpuRegister()) {
3069 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3070 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3071 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3072 DCHECK(!const_area->NeedsMaterialization());
3073 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3074 codegen_->LiteralDoubleAddress(
3075 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3076 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3077 } else {
3078 DCHECK(second.IsDoubleStackSlot());
3079 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3080 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003081 break;
3082 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003083
3084 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003085 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003086 }
3087}
3088
Roland Levillain232ade02015-04-20 15:14:36 +01003089void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3090 uint32_t temp_offset,
3091 uint32_t stack_adjustment,
3092 bool is_fp,
3093 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003094 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003095 DCHECK(!is_wide);
3096 if (is_fp) {
3097 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3098 } else {
3099 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3100 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003101 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003102 DCHECK(is_wide);
3103 if (is_fp) {
3104 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3105 } else {
3106 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3107 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003108 } else {
3109 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003110 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003111 Location stack_temp = Location::StackSlot(temp_offset);
3112 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003113 if (is_fp) {
3114 __ flds(Address(ESP, temp_offset));
3115 } else {
3116 __ filds(Address(ESP, temp_offset));
3117 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003118 } else {
3119 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3120 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003121 if (is_fp) {
3122 __ fldl(Address(ESP, temp_offset));
3123 } else {
3124 __ fildl(Address(ESP, temp_offset));
3125 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003126 }
3127 }
3128}
3129
3130void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3131 Primitive::Type type = rem->GetResultType();
3132 bool is_float = type == Primitive::kPrimFloat;
3133 size_t elem_size = Primitive::ComponentSize(type);
3134 LocationSummary* locations = rem->GetLocations();
3135 Location first = locations->InAt(0);
3136 Location second = locations->InAt(1);
3137 Location out = locations->Out();
3138
3139 // Create stack space for 2 elements.
3140 // TODO: enhance register allocator to ask for stack temporaries.
3141 __ subl(ESP, Immediate(2 * elem_size));
3142
3143 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003144 const bool is_wide = !is_float;
3145 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3146 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003147
3148 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003149 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003150 __ Bind(&retry);
3151 __ fprem();
3152
3153 // Move FP status to AX.
3154 __ fstsw();
3155
3156 // And see if the argument reduction is complete. This is signaled by the
3157 // C2 FPU flag bit set to 0.
3158 __ andl(EAX, Immediate(kC2ConditionMask));
3159 __ j(kNotEqual, &retry);
3160
3161 // We have settled on the final value. Retrieve it into an XMM register.
3162 // Store FP top of stack to real stack.
3163 if (is_float) {
3164 __ fsts(Address(ESP, 0));
3165 } else {
3166 __ fstl(Address(ESP, 0));
3167 }
3168
3169 // Pop the 2 items from the FP stack.
3170 __ fucompp();
3171
3172 // Load the value from the stack into an XMM register.
3173 DCHECK(out.IsFpuRegister()) << out;
3174 if (is_float) {
3175 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3176 } else {
3177 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3178 }
3179
3180 // And remove the temporary stack space we allocated.
3181 __ addl(ESP, Immediate(2 * elem_size));
3182}
3183
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003184
3185void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3186 DCHECK(instruction->IsDiv() || instruction->IsRem());
3187
3188 LocationSummary* locations = instruction->GetLocations();
3189 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003190 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003191
3192 Register out_register = locations->Out().AsRegister<Register>();
3193 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003194 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003195
3196 DCHECK(imm == 1 || imm == -1);
3197
3198 if (instruction->IsRem()) {
3199 __ xorl(out_register, out_register);
3200 } else {
3201 __ movl(out_register, input_register);
3202 if (imm == -1) {
3203 __ negl(out_register);
3204 }
3205 }
3206}
3207
3208
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003209void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003210 LocationSummary* locations = instruction->GetLocations();
3211
3212 Register out_register = locations->Out().AsRegister<Register>();
3213 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003214 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003215
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003216 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003217 Register num = locations->GetTemp(0).AsRegister<Register>();
3218
3219 __ leal(num, Address(input_register, std::abs(imm) - 1));
3220 __ testl(input_register, input_register);
3221 __ cmovl(kGreaterEqual, num, input_register);
3222 int shift = CTZ(imm);
3223 __ sarl(num, Immediate(shift));
3224
3225 if (imm < 0) {
3226 __ negl(num);
3227 }
3228
3229 __ movl(out_register, num);
3230}
3231
3232void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3233 DCHECK(instruction->IsDiv() || instruction->IsRem());
3234
3235 LocationSummary* locations = instruction->GetLocations();
3236 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3237
3238 Register eax = locations->InAt(0).AsRegister<Register>();
3239 Register out = locations->Out().AsRegister<Register>();
3240 Register num;
3241 Register edx;
3242
3243 if (instruction->IsDiv()) {
3244 edx = locations->GetTemp(0).AsRegister<Register>();
3245 num = locations->GetTemp(1).AsRegister<Register>();
3246 } else {
3247 edx = locations->Out().AsRegister<Register>();
3248 num = locations->GetTemp(0).AsRegister<Register>();
3249 }
3250
3251 DCHECK_EQ(EAX, eax);
3252 DCHECK_EQ(EDX, edx);
3253 if (instruction->IsDiv()) {
3254 DCHECK_EQ(EAX, out);
3255 } else {
3256 DCHECK_EQ(EDX, out);
3257 }
3258
3259 int64_t magic;
3260 int shift;
3261 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3262
Mark Mendell0c9497d2015-08-21 09:30:05 -04003263 NearLabel ndiv;
3264 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003265 // If numerator is 0, the result is 0, no computation needed.
3266 __ testl(eax, eax);
3267 __ j(kNotEqual, &ndiv);
3268
3269 __ xorl(out, out);
3270 __ jmp(&end);
3271
3272 __ Bind(&ndiv);
3273
3274 // Save the numerator.
3275 __ movl(num, eax);
3276
3277 // EAX = magic
3278 __ movl(eax, Immediate(magic));
3279
3280 // EDX:EAX = magic * numerator
3281 __ imull(num);
3282
3283 if (imm > 0 && magic < 0) {
3284 // EDX += num
3285 __ addl(edx, num);
3286 } else if (imm < 0 && magic > 0) {
3287 __ subl(edx, num);
3288 }
3289
3290 // Shift if needed.
3291 if (shift != 0) {
3292 __ sarl(edx, Immediate(shift));
3293 }
3294
3295 // EDX += 1 if EDX < 0
3296 __ movl(eax, edx);
3297 __ shrl(edx, Immediate(31));
3298 __ addl(edx, eax);
3299
3300 if (instruction->IsRem()) {
3301 __ movl(eax, num);
3302 __ imull(edx, Immediate(imm));
3303 __ subl(eax, edx);
3304 __ movl(edx, eax);
3305 } else {
3306 __ movl(eax, edx);
3307 }
3308 __ Bind(&end);
3309}
3310
Calin Juravlebacfec32014-11-14 15:54:36 +00003311void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3312 DCHECK(instruction->IsDiv() || instruction->IsRem());
3313
3314 LocationSummary* locations = instruction->GetLocations();
3315 Location out = locations->Out();
3316 Location first = locations->InAt(0);
3317 Location second = locations->InAt(1);
3318 bool is_div = instruction->IsDiv();
3319
3320 switch (instruction->GetResultType()) {
3321 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003322 DCHECK_EQ(EAX, first.AsRegister<Register>());
3323 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003324
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003325 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003326 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003327
3328 if (imm == 0) {
3329 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3330 } else if (imm == 1 || imm == -1) {
3331 DivRemOneOrMinusOne(instruction);
3332 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003333 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334 } else {
3335 DCHECK(imm <= -2 || imm >= 2);
3336 GenerateDivRemWithAnyConstant(instruction);
3337 }
3338 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003339 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003340 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003341 is_div);
3342 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003343
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 Register second_reg = second.AsRegister<Register>();
3345 // 0x80000000/-1 triggers an arithmetic exception!
3346 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3347 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003348
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349 __ cmpl(second_reg, Immediate(-1));
3350 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003351
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352 // edx:eax <- sign-extended of eax
3353 __ cdq();
3354 // eax = quotient, edx = remainder
3355 __ idivl(second_reg);
3356 __ Bind(slow_path->GetExitLabel());
3357 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003358 break;
3359 }
3360
3361 case Primitive::kPrimLong: {
3362 InvokeRuntimeCallingConvention calling_convention;
3363 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3364 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3365 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3366 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3367 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3368 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3369
3370 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003371 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3372 instruction,
3373 instruction->GetDexPc(),
3374 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003375 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003376 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003377 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3378 instruction,
3379 instruction->GetDexPc(),
3380 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003381 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003382 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003383 break;
3384 }
3385
3386 default:
3387 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3388 }
3389}
3390
Calin Juravle7c4954d2014-10-28 16:57:40 +00003391void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003392 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003393 ? LocationSummary::kCall
3394 : LocationSummary::kNoCall;
3395 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3396
Calin Juravle7c4954d2014-10-28 16:57:40 +00003397 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003398 case Primitive::kPrimInt: {
3399 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003400 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003401 locations->SetOut(Location::SameAsFirstInput());
3402 // Intel uses edx:eax as the dividend.
3403 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003404 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3405 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3406 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003407 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003408 locations->AddTemp(Location::RequiresRegister());
3409 }
Calin Juravled0d48522014-11-04 16:40:20 +00003410 break;
3411 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003412 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003413 InvokeRuntimeCallingConvention calling_convention;
3414 locations->SetInAt(0, Location::RegisterPairLocation(
3415 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3416 locations->SetInAt(1, Location::RegisterPairLocation(
3417 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3418 // Runtime helper puts the result in EAX, EDX.
3419 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003420 break;
3421 }
3422 case Primitive::kPrimFloat:
3423 case Primitive::kPrimDouble: {
3424 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003425 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003426 locations->SetOut(Location::SameAsFirstInput());
3427 break;
3428 }
3429
3430 default:
3431 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3432 }
3433}
3434
3435void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3436 LocationSummary* locations = div->GetLocations();
3437 Location first = locations->InAt(0);
3438 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003439
3440 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003441 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003442 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003443 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003444 break;
3445 }
3446
3447 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003448 if (second.IsFpuRegister()) {
3449 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3450 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3451 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3452 DCHECK(!const_area->NeedsMaterialization());
3453 __ divss(first.AsFpuRegister<XmmRegister>(),
3454 codegen_->LiteralFloatAddress(
3455 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3456 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3457 } else {
3458 DCHECK(second.IsStackSlot());
3459 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3460 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003461 break;
3462 }
3463
3464 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003465 if (second.IsFpuRegister()) {
3466 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3467 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3468 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3469 DCHECK(!const_area->NeedsMaterialization());
3470 __ divsd(first.AsFpuRegister<XmmRegister>(),
3471 codegen_->LiteralDoubleAddress(
3472 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3473 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3474 } else {
3475 DCHECK(second.IsDoubleStackSlot());
3476 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3477 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003478 break;
3479 }
3480
3481 default:
3482 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3483 }
3484}
3485
Calin Juravlebacfec32014-11-14 15:54:36 +00003486void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003487 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003488
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003489 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3490 ? LocationSummary::kCall
3491 : LocationSummary::kNoCall;
3492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003493
Calin Juravled2ec87d2014-12-08 14:24:46 +00003494 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003495 case Primitive::kPrimInt: {
3496 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003497 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003498 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003499 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3500 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3501 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003502 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503 locations->AddTemp(Location::RequiresRegister());
3504 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003505 break;
3506 }
3507 case Primitive::kPrimLong: {
3508 InvokeRuntimeCallingConvention calling_convention;
3509 locations->SetInAt(0, Location::RegisterPairLocation(
3510 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3511 locations->SetInAt(1, Location::RegisterPairLocation(
3512 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3513 // Runtime helper puts the result in EAX, EDX.
3514 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3515 break;
3516 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003517 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003518 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003519 locations->SetInAt(0, Location::Any());
3520 locations->SetInAt(1, Location::Any());
3521 locations->SetOut(Location::RequiresFpuRegister());
3522 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003523 break;
3524 }
3525
3526 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003527 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003528 }
3529}
3530
3531void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3532 Primitive::Type type = rem->GetResultType();
3533 switch (type) {
3534 case Primitive::kPrimInt:
3535 case Primitive::kPrimLong: {
3536 GenerateDivRemIntegral(rem);
3537 break;
3538 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003539 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003541 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003542 break;
3543 }
3544 default:
3545 LOG(FATAL) << "Unexpected rem type " << type;
3546 }
3547}
3548
Calin Juravled0d48522014-11-04 16:40:20 +00003549void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003550 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3551 ? LocationSummary::kCallOnSlowPath
3552 : LocationSummary::kNoCall;
3553 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003554 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003555 case Primitive::kPrimByte:
3556 case Primitive::kPrimChar:
3557 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003558 case Primitive::kPrimInt: {
3559 locations->SetInAt(0, Location::Any());
3560 break;
3561 }
3562 case Primitive::kPrimLong: {
3563 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3564 if (!instruction->IsConstant()) {
3565 locations->AddTemp(Location::RequiresRegister());
3566 }
3567 break;
3568 }
3569 default:
3570 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3571 }
Calin Juravled0d48522014-11-04 16:40:20 +00003572 if (instruction->HasUses()) {
3573 locations->SetOut(Location::SameAsFirstInput());
3574 }
3575}
3576
3577void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003578 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003579 codegen_->AddSlowPath(slow_path);
3580
3581 LocationSummary* locations = instruction->GetLocations();
3582 Location value = locations->InAt(0);
3583
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003584 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003585 case Primitive::kPrimByte:
3586 case Primitive::kPrimChar:
3587 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003588 case Primitive::kPrimInt: {
3589 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003590 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003591 __ j(kEqual, slow_path->GetEntryLabel());
3592 } else if (value.IsStackSlot()) {
3593 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3594 __ j(kEqual, slow_path->GetEntryLabel());
3595 } else {
3596 DCHECK(value.IsConstant()) << value;
3597 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3598 __ jmp(slow_path->GetEntryLabel());
3599 }
3600 }
3601 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003602 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003603 case Primitive::kPrimLong: {
3604 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003606 __ movl(temp, value.AsRegisterPairLow<Register>());
3607 __ orl(temp, value.AsRegisterPairHigh<Register>());
3608 __ j(kEqual, slow_path->GetEntryLabel());
3609 } else {
3610 DCHECK(value.IsConstant()) << value;
3611 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3612 __ jmp(slow_path->GetEntryLabel());
3613 }
3614 }
3615 break;
3616 }
3617 default:
3618 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003619 }
Calin Juravled0d48522014-11-04 16:40:20 +00003620}
3621
Calin Juravle9aec02f2014-11-18 23:06:35 +00003622void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3623 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3624
3625 LocationSummary* locations =
3626 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3627
3628 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003629 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003630 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003631 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003632 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003633 // The shift count needs to be in CL or a constant.
3634 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003635 locations->SetOut(Location::SameAsFirstInput());
3636 break;
3637 }
3638 default:
3639 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3640 }
3641}
3642
3643void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3644 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3645
3646 LocationSummary* locations = op->GetLocations();
3647 Location first = locations->InAt(0);
3648 Location second = locations->InAt(1);
3649 DCHECK(first.Equals(locations->Out()));
3650
3651 switch (op->GetResultType()) {
3652 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003653 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003654 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003655 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003656 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003657 DCHECK_EQ(ECX, second_reg);
3658 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003659 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003660 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003661 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003662 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003663 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003664 }
3665 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003666 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3667 if (shift == 0) {
3668 return;
3669 }
3670 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003671 if (op->IsShl()) {
3672 __ shll(first_reg, imm);
3673 } else if (op->IsShr()) {
3674 __ sarl(first_reg, imm);
3675 } else {
3676 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677 }
3678 }
3679 break;
3680 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003681 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003682 if (second.IsRegister()) {
3683 Register second_reg = second.AsRegister<Register>();
3684 DCHECK_EQ(ECX, second_reg);
3685 if (op->IsShl()) {
3686 GenerateShlLong(first, second_reg);
3687 } else if (op->IsShr()) {
3688 GenerateShrLong(first, second_reg);
3689 } else {
3690 GenerateUShrLong(first, second_reg);
3691 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003692 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003693 // Shift by a constant.
3694 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3695 // Nothing to do if the shift is 0, as the input is already the output.
3696 if (shift != 0) {
3697 if (op->IsShl()) {
3698 GenerateShlLong(first, shift);
3699 } else if (op->IsShr()) {
3700 GenerateShrLong(first, shift);
3701 } else {
3702 GenerateUShrLong(first, shift);
3703 }
3704 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003705 }
3706 break;
3707 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 default:
3709 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3710 }
3711}
3712
Mark P Mendell73945692015-04-29 14:56:17 +00003713void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3714 Register low = loc.AsRegisterPairLow<Register>();
3715 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003716 if (shift == 1) {
3717 // This is just an addition.
3718 __ addl(low, low);
3719 __ adcl(high, high);
3720 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003721 // Shift by 32 is easy. High gets low, and low gets 0.
3722 codegen_->EmitParallelMoves(
3723 loc.ToLow(),
3724 loc.ToHigh(),
3725 Primitive::kPrimInt,
3726 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3727 loc.ToLow(),
3728 Primitive::kPrimInt);
3729 } else if (shift > 32) {
3730 // Low part becomes 0. High part is low part << (shift-32).
3731 __ movl(high, low);
3732 __ shll(high, Immediate(shift - 32));
3733 __ xorl(low, low);
3734 } else {
3735 // Between 1 and 31.
3736 __ shld(high, low, Immediate(shift));
3737 __ shll(low, Immediate(shift));
3738 }
3739}
3740
Calin Juravle9aec02f2014-11-18 23:06:35 +00003741void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003742 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003743 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3744 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3745 __ testl(shifter, Immediate(32));
3746 __ j(kEqual, &done);
3747 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3748 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3749 __ Bind(&done);
3750}
3751
Mark P Mendell73945692015-04-29 14:56:17 +00003752void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3753 Register low = loc.AsRegisterPairLow<Register>();
3754 Register high = loc.AsRegisterPairHigh<Register>();
3755 if (shift == 32) {
3756 // Need to copy the sign.
3757 DCHECK_NE(low, high);
3758 __ movl(low, high);
3759 __ sarl(high, Immediate(31));
3760 } else if (shift > 32) {
3761 DCHECK_NE(low, high);
3762 // High part becomes sign. Low part is shifted by shift - 32.
3763 __ movl(low, high);
3764 __ sarl(high, Immediate(31));
3765 __ sarl(low, Immediate(shift - 32));
3766 } else {
3767 // Between 1 and 31.
3768 __ shrd(low, high, Immediate(shift));
3769 __ sarl(high, Immediate(shift));
3770 }
3771}
3772
Calin Juravle9aec02f2014-11-18 23:06:35 +00003773void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003774 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003775 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3776 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3777 __ testl(shifter, Immediate(32));
3778 __ j(kEqual, &done);
3779 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3780 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3781 __ Bind(&done);
3782}
3783
Mark P Mendell73945692015-04-29 14:56:17 +00003784void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3785 Register low = loc.AsRegisterPairLow<Register>();
3786 Register high = loc.AsRegisterPairHigh<Register>();
3787 if (shift == 32) {
3788 // Shift by 32 is easy. Low gets high, and high gets 0.
3789 codegen_->EmitParallelMoves(
3790 loc.ToHigh(),
3791 loc.ToLow(),
3792 Primitive::kPrimInt,
3793 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3794 loc.ToHigh(),
3795 Primitive::kPrimInt);
3796 } else if (shift > 32) {
3797 // Low part is high >> (shift - 32). High part becomes 0.
3798 __ movl(low, high);
3799 __ shrl(low, Immediate(shift - 32));
3800 __ xorl(high, high);
3801 } else {
3802 // Between 1 and 31.
3803 __ shrd(low, high, Immediate(shift));
3804 __ shrl(high, Immediate(shift));
3805 }
3806}
3807
Calin Juravle9aec02f2014-11-18 23:06:35 +00003808void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003809 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003810 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3811 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3812 __ testl(shifter, Immediate(32));
3813 __ j(kEqual, &done);
3814 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3815 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3816 __ Bind(&done);
3817}
3818
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003819void LocationsBuilderX86::VisitRor(HRor* ror) {
3820 LocationSummary* locations =
3821 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3822
3823 switch (ror->GetResultType()) {
3824 case Primitive::kPrimLong:
3825 // Add the temporary needed.
3826 locations->AddTemp(Location::RequiresRegister());
3827 FALLTHROUGH_INTENDED;
3828 case Primitive::kPrimInt:
3829 locations->SetInAt(0, Location::RequiresRegister());
3830 // The shift count needs to be in CL (unless it is a constant).
3831 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3832 locations->SetOut(Location::SameAsFirstInput());
3833 break;
3834 default:
3835 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3836 UNREACHABLE();
3837 }
3838}
3839
3840void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3841 LocationSummary* locations = ror->GetLocations();
3842 Location first = locations->InAt(0);
3843 Location second = locations->InAt(1);
3844
3845 if (ror->GetResultType() == Primitive::kPrimInt) {
3846 Register first_reg = first.AsRegister<Register>();
3847 if (second.IsRegister()) {
3848 Register second_reg = second.AsRegister<Register>();
3849 __ rorl(first_reg, second_reg);
3850 } else {
3851 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3852 __ rorl(first_reg, imm);
3853 }
3854 return;
3855 }
3856
3857 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3858 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3859 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3860 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3861 if (second.IsRegister()) {
3862 Register second_reg = second.AsRegister<Register>();
3863 DCHECK_EQ(second_reg, ECX);
3864 __ movl(temp_reg, first_reg_hi);
3865 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3866 __ shrd(first_reg_lo, temp_reg, second_reg);
3867 __ movl(temp_reg, first_reg_hi);
3868 __ testl(second_reg, Immediate(32));
3869 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3870 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3871 } else {
3872 int32_t shift_amt =
3873 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3874 if (shift_amt == 0) {
3875 // Already fine.
3876 return;
3877 }
3878 if (shift_amt == 32) {
3879 // Just swap.
3880 __ movl(temp_reg, first_reg_lo);
3881 __ movl(first_reg_lo, first_reg_hi);
3882 __ movl(first_reg_hi, temp_reg);
3883 return;
3884 }
3885
3886 Immediate imm(shift_amt);
3887 // Save the constents of the low value.
3888 __ movl(temp_reg, first_reg_lo);
3889
3890 // Shift right into low, feeding bits from high.
3891 __ shrd(first_reg_lo, first_reg_hi, imm);
3892
3893 // Shift right into high, feeding bits from the original low.
3894 __ shrd(first_reg_hi, temp_reg, imm);
3895
3896 // Swap if needed.
3897 if (shift_amt > 32) {
3898 __ movl(temp_reg, first_reg_lo);
3899 __ movl(first_reg_lo, first_reg_hi);
3900 __ movl(first_reg_hi, temp_reg);
3901 }
3902 }
3903}
3904
Calin Juravle9aec02f2014-11-18 23:06:35 +00003905void LocationsBuilderX86::VisitShl(HShl* shl) {
3906 HandleShift(shl);
3907}
3908
3909void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3910 HandleShift(shl);
3911}
3912
3913void LocationsBuilderX86::VisitShr(HShr* shr) {
3914 HandleShift(shr);
3915}
3916
3917void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3918 HandleShift(shr);
3919}
3920
3921void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3922 HandleShift(ushr);
3923}
3924
3925void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3926 HandleShift(ushr);
3927}
3928
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003929void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003930 LocationSummary* locations =
3931 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003932 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003933 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003934 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3935 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003936}
3937
3938void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003939 // Note: if heap poisoning is enabled, the entry point takes cares
3940 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003941 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3942 instruction,
3943 instruction->GetDexPc(),
3944 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003945 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003946 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003947}
3948
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003949void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3950 LocationSummary* locations =
3951 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3952 locations->SetOut(Location::RegisterLocation(EAX));
3953 InvokeRuntimeCallingConvention calling_convention;
3954 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003955 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003956 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003957}
3958
3959void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3960 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003961 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003962 // Note: if heap poisoning is enabled, the entry point takes cares
3963 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003964 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3965 instruction,
3966 instruction->GetDexPc(),
3967 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003968 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003969 DCHECK(!codegen_->IsLeafMethod());
3970}
3971
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003972void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003973 LocationSummary* locations =
3974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003975 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3976 if (location.IsStackSlot()) {
3977 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3978 } else if (location.IsDoubleStackSlot()) {
3979 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003980 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003981 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003982}
3983
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003984void InstructionCodeGeneratorX86::VisitParameterValue(
3985 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3986}
3987
3988void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3989 LocationSummary* locations =
3990 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3991 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3992}
3993
3994void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003995}
3996
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003997void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003998 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003999 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004000 locations->SetInAt(0, Location::RequiresRegister());
4001 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004002}
4003
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004004void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4005 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004006 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004007 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004008 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004009 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004010 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004011 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004012 break;
4013
4014 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004015 __ notl(out.AsRegisterPairLow<Register>());
4016 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004017 break;
4018
4019 default:
4020 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4021 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004022}
4023
David Brazdil66d126e2015-04-03 16:02:44 +01004024void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4025 LocationSummary* locations =
4026 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4027 locations->SetInAt(0, Location::RequiresRegister());
4028 locations->SetOut(Location::SameAsFirstInput());
4029}
4030
4031void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004032 LocationSummary* locations = bool_not->GetLocations();
4033 Location in = locations->InAt(0);
4034 Location out = locations->Out();
4035 DCHECK(in.Equals(out));
4036 __ xorl(out.AsRegister<Register>(), Immediate(1));
4037}
4038
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004039void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004040 LocationSummary* locations =
4041 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004042 switch (compare->InputAt(0)->GetType()) {
4043 case Primitive::kPrimLong: {
4044 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004045 locations->SetInAt(1, Location::Any());
4046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4047 break;
4048 }
4049 case Primitive::kPrimFloat:
4050 case Primitive::kPrimDouble: {
4051 locations->SetInAt(0, Location::RequiresFpuRegister());
4052 locations->SetInAt(1, Location::RequiresFpuRegister());
4053 locations->SetOut(Location::RequiresRegister());
4054 break;
4055 }
4056 default:
4057 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4058 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004059}
4060
4061void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004062 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004063 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004064 Location left = locations->InAt(0);
4065 Location right = locations->InAt(1);
4066
Mark Mendell0c9497d2015-08-21 09:30:05 -04004067 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004068 switch (compare->InputAt(0)->GetType()) {
4069 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004070 Register left_low = left.AsRegisterPairLow<Register>();
4071 Register left_high = left.AsRegisterPairHigh<Register>();
4072 int32_t val_low = 0;
4073 int32_t val_high = 0;
4074 bool right_is_const = false;
4075
4076 if (right.IsConstant()) {
4077 DCHECK(right.GetConstant()->IsLongConstant());
4078 right_is_const = true;
4079 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4080 val_low = Low32Bits(val);
4081 val_high = High32Bits(val);
4082 }
4083
Calin Juravleddb7df22014-11-25 20:56:51 +00004084 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004085 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004086 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004087 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004088 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004089 DCHECK(right_is_const) << right;
4090 if (val_high == 0) {
4091 __ testl(left_high, left_high);
4092 } else {
4093 __ cmpl(left_high, Immediate(val_high));
4094 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004095 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004096 __ j(kLess, &less); // Signed compare.
4097 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004098 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004099 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004100 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004101 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004102 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004103 DCHECK(right_is_const) << right;
4104 if (val_low == 0) {
4105 __ testl(left_low, left_low);
4106 } else {
4107 __ cmpl(left_low, Immediate(val_low));
4108 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004109 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004110 break;
4111 }
4112 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004113 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004114 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4115 break;
4116 }
4117 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004118 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004119 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004120 break;
4121 }
4122 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004123 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004124 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004125 __ movl(out, Immediate(0));
4126 __ j(kEqual, &done);
4127 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4128
4129 __ Bind(&greater);
4130 __ movl(out, Immediate(1));
4131 __ jmp(&done);
4132
4133 __ Bind(&less);
4134 __ movl(out, Immediate(-1));
4135
4136 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004137}
4138
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004139void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004140 LocationSummary* locations =
4141 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004142 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4143 locations->SetInAt(i, Location::Any());
4144 }
4145 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004146}
4147
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004148void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004149 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004150}
4151
Roland Levillain7c1559a2015-12-15 10:55:36 +00004152void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004153 /*
4154 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4155 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4156 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4157 */
4158 switch (kind) {
4159 case MemBarrierKind::kAnyAny: {
Aart Bik0da3b912015-12-16 19:06:17 +00004160 __ mfence();
Calin Juravle52c48962014-12-16 17:02:57 +00004161 break;
4162 }
4163 case MemBarrierKind::kAnyStore:
4164 case MemBarrierKind::kLoadAny:
4165 case MemBarrierKind::kStoreStore: {
4166 // nop
4167 break;
4168 }
4169 default:
4170 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004171 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004172}
4173
Vladimir Markodc151b22015-10-15 18:02:30 +01004174HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4175 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4176 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004177 switch (desired_dispatch_info.code_ptr_location) {
4178 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4179 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4180 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4181 // (Though the direct CALL ptr16:32 is available for consideration).
4182 return HInvokeStaticOrDirect::DispatchInfo {
4183 desired_dispatch_info.method_load_kind,
4184 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4185 desired_dispatch_info.method_load_data,
4186 0u
4187 };
4188 default:
4189 return desired_dispatch_info;
4190 }
4191}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004192
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004193Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4194 Register temp) {
4195 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004196 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004197 if (!invoke->GetLocations()->Intrinsified()) {
4198 return location.AsRegister<Register>();
4199 }
4200 // For intrinsics we allow any location, so it may be on the stack.
4201 if (!location.IsRegister()) {
4202 __ movl(temp, Address(ESP, location.GetStackIndex()));
4203 return temp;
4204 }
4205 // For register locations, check if the register was saved. If so, get it from the stack.
4206 // Note: There is a chance that the register was saved but not overwritten, so we could
4207 // save one load. However, since this is just an intrinsic slow path we prefer this
4208 // simple and more robust approach rather that trying to determine if that's the case.
4209 SlowPathCode* slow_path = GetCurrentSlowPath();
4210 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4211 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4212 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4213 __ movl(temp, Address(ESP, stack_offset));
4214 return temp;
4215 }
4216 return location.AsRegister<Register>();
4217}
4218
Vladimir Marko58155012015-08-19 12:49:41 +00004219void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4220 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4221 switch (invoke->GetMethodLoadKind()) {
4222 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4223 // temp = thread->string_init_entrypoint
4224 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4225 break;
4226 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004227 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004228 break;
4229 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4230 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4231 break;
4232 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4233 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4234 method_patches_.emplace_back(invoke->GetTargetMethod());
4235 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4236 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004237 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4238 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4239 temp.AsRegister<Register>());
4240 uint32_t offset = invoke->GetDexCacheArrayOffset();
4241 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4242 // Add the patch entry and bind its label at the end of the instruction.
4243 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4244 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4245 break;
4246 }
Vladimir Marko58155012015-08-19 12:49:41 +00004247 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004248 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004249 Register method_reg;
4250 Register reg = temp.AsRegister<Register>();
4251 if (current_method.IsRegister()) {
4252 method_reg = current_method.AsRegister<Register>();
4253 } else {
4254 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4255 DCHECK(!current_method.IsValid());
4256 method_reg = reg;
4257 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4258 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004259 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004260 __ movl(reg, Address(method_reg,
4261 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004262 // temp = temp[index_in_cache]
4263 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4264 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4265 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004266 }
Vladimir Marko58155012015-08-19 12:49:41 +00004267 }
4268
4269 switch (invoke->GetCodePtrLocation()) {
4270 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4271 __ call(GetFrameEntryLabel());
4272 break;
4273 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4274 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4275 Label* label = &relative_call_patches_.back().label;
4276 __ call(label); // Bind to the patch label, override at link time.
4277 __ Bind(label); // Bind the label at the end of the "call" insn.
4278 break;
4279 }
4280 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4281 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004282 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4283 LOG(FATAL) << "Unsupported";
4284 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004285 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4286 // (callee_method + offset_of_quick_compiled_code)()
4287 __ call(Address(callee_method.AsRegister<Register>(),
4288 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4289 kX86WordSize).Int32Value()));
4290 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004291 }
4292
4293 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004294}
4295
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004296void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4297 Register temp = temp_in.AsRegister<Register>();
4298 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4299 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004300
4301 // Use the calling convention instead of the location of the receiver, as
4302 // intrinsics may have put the receiver in a different register. In the intrinsics
4303 // slow path, the arguments have been moved to the right place, so here we are
4304 // guaranteed that the receiver is the first register of the calling convention.
4305 InvokeDexCallingConvention calling_convention;
4306 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004307 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004308 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004309 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004310 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004311 // Instead of simply (possibly) unpoisoning `temp` here, we should
4312 // emit a read barrier for the previous class reference load.
4313 // However this is not required in practice, as this is an
4314 // intermediate/temporary reference and because the current
4315 // concurrent copying collector keeps the from-space memory
4316 // intact/accessible until the end of the marking phase (the
4317 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004318 __ MaybeUnpoisonHeapReference(temp);
4319 // temp = temp->GetMethodAt(method_offset);
4320 __ movl(temp, Address(temp, method_offset));
4321 // call temp->GetEntryPoint();
4322 __ call(Address(
4323 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4324}
4325
Vladimir Marko58155012015-08-19 12:49:41 +00004326void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4327 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004328 size_t size =
4329 method_patches_.size() +
4330 relative_call_patches_.size() +
4331 pc_relative_dex_cache_patches_.size();
4332 linker_patches->reserve(size);
4333 // The label points to the end of the "movl" insn but the literal offset for method
4334 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4335 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004336 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004337 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004338 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4339 info.target_method.dex_file,
4340 info.target_method.dex_method_index));
4341 }
4342 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004343 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004344 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4345 info.target_method.dex_file,
4346 info.target_method.dex_method_index));
4347 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004348 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4349 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4350 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4351 &info.target_dex_file,
4352 GetMethodAddressOffset(),
4353 info.element_offset));
4354 }
Vladimir Marko58155012015-08-19 12:49:41 +00004355}
4356
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004357void CodeGeneratorX86::MarkGCCard(Register temp,
4358 Register card,
4359 Register object,
4360 Register value,
4361 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004362 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004363 if (value_can_be_null) {
4364 __ testl(value, value);
4365 __ j(kEqual, &is_null);
4366 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004367 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4368 __ movl(temp, object);
4369 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004370 __ movb(Address(temp, card, TIMES_1, 0),
4371 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004372 if (value_can_be_null) {
4373 __ Bind(&is_null);
4374 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004375}
4376
Calin Juravle52c48962014-12-16 17:02:57 +00004377void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4378 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004379
4380 bool object_field_get_with_read_barrier =
4381 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004382 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004383 new (GetGraph()->GetArena()) LocationSummary(instruction,
4384 kEmitCompilerReadBarrier ?
4385 LocationSummary::kCallOnSlowPath :
4386 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004387 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004388
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004389 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4390 locations->SetOut(Location::RequiresFpuRegister());
4391 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004392 // The output overlaps in case of long: we don't want the low move
4393 // to overwrite the object's location. Likewise, in the case of
4394 // an object field get with read barriers enabled, we do not want
4395 // the move to overwrite the object's location, as we need it to emit
4396 // the read barrier.
4397 locations->SetOut(
4398 Location::RequiresRegister(),
4399 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4400 Location::kOutputOverlap :
4401 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004402 }
Calin Juravle52c48962014-12-16 17:02:57 +00004403
4404 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4405 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004406 // So we use an XMM register as a temp to achieve atomicity (first
4407 // load the temp into the XMM and then copy the XMM into the
4408 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004409 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004410 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4411 // We need a temporary register for the read barrier marking slow
4412 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4413 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004414 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004415}
4416
Calin Juravle52c48962014-12-16 17:02:57 +00004417void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4418 const FieldInfo& field_info) {
4419 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004420
Calin Juravle52c48962014-12-16 17:02:57 +00004421 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004422 Location base_loc = locations->InAt(0);
4423 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004424 Location out = locations->Out();
4425 bool is_volatile = field_info.IsVolatile();
4426 Primitive::Type field_type = field_info.GetFieldType();
4427 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4428
4429 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004431 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004432 break;
4433 }
4434
4435 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004436 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004437 break;
4438 }
4439
4440 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004441 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004442 break;
4443 }
4444
4445 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004446 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447 break;
4448 }
4449
4450 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004451 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004452 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004453
4454 case Primitive::kPrimNot: {
4455 // /* HeapReference<Object> */ out = *(base + offset)
4456 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4457 Location temp_loc = locations->GetTemp(0);
4458 // Note that a potential implicit null check is handled in this
4459 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4460 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4461 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4462 if (is_volatile) {
4463 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4464 }
4465 } else {
4466 __ movl(out.AsRegister<Register>(), Address(base, offset));
4467 codegen_->MaybeRecordImplicitNullCheck(instruction);
4468 if (is_volatile) {
4469 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4470 }
4471 // If read barriers are enabled, emit read barriers other than
4472 // Baker's using a slow path (and also unpoison the loaded
4473 // reference, if heap poisoning is enabled).
4474 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4475 }
4476 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004477 }
4478
4479 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004480 if (is_volatile) {
4481 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4482 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004484 __ movd(out.AsRegisterPairLow<Register>(), temp);
4485 __ psrlq(temp, Immediate(32));
4486 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4487 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004488 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004489 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004490 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004491 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4492 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004493 break;
4494 }
4495
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004496 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004497 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004498 break;
4499 }
4500
4501 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004502 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004503 break;
4504 }
4505
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004506 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004507 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004508 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004509 }
Calin Juravle52c48962014-12-16 17:02:57 +00004510
Roland Levillain7c1559a2015-12-15 10:55:36 +00004511 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4512 // Potential implicit null checks, in the case of reference or
4513 // long fields, are handled in the previous switch statement.
4514 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004515 codegen_->MaybeRecordImplicitNullCheck(instruction);
4516 }
4517
Calin Juravle52c48962014-12-16 17:02:57 +00004518 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004519 if (field_type == Primitive::kPrimNot) {
4520 // Memory barriers, in the case of references, are also handled
4521 // in the previous switch statement.
4522 } else {
4523 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4524 }
Roland Levillain4d027112015-07-01 15:41:14 +01004525 }
Calin Juravle52c48962014-12-16 17:02:57 +00004526}
4527
4528void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4529 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4530
4531 LocationSummary* locations =
4532 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4533 locations->SetInAt(0, Location::RequiresRegister());
4534 bool is_volatile = field_info.IsVolatile();
4535 Primitive::Type field_type = field_info.GetFieldType();
4536 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4537 || (field_type == Primitive::kPrimByte);
4538
4539 // The register allocator does not support multiple
4540 // inputs that die at entry with one in a specific register.
4541 if (is_byte_type) {
4542 // Ensure the value is in a byte register.
4543 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004544 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004545 if (is_volatile && field_type == Primitive::kPrimDouble) {
4546 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4547 locations->SetInAt(1, Location::RequiresFpuRegister());
4548 } else {
4549 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4550 }
4551 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4552 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004553 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004554
Calin Juravle52c48962014-12-16 17:02:57 +00004555 // 64bits value can be atomically written to an address with movsd and an XMM register.
4556 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4557 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4558 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4559 // isolated cases when we need this it isn't worth adding the extra complexity.
4560 locations->AddTemp(Location::RequiresFpuRegister());
4561 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004562 } else {
4563 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4564
4565 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4566 // Temporary registers for the write barrier.
4567 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4568 // Ensure the card is in a byte register.
4569 locations->AddTemp(Location::RegisterLocation(ECX));
4570 }
Calin Juravle52c48962014-12-16 17:02:57 +00004571 }
4572}
4573
4574void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004575 const FieldInfo& field_info,
4576 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004577 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4578
4579 LocationSummary* locations = instruction->GetLocations();
4580 Register base = locations->InAt(0).AsRegister<Register>();
4581 Location value = locations->InAt(1);
4582 bool is_volatile = field_info.IsVolatile();
4583 Primitive::Type field_type = field_info.GetFieldType();
4584 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004585 bool needs_write_barrier =
4586 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004587
4588 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004589 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004590 }
4591
Mark Mendell81489372015-11-04 11:30:41 -05004592 bool maybe_record_implicit_null_check_done = false;
4593
Calin Juravle52c48962014-12-16 17:02:57 +00004594 switch (field_type) {
4595 case Primitive::kPrimBoolean:
4596 case Primitive::kPrimByte: {
4597 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4598 break;
4599 }
4600
4601 case Primitive::kPrimShort:
4602 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004603 if (value.IsConstant()) {
4604 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4605 __ movw(Address(base, offset), Immediate(v));
4606 } else {
4607 __ movw(Address(base, offset), value.AsRegister<Register>());
4608 }
Calin Juravle52c48962014-12-16 17:02:57 +00004609 break;
4610 }
4611
4612 case Primitive::kPrimInt:
4613 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004614 if (kPoisonHeapReferences && needs_write_barrier) {
4615 // Note that in the case where `value` is a null reference,
4616 // we do not enter this block, as the reference does not
4617 // need poisoning.
4618 DCHECK_EQ(field_type, Primitive::kPrimNot);
4619 Register temp = locations->GetTemp(0).AsRegister<Register>();
4620 __ movl(temp, value.AsRegister<Register>());
4621 __ PoisonHeapReference(temp);
4622 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004623 } else if (value.IsConstant()) {
4624 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4625 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004626 } else {
4627 __ movl(Address(base, offset), value.AsRegister<Register>());
4628 }
Calin Juravle52c48962014-12-16 17:02:57 +00004629 break;
4630 }
4631
4632 case Primitive::kPrimLong: {
4633 if (is_volatile) {
4634 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4635 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4636 __ movd(temp1, value.AsRegisterPairLow<Register>());
4637 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4638 __ punpckldq(temp1, temp2);
4639 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004640 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004641 } else if (value.IsConstant()) {
4642 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4643 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4644 codegen_->MaybeRecordImplicitNullCheck(instruction);
4645 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004646 } else {
4647 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004648 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004649 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4650 }
Mark Mendell81489372015-11-04 11:30:41 -05004651 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004652 break;
4653 }
4654
4655 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004656 if (value.IsConstant()) {
4657 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4658 __ movl(Address(base, offset), Immediate(v));
4659 } else {
4660 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4661 }
Calin Juravle52c48962014-12-16 17:02:57 +00004662 break;
4663 }
4664
4665 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004666 if (value.IsConstant()) {
4667 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4668 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4669 codegen_->MaybeRecordImplicitNullCheck(instruction);
4670 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4671 maybe_record_implicit_null_check_done = true;
4672 } else {
4673 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4674 }
Calin Juravle52c48962014-12-16 17:02:57 +00004675 break;
4676 }
4677
4678 case Primitive::kPrimVoid:
4679 LOG(FATAL) << "Unreachable type " << field_type;
4680 UNREACHABLE();
4681 }
4682
Mark Mendell81489372015-11-04 11:30:41 -05004683 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004684 codegen_->MaybeRecordImplicitNullCheck(instruction);
4685 }
4686
Roland Levillain4d027112015-07-01 15:41:14 +01004687 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004688 Register temp = locations->GetTemp(0).AsRegister<Register>();
4689 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004690 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004691 }
4692
Calin Juravle52c48962014-12-16 17:02:57 +00004693 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004694 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004695 }
4696}
4697
4698void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4699 HandleFieldGet(instruction, instruction->GetFieldInfo());
4700}
4701
4702void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4703 HandleFieldGet(instruction, instruction->GetFieldInfo());
4704}
4705
4706void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4707 HandleFieldSet(instruction, instruction->GetFieldInfo());
4708}
4709
4710void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004711 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004712}
4713
4714void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4715 HandleFieldSet(instruction, instruction->GetFieldInfo());
4716}
4717
4718void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004719 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004720}
4721
4722void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4723 HandleFieldGet(instruction, instruction->GetFieldInfo());
4724}
4725
4726void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4727 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004728}
4729
Calin Juravlee460d1d2015-09-29 04:52:17 +01004730void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4731 HUnresolvedInstanceFieldGet* instruction) {
4732 FieldAccessCallingConventionX86 calling_convention;
4733 codegen_->CreateUnresolvedFieldLocationSummary(
4734 instruction, instruction->GetFieldType(), calling_convention);
4735}
4736
4737void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4738 HUnresolvedInstanceFieldGet* instruction) {
4739 FieldAccessCallingConventionX86 calling_convention;
4740 codegen_->GenerateUnresolvedFieldAccess(instruction,
4741 instruction->GetFieldType(),
4742 instruction->GetFieldIndex(),
4743 instruction->GetDexPc(),
4744 calling_convention);
4745}
4746
4747void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4748 HUnresolvedInstanceFieldSet* instruction) {
4749 FieldAccessCallingConventionX86 calling_convention;
4750 codegen_->CreateUnresolvedFieldLocationSummary(
4751 instruction, instruction->GetFieldType(), calling_convention);
4752}
4753
4754void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4755 HUnresolvedInstanceFieldSet* instruction) {
4756 FieldAccessCallingConventionX86 calling_convention;
4757 codegen_->GenerateUnresolvedFieldAccess(instruction,
4758 instruction->GetFieldType(),
4759 instruction->GetFieldIndex(),
4760 instruction->GetDexPc(),
4761 calling_convention);
4762}
4763
4764void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4765 HUnresolvedStaticFieldGet* instruction) {
4766 FieldAccessCallingConventionX86 calling_convention;
4767 codegen_->CreateUnresolvedFieldLocationSummary(
4768 instruction, instruction->GetFieldType(), calling_convention);
4769}
4770
4771void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4772 HUnresolvedStaticFieldGet* instruction) {
4773 FieldAccessCallingConventionX86 calling_convention;
4774 codegen_->GenerateUnresolvedFieldAccess(instruction,
4775 instruction->GetFieldType(),
4776 instruction->GetFieldIndex(),
4777 instruction->GetDexPc(),
4778 calling_convention);
4779}
4780
4781void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4782 HUnresolvedStaticFieldSet* instruction) {
4783 FieldAccessCallingConventionX86 calling_convention;
4784 codegen_->CreateUnresolvedFieldLocationSummary(
4785 instruction, instruction->GetFieldType(), calling_convention);
4786}
4787
4788void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4789 HUnresolvedStaticFieldSet* instruction) {
4790 FieldAccessCallingConventionX86 calling_convention;
4791 codegen_->GenerateUnresolvedFieldAccess(instruction,
4792 instruction->GetFieldType(),
4793 instruction->GetFieldIndex(),
4794 instruction->GetDexPc(),
4795 calling_convention);
4796}
4797
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004798void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004799 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4800 ? LocationSummary::kCallOnSlowPath
4801 : LocationSummary::kNoCall;
4802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4803 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004804 ? Location::RequiresRegister()
4805 : Location::Any();
4806 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004807 if (instruction->HasUses()) {
4808 locations->SetOut(Location::SameAsFirstInput());
4809 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004810}
4811
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004812void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004813 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4814 return;
4815 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004816 LocationSummary* locations = instruction->GetLocations();
4817 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004818
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004819 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4820 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4821}
4822
4823void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004824 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004825 codegen_->AddSlowPath(slow_path);
4826
4827 LocationSummary* locations = instruction->GetLocations();
4828 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004829
4830 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004831 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004832 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004833 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004834 } else {
4835 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004836 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004837 __ jmp(slow_path->GetEntryLabel());
4838 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004839 }
4840 __ j(kEqual, slow_path->GetEntryLabel());
4841}
4842
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004843void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004844 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004845 GenerateImplicitNullCheck(instruction);
4846 } else {
4847 GenerateExplicitNullCheck(instruction);
4848 }
4849}
4850
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004851void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004852 bool object_array_get_with_read_barrier =
4853 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004854 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004855 new (GetGraph()->GetArena()) LocationSummary(instruction,
4856 object_array_get_with_read_barrier ?
4857 LocationSummary::kCallOnSlowPath :
4858 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004859 locations->SetInAt(0, Location::RequiresRegister());
4860 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004861 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4862 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4863 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004864 // The output overlaps in case of long: we don't want the low move
4865 // to overwrite the array's location. Likewise, in the case of an
4866 // object array get with read barriers enabled, we do not want the
4867 // move to overwrite the array's location, as we need it to emit
4868 // the read barrier.
4869 locations->SetOut(
4870 Location::RequiresRegister(),
4871 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4872 Location::kOutputOverlap :
4873 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004874 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004875 // We need a temporary register for the read barrier marking slow
4876 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4877 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4878 locations->AddTemp(Location::RequiresRegister());
4879 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004880}
4881
4882void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4883 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004884 Location obj_loc = locations->InAt(0);
4885 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004886 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004887 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004888
Calin Juravle77520bc2015-01-12 18:45:46 +00004889 Primitive::Type type = instruction->GetType();
4890 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891 case Primitive::kPrimBoolean: {
4892 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004893 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004894 if (index.IsConstant()) {
4895 __ movzxb(out, Address(obj,
4896 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4897 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004898 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899 }
4900 break;
4901 }
4902
4903 case Primitive::kPrimByte: {
4904 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004905 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004906 if (index.IsConstant()) {
4907 __ movsxb(out, Address(obj,
4908 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4909 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004910 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911 }
4912 break;
4913 }
4914
4915 case Primitive::kPrimShort: {
4916 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004917 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918 if (index.IsConstant()) {
4919 __ movsxw(out, Address(obj,
4920 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4921 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004922 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923 }
4924 break;
4925 }
4926
4927 case Primitive::kPrimChar: {
4928 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004929 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004930 if (index.IsConstant()) {
4931 __ movzxw(out, Address(obj,
4932 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4933 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004934 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004935 }
4936 break;
4937 }
4938
Roland Levillain7c1559a2015-12-15 10:55:36 +00004939 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004941 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942 if (index.IsConstant()) {
4943 __ movl(out, Address(obj,
4944 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4945 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004946 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004947 }
4948 break;
4949 }
4950
Roland Levillain7c1559a2015-12-15 10:55:36 +00004951 case Primitive::kPrimNot: {
4952 static_assert(
4953 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4954 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4956 // /* HeapReference<Object> */ out =
4957 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4958 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4959 Location temp = locations->GetTemp(0);
4960 // Note that a potential implicit null check is handled in this
4961 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4962 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4963 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4964 } else {
4965 Register out = out_loc.AsRegister<Register>();
4966 if (index.IsConstant()) {
4967 uint32_t offset =
4968 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4969 __ movl(out, Address(obj, offset));
4970 codegen_->MaybeRecordImplicitNullCheck(instruction);
4971 // If read barriers are enabled, emit read barriers other than
4972 // Baker's using a slow path (and also unpoison the loaded
4973 // reference, if heap poisoning is enabled).
4974 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4975 } else {
4976 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4977 codegen_->MaybeRecordImplicitNullCheck(instruction);
4978 // If read barriers are enabled, emit read barriers other than
4979 // Baker's using a slow path (and also unpoison the loaded
4980 // reference, if heap poisoning is enabled).
4981 codegen_->MaybeGenerateReadBarrierSlow(
4982 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4983 }
4984 }
4985 break;
4986 }
4987
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004988 case Primitive::kPrimLong: {
4989 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004990 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004991 if (index.IsConstant()) {
4992 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004993 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004994 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004995 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004996 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004997 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004998 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004999 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005000 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005001 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005002 }
5003 break;
5004 }
5005
Mark Mendell7c8d0092015-01-26 11:21:33 -05005006 case Primitive::kPrimFloat: {
5007 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005008 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005009 if (index.IsConstant()) {
5010 __ movss(out, Address(obj,
5011 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5012 } else {
5013 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5014 }
5015 break;
5016 }
5017
5018 case Primitive::kPrimDouble: {
5019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005020 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005021 if (index.IsConstant()) {
5022 __ movsd(out, Address(obj,
5023 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5024 } else {
5025 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5026 }
5027 break;
5028 }
5029
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005030 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005031 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005032 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005033 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005034
Roland Levillain7c1559a2015-12-15 10:55:36 +00005035 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5036 // Potential implicit null checks, in the case of reference or
5037 // long arrays, are handled in the previous switch statement.
5038 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005039 codegen_->MaybeRecordImplicitNullCheck(instruction);
5040 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041}
5042
5043void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05005044 // This location builder might end up asking to up to four registers, which is
5045 // not currently possible for baseline. The situation in which we need four
5046 // registers cannot be met by baseline though, because it has not run any
5047 // optimization.
5048
Nicolas Geoffray39468442014-09-02 15:17:15 +01005049 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005050
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005051 bool needs_write_barrier =
5052 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005053 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5054 bool object_array_set_with_read_barrier =
5055 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005056
Nicolas Geoffray39468442014-09-02 15:17:15 +01005057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5058 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005059 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5060 LocationSummary::kCallOnSlowPath :
5061 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005062
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005063 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5064 || (value_type == Primitive::kPrimByte);
5065 // We need the inputs to be different than the output in case of long operation.
5066 // In case of a byte operation, the register allocator does not support multiple
5067 // inputs that die at entry with one in a specific register.
5068 locations->SetInAt(0, Location::RequiresRegister());
5069 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5070 if (is_byte_type) {
5071 // Ensure the value is in a byte register.
5072 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5073 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005074 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005076 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5077 }
5078 if (needs_write_barrier) {
5079 // Temporary registers for the write barrier.
5080 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5081 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005082 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005084}
5085
5086void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5087 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005088 Location array_loc = locations->InAt(0);
5089 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005091 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005092 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005093 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5094 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5095 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005096 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005097 bool needs_write_barrier =
5098 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099
5100 switch (value_type) {
5101 case Primitive::kPrimBoolean:
5102 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005103 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5104 Address address = index.IsConstant()
5105 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5106 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5107 if (value.IsRegister()) {
5108 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005109 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005110 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005111 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005112 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113 break;
5114 }
5115
5116 case Primitive::kPrimShort:
5117 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005118 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5119 Address address = index.IsConstant()
5120 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5121 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5122 if (value.IsRegister()) {
5123 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005127 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128 break;
5129 }
5130
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005131 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005132 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5133 Address address = index.IsConstant()
5134 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5135 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005136
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005137 if (!value.IsRegister()) {
5138 // Just setting null.
5139 DCHECK(instruction->InputAt(2)->IsNullConstant());
5140 DCHECK(value.IsConstant()) << value;
5141 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005143 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005144 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005145 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005147
5148 DCHECK(needs_write_barrier);
5149 Register register_value = value.AsRegister<Register>();
5150 NearLabel done, not_null, do_put;
5151 SlowPathCode* slow_path = nullptr;
5152 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005153 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005154 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5155 codegen_->AddSlowPath(slow_path);
5156 if (instruction->GetValueCanBeNull()) {
5157 __ testl(register_value, register_value);
5158 __ j(kNotEqual, &not_null);
5159 __ movl(address, Immediate(0));
5160 codegen_->MaybeRecordImplicitNullCheck(instruction);
5161 __ jmp(&done);
5162 __ Bind(&not_null);
5163 }
5164
Roland Levillain0d5a2812015-11-13 10:07:31 +00005165 if (kEmitCompilerReadBarrier) {
5166 // When read barriers are enabled, the type checking
5167 // instrumentation requires two read barriers:
5168 //
5169 // __ movl(temp2, temp);
5170 // // /* HeapReference<Class> */ temp = temp->component_type_
5171 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005172 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005173 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5174 //
5175 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5176 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005177 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005178 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5179 //
5180 // __ cmpl(temp, temp2);
5181 //
5182 // However, the second read barrier may trash `temp`, as it
5183 // is a temporary register, and as such would not be saved
5184 // along with live registers before calling the runtime (nor
5185 // restored afterwards). So in this case, we bail out and
5186 // delegate the work to the array set slow path.
5187 //
5188 // TODO: Extend the register allocator to support a new
5189 // "(locally) live temp" location so as to avoid always
5190 // going into the slow path when read barriers are enabled.
5191 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005192 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005193 // /* HeapReference<Class> */ temp = array->klass_
5194 __ movl(temp, Address(array, class_offset));
5195 codegen_->MaybeRecordImplicitNullCheck(instruction);
5196 __ MaybeUnpoisonHeapReference(temp);
5197
5198 // /* HeapReference<Class> */ temp = temp->component_type_
5199 __ movl(temp, Address(temp, component_offset));
5200 // If heap poisoning is enabled, no need to unpoison `temp`
5201 // nor the object reference in `register_value->klass`, as
5202 // we are comparing two poisoned references.
5203 __ cmpl(temp, Address(register_value, class_offset));
5204
5205 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5206 __ j(kEqual, &do_put);
5207 // If heap poisoning is enabled, the `temp` reference has
5208 // not been unpoisoned yet; unpoison it now.
5209 __ MaybeUnpoisonHeapReference(temp);
5210
5211 // /* HeapReference<Class> */ temp = temp->super_class_
5212 __ movl(temp, Address(temp, super_offset));
5213 // If heap poisoning is enabled, no need to unpoison
5214 // `temp`, as we are comparing against null below.
5215 __ testl(temp, temp);
5216 __ j(kNotEqual, slow_path->GetEntryLabel());
5217 __ Bind(&do_put);
5218 } else {
5219 __ j(kNotEqual, slow_path->GetEntryLabel());
5220 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005221 }
5222 }
5223
5224 if (kPoisonHeapReferences) {
5225 __ movl(temp, register_value);
5226 __ PoisonHeapReference(temp);
5227 __ movl(address, temp);
5228 } else {
5229 __ movl(address, register_value);
5230 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005231 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005232 codegen_->MaybeRecordImplicitNullCheck(instruction);
5233 }
5234
5235 Register card = locations->GetTemp(1).AsRegister<Register>();
5236 codegen_->MarkGCCard(
5237 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5238 __ Bind(&done);
5239
5240 if (slow_path != nullptr) {
5241 __ Bind(slow_path->GetExitLabel());
5242 }
5243
5244 break;
5245 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005246
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005247 case Primitive::kPrimInt: {
5248 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5249 Address address = index.IsConstant()
5250 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5251 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5252 if (value.IsRegister()) {
5253 __ movl(address, value.AsRegister<Register>());
5254 } else {
5255 DCHECK(value.IsConstant()) << value;
5256 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5257 __ movl(address, Immediate(v));
5258 }
5259 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 break;
5261 }
5262
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 case Primitive::kPrimLong: {
5264 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005265 if (index.IsConstant()) {
5266 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005267 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005268 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005269 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005270 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005271 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005272 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005273 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005274 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005275 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005276 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005277 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005279 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005281 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005284 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005285 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005286 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005287 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005289 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005291 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005292 Immediate(High32Bits(val)));
5293 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 }
5295 break;
5296 }
5297
Mark Mendell7c8d0092015-01-26 11:21:33 -05005298 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5300 Address address = index.IsConstant()
5301 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5302 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005303 if (value.IsFpuRegister()) {
5304 __ movss(address, value.AsFpuRegister<XmmRegister>());
5305 } else {
5306 DCHECK(value.IsConstant());
5307 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5308 __ movl(address, Immediate(v));
5309 }
5310 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005311 break;
5312 }
5313
5314 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5316 Address address = index.IsConstant()
5317 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5318 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005319 if (value.IsFpuRegister()) {
5320 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5321 } else {
5322 DCHECK(value.IsConstant());
5323 Address address_hi = index.IsConstant() ?
5324 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5325 offset + kX86WordSize) :
5326 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5327 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5328 __ movl(address, Immediate(Low32Bits(v)));
5329 codegen_->MaybeRecordImplicitNullCheck(instruction);
5330 __ movl(address_hi, Immediate(High32Bits(v)));
5331 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005332 break;
5333 }
5334
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 case Primitive::kPrimVoid:
5336 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005337 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005338 }
5339}
5340
5341void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5342 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005343 locations->SetInAt(0, Location::RequiresRegister());
5344 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345}
5346
5347void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5348 LocationSummary* locations = instruction->GetLocations();
5349 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005350 Register obj = locations->InAt(0).AsRegister<Register>();
5351 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005353 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354}
5355
5356void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005357 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5358 ? LocationSummary::kCallOnSlowPath
5359 : LocationSummary::kNoCall;
5360 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005361 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005362 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005363 if (instruction->HasUses()) {
5364 locations->SetOut(Location::SameAsFirstInput());
5365 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005366}
5367
5368void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5369 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005370 Location index_loc = locations->InAt(0);
5371 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005372 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005373 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005374
Mark Mendell99dbd682015-04-22 16:18:52 -04005375 if (length_loc.IsConstant()) {
5376 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5377 if (index_loc.IsConstant()) {
5378 // BCE will remove the bounds check if we are guarenteed to pass.
5379 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5380 if (index < 0 || index >= length) {
5381 codegen_->AddSlowPath(slow_path);
5382 __ jmp(slow_path->GetEntryLabel());
5383 } else {
5384 // Some optimization after BCE may have generated this, and we should not
5385 // generate a bounds check if it is a valid range.
5386 }
5387 return;
5388 }
5389
5390 // We have to reverse the jump condition because the length is the constant.
5391 Register index_reg = index_loc.AsRegister<Register>();
5392 __ cmpl(index_reg, Immediate(length));
5393 codegen_->AddSlowPath(slow_path);
5394 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005395 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005396 Register length = length_loc.AsRegister<Register>();
5397 if (index_loc.IsConstant()) {
5398 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5399 __ cmpl(length, Immediate(value));
5400 } else {
5401 __ cmpl(length, index_loc.AsRegister<Register>());
5402 }
5403 codegen_->AddSlowPath(slow_path);
5404 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005405 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406}
5407
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005408void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5409 temp->SetLocations(nullptr);
5410}
5411
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005412void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005413 // Nothing to do, this is driven by the code generator.
5414}
5415
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005416void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005417 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005418}
5419
5420void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005421 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5422}
5423
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005424void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5425 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5426}
5427
5428void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005429 HBasicBlock* block = instruction->GetBlock();
5430 if (block->GetLoopInformation() != nullptr) {
5431 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5432 // The back edge will generate the suspend check.
5433 return;
5434 }
5435 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5436 // The goto will generate the suspend check.
5437 return;
5438 }
5439 GenerateSuspendCheck(instruction, nullptr);
5440}
5441
5442void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5443 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005444 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005445 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5446 if (slow_path == nullptr) {
5447 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5448 instruction->SetSlowPath(slow_path);
5449 codegen_->AddSlowPath(slow_path);
5450 if (successor != nullptr) {
5451 DCHECK(successor->IsLoopHeader());
5452 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5453 }
5454 } else {
5455 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5456 }
5457
Roland Levillain7c1559a2015-12-15 10:55:36 +00005458 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5459 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005460 if (successor == nullptr) {
5461 __ j(kNotEqual, slow_path->GetEntryLabel());
5462 __ Bind(slow_path->GetReturnLabel());
5463 } else {
5464 __ j(kEqual, codegen_->GetLabelOf(successor));
5465 __ jmp(slow_path->GetEntryLabel());
5466 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005467}
5468
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005469X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5470 return codegen_->GetAssembler();
5471}
5472
Mark Mendell7c8d0092015-01-26 11:21:33 -05005473void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005474 ScratchRegisterScope ensure_scratch(
5475 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5476 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5477 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5478 __ movl(temp_reg, Address(ESP, src + stack_offset));
5479 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005480}
5481
5482void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005483 ScratchRegisterScope ensure_scratch(
5484 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5485 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5486 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5487 __ movl(temp_reg, Address(ESP, src + stack_offset));
5488 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5489 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5490 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005491}
5492
5493void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005494 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005495 Location source = move->GetSource();
5496 Location destination = move->GetDestination();
5497
5498 if (source.IsRegister()) {
5499 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005500 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005501 } else {
5502 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005503 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005504 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005505 } else if (source.IsFpuRegister()) {
5506 if (destination.IsFpuRegister()) {
5507 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5508 } else if (destination.IsStackSlot()) {
5509 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5510 } else {
5511 DCHECK(destination.IsDoubleStackSlot());
5512 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5513 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005514 } else if (source.IsStackSlot()) {
5515 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005516 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005517 } else if (destination.IsFpuRegister()) {
5518 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005519 } else {
5520 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005521 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5522 }
5523 } else if (source.IsDoubleStackSlot()) {
5524 if (destination.IsFpuRegister()) {
5525 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5526 } else {
5527 DCHECK(destination.IsDoubleStackSlot()) << destination;
5528 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005529 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005530 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005531 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005532 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005533 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005534 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005535 if (value == 0) {
5536 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5537 } else {
5538 __ movl(destination.AsRegister<Register>(), Immediate(value));
5539 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005540 } else {
5541 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005542 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005543 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005544 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005545 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005546 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005547 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005548 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005549 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5550 if (value == 0) {
5551 // Easy handling of 0.0.
5552 __ xorps(dest, dest);
5553 } else {
5554 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005555 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5556 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5557 __ movl(temp, Immediate(value));
5558 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005559 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005560 } else {
5561 DCHECK(destination.IsStackSlot()) << destination;
5562 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5563 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005564 } else if (constant->IsLongConstant()) {
5565 int64_t value = constant->AsLongConstant()->GetValue();
5566 int32_t low_value = Low32Bits(value);
5567 int32_t high_value = High32Bits(value);
5568 Immediate low(low_value);
5569 Immediate high(high_value);
5570 if (destination.IsDoubleStackSlot()) {
5571 __ movl(Address(ESP, destination.GetStackIndex()), low);
5572 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5573 } else {
5574 __ movl(destination.AsRegisterPairLow<Register>(), low);
5575 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5576 }
5577 } else {
5578 DCHECK(constant->IsDoubleConstant());
5579 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005580 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005581 int32_t low_value = Low32Bits(value);
5582 int32_t high_value = High32Bits(value);
5583 Immediate low(low_value);
5584 Immediate high(high_value);
5585 if (destination.IsFpuRegister()) {
5586 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5587 if (value == 0) {
5588 // Easy handling of 0.0.
5589 __ xorpd(dest, dest);
5590 } else {
5591 __ pushl(high);
5592 __ pushl(low);
5593 __ movsd(dest, Address(ESP, 0));
5594 __ addl(ESP, Immediate(8));
5595 }
5596 } else {
5597 DCHECK(destination.IsDoubleStackSlot()) << destination;
5598 __ movl(Address(ESP, destination.GetStackIndex()), low);
5599 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5600 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005601 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005602 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005603 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005604 }
5605}
5606
Mark Mendella5c19ce2015-04-01 12:51:05 -04005607void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005608 Register suggested_scratch = reg == EAX ? EBX : EAX;
5609 ScratchRegisterScope ensure_scratch(
5610 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5611
5612 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5613 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5614 __ movl(Address(ESP, mem + stack_offset), reg);
5615 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005616}
5617
Mark Mendell7c8d0092015-01-26 11:21:33 -05005618void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005619 ScratchRegisterScope ensure_scratch(
5620 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5621
5622 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5623 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5624 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5625 __ movss(Address(ESP, mem + stack_offset), reg);
5626 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005627}
5628
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005629void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005630 ScratchRegisterScope ensure_scratch1(
5631 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005632
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005633 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5634 ScratchRegisterScope ensure_scratch2(
5635 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005636
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005637 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5638 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5639 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5640 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5641 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5642 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005643}
5644
5645void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005646 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005647 Location source = move->GetSource();
5648 Location destination = move->GetDestination();
5649
5650 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005651 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5652 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5653 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5654 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5655 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005656 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005657 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005658 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005659 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005660 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5661 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005662 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5663 // Use XOR Swap algorithm to avoid a temporary.
5664 DCHECK_NE(source.reg(), destination.reg());
5665 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5666 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5667 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5668 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5669 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5670 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5671 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005672 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5673 // Take advantage of the 16 bytes in the XMM register.
5674 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5675 Address stack(ESP, destination.GetStackIndex());
5676 // Load the double into the high doubleword.
5677 __ movhpd(reg, stack);
5678
5679 // Store the low double into the destination.
5680 __ movsd(stack, reg);
5681
5682 // Move the high double to the low double.
5683 __ psrldq(reg, Immediate(8));
5684 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5685 // Take advantage of the 16 bytes in the XMM register.
5686 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5687 Address stack(ESP, source.GetStackIndex());
5688 // Load the double into the high doubleword.
5689 __ movhpd(reg, stack);
5690
5691 // Store the low double into the destination.
5692 __ movsd(stack, reg);
5693
5694 // Move the high double to the low double.
5695 __ psrldq(reg, Immediate(8));
5696 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5697 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5698 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005699 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005700 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005701 }
5702}
5703
5704void ParallelMoveResolverX86::SpillScratch(int reg) {
5705 __ pushl(static_cast<Register>(reg));
5706}
5707
5708void ParallelMoveResolverX86::RestoreScratch(int reg) {
5709 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005710}
5711
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005712void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005713 InvokeRuntimeCallingConvention calling_convention;
5714 CodeGenerator::CreateLoadClassLocationSummary(
5715 cls,
5716 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005717 Location::RegisterLocation(EAX),
5718 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005719}
5720
5721void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005722 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005723 if (cls->NeedsAccessCheck()) {
5724 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5725 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5726 cls,
5727 cls->GetDexPc(),
5728 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005729 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005730 return;
5731 }
5732
Roland Levillain0d5a2812015-11-13 10:07:31 +00005733 Location out_loc = locations->Out();
5734 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005735 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005736
Calin Juravle580b6092015-10-06 17:35:58 +01005737 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005738 DCHECK(!cls->CanCallRuntime());
5739 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005740 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5741 GenerateGcRootFieldLoad(
5742 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005743 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005744 // /* GcRoot<mirror::Class>[] */ out =
5745 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5746 __ movl(out, Address(current_method,
5747 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005748 // /* GcRoot<mirror::Class> */ out = out[type_index]
5749 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005750
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005751 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5752 DCHECK(cls->CanCallRuntime());
5753 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5754 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5755 codegen_->AddSlowPath(slow_path);
5756
5757 if (!cls->IsInDexCache()) {
5758 __ testl(out, out);
5759 __ j(kEqual, slow_path->GetEntryLabel());
5760 }
5761
5762 if (cls->MustGenerateClinitCheck()) {
5763 GenerateClassInitializationCheck(slow_path, out);
5764 } else {
5765 __ Bind(slow_path->GetExitLabel());
5766 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005767 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005768 }
5769}
5770
5771void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5772 LocationSummary* locations =
5773 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5774 locations->SetInAt(0, Location::RequiresRegister());
5775 if (check->HasUses()) {
5776 locations->SetOut(Location::SameAsFirstInput());
5777 }
5778}
5779
5780void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005781 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005782 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005783 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005784 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005785 GenerateClassInitializationCheck(slow_path,
5786 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005787}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005788
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005789void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005790 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005791 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5792 Immediate(mirror::Class::kStatusInitialized));
5793 __ j(kLess, slow_path->GetEntryLabel());
5794 __ Bind(slow_path->GetExitLabel());
5795 // No need for memory fence, thanks to the X86 memory model.
5796}
5797
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005798void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005799 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5800 ? LocationSummary::kCallOnSlowPath
5801 : LocationSummary::kNoCall;
5802 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005803 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005804 locations->SetOut(Location::RequiresRegister());
5805}
5806
5807void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005808 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809 Location out_loc = locations->Out();
5810 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005811 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005812
Roland Levillain7c1559a2015-12-15 10:55:36 +00005813 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5814 GenerateGcRootFieldLoad(
5815 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005817 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005818 // /* GcRoot<mirror::String> */ out = out[string_index]
5819 GenerateGcRootFieldLoad(
5820 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005821
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005822 if (!load->IsInDexCache()) {
5823 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5824 codegen_->AddSlowPath(slow_path);
5825 __ testl(out, out);
5826 __ j(kEqual, slow_path->GetEntryLabel());
5827 __ Bind(slow_path->GetExitLabel());
5828 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005829}
5830
David Brazdilcb1c0552015-08-04 16:22:25 +01005831static Address GetExceptionTlsAddress() {
5832 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5833}
5834
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005835void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5836 LocationSummary* locations =
5837 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5838 locations->SetOut(Location::RequiresRegister());
5839}
5840
5841void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005842 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5843}
5844
5845void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5846 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5847}
5848
5849void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5850 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005851}
5852
5853void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5854 LocationSummary* locations =
5855 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5856 InvokeRuntimeCallingConvention calling_convention;
5857 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5858}
5859
5860void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005861 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5862 instruction,
5863 instruction->GetDexPc(),
5864 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005865 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005866}
5867
Roland Levillain7c1559a2015-12-15 10:55:36 +00005868static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5869 return kEmitCompilerReadBarrier &&
5870 (kUseBakerReadBarrier ||
5871 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5872 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5873 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5874}
5875
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005876void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005877 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005878 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5879 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005880 case TypeCheckKind::kExactCheck:
5881 case TypeCheckKind::kAbstractClassCheck:
5882 case TypeCheckKind::kClassHierarchyCheck:
5883 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005884 call_kind =
5885 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005886 break;
5887 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005888 case TypeCheckKind::kUnresolvedCheck:
5889 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005890 call_kind = LocationSummary::kCallOnSlowPath;
5891 break;
5892 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005894 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005895 locations->SetInAt(0, Location::RequiresRegister());
5896 locations->SetInAt(1, Location::Any());
5897 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5898 locations->SetOut(Location::RequiresRegister());
5899 // When read barriers are enabled, we need a temporary register for
5900 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005901 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005903 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005904}
5905
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005906void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005907 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005908 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909 Location obj_loc = locations->InAt(0);
5910 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005911 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005912 Location out_loc = locations->Out();
5913 Register out = out_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005914 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5915 locations->GetTemp(0) :
5916 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005917 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5919 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5920 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005921 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005922 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005923
5924 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005925 // Avoid null check if we know obj is not null.
5926 if (instruction->MustDoNullCheck()) {
5927 __ testl(obj, obj);
5928 __ j(kEqual, &zero);
5929 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930
Roland Levillain0d5a2812015-11-13 10:07:31 +00005931 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005932 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005933
Roland Levillain7c1559a2015-12-15 10:55:36 +00005934 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005935 case TypeCheckKind::kExactCheck: {
5936 if (cls.IsRegister()) {
5937 __ cmpl(out, cls.AsRegister<Register>());
5938 } else {
5939 DCHECK(cls.IsStackSlot()) << cls;
5940 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5941 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005942
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 // Classes must be equal for the instanceof to succeed.
5944 __ j(kNotEqual, &zero);
5945 __ movl(out, Immediate(1));
5946 __ jmp(&done);
5947 break;
5948 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005949
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 case TypeCheckKind::kAbstractClassCheck: {
5951 // If the class is abstract, we eagerly fetch the super class of the
5952 // object to avoid doing a comparison we know will fail.
5953 NearLabel loop;
5954 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005955 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005956 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 __ testl(out, out);
5958 // If `out` is null, we use it for the result, and jump to `done`.
5959 __ j(kEqual, &done);
5960 if (cls.IsRegister()) {
5961 __ cmpl(out, cls.AsRegister<Register>());
5962 } else {
5963 DCHECK(cls.IsStackSlot()) << cls;
5964 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5965 }
5966 __ j(kNotEqual, &loop);
5967 __ movl(out, Immediate(1));
5968 if (zero.IsLinked()) {
5969 __ jmp(&done);
5970 }
5971 break;
5972 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005973
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005974 case TypeCheckKind::kClassHierarchyCheck: {
5975 // Walk over the class hierarchy to find a match.
5976 NearLabel loop, success;
5977 __ Bind(&loop);
5978 if (cls.IsRegister()) {
5979 __ cmpl(out, cls.AsRegister<Register>());
5980 } else {
5981 DCHECK(cls.IsStackSlot()) << cls;
5982 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5983 }
5984 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005985 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005986 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 __ testl(out, out);
5988 __ j(kNotEqual, &loop);
5989 // If `out` is null, we use it for the result, and jump to `done`.
5990 __ jmp(&done);
5991 __ Bind(&success);
5992 __ movl(out, Immediate(1));
5993 if (zero.IsLinked()) {
5994 __ jmp(&done);
5995 }
5996 break;
5997 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006000 // Do an exact check.
6001 NearLabel exact_check;
6002 if (cls.IsRegister()) {
6003 __ cmpl(out, cls.AsRegister<Register>());
6004 } else {
6005 DCHECK(cls.IsStackSlot()) << cls;
6006 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6007 }
6008 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006011 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006012 __ testl(out, out);
6013 // If `out` is null, we use it for the result, and jump to `done`.
6014 __ j(kEqual, &done);
6015 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6016 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006017 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006018 __ movl(out, Immediate(1));
6019 __ jmp(&done);
6020 break;
6021 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006022
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006023 case TypeCheckKind::kArrayCheck: {
6024 if (cls.IsRegister()) {
6025 __ cmpl(out, cls.AsRegister<Register>());
6026 } else {
6027 DCHECK(cls.IsStackSlot()) << cls;
6028 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6029 }
6030 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6032 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 codegen_->AddSlowPath(slow_path);
6034 __ j(kNotEqual, slow_path->GetEntryLabel());
6035 __ movl(out, Immediate(1));
6036 if (zero.IsLinked()) {
6037 __ jmp(&done);
6038 }
6039 break;
6040 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006041
Calin Juravle98893e12015-10-02 21:05:03 +01006042 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006043 case TypeCheckKind::kInterfaceCheck: {
6044 // Note that we indeed only call on slow path, but we always go
6045 // into the slow path for the unresolved & interface check
6046 // cases.
6047 //
6048 // We cannot directly call the InstanceofNonTrivial runtime
6049 // entry point without resorting to a type checking slow path
6050 // here (i.e. by calling InvokeRuntime directly), as it would
6051 // require to assign fixed registers for the inputs of this
6052 // HInstanceOf instruction (following the runtime calling
6053 // convention), which might be cluttered by the potential first
6054 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006055 //
6056 // TODO: Introduce a new runtime entry point taking the object
6057 // to test (instead of its class) as argument, and let it deal
6058 // with the read barrier issues. This will let us refactor this
6059 // case of the `switch` code as it was previously (with a direct
6060 // call to the runtime not using a type checking slow path).
6061 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006062 DCHECK(locations->OnlyCallsOnSlowPath());
6063 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6064 /* is_fatal */ false);
6065 codegen_->AddSlowPath(slow_path);
6066 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006067 if (zero.IsLinked()) {
6068 __ jmp(&done);
6069 }
6070 break;
6071 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006072 }
6073
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006075 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006076 __ xorl(out, out);
6077 }
6078
6079 if (done.IsLinked()) {
6080 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006081 }
6082
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006083 if (slow_path != nullptr) {
6084 __ Bind(slow_path->GetExitLabel());
6085 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006086}
6087
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006088void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6090 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006091 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6092 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006093 case TypeCheckKind::kExactCheck:
6094 case TypeCheckKind::kAbstractClassCheck:
6095 case TypeCheckKind::kClassHierarchyCheck:
6096 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006097 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6098 LocationSummary::kCallOnSlowPath :
6099 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100 break;
6101 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006102 case TypeCheckKind::kUnresolvedCheck:
6103 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 call_kind = LocationSummary::kCallOnSlowPath;
6105 break;
6106 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006107 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6108 locations->SetInAt(0, Location::RequiresRegister());
6109 locations->SetInAt(1, Location::Any());
6110 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6111 locations->AddTemp(Location::RequiresRegister());
6112 // When read barriers are enabled, we need an additional temporary
6113 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006114 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006115 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006116 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006117}
6118
6119void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006120 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006121 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 Location obj_loc = locations->InAt(0);
6123 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006124 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006125 Location temp_loc = locations->GetTemp(0);
6126 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006127 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
6128 locations->GetTemp(1) :
6129 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6131 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6132 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6133 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006134
Roland Levillain0d5a2812015-11-13 10:07:31 +00006135 bool is_type_check_slow_path_fatal =
6136 (type_check_kind == TypeCheckKind::kExactCheck ||
6137 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6138 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6139 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6140 !instruction->CanThrowIntoCatchBlock();
6141 SlowPathCode* type_check_slow_path =
6142 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6143 is_type_check_slow_path_fatal);
6144 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145
Roland Levillain0d5a2812015-11-13 10:07:31 +00006146 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006147 // Avoid null check if we know obj is not null.
6148 if (instruction->MustDoNullCheck()) {
6149 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006151 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006154 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155
Roland Levillain0d5a2812015-11-13 10:07:31 +00006156 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 case TypeCheckKind::kExactCheck:
6158 case TypeCheckKind::kArrayCheck: {
6159 if (cls.IsRegister()) {
6160 __ cmpl(temp, cls.AsRegister<Register>());
6161 } else {
6162 DCHECK(cls.IsStackSlot()) << cls;
6163 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6164 }
6165 // Jump to slow path for throwing the exception or doing a
6166 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006167 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 break;
6169 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006170
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 case TypeCheckKind::kAbstractClassCheck: {
6172 // If the class is abstract, we eagerly fetch the super class of the
6173 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006174 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006175 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006176 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006177 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178
6179 // If the class reference currently in `temp` is not null, jump
6180 // to the `compare_classes` label to compare it with the checked
6181 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183 __ j(kNotEqual, &compare_classes);
6184 // Otherwise, jump to the slow path to throw the exception.
6185 //
6186 // But before, move back the object's class into `temp` before
6187 // going into the slow path, as it has been overwritten in the
6188 // meantime.
6189 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006190 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191 __ jmp(type_check_slow_path->GetEntryLabel());
6192
6193 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006194 if (cls.IsRegister()) {
6195 __ cmpl(temp, cls.AsRegister<Register>());
6196 } else {
6197 DCHECK(cls.IsStackSlot()) << cls;
6198 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6199 }
6200 __ j(kNotEqual, &loop);
6201 break;
6202 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006203
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006204 case TypeCheckKind::kClassHierarchyCheck: {
6205 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006206 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006207 __ Bind(&loop);
6208 if (cls.IsRegister()) {
6209 __ cmpl(temp, cls.AsRegister<Register>());
6210 } else {
6211 DCHECK(cls.IsStackSlot()) << cls;
6212 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6213 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006214 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006215
Roland Levillain0d5a2812015-11-13 10:07:31 +00006216 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006217 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218
6219 // If the class reference currently in `temp` is not null, jump
6220 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 __ testl(temp, temp);
6222 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006223 // Otherwise, jump to the slow path to throw the exception.
6224 //
6225 // But before, move back the object's class into `temp` before
6226 // going into the slow path, as it has been overwritten in the
6227 // meantime.
6228 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006229 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006231 break;
6232 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006233
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006234 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006235 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006236 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006237 if (cls.IsRegister()) {
6238 __ cmpl(temp, cls.AsRegister<Register>());
6239 } else {
6240 DCHECK(cls.IsStackSlot()) << cls;
6241 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6242 }
6243 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244
6245 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006246 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006247 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248
6249 // If the component type is not null (i.e. the object is indeed
6250 // an array), jump to label `check_non_primitive_component_type`
6251 // to further check that this component type is not a primitive
6252 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006254 __ j(kNotEqual, &check_non_primitive_component_type);
6255 // Otherwise, jump to the slow path to throw the exception.
6256 //
6257 // But before, move back the object's class into `temp` before
6258 // going into the slow path, as it has been overwritten in the
6259 // meantime.
6260 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006261 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 __ jmp(type_check_slow_path->GetEntryLabel());
6263
6264 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006266 __ j(kEqual, &done);
6267 // Same comment as above regarding `temp` and the slow path.
6268 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006269 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006270 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006271 break;
6272 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006273
Calin Juravle98893e12015-10-02 21:05:03 +01006274 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006275 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276 // We always go into the type check slow path for the unresolved &
6277 // interface check cases.
6278 //
6279 // We cannot directly call the CheckCast runtime entry point
6280 // without resorting to a type checking slow path here (i.e. by
6281 // calling InvokeRuntime directly), as it would require to
6282 // assign fixed registers for the inputs of this HInstanceOf
6283 // instruction (following the runtime calling convention), which
6284 // might be cluttered by the potential first read barrier
6285 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006286 //
6287 // TODO: Introduce a new runtime entry point taking the object
6288 // to test (instead of its class) as argument, and let it deal
6289 // with the read barrier issues. This will let us refactor this
6290 // case of the `switch` code as it was previously (with a direct
6291 // call to the runtime not using a type checking slow path).
6292 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006293 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006294 break;
6295 }
6296 __ Bind(&done);
6297
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006299}
6300
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006301void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6302 LocationSummary* locations =
6303 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6304 InvokeRuntimeCallingConvention calling_convention;
6305 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6306}
6307
6308void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006309 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6310 : QUICK_ENTRY_POINT(pUnlockObject),
6311 instruction,
6312 instruction->GetDexPc(),
6313 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006314 if (instruction->IsEnter()) {
6315 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6316 } else {
6317 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6318 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006319}
6320
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006321void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6322void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6323void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6324
6325void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6326 LocationSummary* locations =
6327 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6328 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6329 || instruction->GetResultType() == Primitive::kPrimLong);
6330 locations->SetInAt(0, Location::RequiresRegister());
6331 locations->SetInAt(1, Location::Any());
6332 locations->SetOut(Location::SameAsFirstInput());
6333}
6334
6335void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6336 HandleBitwiseOperation(instruction);
6337}
6338
6339void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6340 HandleBitwiseOperation(instruction);
6341}
6342
6343void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6344 HandleBitwiseOperation(instruction);
6345}
6346
6347void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6348 LocationSummary* locations = instruction->GetLocations();
6349 Location first = locations->InAt(0);
6350 Location second = locations->InAt(1);
6351 DCHECK(first.Equals(locations->Out()));
6352
6353 if (instruction->GetResultType() == Primitive::kPrimInt) {
6354 if (second.IsRegister()) {
6355 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006356 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006357 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006358 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006359 } else {
6360 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006361 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006362 }
6363 } else if (second.IsConstant()) {
6364 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006365 __ andl(first.AsRegister<Register>(),
6366 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006367 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006368 __ orl(first.AsRegister<Register>(),
6369 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 } else {
6371 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006372 __ xorl(first.AsRegister<Register>(),
6373 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006374 }
6375 } else {
6376 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006377 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006378 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006379 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006380 } else {
6381 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006382 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006383 }
6384 }
6385 } else {
6386 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6387 if (second.IsRegisterPair()) {
6388 if (instruction->IsAnd()) {
6389 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6390 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6391 } else if (instruction->IsOr()) {
6392 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6393 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6394 } else {
6395 DCHECK(instruction->IsXor());
6396 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6397 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6398 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006399 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006400 if (instruction->IsAnd()) {
6401 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6402 __ andl(first.AsRegisterPairHigh<Register>(),
6403 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6404 } else if (instruction->IsOr()) {
6405 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6406 __ orl(first.AsRegisterPairHigh<Register>(),
6407 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6408 } else {
6409 DCHECK(instruction->IsXor());
6410 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6411 __ xorl(first.AsRegisterPairHigh<Register>(),
6412 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6413 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006414 } else {
6415 DCHECK(second.IsConstant()) << second;
6416 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006417 int32_t low_value = Low32Bits(value);
6418 int32_t high_value = High32Bits(value);
6419 Immediate low(low_value);
6420 Immediate high(high_value);
6421 Register first_low = first.AsRegisterPairLow<Register>();
6422 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006423 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006424 if (low_value == 0) {
6425 __ xorl(first_low, first_low);
6426 } else if (low_value != -1) {
6427 __ andl(first_low, low);
6428 }
6429 if (high_value == 0) {
6430 __ xorl(first_high, first_high);
6431 } else if (high_value != -1) {
6432 __ andl(first_high, high);
6433 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006434 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006435 if (low_value != 0) {
6436 __ orl(first_low, low);
6437 }
6438 if (high_value != 0) {
6439 __ orl(first_high, high);
6440 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006441 } else {
6442 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006443 if (low_value != 0) {
6444 __ xorl(first_low, low);
6445 }
6446 if (high_value != 0) {
6447 __ xorl(first_high, high);
6448 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006449 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006450 }
6451 }
6452}
6453
Roland Levillain7c1559a2015-12-15 10:55:36 +00006454void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6455 Location out,
6456 uint32_t offset,
6457 Location temp) {
6458 Register out_reg = out.AsRegister<Register>();
6459 if (kEmitCompilerReadBarrier) {
6460 if (kUseBakerReadBarrier) {
6461 // Load with fast path based Baker's read barrier.
6462 // /* HeapReference<Object> */ out = *(out + offset)
6463 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6464 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6465 } else {
6466 // Load with slow path based read barrier.
6467 // Save the value of `out` into `temp` before overwriting it
6468 // in the following move operation, as we will need it for the
6469 // read barrier below.
6470 __ movl(temp.AsRegister<Register>(), out_reg);
6471 // /* HeapReference<Object> */ out = *(out + offset)
6472 __ movl(out_reg, Address(out_reg, offset));
6473 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6474 }
6475 } else {
6476 // Plain load with no read barrier.
6477 // /* HeapReference<Object> */ out = *(out + offset)
6478 __ movl(out_reg, Address(out_reg, offset));
6479 __ MaybeUnpoisonHeapReference(out_reg);
6480 }
6481}
6482
6483void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6484 Location out,
6485 Location obj,
6486 uint32_t offset,
6487 Location temp) {
6488 Register out_reg = out.AsRegister<Register>();
6489 Register obj_reg = obj.AsRegister<Register>();
6490 if (kEmitCompilerReadBarrier) {
6491 if (kUseBakerReadBarrier) {
6492 // Load with fast path based Baker's read barrier.
6493 // /* HeapReference<Object> */ out = *(obj + offset)
6494 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6495 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6496 } else {
6497 // Load with slow path based read barrier.
6498 // /* HeapReference<Object> */ out = *(obj + offset)
6499 __ movl(out_reg, Address(obj_reg, offset));
6500 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6501 }
6502 } else {
6503 // Plain load with no read barrier.
6504 // /* HeapReference<Object> */ out = *(obj + offset)
6505 __ movl(out_reg, Address(obj_reg, offset));
6506 __ MaybeUnpoisonHeapReference(out_reg);
6507 }
6508}
6509
6510void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6511 Location root,
6512 Register obj,
6513 uint32_t offset) {
6514 Register root_reg = root.AsRegister<Register>();
6515 if (kEmitCompilerReadBarrier) {
6516 if (kUseBakerReadBarrier) {
6517 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6518 // Baker's read barrier are used:
6519 //
6520 // root = obj.field;
6521 // if (Thread::Current()->GetIsGcMarking()) {
6522 // root = ReadBarrier::Mark(root)
6523 // }
6524
6525 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6526 __ movl(root_reg, Address(obj, offset));
6527 static_assert(
6528 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6529 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6530 "have different sizes.");
6531 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6532 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6533 "have different sizes.");
6534
6535 // Slow path used to mark the GC root `root`.
6536 SlowPathCode* slow_path =
6537 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6538 codegen_->AddSlowPath(slow_path);
6539
6540 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6541 Immediate(0));
6542 __ j(kNotEqual, slow_path->GetEntryLabel());
6543 __ Bind(slow_path->GetExitLabel());
6544 } else {
6545 // GC root loaded through a slow path for read barriers other
6546 // than Baker's.
6547 // /* GcRoot<mirror::Object>* */ root = obj + offset
6548 __ leal(root_reg, Address(obj, offset));
6549 // /* mirror::Object* */ root = root->Read()
6550 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6551 }
6552 } else {
6553 // Plain GC root load with no read barrier.
6554 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6555 __ movl(root_reg, Address(obj, offset));
6556 }
6557}
6558
6559void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6560 Location ref,
6561 Register obj,
6562 uint32_t offset,
6563 Location temp,
6564 bool needs_null_check) {
6565 DCHECK(kEmitCompilerReadBarrier);
6566 DCHECK(kUseBakerReadBarrier);
6567
6568 // /* HeapReference<Object> */ ref = *(obj + offset)
6569 Address src(obj, offset);
6570 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6571}
6572
6573void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6574 Location ref,
6575 Register obj,
6576 uint32_t data_offset,
6577 Location index,
6578 Location temp,
6579 bool needs_null_check) {
6580 DCHECK(kEmitCompilerReadBarrier);
6581 DCHECK(kUseBakerReadBarrier);
6582
6583 // /* HeapReference<Object> */ ref =
6584 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6585 Address src = index.IsConstant() ?
6586 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6587 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6588 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6589}
6590
6591void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6592 Location ref,
6593 Register obj,
6594 const Address& src,
6595 Location temp,
6596 bool needs_null_check) {
6597 DCHECK(kEmitCompilerReadBarrier);
6598 DCHECK(kUseBakerReadBarrier);
6599
6600 // In slow path based read barriers, the read barrier call is
6601 // inserted after the original load. However, in fast path based
6602 // Baker's read barriers, we need to perform the load of
6603 // mirror::Object::monitor_ *before* the original reference load.
6604 // This load-load ordering is required by the read barrier.
6605 // The fast path/slow path (for Baker's algorithm) should look like:
6606 //
6607 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6608 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6609 // HeapReference<Object> ref = *src; // Original reference load.
6610 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6611 // if (is_gray) {
6612 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6613 // }
6614 //
6615 // Note: the original implementation in ReadBarrier::Barrier is
6616 // slightly more complex as:
6617 // - it implements the load-load fence using a data dependency on
6618 // the high-bits of rb_state, which are expected to be all zeroes;
6619 // - it performs additional checks that we do not do here for
6620 // performance reasons.
6621
6622 Register ref_reg = ref.AsRegister<Register>();
6623 Register temp_reg = temp.AsRegister<Register>();
6624 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6625
6626 // /* int32_t */ monitor = obj->monitor_
6627 __ movl(temp_reg, Address(obj, monitor_offset));
6628 if (needs_null_check) {
6629 MaybeRecordImplicitNullCheck(instruction);
6630 }
6631 // /* LockWord */ lock_word = LockWord(monitor)
6632 static_assert(sizeof(LockWord) == sizeof(int32_t),
6633 "art::LockWord and int32_t have different sizes.");
6634 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6635 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6636 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6637 static_assert(
6638 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6639 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6640
6641 // Load fence to prevent load-load reordering.
6642 // Note that this is a no-op, thanks to the x86 memory model.
6643 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6644
6645 // The actual reference load.
6646 // /* HeapReference<Object> */ ref = *src
6647 __ movl(ref_reg, src);
6648
6649 // Object* ref = ref_addr->AsMirrorPtr()
6650 __ MaybeUnpoisonHeapReference(ref_reg);
6651
6652 // Slow path used to mark the object `ref` when it is gray.
6653 SlowPathCode* slow_path =
6654 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6655 AddSlowPath(slow_path);
6656
6657 // if (rb_state == ReadBarrier::gray_ptr_)
6658 // ref = ReadBarrier::Mark(ref);
6659 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6660 __ j(kEqual, slow_path->GetEntryLabel());
6661 __ Bind(slow_path->GetExitLabel());
6662}
6663
6664void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6665 Location out,
6666 Location ref,
6667 Location obj,
6668 uint32_t offset,
6669 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006670 DCHECK(kEmitCompilerReadBarrier);
6671
Roland Levillain7c1559a2015-12-15 10:55:36 +00006672 // Insert a slow path based read barrier *after* the reference load.
6673 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006674 // If heap poisoning is enabled, the unpoisoning of the loaded
6675 // reference will be carried out by the runtime within the slow
6676 // path.
6677 //
6678 // Note that `ref` currently does not get unpoisoned (when heap
6679 // poisoning is enabled), which is alright as the `ref` argument is
6680 // not used by the artReadBarrierSlow entry point.
6681 //
6682 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6683 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6684 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6685 AddSlowPath(slow_path);
6686
Roland Levillain0d5a2812015-11-13 10:07:31 +00006687 __ jmp(slow_path->GetEntryLabel());
6688 __ Bind(slow_path->GetExitLabel());
6689}
6690
Roland Levillain7c1559a2015-12-15 10:55:36 +00006691void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6692 Location out,
6693 Location ref,
6694 Location obj,
6695 uint32_t offset,
6696 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006698 // Baker's read barriers shall be handled by the fast path
6699 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6700 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006701 // If heap poisoning is enabled, unpoisoning will be taken care of
6702 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006703 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 } else if (kPoisonHeapReferences) {
6705 __ UnpoisonHeapReference(out.AsRegister<Register>());
6706 }
6707}
6708
Roland Levillain7c1559a2015-12-15 10:55:36 +00006709void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6710 Location out,
6711 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 DCHECK(kEmitCompilerReadBarrier);
6713
Roland Levillain7c1559a2015-12-15 10:55:36 +00006714 // Insert a slow path based read barrier *after* the GC root load.
6715 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 // Note that GC roots are not affected by heap poisoning, so we do
6717 // not need to do anything special for this here.
6718 SlowPathCode* slow_path =
6719 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6720 AddSlowPath(slow_path);
6721
Roland Levillain0d5a2812015-11-13 10:07:31 +00006722 __ jmp(slow_path->GetEntryLabel());
6723 __ Bind(slow_path->GetExitLabel());
6724}
6725
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006726void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006727 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006728 LOG(FATAL) << "Unreachable";
6729}
6730
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006731void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006732 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006733 LOG(FATAL) << "Unreachable";
6734}
6735
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006736void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6737 DCHECK(codegen_->IsBaseline());
6738 LocationSummary* locations =
6739 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6740 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6741}
6742
6743void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6744 DCHECK(codegen_->IsBaseline());
6745 // Will be generated at use site.
6746}
6747
Mark Mendellfe57faa2015-09-18 09:26:15 -04006748// Simple implementation of packed switch - generate cascaded compare/jumps.
6749void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6750 LocationSummary* locations =
6751 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6752 locations->SetInAt(0, Location::RequiresRegister());
6753}
6754
Nicolas Geoffrayb4c13762015-12-16 12:06:39 +00006755void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6756 int32_t lower_bound = switch_instr->GetStartValue();
6757 int32_t num_entries = switch_instr->GetNumEntries();
6758 LocationSummary* locations = switch_instr->GetLocations();
6759 Register value_reg = locations->InAt(0).AsRegister<Register>();
6760 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
Mark Mendellfe57faa2015-09-18 09:26:15 -04006761
Nicolas Geoffrayb4c13762015-12-16 12:06:39 +00006762 // Create a series of compare/jumps.
6763 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6764 for (int i = 0; i < num_entries; i++) {
6765 int32_t case_value = lower_bound + i;
6766 if (case_value == 0) {
6767 __ testl(value_reg, value_reg);
6768 } else {
6769 __ cmpl(value_reg, Immediate(case_value));
6770 }
6771 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006772 }
6773
6774 // And the default for any other value.
Nicolas Geoffrayb4c13762015-12-16 12:06:39 +00006775 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6776 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006777 }
6778}
6779
Mark Mendell805b3b52015-09-18 14:10:29 -04006780void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6781 LocationSummary* locations =
6782 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6783 locations->SetInAt(0, Location::RequiresRegister());
6784
6785 // Constant area pointer.
6786 locations->SetInAt(1, Location::RequiresRegister());
6787
6788 // And the temporary we need.
6789 locations->AddTemp(Location::RequiresRegister());
6790}
6791
6792void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6793 int32_t lower_bound = switch_instr->GetStartValue();
Nicolas Geoffrayb4c13762015-12-16 12:06:39 +00006794 int32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006795 LocationSummary* locations = switch_instr->GetLocations();
6796 Register value_reg = locations->InAt(0).AsRegister<Register>();
6797 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6798
6799 // Optimizing has a jump area.
6800 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6801 Register constant_area = locations->InAt(1).AsRegister<Register>();
6802
6803 // Remove the bias, if needed.
6804 if (lower_bound != 0) {
6805 __ leal(temp_reg, Address(value_reg, -lower_bound));
6806 value_reg = temp_reg;
6807 }
6808
6809 // Is the value in range?
Nicolas Geoffrayb4c13762015-12-16 12:06:39 +00006810 DCHECK_GE(num_entries, 1);
Mark Mendell805b3b52015-09-18 14:10:29 -04006811 __ cmpl(value_reg, Immediate(num_entries - 1));
6812 __ j(kAbove, codegen_->GetLabelOf(default_block));
6813
6814 // We are in the range of the table.
6815 // Load (target-constant_area) from the jump table, indexing by the value.
6816 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6817
6818 // Compute the actual target address by adding in constant_area.
6819 __ addl(temp_reg, constant_area);
6820
6821 // And jump.
6822 __ jmp(temp_reg);
6823}
6824
Mark Mendell0616ae02015-04-17 12:49:27 -04006825void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6826 HX86ComputeBaseMethodAddress* insn) {
6827 LocationSummary* locations =
6828 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6829 locations->SetOut(Location::RequiresRegister());
6830}
6831
6832void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6833 HX86ComputeBaseMethodAddress* insn) {
6834 LocationSummary* locations = insn->GetLocations();
6835 Register reg = locations->Out().AsRegister<Register>();
6836
6837 // Generate call to next instruction.
6838 Label next_instruction;
6839 __ call(&next_instruction);
6840 __ Bind(&next_instruction);
6841
6842 // Remember this offset for later use with constant area.
6843 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6844
6845 // Grab the return address off the stack.
6846 __ popl(reg);
6847}
6848
6849void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6850 HX86LoadFromConstantTable* insn) {
6851 LocationSummary* locations =
6852 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6853
6854 locations->SetInAt(0, Location::RequiresRegister());
6855 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6856
6857 // If we don't need to be materialized, we only need the inputs to be set.
6858 if (!insn->NeedsMaterialization()) {
6859 return;
6860 }
6861
6862 switch (insn->GetType()) {
6863 case Primitive::kPrimFloat:
6864 case Primitive::kPrimDouble:
6865 locations->SetOut(Location::RequiresFpuRegister());
6866 break;
6867
6868 case Primitive::kPrimInt:
6869 locations->SetOut(Location::RequiresRegister());
6870 break;
6871
6872 default:
6873 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6874 }
6875}
6876
6877void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6878 if (!insn->NeedsMaterialization()) {
6879 return;
6880 }
6881
6882 LocationSummary* locations = insn->GetLocations();
6883 Location out = locations->Out();
6884 Register const_area = locations->InAt(0).AsRegister<Register>();
6885 HConstant *value = insn->GetConstant();
6886
6887 switch (insn->GetType()) {
6888 case Primitive::kPrimFloat:
6889 __ movss(out.AsFpuRegister<XmmRegister>(),
6890 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6891 break;
6892
6893 case Primitive::kPrimDouble:
6894 __ movsd(out.AsFpuRegister<XmmRegister>(),
6895 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6896 break;
6897
6898 case Primitive::kPrimInt:
6899 __ movl(out.AsRegister<Register>(),
6900 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6901 break;
6902
6903 default:
6904 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6905 }
6906}
6907
Mark Mendell0616ae02015-04-17 12:49:27 -04006908/**
6909 * Class to handle late fixup of offsets into constant area.
6910 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006911class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006912 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006913 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6914 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6915
6916 protected:
6917 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6918
6919 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006920
6921 private:
6922 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6923 // Patch the correct offset for the instruction. The place to patch is the
6924 // last 4 bytes of the instruction.
6925 // The value to patch is the distance from the offset in the constant area
6926 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006927 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6928 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006929
6930 // Patch in the right value.
6931 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6932 }
6933
Mark Mendell0616ae02015-04-17 12:49:27 -04006934 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006935 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006936};
6937
Mark Mendell805b3b52015-09-18 14:10:29 -04006938/**
6939 * Class to handle late fixup of offsets to a jump table that will be created in the
6940 * constant area.
6941 */
6942class JumpTableRIPFixup : public RIPFixup {
6943 public:
6944 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6945 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6946
6947 void CreateJumpTable() {
6948 X86Assembler* assembler = codegen_->GetAssembler();
6949
6950 // Ensure that the reference to the jump table has the correct offset.
6951 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6952 SetOffset(offset_in_constant_table);
6953
6954 // The label values in the jump table are computed relative to the
6955 // instruction addressing the constant area.
6956 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6957
6958 // Populate the jump table with the correct values for the jump table.
6959 int32_t num_entries = switch_instr_->GetNumEntries();
6960 HBasicBlock* block = switch_instr_->GetBlock();
6961 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6962 // The value that we want is the target offset - the position of the table.
6963 for (int32_t i = 0; i < num_entries; i++) {
6964 HBasicBlock* b = successors[i];
6965 Label* l = codegen_->GetLabelOf(b);
6966 DCHECK(l->IsBound());
6967 int32_t offset_to_block = l->Position() - relative_offset;
6968 assembler->AppendInt32(offset_to_block);
6969 }
6970 }
6971
6972 private:
6973 const HX86PackedSwitch* switch_instr_;
6974};
6975
6976void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
6977 // Generate the constant area if needed.
6978 X86Assembler* assembler = GetAssembler();
6979 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6980 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
6981 // byte values.
6982 assembler->Align(4, 0);
6983 constant_area_start_ = assembler->CodeSize();
6984
6985 // Populate any jump tables.
6986 for (auto jump_table : fixups_to_jump_tables_) {
6987 jump_table->CreateJumpTable();
6988 }
6989
6990 // And now add the constant area to the generated code.
6991 assembler->AddConstantArea();
6992 }
6993
6994 // And finish up.
6995 CodeGenerator::Finalize(allocator);
6996}
6997
Mark Mendell0616ae02015-04-17 12:49:27 -04006998Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
6999 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7000 return Address(reg, kDummy32BitOffset, fixup);
7001}
7002
7003Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7004 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7005 return Address(reg, kDummy32BitOffset, fixup);
7006}
7007
7008Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7009 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7010 return Address(reg, kDummy32BitOffset, fixup);
7011}
7012
7013Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7014 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7015 return Address(reg, kDummy32BitOffset, fixup);
7016}
7017
Mark Mendell805b3b52015-09-18 14:10:29 -04007018Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7019 Register reg,
7020 Register value) {
7021 // Create a fixup to be used to create and address the jump table.
7022 JumpTableRIPFixup* table_fixup =
7023 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7024
7025 // We have to populate the jump tables.
7026 fixups_to_jump_tables_.push_back(table_fixup);
7027
7028 // We want a scaled address, as we are extracting the correct offset from the table.
7029 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7030}
7031
Andreas Gampe85b62f22015-09-09 13:15:38 -07007032// TODO: target as memory.
7033void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7034 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007035 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007036 return;
7037 }
7038
7039 DCHECK_NE(type, Primitive::kPrimVoid);
7040
7041 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7042 if (target.Equals(return_loc)) {
7043 return;
7044 }
7045
7046 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7047 // with the else branch.
7048 if (type == Primitive::kPrimLong) {
7049 HParallelMove parallel_move(GetGraph()->GetArena());
7050 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7051 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7052 GetMoveResolver()->EmitNativeCode(&parallel_move);
7053 } else {
7054 // Let the parallel move resolver take care of all of this.
7055 HParallelMove parallel_move(GetGraph()->GetArena());
7056 parallel_move.AddMove(return_loc, target, type, nullptr);
7057 GetMoveResolver()->EmitNativeCode(&parallel_move);
7058 }
7059}
7060
Roland Levillain4d027112015-07-01 15:41:14 +01007061#undef __
7062
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007063} // namespace x86
7064} // namespace art