blob: 2fb87d30298cf80eae0c72b92a7534467a97a988 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Vladimir Marko0f7dca42015-11-02 14:36:43 +000029#include "pc_relative_fixups_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010030#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000031#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010032#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000033#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010034#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000036namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010037
Roland Levillain0d5a2812015-11-13 10:07:31 +000038template<class MirrorType>
39class GcRoot;
40
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000041namespace x86 {
42
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010043static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010044static constexpr Register kMethodRegisterArgument = EAX;
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010045
Mark Mendell5f874182015-03-04 15:42:45 -050046static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010047
Mark Mendell24f2dfa2015-01-14 19:51:45 -050048static constexpr int kC2ConditionMask = 0x400;
49
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000050static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000051
Roland Levillain62a46b22015-06-01 18:24:13 +010052#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010053#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010054
Andreas Gampe85b62f22015-09-09 13:15:38 -070055class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010056 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010057 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010058
Alexandre Rames2ed20af2015-03-06 13:55:35 +000059 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010060 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000062 if (instruction_->CanThrowIntoCatchBlock()) {
63 // Live registers will be restored in the catch block if caught.
64 SaveLiveRegisters(codegen, instruction_->GetLocations());
65 }
Alexandre Rames8158f282015-08-07 10:26:17 +010066 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
67 instruction_,
68 instruction_->GetDexPc(),
69 this);
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 Levillain0d5a2812015-11-13 10:07:31 +0000437// Slow path generating a read barrier for a heap reference.
438class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
439 public:
440 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
441 Location out,
442 Location ref,
443 Location obj,
444 uint32_t offset,
445 Location index)
446 : instruction_(instruction),
447 out_(out),
448 ref_(ref),
449 obj_(obj),
450 offset_(offset),
451 index_(index) {
452 DCHECK(kEmitCompilerReadBarrier);
453 // If `obj` is equal to `out` or `ref`, it means the initial object
454 // has been overwritten by (or after) the heap object reference load
455 // to be instrumented, e.g.:
456 //
457 // __ movl(out, Address(out, offset));
458 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
459 //
460 // In that case, we have lost the information about the original
461 // object, and the emitted read barrier cannot work properly.
462 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
463 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
464 }
465
466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
468 LocationSummary* locations = instruction_->GetLocations();
469 Register reg_out = out_.AsRegister<Register>();
470 DCHECK(locations->CanCall());
471 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
472 DCHECK(!instruction_->IsInvoke() ||
473 (instruction_->IsInvokeStaticOrDirect() &&
474 instruction_->GetLocations()->Intrinsified()));
475
476 __ Bind(GetEntryLabel());
477 SaveLiveRegisters(codegen, locations);
478
479 // We may have to change the index's value, but as `index_` is a
480 // constant member (like other "inputs" of this slow path),
481 // introduce a copy of it, `index`.
482 Location index = index_;
483 if (index_.IsValid()) {
484 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
485 if (instruction_->IsArrayGet()) {
486 // Compute the actual memory offset and store it in `index`.
487 Register index_reg = index_.AsRegister<Register>();
488 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
489 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
490 // We are about to change the value of `index_reg` (see the
491 // calls to art::x86::X86Assembler::shll and
492 // art::x86::X86Assembler::AddImmediate below), but it has
493 // not been saved by the previous call to
494 // art::SlowPathCode::SaveLiveRegisters, as it is a
495 // callee-save register --
496 // art::SlowPathCode::SaveLiveRegisters does not consider
497 // callee-save registers, as it has been designed with the
498 // assumption that callee-save registers are supposed to be
499 // handled by the called function. So, as a callee-save
500 // register, `index_reg` _would_ eventually be saved onto
501 // the stack, but it would be too late: we would have
502 // changed its value earlier. Therefore, we manually save
503 // it here into another freely available register,
504 // `free_reg`, chosen of course among the caller-save
505 // registers (as a callee-save `free_reg` register would
506 // exhibit the same problem).
507 //
508 // Note we could have requested a temporary register from
509 // the register allocator instead; but we prefer not to, as
510 // this is a slow path, and we know we can find a
511 // caller-save register that is available.
512 Register free_reg = FindAvailableCallerSaveRegister(codegen);
513 __ movl(free_reg, index_reg);
514 index_reg = free_reg;
515 index = Location::RegisterLocation(index_reg);
516 } else {
517 // The initial register stored in `index_` has already been
518 // saved in the call to art::SlowPathCode::SaveLiveRegisters
519 // (as it is not a callee-save register), so we can freely
520 // use it.
521 }
522 // Shifting the index value contained in `index_reg` by the scale
523 // factor (2) cannot overflow in practice, as the runtime is
524 // unable to allocate object arrays with a size larger than
525 // 2^26 - 1 (that is, 2^28 - 4 bytes).
526 __ shll(index_reg, Immediate(TIMES_4));
527 static_assert(
528 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
529 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
530 __ AddImmediate(index_reg, Immediate(offset_));
531 } else {
532 DCHECK(instruction_->IsInvoke());
533 DCHECK(instruction_->GetLocations()->Intrinsified());
534 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
535 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
536 << instruction_->AsInvoke()->GetIntrinsic();
537 DCHECK_EQ(offset_, 0U);
538 DCHECK(index_.IsRegisterPair());
539 // UnsafeGet's offset location is a register pair, the low
540 // part contains the correct offset.
541 index = index_.ToLow();
542 }
543 }
544
545 // We're moving two or three locations to locations that could
546 // overlap, so we need a parallel move resolver.
547 InvokeRuntimeCallingConvention calling_convention;
548 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
549 parallel_move.AddMove(ref_,
550 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
551 Primitive::kPrimNot,
552 nullptr);
553 parallel_move.AddMove(obj_,
554 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
555 Primitive::kPrimNot,
556 nullptr);
557 if (index.IsValid()) {
558 parallel_move.AddMove(index,
559 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
560 Primitive::kPrimInt,
561 nullptr);
562 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
563 } else {
564 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
565 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
566 }
567 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
568 instruction_,
569 instruction_->GetDexPc(),
570 this);
571 CheckEntrypointTypes<
572 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
573 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
574
575 RestoreLiveRegisters(codegen, locations);
576 __ jmp(GetExitLabel());
577 }
578
579 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
580
581 private:
582 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
583 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
584 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
585 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
586 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
587 return static_cast<Register>(i);
588 }
589 }
590 // We shall never fail to find a free caller-save register, as
591 // there are more than two core caller-save registers on x86
592 // (meaning it is possible to find one which is different from
593 // `ref` and `obj`).
594 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
595 LOG(FATAL) << "Could not find a free caller-save register";
596 UNREACHABLE();
597 }
598
599 HInstruction* const instruction_;
600 const Location out_;
601 const Location ref_;
602 const Location obj_;
603 const uint32_t offset_;
604 // An additional location containing an index to an array.
605 // Only used for HArrayGet and the UnsafeGetObject &
606 // UnsafeGetObjectVolatile intrinsics.
607 const Location index_;
608
609 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
610};
611
612// Slow path generating a read barrier for a GC root.
613class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
614 public:
615 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
616 : instruction_(instruction), out_(out), root_(root) {}
617
618 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
619 LocationSummary* locations = instruction_->GetLocations();
620 Register reg_out = out_.AsRegister<Register>();
621 DCHECK(locations->CanCall());
622 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
623 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
624
625 __ Bind(GetEntryLabel());
626 SaveLiveRegisters(codegen, locations);
627
628 InvokeRuntimeCallingConvention calling_convention;
629 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
630 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
631 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
632 instruction_,
633 instruction_->GetDexPc(),
634 this);
635 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
636 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
637
638 RestoreLiveRegisters(codegen, locations);
639 __ jmp(GetExitLabel());
640 }
641
642 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
643
644 private:
645 HInstruction* const instruction_;
646 const Location out_;
647 const Location root_;
648
649 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
650};
651
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100652#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100653#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100654
Aart Bike9f37602015-10-09 11:15:55 -0700655inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700656 switch (cond) {
657 case kCondEQ: return kEqual;
658 case kCondNE: return kNotEqual;
659 case kCondLT: return kLess;
660 case kCondLE: return kLessEqual;
661 case kCondGT: return kGreater;
662 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700663 case kCondB: return kBelow;
664 case kCondBE: return kBelowEqual;
665 case kCondA: return kAbove;
666 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700667 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100668 LOG(FATAL) << "Unreachable";
669 UNREACHABLE();
670}
671
Aart Bike9f37602015-10-09 11:15:55 -0700672// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100673inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
674 switch (cond) {
675 case kCondEQ: return kEqual;
676 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700677 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100678 case kCondLT: return kBelow;
679 case kCondLE: return kBelowEqual;
680 case kCondGT: return kAbove;
681 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700682 // Unsigned remain unchanged.
683 case kCondB: return kBelow;
684 case kCondBE: return kBelowEqual;
685 case kCondA: return kAbove;
686 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100687 }
688 LOG(FATAL) << "Unreachable";
689 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700690}
691
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100692void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100693 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100694}
695
696void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100697 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100698}
699
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100700size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
701 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
702 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100703}
704
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100705size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
706 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
707 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100708}
709
Mark Mendell7c8d0092015-01-26 11:21:33 -0500710size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
711 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
712 return GetFloatingPointSpillSlotSize();
713}
714
715size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
716 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
717 return GetFloatingPointSpillSlotSize();
718}
719
Calin Juravle175dc732015-08-25 15:42:32 +0100720void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
721 HInstruction* instruction,
722 uint32_t dex_pc,
723 SlowPathCode* slow_path) {
724 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
725 instruction,
726 dex_pc,
727 slow_path);
728}
729
730void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100731 HInstruction* instruction,
732 uint32_t dex_pc,
733 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100734 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100735 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100736 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100737}
738
Mark Mendellfb8d2792015-03-31 22:16:59 -0400739CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000740 const X86InstructionSetFeatures& isa_features,
741 const CompilerOptions& compiler_options,
742 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500743 : CodeGenerator(graph,
744 kNumberOfCpuRegisters,
745 kNumberOfXmmRegisters,
746 kNumberOfRegisterPairs,
747 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
748 arraysize(kCoreCalleeSaves))
749 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100750 0,
751 compiler_options,
752 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100753 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100754 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100755 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400756 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000757 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100758 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400759 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000760 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400761 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000762 // Use a fake return address register to mimic Quick.
763 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100764}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100765
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100766Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100767 switch (type) {
768 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100769 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100770 X86ManagedRegister pair =
771 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100772 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
773 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100774 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
775 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100776 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100777 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100778 }
779
780 case Primitive::kPrimByte:
781 case Primitive::kPrimBoolean:
782 case Primitive::kPrimChar:
783 case Primitive::kPrimShort:
784 case Primitive::kPrimInt:
785 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100786 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100787 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100788 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100789 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
790 X86ManagedRegister current =
791 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
792 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100793 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100794 }
795 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100796 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100797 }
798
799 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100800 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100801 return Location::FpuRegisterLocation(
802 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100803 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100804
805 case Primitive::kPrimVoid:
806 LOG(FATAL) << "Unreachable type " << type;
807 }
808
Roland Levillain0d5a2812015-11-13 10:07:31 +0000809 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100810}
811
Mark Mendell5f874182015-03-04 15:42:45 -0500812void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100813 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100814 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100815
816 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100817 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100818
Mark Mendell5f874182015-03-04 15:42:45 -0500819 if (is_baseline) {
820 blocked_core_registers_[EBP] = true;
821 blocked_core_registers_[ESI] = true;
822 blocked_core_registers_[EDI] = true;
823 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100824
825 UpdateBlockedPairRegisters();
826}
827
828void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
829 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
830 X86ManagedRegister current =
831 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
832 if (blocked_core_registers_[current.AsRegisterPairLow()]
833 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
834 blocked_register_pairs_[i] = true;
835 }
836 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100837}
838
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100839InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
840 : HGraphVisitor(graph),
841 assembler_(codegen->GetAssembler()),
842 codegen_(codegen) {}
843
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100844static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100845 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100846}
847
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000848void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100849 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000850 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000851 bool skip_overflow_check =
852 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000853 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000854
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000855 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100856 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100857 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100858 }
859
Mark Mendell5f874182015-03-04 15:42:45 -0500860 if (HasEmptyFrame()) {
861 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000862 }
Mark Mendell5f874182015-03-04 15:42:45 -0500863
864 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
865 Register reg = kCoreCalleeSaves[i];
866 if (allocated_registers_.ContainsCoreRegister(reg)) {
867 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100868 __ cfi().AdjustCFAOffset(kX86WordSize);
869 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500870 }
871 }
872
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100873 int adjust = GetFrameSize() - FrameEntrySpillSize();
874 __ subl(ESP, Immediate(adjust));
875 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100876 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000877}
878
879void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100880 __ cfi().RememberState();
881 if (!HasEmptyFrame()) {
882 int adjust = GetFrameSize() - FrameEntrySpillSize();
883 __ addl(ESP, Immediate(adjust));
884 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500885
David Srbeckyc34dc932015-04-12 09:27:43 +0100886 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
887 Register reg = kCoreCalleeSaves[i];
888 if (allocated_registers_.ContainsCoreRegister(reg)) {
889 __ popl(reg);
890 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
891 __ cfi().Restore(DWARFReg(reg));
892 }
Mark Mendell5f874182015-03-04 15:42:45 -0500893 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000894 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100895 __ ret();
896 __ cfi().RestoreState();
897 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000898}
899
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100900void CodeGeneratorX86::Bind(HBasicBlock* block) {
901 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000902}
903
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100904Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
905 switch (load->GetType()) {
906 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100907 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100908 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100909
910 case Primitive::kPrimInt:
911 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100912 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100913 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914
915 case Primitive::kPrimBoolean:
916 case Primitive::kPrimByte:
917 case Primitive::kPrimChar:
918 case Primitive::kPrimShort:
919 case Primitive::kPrimVoid:
920 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700921 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100922 }
923
924 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700925 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100926}
927
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100928Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
929 switch (type) {
930 case Primitive::kPrimBoolean:
931 case Primitive::kPrimByte:
932 case Primitive::kPrimChar:
933 case Primitive::kPrimShort:
934 case Primitive::kPrimInt:
935 case Primitive::kPrimNot:
936 return Location::RegisterLocation(EAX);
937
938 case Primitive::kPrimLong:
939 return Location::RegisterPairLocation(EAX, EDX);
940
941 case Primitive::kPrimVoid:
942 return Location::NoLocation();
943
944 case Primitive::kPrimDouble:
945 case Primitive::kPrimFloat:
946 return Location::FpuRegisterLocation(XMM0);
947 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100948
949 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100950}
951
952Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
953 return Location::RegisterLocation(kMethodRegisterArgument);
954}
955
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100956Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100957 switch (type) {
958 case Primitive::kPrimBoolean:
959 case Primitive::kPrimByte:
960 case Primitive::kPrimChar:
961 case Primitive::kPrimShort:
962 case Primitive::kPrimInt:
963 case Primitive::kPrimNot: {
964 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000965 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100966 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100967 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000969 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100970 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100971 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100972
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000973 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974 uint32_t index = gp_index_;
975 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000976 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100977 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100978 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
979 calling_convention.GetRegisterPairAt(index));
980 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100981 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000982 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
983 }
984 }
985
986 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100987 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000988 stack_index_++;
989 if (index < calling_convention.GetNumberOfFpuRegisters()) {
990 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
991 } else {
992 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
993 }
994 }
995
996 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100997 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000998 stack_index_ += 2;
999 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1000 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1001 } else {
1002 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001003 }
1004 }
1005
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001006 case Primitive::kPrimVoid:
1007 LOG(FATAL) << "Unexpected parameter type " << type;
1008 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001009 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001010 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001011}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001012
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013void CodeGeneratorX86::Move32(Location destination, Location source) {
1014 if (source.Equals(destination)) {
1015 return;
1016 }
1017 if (destination.IsRegister()) {
1018 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001019 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001020 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001021 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022 } else {
1023 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001024 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001025 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001026 } else if (destination.IsFpuRegister()) {
1027 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001028 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001029 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001030 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 } else {
1032 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001033 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001034 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001035 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001036 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001038 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001039 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001040 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001041 } else if (source.IsConstant()) {
1042 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001043 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001044 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 } else {
1046 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001047 __ pushl(Address(ESP, source.GetStackIndex()));
1048 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 }
1050 }
1051}
1052
1053void CodeGeneratorX86::Move64(Location destination, Location source) {
1054 if (source.Equals(destination)) {
1055 return;
1056 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001057 if (destination.IsRegisterPair()) {
1058 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001059 EmitParallelMoves(
1060 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1061 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001062 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001063 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001064 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1065 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001066 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001067 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1068 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1069 __ psrlq(src_reg, Immediate(32));
1070 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001072 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1075 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1077 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001079 if (source.IsFpuRegister()) {
1080 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1081 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001082 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001083 } else if (source.IsRegisterPair()) {
1084 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1085 // Create stack space for 2 elements.
1086 __ subl(ESP, Immediate(2 * elem_size));
1087 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1088 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1089 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1090 // And remove the temporary stack space we allocated.
1091 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001092 } else {
1093 LOG(FATAL) << "Unimplemented";
1094 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001095 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001096 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001097 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001098 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001102 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001103 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001104 } else if (source.IsConstant()) {
1105 HConstant* constant = source.GetConstant();
1106 int64_t value;
1107 if (constant->IsLongConstant()) {
1108 value = constant->AsLongConstant()->GetValue();
1109 } else {
1110 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001111 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001112 }
1113 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1114 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001115 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001116 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001117 EmitParallelMoves(
1118 Location::StackSlot(source.GetStackIndex()),
1119 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001120 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001121 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001122 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1123 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001124 }
1125 }
1126}
1127
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001128void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001129 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001130 if (instruction->IsCurrentMethod()) {
1131 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1132 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001133 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001134 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001135 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001136 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1137 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001138 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001139 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001140 } else if (location.IsStackSlot()) {
1141 __ movl(Address(ESP, location.GetStackIndex()), imm);
1142 } else {
1143 DCHECK(location.IsConstant());
1144 DCHECK_EQ(location.GetConstant(), const_to_move);
1145 }
1146 } else if (const_to_move->IsLongConstant()) {
1147 int64_t value = const_to_move->AsLongConstant()->GetValue();
1148 if (location.IsRegisterPair()) {
1149 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1150 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1151 } else if (location.IsDoubleStackSlot()) {
1152 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001153 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1154 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001155 } else {
1156 DCHECK(location.IsConstant());
1157 DCHECK_EQ(location.GetConstant(), instruction);
1158 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001159 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001160 } else if (instruction->IsTemporary()) {
1161 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001162 if (temp_location.IsStackSlot()) {
1163 Move32(location, temp_location);
1164 } else {
1165 DCHECK(temp_location.IsDoubleStackSlot());
1166 Move64(location, temp_location);
1167 }
Roland Levillain476df552014-10-09 17:51:36 +01001168 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001169 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 switch (instruction->GetType()) {
1171 case Primitive::kPrimBoolean:
1172 case Primitive::kPrimByte:
1173 case Primitive::kPrimChar:
1174 case Primitive::kPrimShort:
1175 case Primitive::kPrimInt:
1176 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001177 case Primitive::kPrimFloat:
1178 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 break;
1180
1181 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001182 case Primitive::kPrimDouble:
1183 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001184 break;
1185
1186 default:
1187 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1188 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001189 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001190 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001191 switch (instruction->GetType()) {
1192 case Primitive::kPrimBoolean:
1193 case Primitive::kPrimByte:
1194 case Primitive::kPrimChar:
1195 case Primitive::kPrimShort:
1196 case Primitive::kPrimInt:
1197 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001198 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001199 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001200 break;
1201
1202 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001203 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001204 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001205 break;
1206
1207 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001208 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001209 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001210 }
1211}
1212
Calin Juravle175dc732015-08-25 15:42:32 +01001213void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1214 DCHECK(location.IsRegister());
1215 __ movl(location.AsRegister<Register>(), Immediate(value));
1216}
1217
Calin Juravlee460d1d2015-09-29 04:52:17 +01001218void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1219 if (Primitive::Is64BitType(dst_type)) {
1220 Move64(dst, src);
1221 } else {
1222 Move32(dst, src);
1223 }
1224}
1225
1226void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1227 if (location.IsRegister()) {
1228 locations->AddTemp(location);
1229 } else if (location.IsRegisterPair()) {
1230 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1231 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1232 } else {
1233 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1234 }
1235}
1236
David Brazdilfc6a86a2015-06-26 10:33:45 +00001237void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001238 DCHECK(!successor->IsExitBlock());
1239
1240 HBasicBlock* block = got->GetBlock();
1241 HInstruction* previous = got->GetPrevious();
1242
1243 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001244 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001245 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1246 return;
1247 }
1248
1249 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1250 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1251 }
1252 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001253 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001254 }
1255}
1256
David Brazdilfc6a86a2015-06-26 10:33:45 +00001257void LocationsBuilderX86::VisitGoto(HGoto* got) {
1258 got->SetLocations(nullptr);
1259}
1260
1261void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1262 HandleGoto(got, got->GetSuccessor());
1263}
1264
1265void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1266 try_boundary->SetLocations(nullptr);
1267}
1268
1269void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1270 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1271 if (!successor->IsExitBlock()) {
1272 HandleGoto(try_boundary, successor);
1273 }
1274}
1275
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001276void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001277 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001278}
1279
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001280void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001281}
1282
Mark Mendellc4701932015-04-10 13:18:51 -04001283void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1284 Label* true_label,
1285 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001286 if (cond->IsFPConditionTrueIfNaN()) {
1287 __ j(kUnordered, true_label);
1288 } else if (cond->IsFPConditionFalseIfNaN()) {
1289 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001290 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001291 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001292}
1293
1294void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1295 Label* true_label,
1296 Label* false_label) {
1297 LocationSummary* locations = cond->GetLocations();
1298 Location left = locations->InAt(0);
1299 Location right = locations->InAt(1);
1300 IfCondition if_cond = cond->GetCondition();
1301
Mark Mendellc4701932015-04-10 13:18:51 -04001302 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001303 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001304 IfCondition true_high_cond = if_cond;
1305 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001306 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001307
1308 // Set the conditions for the test, remembering that == needs to be
1309 // decided using the low words.
1310 switch (if_cond) {
1311 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001312 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001313 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001314 break;
1315 case kCondLT:
1316 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001317 break;
1318 case kCondLE:
1319 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001320 break;
1321 case kCondGT:
1322 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001323 break;
1324 case kCondGE:
1325 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001326 break;
Aart Bike9f37602015-10-09 11:15:55 -07001327 case kCondB:
1328 false_high_cond = kCondA;
1329 break;
1330 case kCondBE:
1331 true_high_cond = kCondB;
1332 break;
1333 case kCondA:
1334 false_high_cond = kCondB;
1335 break;
1336 case kCondAE:
1337 true_high_cond = kCondA;
1338 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001339 }
1340
1341 if (right.IsConstant()) {
1342 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001343 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001344 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001345
1346 if (val_high == 0) {
1347 __ testl(left_high, left_high);
1348 } else {
1349 __ cmpl(left_high, Immediate(val_high));
1350 }
1351 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001352 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001353 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001354 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001355 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001356 __ j(X86Condition(true_high_cond), true_label);
1357 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001358 }
1359 // Must be equal high, so compare the lows.
1360 if (val_low == 0) {
1361 __ testl(left_low, left_low);
1362 } else {
1363 __ cmpl(left_low, Immediate(val_low));
1364 }
1365 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001366 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001367 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001368
1369 __ cmpl(left_high, right_high);
1370 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001371 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001372 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001373 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001374 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001375 __ j(X86Condition(true_high_cond), true_label);
1376 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001377 }
1378 // Must be equal high, so compare the lows.
1379 __ cmpl(left_low, right_low);
1380 }
1381 // The last comparison might be unsigned.
1382 __ j(final_condition, true_label);
1383}
1384
David Brazdil0debae72015-11-12 18:37:00 +00001385void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
1386 Label* true_target_in,
1387 Label* false_target_in) {
1388 // Generated branching requires both targets to be explicit. If either of the
1389 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1390 Label fallthrough_target;
1391 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1392 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1393
Mark Mendellc4701932015-04-10 13:18:51 -04001394 LocationSummary* locations = condition->GetLocations();
1395 Location left = locations->InAt(0);
1396 Location right = locations->InAt(1);
1397
Mark Mendellc4701932015-04-10 13:18:51 -04001398 Primitive::Type type = condition->InputAt(0)->GetType();
1399 switch (type) {
1400 case Primitive::kPrimLong:
1401 GenerateLongComparesAndJumps(condition, true_target, false_target);
1402 break;
1403 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001404 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1405 GenerateFPJumps(condition, true_target, false_target);
1406 break;
1407 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001408 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1409 GenerateFPJumps(condition, true_target, false_target);
1410 break;
1411 default:
1412 LOG(FATAL) << "Unexpected compare type " << type;
1413 }
1414
David Brazdil0debae72015-11-12 18:37:00 +00001415 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001416 __ jmp(false_target);
1417 }
David Brazdil0debae72015-11-12 18:37:00 +00001418
1419 if (fallthrough_target.IsLinked()) {
1420 __ Bind(&fallthrough_target);
1421 }
Mark Mendellc4701932015-04-10 13:18:51 -04001422}
1423
David Brazdil0debae72015-11-12 18:37:00 +00001424static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1425 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1426 // are set only strictly before `branch`. We can't use the eflags on long/FP
1427 // conditions if they are materialized due to the complex branching.
1428 return cond->IsCondition() &&
1429 cond->GetNext() == branch &&
1430 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1431 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1432}
1433
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001434void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001435 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001436 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001437 Label* false_target) {
1438 HInstruction* cond = instruction->InputAt(condition_input_index);
1439
1440 if (true_target == nullptr && false_target == nullptr) {
1441 // Nothing to do. The code always falls through.
1442 return;
1443 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001444 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001445 if (cond->AsIntConstant()->IsOne()) {
1446 if (true_target != nullptr) {
1447 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001448 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001449 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001450 DCHECK(cond->AsIntConstant()->IsZero());
1451 if (false_target != nullptr) {
1452 __ jmp(false_target);
1453 }
1454 }
1455 return;
1456 }
1457
1458 // The following code generates these patterns:
1459 // (1) true_target == nullptr && false_target != nullptr
1460 // - opposite condition true => branch to false_target
1461 // (2) true_target != nullptr && false_target == nullptr
1462 // - condition true => branch to true_target
1463 // (3) true_target != nullptr && false_target != nullptr
1464 // - condition true => branch to true_target
1465 // - branch to false_target
1466 if (IsBooleanValueOrMaterializedCondition(cond)) {
1467 if (AreEflagsSetFrom(cond, instruction)) {
1468 if (true_target == nullptr) {
1469 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1470 } else {
1471 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1472 }
1473 } else {
1474 // Materialized condition, compare against 0.
1475 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1476 if (lhs.IsRegister()) {
1477 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1478 } else {
1479 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1480 }
1481 if (true_target == nullptr) {
1482 __ j(kEqual, false_target);
1483 } else {
1484 __ j(kNotEqual, true_target);
1485 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001486 }
1487 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001488 // Condition has not been materialized, use its inputs as the comparison and
1489 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001490 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001491
1492 // If this is a long or FP comparison that has been folded into
1493 // the HCondition, generate the comparison directly.
1494 Primitive::Type type = condition->InputAt(0)->GetType();
1495 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1496 GenerateCompareTestAndBranch(condition, true_target, false_target);
1497 return;
1498 }
1499
1500 Location lhs = condition->GetLocations()->InAt(0);
1501 Location rhs = condition->GetLocations()->InAt(1);
1502 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
1503 if (rhs.IsRegister()) {
1504 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1505 } else if (rhs.IsConstant()) {
1506 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1507 if (constant == 0) {
1508 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001509 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001510 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001511 }
1512 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001513 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1514 }
1515 if (true_target == nullptr) {
1516 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1517 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001518 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001519 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001520 }
David Brazdil0debae72015-11-12 18:37:00 +00001521
1522 // If neither branch falls through (case 3), the conditional branch to `true_target`
1523 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1524 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001525 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001526 }
1527}
1528
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001529void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1531 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001532 locations->SetInAt(0, Location::Any());
1533 }
1534}
1535
1536void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001537 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1538 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1539 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1540 nullptr : codegen_->GetLabelOf(true_successor);
1541 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1542 nullptr : codegen_->GetLabelOf(false_successor);
1543 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001544}
1545
1546void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1547 LocationSummary* locations = new (GetGraph()->GetArena())
1548 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001549 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001550 locations->SetInAt(0, Location::Any());
1551 }
1552}
1553
1554void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001555 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001556 DeoptimizationSlowPathX86(deoptimize);
1557 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001558 GenerateTestAndBranch(deoptimize,
1559 /* condition_input_index */ 0,
1560 slow_path->GetEntryLabel(),
1561 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001562}
1563
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001564void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001565 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001566}
1567
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001568void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1569 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001570}
1571
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001572void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001573 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001574}
1575
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001576void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001577 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001578}
1579
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001580void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001581 LocationSummary* locations =
1582 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001583 switch (store->InputAt(1)->GetType()) {
1584 case Primitive::kPrimBoolean:
1585 case Primitive::kPrimByte:
1586 case Primitive::kPrimChar:
1587 case Primitive::kPrimShort:
1588 case Primitive::kPrimInt:
1589 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001590 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001591 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1592 break;
1593
1594 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001595 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001596 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1597 break;
1598
1599 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001600 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001601 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001602}
1603
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001604void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001605}
1606
Roland Levillain0d37cd02015-05-27 16:39:19 +01001607void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001608 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001609 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001610 // Handle the long/FP comparisons made in instruction simplification.
1611 switch (cond->InputAt(0)->GetType()) {
1612 case Primitive::kPrimLong: {
1613 locations->SetInAt(0, Location::RequiresRegister());
1614 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1615 if (cond->NeedsMaterialization()) {
1616 locations->SetOut(Location::RequiresRegister());
1617 }
1618 break;
1619 }
1620 case Primitive::kPrimFloat:
1621 case Primitive::kPrimDouble: {
1622 locations->SetInAt(0, Location::RequiresFpuRegister());
1623 locations->SetInAt(1, Location::RequiresFpuRegister());
1624 if (cond->NeedsMaterialization()) {
1625 locations->SetOut(Location::RequiresRegister());
1626 }
1627 break;
1628 }
1629 default:
1630 locations->SetInAt(0, Location::RequiresRegister());
1631 locations->SetInAt(1, Location::Any());
1632 if (cond->NeedsMaterialization()) {
1633 // We need a byte register.
1634 locations->SetOut(Location::RegisterLocation(ECX));
1635 }
1636 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001637 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001638}
1639
Roland Levillain0d37cd02015-05-27 16:39:19 +01001640void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001641 if (!cond->NeedsMaterialization()) {
1642 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001643 }
Mark Mendellc4701932015-04-10 13:18:51 -04001644
1645 LocationSummary* locations = cond->GetLocations();
1646 Location lhs = locations->InAt(0);
1647 Location rhs = locations->InAt(1);
1648 Register reg = locations->Out().AsRegister<Register>();
1649 Label true_label, false_label;
1650
1651 switch (cond->InputAt(0)->GetType()) {
1652 default: {
1653 // Integer case.
1654
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001655 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001656 __ xorl(reg, reg);
1657
1658 if (rhs.IsRegister()) {
1659 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1660 } else if (rhs.IsConstant()) {
1661 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1662 if (constant == 0) {
1663 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1664 } else {
1665 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1666 }
1667 } else {
1668 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1669 }
Aart Bike9f37602015-10-09 11:15:55 -07001670 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001671 return;
1672 }
1673 case Primitive::kPrimLong:
1674 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1675 break;
1676 case Primitive::kPrimFloat:
1677 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1678 GenerateFPJumps(cond, &true_label, &false_label);
1679 break;
1680 case Primitive::kPrimDouble:
1681 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1682 GenerateFPJumps(cond, &true_label, &false_label);
1683 break;
1684 }
1685
1686 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001687 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001688
Roland Levillain4fa13f62015-07-06 18:11:54 +01001689 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001690 __ Bind(&false_label);
1691 __ xorl(reg, reg);
1692 __ jmp(&done_label);
1693
Roland Levillain4fa13f62015-07-06 18:11:54 +01001694 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001695 __ Bind(&true_label);
1696 __ movl(reg, Immediate(1));
1697 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001698}
1699
1700void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1701 VisitCondition(comp);
1702}
1703
1704void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1705 VisitCondition(comp);
1706}
1707
1708void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1709 VisitCondition(comp);
1710}
1711
1712void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1713 VisitCondition(comp);
1714}
1715
1716void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1717 VisitCondition(comp);
1718}
1719
1720void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1721 VisitCondition(comp);
1722}
1723
1724void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1725 VisitCondition(comp);
1726}
1727
1728void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1729 VisitCondition(comp);
1730}
1731
1732void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1733 VisitCondition(comp);
1734}
1735
1736void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1737 VisitCondition(comp);
1738}
1739
1740void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1741 VisitCondition(comp);
1742}
1743
1744void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1745 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001746}
1747
Aart Bike9f37602015-10-09 11:15:55 -07001748void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1749 VisitCondition(comp);
1750}
1751
1752void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1753 VisitCondition(comp);
1754}
1755
1756void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1757 VisitCondition(comp);
1758}
1759
1760void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1761 VisitCondition(comp);
1762}
1763
1764void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1765 VisitCondition(comp);
1766}
1767
1768void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1769 VisitCondition(comp);
1770}
1771
1772void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1773 VisitCondition(comp);
1774}
1775
1776void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1777 VisitCondition(comp);
1778}
1779
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001780void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001781 LocationSummary* locations =
1782 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001783 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001784}
1785
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001786void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001787 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001788}
1789
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001790void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1791 LocationSummary* locations =
1792 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1793 locations->SetOut(Location::ConstantLocation(constant));
1794}
1795
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001796void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001797 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001798}
1799
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001800void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001801 LocationSummary* locations =
1802 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001803 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001804}
1805
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001806void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001807 // Will be generated at use site.
1808}
1809
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001810void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1811 LocationSummary* locations =
1812 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1813 locations->SetOut(Location::ConstantLocation(constant));
1814}
1815
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001816void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001817 // Will be generated at use site.
1818}
1819
1820void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1821 LocationSummary* locations =
1822 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1823 locations->SetOut(Location::ConstantLocation(constant));
1824}
1825
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001826void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001827 // Will be generated at use site.
1828}
1829
Calin Juravle27df7582015-04-17 19:12:31 +01001830void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1831 memory_barrier->SetLocations(nullptr);
1832}
1833
1834void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1835 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1836}
1837
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001838void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001839 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001840}
1841
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001842void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001843 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001844}
1845
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001846void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001847 LocationSummary* locations =
1848 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001849 switch (ret->InputAt(0)->GetType()) {
1850 case Primitive::kPrimBoolean:
1851 case Primitive::kPrimByte:
1852 case Primitive::kPrimChar:
1853 case Primitive::kPrimShort:
1854 case Primitive::kPrimInt:
1855 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001856 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001857 break;
1858
1859 case Primitive::kPrimLong:
1860 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001861 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001862 break;
1863
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001864 case Primitive::kPrimFloat:
1865 case Primitive::kPrimDouble:
1866 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001867 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001868 break;
1869
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001871 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001872 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001873}
1874
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001875void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 if (kIsDebugBuild) {
1877 switch (ret->InputAt(0)->GetType()) {
1878 case Primitive::kPrimBoolean:
1879 case Primitive::kPrimByte:
1880 case Primitive::kPrimChar:
1881 case Primitive::kPrimShort:
1882 case Primitive::kPrimInt:
1883 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001884 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001885 break;
1886
1887 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001888 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1889 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890 break;
1891
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 case Primitive::kPrimFloat:
1893 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001894 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001895 break;
1896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001898 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001899 }
1900 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001901 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001902}
1903
Calin Juravle175dc732015-08-25 15:42:32 +01001904void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1905 // The trampoline uses the same calling convention as dex calling conventions,
1906 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1907 // the method_idx.
1908 HandleInvoke(invoke);
1909}
1910
1911void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1912 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1913}
1914
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001915void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001916 // When we do not run baseline, explicit clinit checks triggered by static
1917 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1918 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001919
Mark Mendellfb8d2792015-03-31 22:16:59 -04001920 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001921 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001922 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001923 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001924 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001925 return;
1926 }
1927
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001928 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001929
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001930 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1931 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001932 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001933 }
1934
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001935 if (codegen_->IsBaseline()) {
1936 // Baseline does not have enough registers if the current method also
1937 // needs a register. We therefore do not require a register for it, and let
1938 // the code generation of the invoke handle it.
1939 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00001940 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001941 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001942 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001943 }
1944 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001945}
1946
Mark Mendell09ed1a32015-03-25 08:30:06 -04001947static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1948 if (invoke->GetLocations()->Intrinsified()) {
1949 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1950 intrinsic.Dispatch(invoke);
1951 return true;
1952 }
1953 return false;
1954}
1955
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001956void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001957 // When we do not run baseline, explicit clinit checks triggered by static
1958 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1959 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001960
Mark Mendell09ed1a32015-03-25 08:30:06 -04001961 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1962 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001963 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001964
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001965 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001966 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001967 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001968 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001969}
1970
1971void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001972 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1973 if (intrinsic.TryDispatch(invoke)) {
1974 return;
1975 }
1976
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001977 HandleInvoke(invoke);
1978}
1979
1980void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001981 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001982 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001983}
1984
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001985void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001986 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1987 return;
1988 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001989
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001990 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001991 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001992 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001993}
1994
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001995void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001996 // This call to HandleInvoke allocates a temporary (core) register
1997 // which is also used to transfer the hidden argument from FP to
1998 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001999 HandleInvoke(invoke);
2000 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002001 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002002}
2003
2004void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2005 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002006 LocationSummary* locations = invoke->GetLocations();
2007 Register temp = locations->GetTemp(0).AsRegister<Register>();
2008 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002009 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2010 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002011 Location receiver = locations->InAt(0);
2012 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2013
Roland Levillain0d5a2812015-11-13 10:07:31 +00002014 // Set the hidden argument. This is safe to do this here, as XMM7
2015 // won't be modified thereafter, before the `call` instruction.
2016 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002017 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002018 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002020 if (receiver.IsStackSlot()) {
2021 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002022 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 __ movl(temp, Address(temp, class_offset));
2024 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002025 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002026 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027 }
Roland Levillain4d027112015-07-01 15:41:14 +01002028 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002029 // Instead of simply (possibly) unpoisoning `temp` here, we should
2030 // emit a read barrier for the previous class reference load.
2031 // However this is not required in practice, as this is an
2032 // intermediate/temporary reference and because the current
2033 // concurrent copying collector keeps the from-space memory
2034 // intact/accessible until the end of the marking phase (the
2035 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002036 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002037 // temp = temp->GetImtEntryAt(method_offset);
2038 __ movl(temp, Address(temp, method_offset));
2039 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002040 __ call(Address(temp,
2041 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002042
2043 DCHECK(!codegen_->IsLeafMethod());
2044 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2045}
2046
Roland Levillain88cb1752014-10-20 16:36:47 +01002047void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2048 LocationSummary* locations =
2049 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2050 switch (neg->GetResultType()) {
2051 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002052 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002053 locations->SetInAt(0, Location::RequiresRegister());
2054 locations->SetOut(Location::SameAsFirstInput());
2055 break;
2056
Roland Levillain88cb1752014-10-20 16:36:47 +01002057 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002058 locations->SetInAt(0, Location::RequiresFpuRegister());
2059 locations->SetOut(Location::SameAsFirstInput());
2060 locations->AddTemp(Location::RequiresRegister());
2061 locations->AddTemp(Location::RequiresFpuRegister());
2062 break;
2063
Roland Levillain88cb1752014-10-20 16:36:47 +01002064 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002065 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002066 locations->SetOut(Location::SameAsFirstInput());
2067 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002068 break;
2069
2070 default:
2071 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2072 }
2073}
2074
2075void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2076 LocationSummary* locations = neg->GetLocations();
2077 Location out = locations->Out();
2078 Location in = locations->InAt(0);
2079 switch (neg->GetResultType()) {
2080 case Primitive::kPrimInt:
2081 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002082 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002083 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002084 break;
2085
2086 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002087 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002088 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002089 __ negl(out.AsRegisterPairLow<Register>());
2090 // Negation is similar to subtraction from zero. The least
2091 // significant byte triggers a borrow when it is different from
2092 // zero; to take it into account, add 1 to the most significant
2093 // byte if the carry flag (CF) is set to 1 after the first NEGL
2094 // operation.
2095 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2096 __ negl(out.AsRegisterPairHigh<Register>());
2097 break;
2098
Roland Levillain5368c212014-11-27 15:03:41 +00002099 case Primitive::kPrimFloat: {
2100 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002101 Register constant = locations->GetTemp(0).AsRegister<Register>();
2102 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002103 // Implement float negation with an exclusive or with value
2104 // 0x80000000 (mask for bit 31, representing the sign of a
2105 // single-precision floating-point number).
2106 __ movl(constant, Immediate(INT32_C(0x80000000)));
2107 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002108 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002109 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002110 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002111
Roland Levillain5368c212014-11-27 15:03:41 +00002112 case Primitive::kPrimDouble: {
2113 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002115 // Implement double negation with an exclusive or with value
2116 // 0x8000000000000000 (mask for bit 63, representing the sign of
2117 // a double-precision floating-point number).
2118 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002119 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002120 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002121 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002122
2123 default:
2124 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2125 }
2126}
2127
Roland Levillaindff1f282014-11-05 14:15:05 +00002128void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002129 Primitive::Type result_type = conversion->GetResultType();
2130 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002131 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002132
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002133 // The float-to-long and double-to-long type conversions rely on a
2134 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002135 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002136 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2137 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002138 ? LocationSummary::kCall
2139 : LocationSummary::kNoCall;
2140 LocationSummary* locations =
2141 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2142
David Brazdilb2bd1c52015-03-25 11:17:37 +00002143 // The Java language does not allow treating boolean as an integral type but
2144 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002145
Roland Levillaindff1f282014-11-05 14:15:05 +00002146 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002147 case Primitive::kPrimByte:
2148 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002149 case Primitive::kPrimBoolean:
2150 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002151 case Primitive::kPrimShort:
2152 case Primitive::kPrimInt:
2153 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002154 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002155 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2156 // Make the output overlap to please the register allocator. This greatly simplifies
2157 // the validation of the linear scan implementation
2158 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002159 break;
2160
2161 default:
2162 LOG(FATAL) << "Unexpected type conversion from " << input_type
2163 << " to " << result_type;
2164 }
2165 break;
2166
Roland Levillain01a8d712014-11-14 16:27:39 +00002167 case Primitive::kPrimShort:
2168 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002169 case Primitive::kPrimBoolean:
2170 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002171 case Primitive::kPrimByte:
2172 case Primitive::kPrimInt:
2173 case Primitive::kPrimChar:
2174 // Processing a Dex `int-to-short' instruction.
2175 locations->SetInAt(0, Location::Any());
2176 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2177 break;
2178
2179 default:
2180 LOG(FATAL) << "Unexpected type conversion from " << input_type
2181 << " to " << result_type;
2182 }
2183 break;
2184
Roland Levillain946e1432014-11-11 17:35:19 +00002185 case Primitive::kPrimInt:
2186 switch (input_type) {
2187 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002188 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002189 locations->SetInAt(0, Location::Any());
2190 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2191 break;
2192
2193 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002194 // Processing a Dex `float-to-int' instruction.
2195 locations->SetInAt(0, Location::RequiresFpuRegister());
2196 locations->SetOut(Location::RequiresRegister());
2197 locations->AddTemp(Location::RequiresFpuRegister());
2198 break;
2199
Roland Levillain946e1432014-11-11 17:35:19 +00002200 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002201 // Processing a Dex `double-to-int' instruction.
2202 locations->SetInAt(0, Location::RequiresFpuRegister());
2203 locations->SetOut(Location::RequiresRegister());
2204 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002205 break;
2206
2207 default:
2208 LOG(FATAL) << "Unexpected type conversion from " << input_type
2209 << " to " << result_type;
2210 }
2211 break;
2212
Roland Levillaindff1f282014-11-05 14:15:05 +00002213 case Primitive::kPrimLong:
2214 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002215 case Primitive::kPrimBoolean:
2216 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002217 case Primitive::kPrimByte:
2218 case Primitive::kPrimShort:
2219 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002220 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002221 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002222 locations->SetInAt(0, Location::RegisterLocation(EAX));
2223 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2224 break;
2225
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002226 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002227 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002228 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002229 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002230 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2231 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2232
Vladimir Marko949c91f2015-01-27 10:48:44 +00002233 // The runtime helper puts the result in EAX, EDX.
2234 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002235 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002236 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002237
2238 default:
2239 LOG(FATAL) << "Unexpected type conversion from " << input_type
2240 << " to " << result_type;
2241 }
2242 break;
2243
Roland Levillain981e4542014-11-14 11:47:14 +00002244 case Primitive::kPrimChar:
2245 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002246 case Primitive::kPrimBoolean:
2247 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002248 case Primitive::kPrimByte:
2249 case Primitive::kPrimShort:
2250 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002251 // Processing a Dex `int-to-char' instruction.
2252 locations->SetInAt(0, Location::Any());
2253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260 break;
2261
Roland Levillaindff1f282014-11-05 14:15:05 +00002262 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002263 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002264 case Primitive::kPrimBoolean:
2265 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002266 case Primitive::kPrimByte:
2267 case Primitive::kPrimShort:
2268 case Primitive::kPrimInt:
2269 case Primitive::kPrimChar:
2270 // Processing a Dex `int-to-float' instruction.
2271 locations->SetInAt(0, Location::RequiresRegister());
2272 locations->SetOut(Location::RequiresFpuRegister());
2273 break;
2274
2275 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002276 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002277 locations->SetInAt(0, Location::Any());
2278 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002279 break;
2280
Roland Levillaincff13742014-11-17 14:32:17 +00002281 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002282 // Processing a Dex `double-to-float' instruction.
2283 locations->SetInAt(0, Location::RequiresFpuRegister());
2284 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002285 break;
2286
2287 default:
2288 LOG(FATAL) << "Unexpected type conversion from " << input_type
2289 << " to " << result_type;
2290 };
2291 break;
2292
Roland Levillaindff1f282014-11-05 14:15:05 +00002293 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002294 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002295 case Primitive::kPrimBoolean:
2296 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002297 case Primitive::kPrimByte:
2298 case Primitive::kPrimShort:
2299 case Primitive::kPrimInt:
2300 case Primitive::kPrimChar:
2301 // Processing a Dex `int-to-double' instruction.
2302 locations->SetInAt(0, Location::RequiresRegister());
2303 locations->SetOut(Location::RequiresFpuRegister());
2304 break;
2305
2306 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002307 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002308 locations->SetInAt(0, Location::Any());
2309 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002310 break;
2311
Roland Levillaincff13742014-11-17 14:32:17 +00002312 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002313 // Processing a Dex `float-to-double' instruction.
2314 locations->SetInAt(0, Location::RequiresFpuRegister());
2315 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002316 break;
2317
2318 default:
2319 LOG(FATAL) << "Unexpected type conversion from " << input_type
2320 << " to " << result_type;
2321 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002322 break;
2323
2324 default:
2325 LOG(FATAL) << "Unexpected type conversion from " << input_type
2326 << " to " << result_type;
2327 }
2328}
2329
2330void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2331 LocationSummary* locations = conversion->GetLocations();
2332 Location out = locations->Out();
2333 Location in = locations->InAt(0);
2334 Primitive::Type result_type = conversion->GetResultType();
2335 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002336 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002337 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002338 case Primitive::kPrimByte:
2339 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002340 case Primitive::kPrimBoolean:
2341 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002342 case Primitive::kPrimShort:
2343 case Primitive::kPrimInt:
2344 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002345 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002346 if (in.IsRegister()) {
2347 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002348 } else {
2349 DCHECK(in.GetConstant()->IsIntConstant());
2350 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2351 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2352 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002353 break;
2354
2355 default:
2356 LOG(FATAL) << "Unexpected type conversion from " << input_type
2357 << " to " << result_type;
2358 }
2359 break;
2360
Roland Levillain01a8d712014-11-14 16:27:39 +00002361 case Primitive::kPrimShort:
2362 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002363 case Primitive::kPrimBoolean:
2364 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002365 case Primitive::kPrimByte:
2366 case Primitive::kPrimInt:
2367 case Primitive::kPrimChar:
2368 // Processing a Dex `int-to-short' instruction.
2369 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002370 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002371 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002372 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002373 } else {
2374 DCHECK(in.GetConstant()->IsIntConstant());
2375 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002376 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002377 }
2378 break;
2379
2380 default:
2381 LOG(FATAL) << "Unexpected type conversion from " << input_type
2382 << " to " << result_type;
2383 }
2384 break;
2385
Roland Levillain946e1432014-11-11 17:35:19 +00002386 case Primitive::kPrimInt:
2387 switch (input_type) {
2388 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002389 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002390 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002391 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002392 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002393 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002394 } else {
2395 DCHECK(in.IsConstant());
2396 DCHECK(in.GetConstant()->IsLongConstant());
2397 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002398 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002399 }
2400 break;
2401
Roland Levillain3f8f9362014-12-02 17:45:01 +00002402 case Primitive::kPrimFloat: {
2403 // Processing a Dex `float-to-int' instruction.
2404 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2405 Register output = out.AsRegister<Register>();
2406 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002407 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002408
2409 __ movl(output, Immediate(kPrimIntMax));
2410 // temp = int-to-float(output)
2411 __ cvtsi2ss(temp, output);
2412 // if input >= temp goto done
2413 __ comiss(input, temp);
2414 __ j(kAboveEqual, &done);
2415 // if input == NaN goto nan
2416 __ j(kUnordered, &nan);
2417 // output = float-to-int-truncate(input)
2418 __ cvttss2si(output, input);
2419 __ jmp(&done);
2420 __ Bind(&nan);
2421 // output = 0
2422 __ xorl(output, output);
2423 __ Bind(&done);
2424 break;
2425 }
2426
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002427 case Primitive::kPrimDouble: {
2428 // Processing a Dex `double-to-int' instruction.
2429 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2430 Register output = out.AsRegister<Register>();
2431 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002432 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002433
2434 __ movl(output, Immediate(kPrimIntMax));
2435 // temp = int-to-double(output)
2436 __ cvtsi2sd(temp, output);
2437 // if input >= temp goto done
2438 __ comisd(input, temp);
2439 __ j(kAboveEqual, &done);
2440 // if input == NaN goto nan
2441 __ j(kUnordered, &nan);
2442 // output = double-to-int-truncate(input)
2443 __ cvttsd2si(output, input);
2444 __ jmp(&done);
2445 __ Bind(&nan);
2446 // output = 0
2447 __ xorl(output, output);
2448 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002449 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002450 }
Roland Levillain946e1432014-11-11 17:35:19 +00002451
2452 default:
2453 LOG(FATAL) << "Unexpected type conversion from " << input_type
2454 << " to " << result_type;
2455 }
2456 break;
2457
Roland Levillaindff1f282014-11-05 14:15:05 +00002458 case Primitive::kPrimLong:
2459 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002460 case Primitive::kPrimBoolean:
2461 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002462 case Primitive::kPrimByte:
2463 case Primitive::kPrimShort:
2464 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002465 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002466 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002467 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2468 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002469 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002470 __ cdq();
2471 break;
2472
2473 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002474 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002475 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2476 conversion,
2477 conversion->GetDexPc(),
2478 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002479 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002480 break;
2481
Roland Levillaindff1f282014-11-05 14:15:05 +00002482 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002483 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002484 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2485 conversion,
2486 conversion->GetDexPc(),
2487 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002488 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002489 break;
2490
2491 default:
2492 LOG(FATAL) << "Unexpected type conversion from " << input_type
2493 << " to " << result_type;
2494 }
2495 break;
2496
Roland Levillain981e4542014-11-14 11:47:14 +00002497 case Primitive::kPrimChar:
2498 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002499 case Primitive::kPrimBoolean:
2500 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002501 case Primitive::kPrimByte:
2502 case Primitive::kPrimShort:
2503 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002504 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2505 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002506 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002507 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002508 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002509 } else {
2510 DCHECK(in.GetConstant()->IsIntConstant());
2511 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002512 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002513 }
2514 break;
2515
2516 default:
2517 LOG(FATAL) << "Unexpected type conversion from " << input_type
2518 << " to " << result_type;
2519 }
2520 break;
2521
Roland Levillaindff1f282014-11-05 14:15:05 +00002522 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002523 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002524 case Primitive::kPrimBoolean:
2525 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002526 case Primitive::kPrimByte:
2527 case Primitive::kPrimShort:
2528 case Primitive::kPrimInt:
2529 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002530 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002531 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002532 break;
2533
Roland Levillain6d0e4832014-11-27 18:31:21 +00002534 case Primitive::kPrimLong: {
2535 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002536 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002537
Roland Levillain232ade02015-04-20 15:14:36 +01002538 // Create stack space for the call to
2539 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2540 // TODO: enhance register allocator to ask for stack temporaries.
2541 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2542 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2543 __ subl(ESP, Immediate(adjustment));
2544 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002545
Roland Levillain232ade02015-04-20 15:14:36 +01002546 // Load the value to the FP stack, using temporaries if needed.
2547 PushOntoFPStack(in, 0, adjustment, false, true);
2548
2549 if (out.IsStackSlot()) {
2550 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2551 } else {
2552 __ fstps(Address(ESP, 0));
2553 Location stack_temp = Location::StackSlot(0);
2554 codegen_->Move32(out, stack_temp);
2555 }
2556
2557 // Remove the temporary stack space we allocated.
2558 if (adjustment != 0) {
2559 __ addl(ESP, Immediate(adjustment));
2560 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002561 break;
2562 }
2563
Roland Levillaincff13742014-11-17 14:32:17 +00002564 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002565 // Processing a Dex `double-to-float' instruction.
2566 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002567 break;
2568
2569 default:
2570 LOG(FATAL) << "Unexpected type conversion from " << input_type
2571 << " to " << result_type;
2572 };
2573 break;
2574
Roland Levillaindff1f282014-11-05 14:15:05 +00002575 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002576 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002577 case Primitive::kPrimBoolean:
2578 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002579 case Primitive::kPrimByte:
2580 case Primitive::kPrimShort:
2581 case Primitive::kPrimInt:
2582 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002583 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002584 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002585 break;
2586
Roland Levillain647b9ed2014-11-27 12:06:00 +00002587 case Primitive::kPrimLong: {
2588 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002589 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002590
Roland Levillain232ade02015-04-20 15:14:36 +01002591 // Create stack space for the call to
2592 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2593 // TODO: enhance register allocator to ask for stack temporaries.
2594 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2595 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2596 __ subl(ESP, Immediate(adjustment));
2597 }
2598
2599 // Load the value to the FP stack, using temporaries if needed.
2600 PushOntoFPStack(in, 0, adjustment, false, true);
2601
2602 if (out.IsDoubleStackSlot()) {
2603 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2604 } else {
2605 __ fstpl(Address(ESP, 0));
2606 Location stack_temp = Location::DoubleStackSlot(0);
2607 codegen_->Move64(out, stack_temp);
2608 }
2609
2610 // Remove the temporary stack space we allocated.
2611 if (adjustment != 0) {
2612 __ addl(ESP, Immediate(adjustment));
2613 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002614 break;
2615 }
2616
Roland Levillaincff13742014-11-17 14:32:17 +00002617 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002618 // Processing a Dex `float-to-double' instruction.
2619 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002620 break;
2621
2622 default:
2623 LOG(FATAL) << "Unexpected type conversion from " << input_type
2624 << " to " << result_type;
2625 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002626 break;
2627
2628 default:
2629 LOG(FATAL) << "Unexpected type conversion from " << input_type
2630 << " to " << result_type;
2631 }
2632}
2633
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002634void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002635 LocationSummary* locations =
2636 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002637 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002638 case Primitive::kPrimInt: {
2639 locations->SetInAt(0, Location::RequiresRegister());
2640 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2641 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2642 break;
2643 }
2644
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002645 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002646 locations->SetInAt(0, Location::RequiresRegister());
2647 locations->SetInAt(1, Location::Any());
2648 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002649 break;
2650 }
2651
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002652 case Primitive::kPrimFloat:
2653 case Primitive::kPrimDouble: {
2654 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002655 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002656 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002657 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002658 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002659
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002660 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002661 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2662 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002663 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002664}
2665
2666void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2667 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002668 Location first = locations->InAt(0);
2669 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002670 Location out = locations->Out();
2671
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002672 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002673 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002674 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002675 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2676 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002677 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2678 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002679 } else {
2680 __ leal(out.AsRegister<Register>(), Address(
2681 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2682 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002683 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002684 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2685 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2686 __ addl(out.AsRegister<Register>(), Immediate(value));
2687 } else {
2688 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2689 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002690 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002691 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002692 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002693 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002694 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002695 }
2696
2697 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002698 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002699 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2700 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002701 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002702 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2703 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002704 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002705 } else {
2706 DCHECK(second.IsConstant()) << second;
2707 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2708 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2709 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002710 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002711 break;
2712 }
2713
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002714 case Primitive::kPrimFloat: {
2715 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002716 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002717 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2718 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2719 DCHECK(!const_area->NeedsMaterialization());
2720 __ addss(first.AsFpuRegister<XmmRegister>(),
2721 codegen_->LiteralFloatAddress(
2722 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2723 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2724 } else {
2725 DCHECK(second.IsStackSlot());
2726 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002727 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002728 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002729 }
2730
2731 case Primitive::kPrimDouble: {
2732 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002733 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002734 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2735 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2736 DCHECK(!const_area->NeedsMaterialization());
2737 __ addsd(first.AsFpuRegister<XmmRegister>(),
2738 codegen_->LiteralDoubleAddress(
2739 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2740 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2741 } else {
2742 DCHECK(second.IsDoubleStackSlot());
2743 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002744 }
2745 break;
2746 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002747
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002748 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002749 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002750 }
2751}
2752
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002753void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002754 LocationSummary* locations =
2755 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002756 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002757 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002758 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002759 locations->SetInAt(0, Location::RequiresRegister());
2760 locations->SetInAt(1, Location::Any());
2761 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762 break;
2763 }
Calin Juravle11351682014-10-23 15:38:15 +01002764 case Primitive::kPrimFloat:
2765 case Primitive::kPrimDouble: {
2766 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002767 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002768 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002769 break;
Calin Juravle11351682014-10-23 15:38:15 +01002770 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002771
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002772 default:
Calin Juravle11351682014-10-23 15:38:15 +01002773 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002774 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002775}
2776
2777void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2778 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002779 Location first = locations->InAt(0);
2780 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002781 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002782 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002783 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002784 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002785 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002786 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002787 __ subl(first.AsRegister<Register>(),
2788 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002789 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002791 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002792 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002793 }
2794
2795 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002796 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002797 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2798 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002799 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002800 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002801 __ sbbl(first.AsRegisterPairHigh<Register>(),
2802 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002803 } else {
2804 DCHECK(second.IsConstant()) << second;
2805 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2806 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2807 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002808 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002809 break;
2810 }
2811
Calin Juravle11351682014-10-23 15:38:15 +01002812 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002813 if (second.IsFpuRegister()) {
2814 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2815 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2816 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2817 DCHECK(!const_area->NeedsMaterialization());
2818 __ subss(first.AsFpuRegister<XmmRegister>(),
2819 codegen_->LiteralFloatAddress(
2820 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2821 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2822 } else {
2823 DCHECK(second.IsStackSlot());
2824 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2825 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002826 break;
Calin Juravle11351682014-10-23 15:38:15 +01002827 }
2828
2829 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002830 if (second.IsFpuRegister()) {
2831 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2832 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2833 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2834 DCHECK(!const_area->NeedsMaterialization());
2835 __ subsd(first.AsFpuRegister<XmmRegister>(),
2836 codegen_->LiteralDoubleAddress(
2837 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2838 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2839 } else {
2840 DCHECK(second.IsDoubleStackSlot());
2841 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2842 }
Calin Juravle11351682014-10-23 15:38:15 +01002843 break;
2844 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002845
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002846 default:
Calin Juravle11351682014-10-23 15:38:15 +01002847 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002848 }
2849}
2850
Calin Juravle34bacdf2014-10-07 20:23:36 +01002851void LocationsBuilderX86::VisitMul(HMul* mul) {
2852 LocationSummary* locations =
2853 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2854 switch (mul->GetResultType()) {
2855 case Primitive::kPrimInt:
2856 locations->SetInAt(0, Location::RequiresRegister());
2857 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002858 if (mul->InputAt(1)->IsIntConstant()) {
2859 // Can use 3 operand multiply.
2860 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2861 } else {
2862 locations->SetOut(Location::SameAsFirstInput());
2863 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002864 break;
2865 case Primitive::kPrimLong: {
2866 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002867 locations->SetInAt(1, Location::Any());
2868 locations->SetOut(Location::SameAsFirstInput());
2869 // Needed for imul on 32bits with 64bits output.
2870 locations->AddTemp(Location::RegisterLocation(EAX));
2871 locations->AddTemp(Location::RegisterLocation(EDX));
2872 break;
2873 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002874 case Primitive::kPrimFloat:
2875 case Primitive::kPrimDouble: {
2876 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002877 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002878 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002879 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002880 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002881
2882 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002883 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002884 }
2885}
2886
2887void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2888 LocationSummary* locations = mul->GetLocations();
2889 Location first = locations->InAt(0);
2890 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002891 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002892
2893 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002894 case Primitive::kPrimInt:
2895 // The constant may have ended up in a register, so test explicitly to avoid
2896 // problems where the output may not be the same as the first operand.
2897 if (mul->InputAt(1)->IsIntConstant()) {
2898 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2899 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2900 } else if (second.IsRegister()) {
2901 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002902 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002903 } else {
2904 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002905 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002906 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002907 }
2908 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002909
2910 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002911 Register in1_hi = first.AsRegisterPairHigh<Register>();
2912 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002913 Register eax = locations->GetTemp(0).AsRegister<Register>();
2914 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002915
2916 DCHECK_EQ(EAX, eax);
2917 DCHECK_EQ(EDX, edx);
2918
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002919 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002920 // output: in1
2921 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2922 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2923 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002924 if (second.IsConstant()) {
2925 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002926
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002927 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2928 int32_t low_value = Low32Bits(value);
2929 int32_t high_value = High32Bits(value);
2930 Immediate low(low_value);
2931 Immediate high(high_value);
2932
2933 __ movl(eax, high);
2934 // eax <- in1.lo * in2.hi
2935 __ imull(eax, in1_lo);
2936 // in1.hi <- in1.hi * in2.lo
2937 __ imull(in1_hi, low);
2938 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2939 __ addl(in1_hi, eax);
2940 // move in2_lo to eax to prepare for double precision
2941 __ movl(eax, low);
2942 // edx:eax <- in1.lo * in2.lo
2943 __ mull(in1_lo);
2944 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2945 __ addl(in1_hi, edx);
2946 // in1.lo <- (in1.lo * in2.lo)[31:0];
2947 __ movl(in1_lo, eax);
2948 } else if (second.IsRegisterPair()) {
2949 Register in2_hi = second.AsRegisterPairHigh<Register>();
2950 Register in2_lo = second.AsRegisterPairLow<Register>();
2951
2952 __ movl(eax, in2_hi);
2953 // eax <- in1.lo * in2.hi
2954 __ imull(eax, in1_lo);
2955 // in1.hi <- in1.hi * in2.lo
2956 __ imull(in1_hi, in2_lo);
2957 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2958 __ addl(in1_hi, eax);
2959 // move in1_lo to eax to prepare for double precision
2960 __ movl(eax, in1_lo);
2961 // edx:eax <- in1.lo * in2.lo
2962 __ mull(in2_lo);
2963 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2964 __ addl(in1_hi, edx);
2965 // in1.lo <- (in1.lo * in2.lo)[31:0];
2966 __ movl(in1_lo, eax);
2967 } else {
2968 DCHECK(second.IsDoubleStackSlot()) << second;
2969 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2970 Address in2_lo(ESP, second.GetStackIndex());
2971
2972 __ movl(eax, in2_hi);
2973 // eax <- in1.lo * in2.hi
2974 __ imull(eax, in1_lo);
2975 // in1.hi <- in1.hi * in2.lo
2976 __ imull(in1_hi, in2_lo);
2977 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2978 __ addl(in1_hi, eax);
2979 // move in1_lo to eax to prepare for double precision
2980 __ movl(eax, in1_lo);
2981 // edx:eax <- in1.lo * in2.lo
2982 __ mull(in2_lo);
2983 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2984 __ addl(in1_hi, edx);
2985 // in1.lo <- (in1.lo * in2.lo)[31:0];
2986 __ movl(in1_lo, eax);
2987 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002988
2989 break;
2990 }
2991
Calin Juravleb5bfa962014-10-21 18:02:24 +01002992 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002993 DCHECK(first.Equals(locations->Out()));
2994 if (second.IsFpuRegister()) {
2995 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2996 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2997 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2998 DCHECK(!const_area->NeedsMaterialization());
2999 __ mulss(first.AsFpuRegister<XmmRegister>(),
3000 codegen_->LiteralFloatAddress(
3001 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3002 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3003 } else {
3004 DCHECK(second.IsStackSlot());
3005 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3006 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003008 }
3009
3010 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003011 DCHECK(first.Equals(locations->Out()));
3012 if (second.IsFpuRegister()) {
3013 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3014 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3015 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3016 DCHECK(!const_area->NeedsMaterialization());
3017 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3018 codegen_->LiteralDoubleAddress(
3019 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3020 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3021 } else {
3022 DCHECK(second.IsDoubleStackSlot());
3023 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3024 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003025 break;
3026 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003027
3028 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003029 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003030 }
3031}
3032
Roland Levillain232ade02015-04-20 15:14:36 +01003033void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3034 uint32_t temp_offset,
3035 uint32_t stack_adjustment,
3036 bool is_fp,
3037 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003038 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003039 DCHECK(!is_wide);
3040 if (is_fp) {
3041 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3042 } else {
3043 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3044 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003045 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003046 DCHECK(is_wide);
3047 if (is_fp) {
3048 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3049 } else {
3050 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3051 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003052 } else {
3053 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003054 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003055 Location stack_temp = Location::StackSlot(temp_offset);
3056 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003057 if (is_fp) {
3058 __ flds(Address(ESP, temp_offset));
3059 } else {
3060 __ filds(Address(ESP, temp_offset));
3061 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003062 } else {
3063 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3064 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003065 if (is_fp) {
3066 __ fldl(Address(ESP, temp_offset));
3067 } else {
3068 __ fildl(Address(ESP, temp_offset));
3069 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003070 }
3071 }
3072}
3073
3074void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3075 Primitive::Type type = rem->GetResultType();
3076 bool is_float = type == Primitive::kPrimFloat;
3077 size_t elem_size = Primitive::ComponentSize(type);
3078 LocationSummary* locations = rem->GetLocations();
3079 Location first = locations->InAt(0);
3080 Location second = locations->InAt(1);
3081 Location out = locations->Out();
3082
3083 // Create stack space for 2 elements.
3084 // TODO: enhance register allocator to ask for stack temporaries.
3085 __ subl(ESP, Immediate(2 * elem_size));
3086
3087 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003088 const bool is_wide = !is_float;
3089 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3090 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003091
3092 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003093 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003094 __ Bind(&retry);
3095 __ fprem();
3096
3097 // Move FP status to AX.
3098 __ fstsw();
3099
3100 // And see if the argument reduction is complete. This is signaled by the
3101 // C2 FPU flag bit set to 0.
3102 __ andl(EAX, Immediate(kC2ConditionMask));
3103 __ j(kNotEqual, &retry);
3104
3105 // We have settled on the final value. Retrieve it into an XMM register.
3106 // Store FP top of stack to real stack.
3107 if (is_float) {
3108 __ fsts(Address(ESP, 0));
3109 } else {
3110 __ fstl(Address(ESP, 0));
3111 }
3112
3113 // Pop the 2 items from the FP stack.
3114 __ fucompp();
3115
3116 // Load the value from the stack into an XMM register.
3117 DCHECK(out.IsFpuRegister()) << out;
3118 if (is_float) {
3119 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3120 } else {
3121 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3122 }
3123
3124 // And remove the temporary stack space we allocated.
3125 __ addl(ESP, Immediate(2 * elem_size));
3126}
3127
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003128
3129void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3130 DCHECK(instruction->IsDiv() || instruction->IsRem());
3131
3132 LocationSummary* locations = instruction->GetLocations();
3133 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003134 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003135
3136 Register out_register = locations->Out().AsRegister<Register>();
3137 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003138 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003139
3140 DCHECK(imm == 1 || imm == -1);
3141
3142 if (instruction->IsRem()) {
3143 __ xorl(out_register, out_register);
3144 } else {
3145 __ movl(out_register, input_register);
3146 if (imm == -1) {
3147 __ negl(out_register);
3148 }
3149 }
3150}
3151
3152
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003153void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003154 LocationSummary* locations = instruction->GetLocations();
3155
3156 Register out_register = locations->Out().AsRegister<Register>();
3157 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003158 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003159
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003160 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003161 Register num = locations->GetTemp(0).AsRegister<Register>();
3162
3163 __ leal(num, Address(input_register, std::abs(imm) - 1));
3164 __ testl(input_register, input_register);
3165 __ cmovl(kGreaterEqual, num, input_register);
3166 int shift = CTZ(imm);
3167 __ sarl(num, Immediate(shift));
3168
3169 if (imm < 0) {
3170 __ negl(num);
3171 }
3172
3173 __ movl(out_register, num);
3174}
3175
3176void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3177 DCHECK(instruction->IsDiv() || instruction->IsRem());
3178
3179 LocationSummary* locations = instruction->GetLocations();
3180 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3181
3182 Register eax = locations->InAt(0).AsRegister<Register>();
3183 Register out = locations->Out().AsRegister<Register>();
3184 Register num;
3185 Register edx;
3186
3187 if (instruction->IsDiv()) {
3188 edx = locations->GetTemp(0).AsRegister<Register>();
3189 num = locations->GetTemp(1).AsRegister<Register>();
3190 } else {
3191 edx = locations->Out().AsRegister<Register>();
3192 num = locations->GetTemp(0).AsRegister<Register>();
3193 }
3194
3195 DCHECK_EQ(EAX, eax);
3196 DCHECK_EQ(EDX, edx);
3197 if (instruction->IsDiv()) {
3198 DCHECK_EQ(EAX, out);
3199 } else {
3200 DCHECK_EQ(EDX, out);
3201 }
3202
3203 int64_t magic;
3204 int shift;
3205 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3206
Mark Mendell0c9497d2015-08-21 09:30:05 -04003207 NearLabel ndiv;
3208 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003209 // If numerator is 0, the result is 0, no computation needed.
3210 __ testl(eax, eax);
3211 __ j(kNotEqual, &ndiv);
3212
3213 __ xorl(out, out);
3214 __ jmp(&end);
3215
3216 __ Bind(&ndiv);
3217
3218 // Save the numerator.
3219 __ movl(num, eax);
3220
3221 // EAX = magic
3222 __ movl(eax, Immediate(magic));
3223
3224 // EDX:EAX = magic * numerator
3225 __ imull(num);
3226
3227 if (imm > 0 && magic < 0) {
3228 // EDX += num
3229 __ addl(edx, num);
3230 } else if (imm < 0 && magic > 0) {
3231 __ subl(edx, num);
3232 }
3233
3234 // Shift if needed.
3235 if (shift != 0) {
3236 __ sarl(edx, Immediate(shift));
3237 }
3238
3239 // EDX += 1 if EDX < 0
3240 __ movl(eax, edx);
3241 __ shrl(edx, Immediate(31));
3242 __ addl(edx, eax);
3243
3244 if (instruction->IsRem()) {
3245 __ movl(eax, num);
3246 __ imull(edx, Immediate(imm));
3247 __ subl(eax, edx);
3248 __ movl(edx, eax);
3249 } else {
3250 __ movl(eax, edx);
3251 }
3252 __ Bind(&end);
3253}
3254
Calin Juravlebacfec32014-11-14 15:54:36 +00003255void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3256 DCHECK(instruction->IsDiv() || instruction->IsRem());
3257
3258 LocationSummary* locations = instruction->GetLocations();
3259 Location out = locations->Out();
3260 Location first = locations->InAt(0);
3261 Location second = locations->InAt(1);
3262 bool is_div = instruction->IsDiv();
3263
3264 switch (instruction->GetResultType()) {
3265 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003266 DCHECK_EQ(EAX, first.AsRegister<Register>());
3267 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003268
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003269 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003270 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003271
3272 if (imm == 0) {
3273 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3274 } else if (imm == 1 || imm == -1) {
3275 DivRemOneOrMinusOne(instruction);
3276 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003277 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003278 } else {
3279 DCHECK(imm <= -2 || imm >= 2);
3280 GenerateDivRemWithAnyConstant(instruction);
3281 }
3282 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003283 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003284 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003285 is_div);
3286 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003287
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003288 Register second_reg = second.AsRegister<Register>();
3289 // 0x80000000/-1 triggers an arithmetic exception!
3290 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3291 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003292
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003293 __ cmpl(second_reg, Immediate(-1));
3294 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003295
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003296 // edx:eax <- sign-extended of eax
3297 __ cdq();
3298 // eax = quotient, edx = remainder
3299 __ idivl(second_reg);
3300 __ Bind(slow_path->GetExitLabel());
3301 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003302 break;
3303 }
3304
3305 case Primitive::kPrimLong: {
3306 InvokeRuntimeCallingConvention calling_convention;
3307 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3308 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3309 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3310 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3311 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3312 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3313
3314 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003315 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3316 instruction,
3317 instruction->GetDexPc(),
3318 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003319 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003320 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003321 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3322 instruction,
3323 instruction->GetDexPc(),
3324 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003325 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003326 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003327 break;
3328 }
3329
3330 default:
3331 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3332 }
3333}
3334
Calin Juravle7c4954d2014-10-28 16:57:40 +00003335void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003336 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003337 ? LocationSummary::kCall
3338 : LocationSummary::kNoCall;
3339 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3340
Calin Juravle7c4954d2014-10-28 16:57:40 +00003341 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003342 case Primitive::kPrimInt: {
3343 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003344 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003345 locations->SetOut(Location::SameAsFirstInput());
3346 // Intel uses edx:eax as the dividend.
3347 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003348 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3349 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3350 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003351 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352 locations->AddTemp(Location::RequiresRegister());
3353 }
Calin Juravled0d48522014-11-04 16:40:20 +00003354 break;
3355 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003356 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003357 InvokeRuntimeCallingConvention calling_convention;
3358 locations->SetInAt(0, Location::RegisterPairLocation(
3359 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3360 locations->SetInAt(1, Location::RegisterPairLocation(
3361 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3362 // Runtime helper puts the result in EAX, EDX.
3363 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003364 break;
3365 }
3366 case Primitive::kPrimFloat:
3367 case Primitive::kPrimDouble: {
3368 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003369 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003370 locations->SetOut(Location::SameAsFirstInput());
3371 break;
3372 }
3373
3374 default:
3375 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3376 }
3377}
3378
3379void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3380 LocationSummary* locations = div->GetLocations();
3381 Location first = locations->InAt(0);
3382 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003383
3384 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003385 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003386 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003387 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003388 break;
3389 }
3390
3391 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003392 if (second.IsFpuRegister()) {
3393 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3394 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3395 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3396 DCHECK(!const_area->NeedsMaterialization());
3397 __ divss(first.AsFpuRegister<XmmRegister>(),
3398 codegen_->LiteralFloatAddress(
3399 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3400 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3401 } else {
3402 DCHECK(second.IsStackSlot());
3403 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3404 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003405 break;
3406 }
3407
3408 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003409 if (second.IsFpuRegister()) {
3410 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3411 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3412 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3413 DCHECK(!const_area->NeedsMaterialization());
3414 __ divsd(first.AsFpuRegister<XmmRegister>(),
3415 codegen_->LiteralDoubleAddress(
3416 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3417 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3418 } else {
3419 DCHECK(second.IsDoubleStackSlot());
3420 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3421 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003422 break;
3423 }
3424
3425 default:
3426 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3427 }
3428}
3429
Calin Juravlebacfec32014-11-14 15:54:36 +00003430void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003431 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003432
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003433 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3434 ? LocationSummary::kCall
3435 : LocationSummary::kNoCall;
3436 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003437
Calin Juravled2ec87d2014-12-08 14:24:46 +00003438 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003439 case Primitive::kPrimInt: {
3440 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003441 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003442 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003443 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3444 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3445 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003446 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 locations->AddTemp(Location::RequiresRegister());
3448 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003449 break;
3450 }
3451 case Primitive::kPrimLong: {
3452 InvokeRuntimeCallingConvention calling_convention;
3453 locations->SetInAt(0, Location::RegisterPairLocation(
3454 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3455 locations->SetInAt(1, Location::RegisterPairLocation(
3456 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3457 // Runtime helper puts the result in EAX, EDX.
3458 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3459 break;
3460 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003461 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003462 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003463 locations->SetInAt(0, Location::Any());
3464 locations->SetInAt(1, Location::Any());
3465 locations->SetOut(Location::RequiresFpuRegister());
3466 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003467 break;
3468 }
3469
3470 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003471 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003472 }
3473}
3474
3475void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3476 Primitive::Type type = rem->GetResultType();
3477 switch (type) {
3478 case Primitive::kPrimInt:
3479 case Primitive::kPrimLong: {
3480 GenerateDivRemIntegral(rem);
3481 break;
3482 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003483 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003484 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003485 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003486 break;
3487 }
3488 default:
3489 LOG(FATAL) << "Unexpected rem type " << type;
3490 }
3491}
3492
Calin Juravled0d48522014-11-04 16:40:20 +00003493void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003494 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3495 ? LocationSummary::kCallOnSlowPath
3496 : LocationSummary::kNoCall;
3497 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003498 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003499 case Primitive::kPrimByte:
3500 case Primitive::kPrimChar:
3501 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003502 case Primitive::kPrimInt: {
3503 locations->SetInAt(0, Location::Any());
3504 break;
3505 }
3506 case Primitive::kPrimLong: {
3507 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3508 if (!instruction->IsConstant()) {
3509 locations->AddTemp(Location::RequiresRegister());
3510 }
3511 break;
3512 }
3513 default:
3514 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3515 }
Calin Juravled0d48522014-11-04 16:40:20 +00003516 if (instruction->HasUses()) {
3517 locations->SetOut(Location::SameAsFirstInput());
3518 }
3519}
3520
3521void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003522 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003523 codegen_->AddSlowPath(slow_path);
3524
3525 LocationSummary* locations = instruction->GetLocations();
3526 Location value = locations->InAt(0);
3527
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003528 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003529 case Primitive::kPrimByte:
3530 case Primitive::kPrimChar:
3531 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003532 case Primitive::kPrimInt: {
3533 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003534 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003535 __ j(kEqual, slow_path->GetEntryLabel());
3536 } else if (value.IsStackSlot()) {
3537 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3538 __ j(kEqual, slow_path->GetEntryLabel());
3539 } else {
3540 DCHECK(value.IsConstant()) << value;
3541 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3542 __ jmp(slow_path->GetEntryLabel());
3543 }
3544 }
3545 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003546 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003547 case Primitive::kPrimLong: {
3548 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003549 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003550 __ movl(temp, value.AsRegisterPairLow<Register>());
3551 __ orl(temp, value.AsRegisterPairHigh<Register>());
3552 __ j(kEqual, slow_path->GetEntryLabel());
3553 } else {
3554 DCHECK(value.IsConstant()) << value;
3555 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3556 __ jmp(slow_path->GetEntryLabel());
3557 }
3558 }
3559 break;
3560 }
3561 default:
3562 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003563 }
Calin Juravled0d48522014-11-04 16:40:20 +00003564}
3565
Calin Juravle9aec02f2014-11-18 23:06:35 +00003566void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3567 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3568
3569 LocationSummary* locations =
3570 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3571
3572 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003573 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003574 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003575 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003576 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003577 // The shift count needs to be in CL or a constant.
3578 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003579 locations->SetOut(Location::SameAsFirstInput());
3580 break;
3581 }
3582 default:
3583 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3584 }
3585}
3586
3587void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3588 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3589
3590 LocationSummary* locations = op->GetLocations();
3591 Location first = locations->InAt(0);
3592 Location second = locations->InAt(1);
3593 DCHECK(first.Equals(locations->Out()));
3594
3595 switch (op->GetResultType()) {
3596 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003597 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003598 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003599 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003600 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003601 DCHECK_EQ(ECX, second_reg);
3602 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003603 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003604 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003605 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003606 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003607 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003608 }
3609 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003610 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3611 if (shift == 0) {
3612 return;
3613 }
3614 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003615 if (op->IsShl()) {
3616 __ shll(first_reg, imm);
3617 } else if (op->IsShr()) {
3618 __ sarl(first_reg, imm);
3619 } else {
3620 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003621 }
3622 }
3623 break;
3624 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003625 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003626 if (second.IsRegister()) {
3627 Register second_reg = second.AsRegister<Register>();
3628 DCHECK_EQ(ECX, second_reg);
3629 if (op->IsShl()) {
3630 GenerateShlLong(first, second_reg);
3631 } else if (op->IsShr()) {
3632 GenerateShrLong(first, second_reg);
3633 } else {
3634 GenerateUShrLong(first, second_reg);
3635 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003636 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003637 // Shift by a constant.
3638 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3639 // Nothing to do if the shift is 0, as the input is already the output.
3640 if (shift != 0) {
3641 if (op->IsShl()) {
3642 GenerateShlLong(first, shift);
3643 } else if (op->IsShr()) {
3644 GenerateShrLong(first, shift);
3645 } else {
3646 GenerateUShrLong(first, shift);
3647 }
3648 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003649 }
3650 break;
3651 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003652 default:
3653 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3654 }
3655}
3656
Mark P Mendell73945692015-04-29 14:56:17 +00003657void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3658 Register low = loc.AsRegisterPairLow<Register>();
3659 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003660 if (shift == 1) {
3661 // This is just an addition.
3662 __ addl(low, low);
3663 __ adcl(high, high);
3664 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003665 // Shift by 32 is easy. High gets low, and low gets 0.
3666 codegen_->EmitParallelMoves(
3667 loc.ToLow(),
3668 loc.ToHigh(),
3669 Primitive::kPrimInt,
3670 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3671 loc.ToLow(),
3672 Primitive::kPrimInt);
3673 } else if (shift > 32) {
3674 // Low part becomes 0. High part is low part << (shift-32).
3675 __ movl(high, low);
3676 __ shll(high, Immediate(shift - 32));
3677 __ xorl(low, low);
3678 } else {
3679 // Between 1 and 31.
3680 __ shld(high, low, Immediate(shift));
3681 __ shll(low, Immediate(shift));
3682 }
3683}
3684
Calin Juravle9aec02f2014-11-18 23:06:35 +00003685void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003686 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003687 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3688 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3689 __ testl(shifter, Immediate(32));
3690 __ j(kEqual, &done);
3691 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3692 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3693 __ Bind(&done);
3694}
3695
Mark P Mendell73945692015-04-29 14:56:17 +00003696void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3697 Register low = loc.AsRegisterPairLow<Register>();
3698 Register high = loc.AsRegisterPairHigh<Register>();
3699 if (shift == 32) {
3700 // Need to copy the sign.
3701 DCHECK_NE(low, high);
3702 __ movl(low, high);
3703 __ sarl(high, Immediate(31));
3704 } else if (shift > 32) {
3705 DCHECK_NE(low, high);
3706 // High part becomes sign. Low part is shifted by shift - 32.
3707 __ movl(low, high);
3708 __ sarl(high, Immediate(31));
3709 __ sarl(low, Immediate(shift - 32));
3710 } else {
3711 // Between 1 and 31.
3712 __ shrd(low, high, Immediate(shift));
3713 __ sarl(high, Immediate(shift));
3714 }
3715}
3716
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003718 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003719 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3720 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3721 __ testl(shifter, Immediate(32));
3722 __ j(kEqual, &done);
3723 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3724 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3725 __ Bind(&done);
3726}
3727
Mark P Mendell73945692015-04-29 14:56:17 +00003728void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3729 Register low = loc.AsRegisterPairLow<Register>();
3730 Register high = loc.AsRegisterPairHigh<Register>();
3731 if (shift == 32) {
3732 // Shift by 32 is easy. Low gets high, and high gets 0.
3733 codegen_->EmitParallelMoves(
3734 loc.ToHigh(),
3735 loc.ToLow(),
3736 Primitive::kPrimInt,
3737 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3738 loc.ToHigh(),
3739 Primitive::kPrimInt);
3740 } else if (shift > 32) {
3741 // Low part is high >> (shift - 32). High part becomes 0.
3742 __ movl(low, high);
3743 __ shrl(low, Immediate(shift - 32));
3744 __ xorl(high, high);
3745 } else {
3746 // Between 1 and 31.
3747 __ shrd(low, high, Immediate(shift));
3748 __ shrl(high, Immediate(shift));
3749 }
3750}
3751
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003753 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3755 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3756 __ testl(shifter, Immediate(32));
3757 __ j(kEqual, &done);
3758 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3759 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3760 __ Bind(&done);
3761}
3762
3763void LocationsBuilderX86::VisitShl(HShl* shl) {
3764 HandleShift(shl);
3765}
3766
3767void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3768 HandleShift(shl);
3769}
3770
3771void LocationsBuilderX86::VisitShr(HShr* shr) {
3772 HandleShift(shr);
3773}
3774
3775void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3776 HandleShift(shr);
3777}
3778
3779void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3780 HandleShift(ushr);
3781}
3782
3783void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3784 HandleShift(ushr);
3785}
3786
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003787void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003788 LocationSummary* locations =
3789 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003790 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003791 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003792 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3793 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003794}
3795
3796void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003797 // Note: if heap poisoning is enabled, the entry point takes cares
3798 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003799 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3800 instruction,
3801 instruction->GetDexPc(),
3802 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003803 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003804 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003805}
3806
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003807void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3808 LocationSummary* locations =
3809 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3810 locations->SetOut(Location::RegisterLocation(EAX));
3811 InvokeRuntimeCallingConvention calling_convention;
3812 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003813 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003814 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003815}
3816
3817void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3818 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003819 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003820 // Note: if heap poisoning is enabled, the entry point takes cares
3821 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003822 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3823 instruction,
3824 instruction->GetDexPc(),
3825 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003826 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003827 DCHECK(!codegen_->IsLeafMethod());
3828}
3829
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003830void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003831 LocationSummary* locations =
3832 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003833 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3834 if (location.IsStackSlot()) {
3835 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3836 } else if (location.IsDoubleStackSlot()) {
3837 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003838 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003839 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003840}
3841
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003842void InstructionCodeGeneratorX86::VisitParameterValue(
3843 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3844}
3845
3846void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3847 LocationSummary* locations =
3848 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3849 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3850}
3851
3852void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003853}
3854
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003855void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003856 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003857 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003858 locations->SetInAt(0, Location::RequiresRegister());
3859 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003860}
3861
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003862void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3863 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003864 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003865 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003866 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003867 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003868 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003869 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003870 break;
3871
3872 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003873 __ notl(out.AsRegisterPairLow<Register>());
3874 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003875 break;
3876
3877 default:
3878 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3879 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003880}
3881
David Brazdil66d126e2015-04-03 16:02:44 +01003882void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3883 LocationSummary* locations =
3884 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3885 locations->SetInAt(0, Location::RequiresRegister());
3886 locations->SetOut(Location::SameAsFirstInput());
3887}
3888
3889void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003890 LocationSummary* locations = bool_not->GetLocations();
3891 Location in = locations->InAt(0);
3892 Location out = locations->Out();
3893 DCHECK(in.Equals(out));
3894 __ xorl(out.AsRegister<Register>(), Immediate(1));
3895}
3896
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003897void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003898 LocationSummary* locations =
3899 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003900 switch (compare->InputAt(0)->GetType()) {
3901 case Primitive::kPrimLong: {
3902 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003903 locations->SetInAt(1, Location::Any());
3904 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3905 break;
3906 }
3907 case Primitive::kPrimFloat:
3908 case Primitive::kPrimDouble: {
3909 locations->SetInAt(0, Location::RequiresFpuRegister());
3910 locations->SetInAt(1, Location::RequiresFpuRegister());
3911 locations->SetOut(Location::RequiresRegister());
3912 break;
3913 }
3914 default:
3915 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3916 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003917}
3918
3919void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003920 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003921 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003922 Location left = locations->InAt(0);
3923 Location right = locations->InAt(1);
3924
Mark Mendell0c9497d2015-08-21 09:30:05 -04003925 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003926 switch (compare->InputAt(0)->GetType()) {
3927 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003928 Register left_low = left.AsRegisterPairLow<Register>();
3929 Register left_high = left.AsRegisterPairHigh<Register>();
3930 int32_t val_low = 0;
3931 int32_t val_high = 0;
3932 bool right_is_const = false;
3933
3934 if (right.IsConstant()) {
3935 DCHECK(right.GetConstant()->IsLongConstant());
3936 right_is_const = true;
3937 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3938 val_low = Low32Bits(val);
3939 val_high = High32Bits(val);
3940 }
3941
Calin Juravleddb7df22014-11-25 20:56:51 +00003942 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003943 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003944 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003945 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003946 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003947 DCHECK(right_is_const) << right;
3948 if (val_high == 0) {
3949 __ testl(left_high, left_high);
3950 } else {
3951 __ cmpl(left_high, Immediate(val_high));
3952 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003953 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003954 __ j(kLess, &less); // Signed compare.
3955 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003956 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003957 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003958 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003959 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003960 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003961 DCHECK(right_is_const) << right;
3962 if (val_low == 0) {
3963 __ testl(left_low, left_low);
3964 } else {
3965 __ cmpl(left_low, Immediate(val_low));
3966 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003967 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003968 break;
3969 }
3970 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003971 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003972 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3973 break;
3974 }
3975 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003976 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003977 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003978 break;
3979 }
3980 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003981 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003982 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003983 __ movl(out, Immediate(0));
3984 __ j(kEqual, &done);
3985 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3986
3987 __ Bind(&greater);
3988 __ movl(out, Immediate(1));
3989 __ jmp(&done);
3990
3991 __ Bind(&less);
3992 __ movl(out, Immediate(-1));
3993
3994 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003995}
3996
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003997void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003998 LocationSummary* locations =
3999 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004000 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4001 locations->SetInAt(i, Location::Any());
4002 }
4003 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004004}
4005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004006void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004007 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004008}
4009
Calin Juravle52c48962014-12-16 17:02:57 +00004010void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
4011 /*
4012 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4013 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4014 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4015 */
4016 switch (kind) {
4017 case MemBarrierKind::kAnyAny: {
4018 __ mfence();
4019 break;
4020 }
4021 case MemBarrierKind::kAnyStore:
4022 case MemBarrierKind::kLoadAny:
4023 case MemBarrierKind::kStoreStore: {
4024 // nop
4025 break;
4026 }
4027 default:
4028 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004029 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004030}
4031
Vladimir Markodc151b22015-10-15 18:02:30 +01004032HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4033 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4034 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004035 switch (desired_dispatch_info.code_ptr_location) {
4036 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4037 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4038 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4039 // (Though the direct CALL ptr16:32 is available for consideration).
4040 return HInvokeStaticOrDirect::DispatchInfo {
4041 desired_dispatch_info.method_load_kind,
4042 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4043 desired_dispatch_info.method_load_data,
4044 0u
4045 };
4046 default:
4047 return desired_dispatch_info;
4048 }
4049}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004050
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004051Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4052 Register temp) {
4053 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004054 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004055 if (!invoke->GetLocations()->Intrinsified()) {
4056 return location.AsRegister<Register>();
4057 }
4058 // For intrinsics we allow any location, so it may be on the stack.
4059 if (!location.IsRegister()) {
4060 __ movl(temp, Address(ESP, location.GetStackIndex()));
4061 return temp;
4062 }
4063 // For register locations, check if the register was saved. If so, get it from the stack.
4064 // Note: There is a chance that the register was saved but not overwritten, so we could
4065 // save one load. However, since this is just an intrinsic slow path we prefer this
4066 // simple and more robust approach rather that trying to determine if that's the case.
4067 SlowPathCode* slow_path = GetCurrentSlowPath();
4068 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4069 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4070 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4071 __ movl(temp, Address(ESP, stack_offset));
4072 return temp;
4073 }
4074 return location.AsRegister<Register>();
4075}
4076
Vladimir Marko58155012015-08-19 12:49:41 +00004077void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4078 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4079 switch (invoke->GetMethodLoadKind()) {
4080 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4081 // temp = thread->string_init_entrypoint
4082 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4083 break;
4084 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004085 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004086 break;
4087 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4088 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4089 break;
4090 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4091 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4092 method_patches_.emplace_back(invoke->GetTargetMethod());
4093 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4094 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004095 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4096 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4097 temp.AsRegister<Register>());
4098 uint32_t offset = invoke->GetDexCacheArrayOffset();
4099 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4100 // Add the patch entry and bind its label at the end of the instruction.
4101 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4102 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4103 break;
4104 }
Vladimir Marko58155012015-08-19 12:49:41 +00004105 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004106 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004107 Register method_reg;
4108 Register reg = temp.AsRegister<Register>();
4109 if (current_method.IsRegister()) {
4110 method_reg = current_method.AsRegister<Register>();
4111 } else {
4112 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4113 DCHECK(!current_method.IsValid());
4114 method_reg = reg;
4115 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4116 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004117 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004118 __ movl(reg, Address(method_reg,
4119 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004120 // temp = temp[index_in_cache]
4121 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4122 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4123 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004124 }
Vladimir Marko58155012015-08-19 12:49:41 +00004125 }
4126
4127 switch (invoke->GetCodePtrLocation()) {
4128 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4129 __ call(GetFrameEntryLabel());
4130 break;
4131 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4132 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4133 Label* label = &relative_call_patches_.back().label;
4134 __ call(label); // Bind to the patch label, override at link time.
4135 __ Bind(label); // Bind the label at the end of the "call" insn.
4136 break;
4137 }
4138 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4139 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004140 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4141 LOG(FATAL) << "Unsupported";
4142 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004143 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4144 // (callee_method + offset_of_quick_compiled_code)()
4145 __ call(Address(callee_method.AsRegister<Register>(),
4146 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4147 kX86WordSize).Int32Value()));
4148 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004149 }
4150
4151 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004152}
4153
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004154void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4155 Register temp = temp_in.AsRegister<Register>();
4156 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4157 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004158
4159 // Use the calling convention instead of the location of the receiver, as
4160 // intrinsics may have put the receiver in a different register. In the intrinsics
4161 // slow path, the arguments have been moved to the right place, so here we are
4162 // guaranteed that the receiver is the first register of the calling convention.
4163 InvokeDexCallingConvention calling_convention;
4164 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004165 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004166 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004167 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004168 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004169 // Instead of simply (possibly) unpoisoning `temp` here, we should
4170 // emit a read barrier for the previous class reference load.
4171 // However this is not required in practice, as this is an
4172 // intermediate/temporary reference and because the current
4173 // concurrent copying collector keeps the from-space memory
4174 // intact/accessible until the end of the marking phase (the
4175 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004176 __ MaybeUnpoisonHeapReference(temp);
4177 // temp = temp->GetMethodAt(method_offset);
4178 __ movl(temp, Address(temp, method_offset));
4179 // call temp->GetEntryPoint();
4180 __ call(Address(
4181 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4182}
4183
Vladimir Marko58155012015-08-19 12:49:41 +00004184void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4185 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004186 size_t size =
4187 method_patches_.size() +
4188 relative_call_patches_.size() +
4189 pc_relative_dex_cache_patches_.size();
4190 linker_patches->reserve(size);
4191 // The label points to the end of the "movl" insn but the literal offset for method
4192 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4193 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004194 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004195 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004196 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4197 info.target_method.dex_file,
4198 info.target_method.dex_method_index));
4199 }
4200 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004201 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004202 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4203 info.target_method.dex_file,
4204 info.target_method.dex_method_index));
4205 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004206 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4207 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4208 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4209 &info.target_dex_file,
4210 GetMethodAddressOffset(),
4211 info.element_offset));
4212 }
Vladimir Marko58155012015-08-19 12:49:41 +00004213}
4214
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004215void CodeGeneratorX86::MarkGCCard(Register temp,
4216 Register card,
4217 Register object,
4218 Register value,
4219 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004220 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004221 if (value_can_be_null) {
4222 __ testl(value, value);
4223 __ j(kEqual, &is_null);
4224 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004225 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4226 __ movl(temp, object);
4227 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004228 __ movb(Address(temp, card, TIMES_1, 0),
4229 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004230 if (value_can_be_null) {
4231 __ Bind(&is_null);
4232 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004233}
4234
Calin Juravle52c48962014-12-16 17:02:57 +00004235void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4236 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004237
4238 bool object_field_get_with_read_barrier =
4239 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004240 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004241 new (GetGraph()->GetArena()) LocationSummary(instruction,
4242 kEmitCompilerReadBarrier ?
4243 LocationSummary::kCallOnSlowPath :
4244 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004245 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004246
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004247 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4248 locations->SetOut(Location::RequiresFpuRegister());
4249 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004250 // The output overlaps in case of long: we don't want the low move
4251 // to overwrite the object's location. Likewise, in the case of
4252 // an object field get with read barriers enabled, we do not want
4253 // the move to overwrite the object's location, as we need it to emit
4254 // the read barrier.
4255 locations->SetOut(
4256 Location::RequiresRegister(),
4257 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4258 Location::kOutputOverlap :
4259 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004260 }
Calin Juravle52c48962014-12-16 17:02:57 +00004261
4262 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4263 // Long values can be loaded atomically into an XMM using movsd.
4264 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
4265 // and then copy the XMM into the output 32bits at a time).
4266 locations->AddTemp(Location::RequiresFpuRegister());
4267 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004268}
4269
Calin Juravle52c48962014-12-16 17:02:57 +00004270void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4271 const FieldInfo& field_info) {
4272 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004273
Calin Juravle52c48962014-12-16 17:02:57 +00004274 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004275 Location base_loc = locations->InAt(0);
4276 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004277 Location out = locations->Out();
4278 bool is_volatile = field_info.IsVolatile();
4279 Primitive::Type field_type = field_info.GetFieldType();
4280 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4281
4282 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004283 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004284 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004285 break;
4286 }
4287
4288 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004289 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004290 break;
4291 }
4292
4293 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004294 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295 break;
4296 }
4297
4298 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004299 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004300 break;
4301 }
4302
4303 case Primitive::kPrimInt:
4304 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00004305 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306 break;
4307 }
4308
4309 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004310 if (is_volatile) {
4311 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4312 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004313 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004314 __ movd(out.AsRegisterPairLow<Register>(), temp);
4315 __ psrlq(temp, Immediate(32));
4316 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4317 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004318 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004319 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004320 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004321 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4322 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004323 break;
4324 }
4325
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004326 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004327 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004328 break;
4329 }
4330
4331 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004332 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004333 break;
4334 }
4335
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004336 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004337 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004338 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004339 }
Calin Juravle52c48962014-12-16 17:02:57 +00004340
Calin Juravle77520bc2015-01-12 18:45:46 +00004341 // Longs are handled in the switch.
4342 if (field_type != Primitive::kPrimLong) {
4343 codegen_->MaybeRecordImplicitNullCheck(instruction);
4344 }
4345
Calin Juravle52c48962014-12-16 17:02:57 +00004346 if (is_volatile) {
4347 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4348 }
Roland Levillain4d027112015-07-01 15:41:14 +01004349
4350 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004351 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004352 }
Calin Juravle52c48962014-12-16 17:02:57 +00004353}
4354
4355void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4356 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4357
4358 LocationSummary* locations =
4359 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4360 locations->SetInAt(0, Location::RequiresRegister());
4361 bool is_volatile = field_info.IsVolatile();
4362 Primitive::Type field_type = field_info.GetFieldType();
4363 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4364 || (field_type == Primitive::kPrimByte);
4365
4366 // The register allocator does not support multiple
4367 // inputs that die at entry with one in a specific register.
4368 if (is_byte_type) {
4369 // Ensure the value is in a byte register.
4370 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004371 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004372 if (is_volatile && field_type == Primitive::kPrimDouble) {
4373 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4374 locations->SetInAt(1, Location::RequiresFpuRegister());
4375 } else {
4376 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4377 }
4378 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4379 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004380 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004381
Calin Juravle52c48962014-12-16 17:02:57 +00004382 // 64bits value can be atomically written to an address with movsd and an XMM register.
4383 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4384 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4385 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4386 // isolated cases when we need this it isn't worth adding the extra complexity.
4387 locations->AddTemp(Location::RequiresFpuRegister());
4388 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004389 } else {
4390 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4391
4392 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4393 // Temporary registers for the write barrier.
4394 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4395 // Ensure the card is in a byte register.
4396 locations->AddTemp(Location::RegisterLocation(ECX));
4397 }
Calin Juravle52c48962014-12-16 17:02:57 +00004398 }
4399}
4400
4401void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004402 const FieldInfo& field_info,
4403 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004404 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4405
4406 LocationSummary* locations = instruction->GetLocations();
4407 Register base = locations->InAt(0).AsRegister<Register>();
4408 Location value = locations->InAt(1);
4409 bool is_volatile = field_info.IsVolatile();
4410 Primitive::Type field_type = field_info.GetFieldType();
4411 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004412 bool needs_write_barrier =
4413 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004414
4415 if (is_volatile) {
4416 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4417 }
4418
Mark Mendell81489372015-11-04 11:30:41 -05004419 bool maybe_record_implicit_null_check_done = false;
4420
Calin Juravle52c48962014-12-16 17:02:57 +00004421 switch (field_type) {
4422 case Primitive::kPrimBoolean:
4423 case Primitive::kPrimByte: {
4424 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4425 break;
4426 }
4427
4428 case Primitive::kPrimShort:
4429 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004430 if (value.IsConstant()) {
4431 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4432 __ movw(Address(base, offset), Immediate(v));
4433 } else {
4434 __ movw(Address(base, offset), value.AsRegister<Register>());
4435 }
Calin Juravle52c48962014-12-16 17:02:57 +00004436 break;
4437 }
4438
4439 case Primitive::kPrimInt:
4440 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004441 if (kPoisonHeapReferences && needs_write_barrier) {
4442 // Note that in the case where `value` is a null reference,
4443 // we do not enter this block, as the reference does not
4444 // need poisoning.
4445 DCHECK_EQ(field_type, Primitive::kPrimNot);
4446 Register temp = locations->GetTemp(0).AsRegister<Register>();
4447 __ movl(temp, value.AsRegister<Register>());
4448 __ PoisonHeapReference(temp);
4449 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004450 } else if (value.IsConstant()) {
4451 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4452 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004453 } else {
4454 __ movl(Address(base, offset), value.AsRegister<Register>());
4455 }
Calin Juravle52c48962014-12-16 17:02:57 +00004456 break;
4457 }
4458
4459 case Primitive::kPrimLong: {
4460 if (is_volatile) {
4461 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4462 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4463 __ movd(temp1, value.AsRegisterPairLow<Register>());
4464 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4465 __ punpckldq(temp1, temp2);
4466 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004467 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004468 } else if (value.IsConstant()) {
4469 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4470 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4471 codegen_->MaybeRecordImplicitNullCheck(instruction);
4472 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004473 } else {
4474 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004475 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004476 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4477 }
Mark Mendell81489372015-11-04 11:30:41 -05004478 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004479 break;
4480 }
4481
4482 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004483 if (value.IsConstant()) {
4484 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4485 __ movl(Address(base, offset), Immediate(v));
4486 } else {
4487 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4488 }
Calin Juravle52c48962014-12-16 17:02:57 +00004489 break;
4490 }
4491
4492 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004493 if (value.IsConstant()) {
4494 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4495 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4496 codegen_->MaybeRecordImplicitNullCheck(instruction);
4497 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4498 maybe_record_implicit_null_check_done = true;
4499 } else {
4500 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4501 }
Calin Juravle52c48962014-12-16 17:02:57 +00004502 break;
4503 }
4504
4505 case Primitive::kPrimVoid:
4506 LOG(FATAL) << "Unreachable type " << field_type;
4507 UNREACHABLE();
4508 }
4509
Mark Mendell81489372015-11-04 11:30:41 -05004510 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004511 codegen_->MaybeRecordImplicitNullCheck(instruction);
4512 }
4513
Roland Levillain4d027112015-07-01 15:41:14 +01004514 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004515 Register temp = locations->GetTemp(0).AsRegister<Register>();
4516 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004517 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004518 }
4519
Calin Juravle52c48962014-12-16 17:02:57 +00004520 if (is_volatile) {
4521 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4522 }
4523}
4524
4525void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4526 HandleFieldGet(instruction, instruction->GetFieldInfo());
4527}
4528
4529void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4530 HandleFieldGet(instruction, instruction->GetFieldInfo());
4531}
4532
4533void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4534 HandleFieldSet(instruction, instruction->GetFieldInfo());
4535}
4536
4537void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004538 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004539}
4540
4541void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4542 HandleFieldSet(instruction, instruction->GetFieldInfo());
4543}
4544
4545void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004546 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004547}
4548
4549void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4550 HandleFieldGet(instruction, instruction->GetFieldInfo());
4551}
4552
4553void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4554 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004555}
4556
Calin Juravlee460d1d2015-09-29 04:52:17 +01004557void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4558 HUnresolvedInstanceFieldGet* instruction) {
4559 FieldAccessCallingConventionX86 calling_convention;
4560 codegen_->CreateUnresolvedFieldLocationSummary(
4561 instruction, instruction->GetFieldType(), calling_convention);
4562}
4563
4564void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4565 HUnresolvedInstanceFieldGet* instruction) {
4566 FieldAccessCallingConventionX86 calling_convention;
4567 codegen_->GenerateUnresolvedFieldAccess(instruction,
4568 instruction->GetFieldType(),
4569 instruction->GetFieldIndex(),
4570 instruction->GetDexPc(),
4571 calling_convention);
4572}
4573
4574void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4575 HUnresolvedInstanceFieldSet* instruction) {
4576 FieldAccessCallingConventionX86 calling_convention;
4577 codegen_->CreateUnresolvedFieldLocationSummary(
4578 instruction, instruction->GetFieldType(), calling_convention);
4579}
4580
4581void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4582 HUnresolvedInstanceFieldSet* instruction) {
4583 FieldAccessCallingConventionX86 calling_convention;
4584 codegen_->GenerateUnresolvedFieldAccess(instruction,
4585 instruction->GetFieldType(),
4586 instruction->GetFieldIndex(),
4587 instruction->GetDexPc(),
4588 calling_convention);
4589}
4590
4591void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4592 HUnresolvedStaticFieldGet* instruction) {
4593 FieldAccessCallingConventionX86 calling_convention;
4594 codegen_->CreateUnresolvedFieldLocationSummary(
4595 instruction, instruction->GetFieldType(), calling_convention);
4596}
4597
4598void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4599 HUnresolvedStaticFieldGet* instruction) {
4600 FieldAccessCallingConventionX86 calling_convention;
4601 codegen_->GenerateUnresolvedFieldAccess(instruction,
4602 instruction->GetFieldType(),
4603 instruction->GetFieldIndex(),
4604 instruction->GetDexPc(),
4605 calling_convention);
4606}
4607
4608void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4609 HUnresolvedStaticFieldSet* instruction) {
4610 FieldAccessCallingConventionX86 calling_convention;
4611 codegen_->CreateUnresolvedFieldLocationSummary(
4612 instruction, instruction->GetFieldType(), calling_convention);
4613}
4614
4615void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4616 HUnresolvedStaticFieldSet* instruction) {
4617 FieldAccessCallingConventionX86 calling_convention;
4618 codegen_->GenerateUnresolvedFieldAccess(instruction,
4619 instruction->GetFieldType(),
4620 instruction->GetFieldIndex(),
4621 instruction->GetDexPc(),
4622 calling_convention);
4623}
4624
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004625void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004626 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4627 ? LocationSummary::kCallOnSlowPath
4628 : LocationSummary::kNoCall;
4629 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4630 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004631 ? Location::RequiresRegister()
4632 : Location::Any();
4633 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004634 if (instruction->HasUses()) {
4635 locations->SetOut(Location::SameAsFirstInput());
4636 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004637}
4638
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004639void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004640 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4641 return;
4642 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004643 LocationSummary* locations = instruction->GetLocations();
4644 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004645
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004646 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4647 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4648}
4649
4650void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004651 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004652 codegen_->AddSlowPath(slow_path);
4653
4654 LocationSummary* locations = instruction->GetLocations();
4655 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004656
4657 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004658 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004659 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004661 } else {
4662 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004663 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004664 __ jmp(slow_path->GetEntryLabel());
4665 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004666 }
4667 __ j(kEqual, slow_path->GetEntryLabel());
4668}
4669
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004670void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004671 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004672 GenerateImplicitNullCheck(instruction);
4673 } else {
4674 GenerateExplicitNullCheck(instruction);
4675 }
4676}
4677
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004678void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004679 bool object_array_get_with_read_barrier =
4680 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004681 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004682 new (GetGraph()->GetArena()) LocationSummary(instruction,
4683 object_array_get_with_read_barrier ?
4684 LocationSummary::kCallOnSlowPath :
4685 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004686 locations->SetInAt(0, Location::RequiresRegister());
4687 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004688 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4689 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4690 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004691 // The output overlaps in case of long: we don't want the low move
4692 // to overwrite the array's location. Likewise, in the case of an
4693 // object array get with read barriers enabled, we do not want the
4694 // move to overwrite the array's location, as we need it to emit
4695 // the read barrier.
4696 locations->SetOut(
4697 Location::RequiresRegister(),
4698 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4699 Location::kOutputOverlap :
4700 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004701 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004702}
4703
4704void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4705 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004706 Location obj_loc = locations->InAt(0);
4707 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004708 Location index = locations->InAt(1);
4709
Calin Juravle77520bc2015-01-12 18:45:46 +00004710 Primitive::Type type = instruction->GetType();
4711 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004712 case Primitive::kPrimBoolean: {
4713 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004714 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004715 if (index.IsConstant()) {
4716 __ movzxb(out, Address(obj,
4717 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4718 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004719 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004720 }
4721 break;
4722 }
4723
4724 case Primitive::kPrimByte: {
4725 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004726 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727 if (index.IsConstant()) {
4728 __ movsxb(out, Address(obj,
4729 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4730 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004731 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004732 }
4733 break;
4734 }
4735
4736 case Primitive::kPrimShort: {
4737 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004738 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004739 if (index.IsConstant()) {
4740 __ movsxw(out, Address(obj,
4741 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4742 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004743 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004744 }
4745 break;
4746 }
4747
4748 case Primitive::kPrimChar: {
4749 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004750 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004751 if (index.IsConstant()) {
4752 __ movzxw(out, Address(obj,
4753 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4754 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004755 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004756 }
4757 break;
4758 }
4759
4760 case Primitive::kPrimInt:
4761 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004762 static_assert(
4763 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4764 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004765 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004766 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004767 if (index.IsConstant()) {
4768 __ movl(out, Address(obj,
4769 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4770 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004771 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004772 }
4773 break;
4774 }
4775
4776 case Primitive::kPrimLong: {
4777 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004778 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004779 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004780 if (index.IsConstant()) {
4781 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004782 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004783 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004784 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004785 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004786 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004787 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004788 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004789 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004790 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004791 }
4792 break;
4793 }
4794
Mark Mendell7c8d0092015-01-26 11:21:33 -05004795 case Primitive::kPrimFloat: {
4796 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4797 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4798 if (index.IsConstant()) {
4799 __ movss(out, Address(obj,
4800 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4801 } else {
4802 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4803 }
4804 break;
4805 }
4806
4807 case Primitive::kPrimDouble: {
4808 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4809 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4810 if (index.IsConstant()) {
4811 __ movsd(out, Address(obj,
4812 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4813 } else {
4814 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4815 }
4816 break;
4817 }
4818
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004819 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004820 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004821 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004822 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004823
4824 if (type != Primitive::kPrimLong) {
4825 codegen_->MaybeRecordImplicitNullCheck(instruction);
4826 }
Roland Levillain4d027112015-07-01 15:41:14 +01004827
4828 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004829 static_assert(
4830 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4831 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4832 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4833 Location out = locations->Out();
4834 if (index.IsConstant()) {
4835 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4836 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4837 } else {
4838 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4839 }
Roland Levillain4d027112015-07-01 15:41:14 +01004840 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004841}
4842
4843void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004844 // This location builder might end up asking to up to four registers, which is
4845 // not currently possible for baseline. The situation in which we need four
4846 // registers cannot be met by baseline though, because it has not run any
4847 // optimization.
4848
Nicolas Geoffray39468442014-09-02 15:17:15 +01004849 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004850
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004851 bool needs_write_barrier =
4852 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004853 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4854 bool object_array_set_with_read_barrier =
4855 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004856
Nicolas Geoffray39468442014-09-02 15:17:15 +01004857 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4858 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004859 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4860 LocationSummary::kCallOnSlowPath :
4861 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004862
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004863 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4864 || (value_type == Primitive::kPrimByte);
4865 // We need the inputs to be different than the output in case of long operation.
4866 // In case of a byte operation, the register allocator does not support multiple
4867 // inputs that die at entry with one in a specific register.
4868 locations->SetInAt(0, Location::RequiresRegister());
4869 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4870 if (is_byte_type) {
4871 // Ensure the value is in a byte register.
4872 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4873 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004874 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004875 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004876 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4877 }
4878 if (needs_write_barrier) {
4879 // Temporary registers for the write barrier.
4880 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4881 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004882 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004883 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004884}
4885
4886void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4887 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004888 Location array_loc = locations->InAt(0);
4889 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004890 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004891 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004892 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004893 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4894 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4895 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004896 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004897 bool needs_write_barrier =
4898 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899
4900 switch (value_type) {
4901 case Primitive::kPrimBoolean:
4902 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004903 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4904 Address address = index.IsConstant()
4905 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4906 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4907 if (value.IsRegister()) {
4908 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004909 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004910 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004912 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004913 break;
4914 }
4915
4916 case Primitive::kPrimShort:
4917 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004918 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4919 Address address = index.IsConstant()
4920 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4921 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4922 if (value.IsRegister()) {
4923 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004924 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004925 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004927 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 break;
4929 }
4930
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004931 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004932 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4933 Address address = index.IsConstant()
4934 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4935 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004936
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004937 if (!value.IsRegister()) {
4938 // Just setting null.
4939 DCHECK(instruction->InputAt(2)->IsNullConstant());
4940 DCHECK(value.IsConstant()) << value;
4941 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004942 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004943 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004944 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004945 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004947
4948 DCHECK(needs_write_barrier);
4949 Register register_value = value.AsRegister<Register>();
4950 NearLabel done, not_null, do_put;
4951 SlowPathCode* slow_path = nullptr;
4952 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004953 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004954 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4955 codegen_->AddSlowPath(slow_path);
4956 if (instruction->GetValueCanBeNull()) {
4957 __ testl(register_value, register_value);
4958 __ j(kNotEqual, &not_null);
4959 __ movl(address, Immediate(0));
4960 codegen_->MaybeRecordImplicitNullCheck(instruction);
4961 __ jmp(&done);
4962 __ Bind(&not_null);
4963 }
4964
Roland Levillain0d5a2812015-11-13 10:07:31 +00004965 if (kEmitCompilerReadBarrier) {
4966 // When read barriers are enabled, the type checking
4967 // instrumentation requires two read barriers:
4968 //
4969 // __ movl(temp2, temp);
4970 // // /* HeapReference<Class> */ temp = temp->component_type_
4971 // __ movl(temp, Address(temp, component_offset));
4972 // codegen_->GenerateReadBarrier(
4973 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4974 //
4975 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4976 // __ movl(temp2, Address(register_value, class_offset));
4977 // codegen_->GenerateReadBarrier(
4978 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4979 //
4980 // __ cmpl(temp, temp2);
4981 //
4982 // However, the second read barrier may trash `temp`, as it
4983 // is a temporary register, and as such would not be saved
4984 // along with live registers before calling the runtime (nor
4985 // restored afterwards). So in this case, we bail out and
4986 // delegate the work to the array set slow path.
4987 //
4988 // TODO: Extend the register allocator to support a new
4989 // "(locally) live temp" location so as to avoid always
4990 // going into the slow path when read barriers are enabled.
4991 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004992 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004993 // /* HeapReference<Class> */ temp = array->klass_
4994 __ movl(temp, Address(array, class_offset));
4995 codegen_->MaybeRecordImplicitNullCheck(instruction);
4996 __ MaybeUnpoisonHeapReference(temp);
4997
4998 // /* HeapReference<Class> */ temp = temp->component_type_
4999 __ movl(temp, Address(temp, component_offset));
5000 // If heap poisoning is enabled, no need to unpoison `temp`
5001 // nor the object reference in `register_value->klass`, as
5002 // we are comparing two poisoned references.
5003 __ cmpl(temp, Address(register_value, class_offset));
5004
5005 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5006 __ j(kEqual, &do_put);
5007 // If heap poisoning is enabled, the `temp` reference has
5008 // not been unpoisoned yet; unpoison it now.
5009 __ MaybeUnpoisonHeapReference(temp);
5010
5011 // /* HeapReference<Class> */ temp = temp->super_class_
5012 __ movl(temp, Address(temp, super_offset));
5013 // If heap poisoning is enabled, no need to unpoison
5014 // `temp`, as we are comparing against null below.
5015 __ testl(temp, temp);
5016 __ j(kNotEqual, slow_path->GetEntryLabel());
5017 __ Bind(&do_put);
5018 } else {
5019 __ j(kNotEqual, slow_path->GetEntryLabel());
5020 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005021 }
5022 }
5023
5024 if (kPoisonHeapReferences) {
5025 __ movl(temp, register_value);
5026 __ PoisonHeapReference(temp);
5027 __ movl(address, temp);
5028 } else {
5029 __ movl(address, register_value);
5030 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005031 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005032 codegen_->MaybeRecordImplicitNullCheck(instruction);
5033 }
5034
5035 Register card = locations->GetTemp(1).AsRegister<Register>();
5036 codegen_->MarkGCCard(
5037 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5038 __ Bind(&done);
5039
5040 if (slow_path != nullptr) {
5041 __ Bind(slow_path->GetExitLabel());
5042 }
5043
5044 break;
5045 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005046
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005047 case Primitive::kPrimInt: {
5048 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5049 Address address = index.IsConstant()
5050 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5051 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5052 if (value.IsRegister()) {
5053 __ movl(address, value.AsRegister<Register>());
5054 } else {
5055 DCHECK(value.IsConstant()) << value;
5056 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5057 __ movl(address, Immediate(v));
5058 }
5059 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005060 break;
5061 }
5062
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 case Primitive::kPrimLong: {
5064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005065 if (index.IsConstant()) {
5066 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005067 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005068 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005069 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005070 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005071 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005072 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005073 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005074 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005075 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005076 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005077 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005079 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005080 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005081 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005082 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005083 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005084 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005085 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005086 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005087 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005088 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005089 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005090 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005091 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005092 Immediate(High32Bits(val)));
5093 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094 }
5095 break;
5096 }
5097
Mark Mendell7c8d0092015-01-26 11:21:33 -05005098 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005099 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5100 Address address = index.IsConstant()
5101 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5102 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005103 if (value.IsFpuRegister()) {
5104 __ movss(address, value.AsFpuRegister<XmmRegister>());
5105 } else {
5106 DCHECK(value.IsConstant());
5107 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5108 __ movl(address, Immediate(v));
5109 }
5110 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005111 break;
5112 }
5113
5114 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005115 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5116 Address address = index.IsConstant()
5117 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5118 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005119 if (value.IsFpuRegister()) {
5120 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5121 } else {
5122 DCHECK(value.IsConstant());
5123 Address address_hi = index.IsConstant() ?
5124 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5125 offset + kX86WordSize) :
5126 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5127 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5128 __ movl(address, Immediate(Low32Bits(v)));
5129 codegen_->MaybeRecordImplicitNullCheck(instruction);
5130 __ movl(address_hi, Immediate(High32Bits(v)));
5131 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005132 break;
5133 }
5134
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 case Primitive::kPrimVoid:
5136 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005137 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005138 }
5139}
5140
5141void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5142 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005143 locations->SetInAt(0, Location::RequiresRegister());
5144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005145}
5146
5147void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5148 LocationSummary* locations = instruction->GetLocations();
5149 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005150 Register obj = locations->InAt(0).AsRegister<Register>();
5151 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005152 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005154}
5155
5156void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005157 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5158 ? LocationSummary::kCallOnSlowPath
5159 : LocationSummary::kNoCall;
5160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005161 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005162 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005163 if (instruction->HasUses()) {
5164 locations->SetOut(Location::SameAsFirstInput());
5165 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166}
5167
5168void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5169 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005170 Location index_loc = locations->InAt(0);
5171 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005172 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005173 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005174
Mark Mendell99dbd682015-04-22 16:18:52 -04005175 if (length_loc.IsConstant()) {
5176 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5177 if (index_loc.IsConstant()) {
5178 // BCE will remove the bounds check if we are guarenteed to pass.
5179 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5180 if (index < 0 || index >= length) {
5181 codegen_->AddSlowPath(slow_path);
5182 __ jmp(slow_path->GetEntryLabel());
5183 } else {
5184 // Some optimization after BCE may have generated this, and we should not
5185 // generate a bounds check if it is a valid range.
5186 }
5187 return;
5188 }
5189
5190 // We have to reverse the jump condition because the length is the constant.
5191 Register index_reg = index_loc.AsRegister<Register>();
5192 __ cmpl(index_reg, Immediate(length));
5193 codegen_->AddSlowPath(slow_path);
5194 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005195 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005196 Register length = length_loc.AsRegister<Register>();
5197 if (index_loc.IsConstant()) {
5198 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5199 __ cmpl(length, Immediate(value));
5200 } else {
5201 __ cmpl(length, index_loc.AsRegister<Register>());
5202 }
5203 codegen_->AddSlowPath(slow_path);
5204 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005205 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206}
5207
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005208void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5209 temp->SetLocations(nullptr);
5210}
5211
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005212void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005213 // Nothing to do, this is driven by the code generator.
5214}
5215
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005216void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005217 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005218}
5219
5220void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005221 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5222}
5223
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005224void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5225 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5226}
5227
5228void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005229 HBasicBlock* block = instruction->GetBlock();
5230 if (block->GetLoopInformation() != nullptr) {
5231 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5232 // The back edge will generate the suspend check.
5233 return;
5234 }
5235 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5236 // The goto will generate the suspend check.
5237 return;
5238 }
5239 GenerateSuspendCheck(instruction, nullptr);
5240}
5241
5242void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5243 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005244 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005245 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5246 if (slow_path == nullptr) {
5247 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5248 instruction->SetSlowPath(slow_path);
5249 codegen_->AddSlowPath(slow_path);
5250 if (successor != nullptr) {
5251 DCHECK(successor->IsLoopHeader());
5252 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5253 }
5254 } else {
5255 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5256 }
5257
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005258 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005259 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005260 if (successor == nullptr) {
5261 __ j(kNotEqual, slow_path->GetEntryLabel());
5262 __ Bind(slow_path->GetReturnLabel());
5263 } else {
5264 __ j(kEqual, codegen_->GetLabelOf(successor));
5265 __ jmp(slow_path->GetEntryLabel());
5266 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005267}
5268
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005269X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5270 return codegen_->GetAssembler();
5271}
5272
Mark Mendell7c8d0092015-01-26 11:21:33 -05005273void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005274 ScratchRegisterScope ensure_scratch(
5275 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5276 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5277 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5278 __ movl(temp_reg, Address(ESP, src + stack_offset));
5279 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005280}
5281
5282void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005283 ScratchRegisterScope ensure_scratch(
5284 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5285 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5286 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5287 __ movl(temp_reg, Address(ESP, src + stack_offset));
5288 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5289 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5290 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005291}
5292
5293void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005294 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005295 Location source = move->GetSource();
5296 Location destination = move->GetDestination();
5297
5298 if (source.IsRegister()) {
5299 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005300 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005301 } else {
5302 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005303 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005304 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005305 } else if (source.IsFpuRegister()) {
5306 if (destination.IsFpuRegister()) {
5307 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5308 } else if (destination.IsStackSlot()) {
5309 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5310 } else {
5311 DCHECK(destination.IsDoubleStackSlot());
5312 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5313 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005314 } else if (source.IsStackSlot()) {
5315 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005316 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005317 } else if (destination.IsFpuRegister()) {
5318 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005319 } else {
5320 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005321 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5322 }
5323 } else if (source.IsDoubleStackSlot()) {
5324 if (destination.IsFpuRegister()) {
5325 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5326 } else {
5327 DCHECK(destination.IsDoubleStackSlot()) << destination;
5328 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005329 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005330 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005331 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005332 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005333 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005334 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005335 if (value == 0) {
5336 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5337 } else {
5338 __ movl(destination.AsRegister<Register>(), Immediate(value));
5339 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005340 } else {
5341 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005342 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005343 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005344 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005345 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005346 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005347 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005348 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005349 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5350 if (value == 0) {
5351 // Easy handling of 0.0.
5352 __ xorps(dest, dest);
5353 } else {
5354 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005355 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5356 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5357 __ movl(temp, Immediate(value));
5358 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005359 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005360 } else {
5361 DCHECK(destination.IsStackSlot()) << destination;
5362 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5363 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005364 } else if (constant->IsLongConstant()) {
5365 int64_t value = constant->AsLongConstant()->GetValue();
5366 int32_t low_value = Low32Bits(value);
5367 int32_t high_value = High32Bits(value);
5368 Immediate low(low_value);
5369 Immediate high(high_value);
5370 if (destination.IsDoubleStackSlot()) {
5371 __ movl(Address(ESP, destination.GetStackIndex()), low);
5372 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5373 } else {
5374 __ movl(destination.AsRegisterPairLow<Register>(), low);
5375 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5376 }
5377 } else {
5378 DCHECK(constant->IsDoubleConstant());
5379 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005380 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005381 int32_t low_value = Low32Bits(value);
5382 int32_t high_value = High32Bits(value);
5383 Immediate low(low_value);
5384 Immediate high(high_value);
5385 if (destination.IsFpuRegister()) {
5386 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5387 if (value == 0) {
5388 // Easy handling of 0.0.
5389 __ xorpd(dest, dest);
5390 } else {
5391 __ pushl(high);
5392 __ pushl(low);
5393 __ movsd(dest, Address(ESP, 0));
5394 __ addl(ESP, Immediate(8));
5395 }
5396 } else {
5397 DCHECK(destination.IsDoubleStackSlot()) << destination;
5398 __ movl(Address(ESP, destination.GetStackIndex()), low);
5399 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5400 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005401 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005402 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005403 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005404 }
5405}
5406
Mark Mendella5c19ce2015-04-01 12:51:05 -04005407void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005408 Register suggested_scratch = reg == EAX ? EBX : EAX;
5409 ScratchRegisterScope ensure_scratch(
5410 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5411
5412 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5413 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5414 __ movl(Address(ESP, mem + stack_offset), reg);
5415 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005416}
5417
Mark Mendell7c8d0092015-01-26 11:21:33 -05005418void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005419 ScratchRegisterScope ensure_scratch(
5420 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5421
5422 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5423 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5424 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5425 __ movss(Address(ESP, mem + stack_offset), reg);
5426 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005427}
5428
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005429void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005430 ScratchRegisterScope ensure_scratch1(
5431 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005432
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005433 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5434 ScratchRegisterScope ensure_scratch2(
5435 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005436
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005437 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5438 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5439 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5440 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5441 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5442 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005443}
5444
5445void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005446 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005447 Location source = move->GetSource();
5448 Location destination = move->GetDestination();
5449
5450 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005451 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5452 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5453 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5454 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5455 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005456 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005457 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005458 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005459 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005460 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5461 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005462 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5463 // Use XOR Swap algorithm to avoid a temporary.
5464 DCHECK_NE(source.reg(), destination.reg());
5465 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5466 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5467 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5468 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5469 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5470 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5471 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005472 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5473 // Take advantage of the 16 bytes in the XMM register.
5474 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5475 Address stack(ESP, destination.GetStackIndex());
5476 // Load the double into the high doubleword.
5477 __ movhpd(reg, stack);
5478
5479 // Store the low double into the destination.
5480 __ movsd(stack, reg);
5481
5482 // Move the high double to the low double.
5483 __ psrldq(reg, Immediate(8));
5484 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5485 // Take advantage of the 16 bytes in the XMM register.
5486 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5487 Address stack(ESP, source.GetStackIndex());
5488 // Load the double into the high doubleword.
5489 __ movhpd(reg, stack);
5490
5491 // Store the low double into the destination.
5492 __ movsd(stack, reg);
5493
5494 // Move the high double to the low double.
5495 __ psrldq(reg, Immediate(8));
5496 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5497 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5498 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005499 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005500 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005501 }
5502}
5503
5504void ParallelMoveResolverX86::SpillScratch(int reg) {
5505 __ pushl(static_cast<Register>(reg));
5506}
5507
5508void ParallelMoveResolverX86::RestoreScratch(int reg) {
5509 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005510}
5511
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005512void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005513 InvokeRuntimeCallingConvention calling_convention;
5514 CodeGenerator::CreateLoadClassLocationSummary(
5515 cls,
5516 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005517 Location::RegisterLocation(EAX),
5518 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005519}
5520
5521void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005522 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005523 if (cls->NeedsAccessCheck()) {
5524 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5525 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5526 cls,
5527 cls->GetDexPc(),
5528 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005529 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005530 return;
5531 }
5532
Roland Levillain0d5a2812015-11-13 10:07:31 +00005533 Location out_loc = locations->Out();
5534 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005535 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005536
Calin Juravle580b6092015-10-06 17:35:58 +01005537 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005538 DCHECK(!cls->CanCallRuntime());
5539 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005540 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5541 if (kEmitCompilerReadBarrier) {
5542 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5543 __ leal(out, Address(current_method, declaring_class_offset));
5544 // /* mirror::Class* */ out = out->Read()
5545 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5546 } else {
5547 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5548 __ movl(out, Address(current_method, declaring_class_offset));
5549 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005550 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005551 // /* GcRoot<mirror::Class>[] */ out =
5552 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5553 __ movl(out, Address(current_method,
5554 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5555
5556 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5557 if (kEmitCompilerReadBarrier) {
5558 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5559 __ leal(out, Address(out, cache_offset));
5560 // /* mirror::Class* */ out = out->Read()
5561 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5562 } else {
5563 // /* GcRoot<mirror::Class> */ out = out[type_index]
5564 __ movl(out, Address(out, cache_offset));
5565 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005566
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005567 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5568 DCHECK(cls->CanCallRuntime());
5569 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5570 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5571 codegen_->AddSlowPath(slow_path);
5572
5573 if (!cls->IsInDexCache()) {
5574 __ testl(out, out);
5575 __ j(kEqual, slow_path->GetEntryLabel());
5576 }
5577
5578 if (cls->MustGenerateClinitCheck()) {
5579 GenerateClassInitializationCheck(slow_path, out);
5580 } else {
5581 __ Bind(slow_path->GetExitLabel());
5582 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005583 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005584 }
5585}
5586
5587void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5588 LocationSummary* locations =
5589 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5590 locations->SetInAt(0, Location::RequiresRegister());
5591 if (check->HasUses()) {
5592 locations->SetOut(Location::SameAsFirstInput());
5593 }
5594}
5595
5596void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005597 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005598 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005599 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005600 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005601 GenerateClassInitializationCheck(slow_path,
5602 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005603}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005604
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005605void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005606 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005607 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5608 Immediate(mirror::Class::kStatusInitialized));
5609 __ j(kLess, slow_path->GetEntryLabel());
5610 __ Bind(slow_path->GetExitLabel());
5611 // No need for memory fence, thanks to the X86 memory model.
5612}
5613
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005614void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
5615 LocationSummary* locations =
5616 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kCallOnSlowPath);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005617 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005618 locations->SetOut(Location::RequiresRegister());
5619}
5620
5621void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005622 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005623 codegen_->AddSlowPath(slow_path);
5624
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005625 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005626 Location out_loc = locations->Out();
5627 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005628 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005629
5630 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5631 if (kEmitCompilerReadBarrier) {
5632 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5633 __ leal(out, Address(current_method, declaring_class_offset));
5634 // /* mirror::Class* */ out = out->Read()
5635 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5636 } else {
5637 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5638 __ movl(out, Address(current_method, declaring_class_offset));
5639 }
5640
5641 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005642 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005643
5644 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5645 if (kEmitCompilerReadBarrier) {
5646 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5647 __ leal(out, Address(out, cache_offset));
5648 // /* mirror::String* */ out = out->Read()
5649 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5650 } else {
5651 // /* GcRoot<mirror::String> */ out = out[string_index]
5652 __ movl(out, Address(out, cache_offset));
5653 }
5654
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005655 __ testl(out, out);
5656 __ j(kEqual, slow_path->GetEntryLabel());
5657 __ Bind(slow_path->GetExitLabel());
5658}
5659
David Brazdilcb1c0552015-08-04 16:22:25 +01005660static Address GetExceptionTlsAddress() {
5661 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5662}
5663
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005664void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5665 LocationSummary* locations =
5666 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5667 locations->SetOut(Location::RequiresRegister());
5668}
5669
5670void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005671 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5672}
5673
5674void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5675 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5676}
5677
5678void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5679 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005680}
5681
5682void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5683 LocationSummary* locations =
5684 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5685 InvokeRuntimeCallingConvention calling_convention;
5686 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5687}
5688
5689void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005690 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5691 instruction,
5692 instruction->GetDexPc(),
5693 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005694 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005695}
5696
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005697void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005698 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005699 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5700 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005701 case TypeCheckKind::kExactCheck:
5702 case TypeCheckKind::kAbstractClassCheck:
5703 case TypeCheckKind::kClassHierarchyCheck:
5704 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005705 call_kind =
5706 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005707 break;
5708 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005709 case TypeCheckKind::kUnresolvedCheck:
5710 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005711 call_kind = LocationSummary::kCallOnSlowPath;
5712 break;
5713 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005714
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005715 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005716 locations->SetInAt(0, Location::RequiresRegister());
5717 locations->SetInAt(1, Location::Any());
5718 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5719 locations->SetOut(Location::RequiresRegister());
5720 // When read barriers are enabled, we need a temporary register for
5721 // some cases.
5722 if (kEmitCompilerReadBarrier &&
5723 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5724 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5725 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5726 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005727 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005728}
5729
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005730void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005731 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732 Location obj_loc = locations->InAt(0);
5733 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005734 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005735 Location out_loc = locations->Out();
5736 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005737 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005738 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5739 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5740 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005741 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005742 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005743
5744 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005745 // Avoid null check if we know obj is not null.
5746 if (instruction->MustDoNullCheck()) {
5747 __ testl(obj, obj);
5748 __ j(kEqual, &zero);
5749 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005750
Roland Levillain0d5a2812015-11-13 10:07:31 +00005751 // /* HeapReference<Class> */ out = obj->klass_
5752 __ movl(out, Address(obj, class_offset));
5753 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005754
5755 switch (instruction->GetTypeCheckKind()) {
5756 case TypeCheckKind::kExactCheck: {
5757 if (cls.IsRegister()) {
5758 __ cmpl(out, cls.AsRegister<Register>());
5759 } else {
5760 DCHECK(cls.IsStackSlot()) << cls;
5761 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5762 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005763
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005764 // Classes must be equal for the instanceof to succeed.
5765 __ j(kNotEqual, &zero);
5766 __ movl(out, Immediate(1));
5767 __ jmp(&done);
5768 break;
5769 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005770
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005771 case TypeCheckKind::kAbstractClassCheck: {
5772 // If the class is abstract, we eagerly fetch the super class of the
5773 // object to avoid doing a comparison we know will fail.
5774 NearLabel loop;
5775 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005776 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5777 if (kEmitCompilerReadBarrier) {
5778 // Save the value of `out` into `temp` before overwriting it
5779 // in the following move operation, as we will need it for the
5780 // read barrier below.
5781 Register temp = temp_loc.AsRegister<Register>();
5782 __ movl(temp, out);
5783 }
5784 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005785 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005786 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 __ testl(out, out);
5788 // If `out` is null, we use it for the result, and jump to `done`.
5789 __ j(kEqual, &done);
5790 if (cls.IsRegister()) {
5791 __ cmpl(out, cls.AsRegister<Register>());
5792 } else {
5793 DCHECK(cls.IsStackSlot()) << cls;
5794 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5795 }
5796 __ j(kNotEqual, &loop);
5797 __ movl(out, Immediate(1));
5798 if (zero.IsLinked()) {
5799 __ jmp(&done);
5800 }
5801 break;
5802 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005803
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005804 case TypeCheckKind::kClassHierarchyCheck: {
5805 // Walk over the class hierarchy to find a match.
5806 NearLabel loop, success;
5807 __ Bind(&loop);
5808 if (cls.IsRegister()) {
5809 __ cmpl(out, cls.AsRegister<Register>());
5810 } else {
5811 DCHECK(cls.IsStackSlot()) << cls;
5812 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5813 }
5814 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005815 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5816 if (kEmitCompilerReadBarrier) {
5817 // Save the value of `out` into `temp` before overwriting it
5818 // in the following move operation, as we will need it for the
5819 // read barrier below.
5820 Register temp = temp_loc.AsRegister<Register>();
5821 __ movl(temp, out);
5822 }
5823 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005824 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005825 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 __ testl(out, out);
5827 __ j(kNotEqual, &loop);
5828 // If `out` is null, we use it for the result, and jump to `done`.
5829 __ jmp(&done);
5830 __ Bind(&success);
5831 __ movl(out, Immediate(1));
5832 if (zero.IsLinked()) {
5833 __ jmp(&done);
5834 }
5835 break;
5836 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005837
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005838 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005839 // Do an exact check.
5840 NearLabel exact_check;
5841 if (cls.IsRegister()) {
5842 __ cmpl(out, cls.AsRegister<Register>());
5843 } else {
5844 DCHECK(cls.IsStackSlot()) << cls;
5845 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5846 }
5847 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005848 // Otherwise, we need to check that the object's class is a non-primitive array.
5849 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5850 if (kEmitCompilerReadBarrier) {
5851 // Save the value of `out` into `temp` before overwriting it
5852 // in the following move operation, as we will need it for the
5853 // read barrier below.
5854 Register temp = temp_loc.AsRegister<Register>();
5855 __ movl(temp, out);
5856 }
5857 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005858 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005859 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 __ testl(out, out);
5861 // If `out` is null, we use it for the result, and jump to `done`.
5862 __ j(kEqual, &done);
5863 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5864 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005865 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005866 __ movl(out, Immediate(1));
5867 __ jmp(&done);
5868 break;
5869 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005870
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005871 case TypeCheckKind::kArrayCheck: {
5872 if (cls.IsRegister()) {
5873 __ cmpl(out, cls.AsRegister<Register>());
5874 } else {
5875 DCHECK(cls.IsStackSlot()) << cls;
5876 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5877 }
5878 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005879 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5880 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005881 codegen_->AddSlowPath(slow_path);
5882 __ j(kNotEqual, slow_path->GetEntryLabel());
5883 __ movl(out, Immediate(1));
5884 if (zero.IsLinked()) {
5885 __ jmp(&done);
5886 }
5887 break;
5888 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005889
Calin Juravle98893e12015-10-02 21:05:03 +01005890 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891 case TypeCheckKind::kInterfaceCheck: {
5892 // Note that we indeed only call on slow path, but we always go
5893 // into the slow path for the unresolved & interface check
5894 // cases.
5895 //
5896 // We cannot directly call the InstanceofNonTrivial runtime
5897 // entry point without resorting to a type checking slow path
5898 // here (i.e. by calling InvokeRuntime directly), as it would
5899 // require to assign fixed registers for the inputs of this
5900 // HInstanceOf instruction (following the runtime calling
5901 // convention), which might be cluttered by the potential first
5902 // read barrier emission at the beginning of this method.
5903 DCHECK(locations->OnlyCallsOnSlowPath());
5904 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5905 /* is_fatal */ false);
5906 codegen_->AddSlowPath(slow_path);
5907 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005908 if (zero.IsLinked()) {
5909 __ jmp(&done);
5910 }
5911 break;
5912 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005913 }
5914
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005915 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005916 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 __ xorl(out, out);
5918 }
5919
5920 if (done.IsLinked()) {
5921 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005922 }
5923
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005924 if (slow_path != nullptr) {
5925 __ Bind(slow_path->GetExitLabel());
5926 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005927}
5928
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005929void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005930 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5931 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005932 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5933 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005934 case TypeCheckKind::kExactCheck:
5935 case TypeCheckKind::kAbstractClassCheck:
5936 case TypeCheckKind::kClassHierarchyCheck:
5937 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005938 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5939 LocationSummary::kCallOnSlowPath :
5940 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941 break;
5942 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005943 case TypeCheckKind::kUnresolvedCheck:
5944 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945 call_kind = LocationSummary::kCallOnSlowPath;
5946 break;
5947 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005948 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5949 locations->SetInAt(0, Location::RequiresRegister());
5950 locations->SetInAt(1, Location::Any());
5951 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
5952 locations->AddTemp(Location::RequiresRegister());
5953 // When read barriers are enabled, we need an additional temporary
5954 // register for some cases.
5955 if (kEmitCompilerReadBarrier &&
5956 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5957 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5958 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005960 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005961}
5962
5963void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5964 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005965 Location obj_loc = locations->InAt(0);
5966 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005967 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005968 Location temp_loc = locations->GetTemp(0);
5969 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5971 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5972 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5973 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005974
Roland Levillain0d5a2812015-11-13 10:07:31 +00005975 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5976 bool is_type_check_slow_path_fatal =
5977 (type_check_kind == TypeCheckKind::kExactCheck ||
5978 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5979 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5980 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5981 !instruction->CanThrowIntoCatchBlock();
5982 SlowPathCode* type_check_slow_path =
5983 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5984 is_type_check_slow_path_fatal);
5985 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005986
Roland Levillain0d5a2812015-11-13 10:07:31 +00005987 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005988 // Avoid null check if we know obj is not null.
5989 if (instruction->MustDoNullCheck()) {
5990 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005991 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005992 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993
Roland Levillain0d5a2812015-11-13 10:07:31 +00005994 // /* HeapReference<Class> */ temp = obj->klass_
5995 __ movl(temp, Address(obj, class_offset));
5996 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005997
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999 case TypeCheckKind::kExactCheck:
6000 case TypeCheckKind::kArrayCheck: {
6001 if (cls.IsRegister()) {
6002 __ cmpl(temp, cls.AsRegister<Register>());
6003 } else {
6004 DCHECK(cls.IsStackSlot()) << cls;
6005 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6006 }
6007 // Jump to slow path for throwing the exception or doing a
6008 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006010 break;
6011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006012
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006013 case TypeCheckKind::kAbstractClassCheck: {
6014 // If the class is abstract, we eagerly fetch the super class of the
6015 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006016 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006017 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018 Location temp2_loc =
6019 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6020 if (kEmitCompilerReadBarrier) {
6021 // Save the value of `temp` into `temp2` before overwriting it
6022 // in the following move operation, as we will need it for the
6023 // read barrier below.
6024 Register temp2 = temp2_loc.AsRegister<Register>();
6025 __ movl(temp2, temp);
6026 }
6027 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006028 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006029 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6030
6031 // If the class reference currently in `temp` is not null, jump
6032 // to the `compare_classes` label to compare it with the checked
6033 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006035 __ j(kNotEqual, &compare_classes);
6036 // Otherwise, jump to the slow path to throw the exception.
6037 //
6038 // But before, move back the object's class into `temp` before
6039 // going into the slow path, as it has been overwritten in the
6040 // meantime.
6041 // /* HeapReference<Class> */ temp = obj->klass_
6042 __ movl(temp, Address(obj, class_offset));
6043 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6044 __ jmp(type_check_slow_path->GetEntryLabel());
6045
6046 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006047 if (cls.IsRegister()) {
6048 __ cmpl(temp, cls.AsRegister<Register>());
6049 } else {
6050 DCHECK(cls.IsStackSlot()) << cls;
6051 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6052 }
6053 __ j(kNotEqual, &loop);
6054 break;
6055 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006056
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006057 case TypeCheckKind::kClassHierarchyCheck: {
6058 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006059 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006060 __ Bind(&loop);
6061 if (cls.IsRegister()) {
6062 __ cmpl(temp, cls.AsRegister<Register>());
6063 } else {
6064 DCHECK(cls.IsStackSlot()) << cls;
6065 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6066 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006067 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068
6069 Location temp2_loc =
6070 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6071 if (kEmitCompilerReadBarrier) {
6072 // Save the value of `temp` into `temp2` before overwriting it
6073 // in the following move operation, as we will need it for the
6074 // read barrier below.
6075 Register temp2 = temp2_loc.AsRegister<Register>();
6076 __ movl(temp2, temp);
6077 }
6078 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006079 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006080 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6081
6082 // If the class reference currently in `temp` is not null, jump
6083 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006084 __ testl(temp, temp);
6085 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006086 // Otherwise, jump to the slow path to throw the exception.
6087 //
6088 // But before, move back the object's class into `temp` before
6089 // going into the slow path, as it has been overwritten in the
6090 // meantime.
6091 // /* HeapReference<Class> */ temp = obj->klass_
6092 __ movl(temp, Address(obj, class_offset));
6093 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6094 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006095 break;
6096 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006097
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006098 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006099 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006100 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006101 if (cls.IsRegister()) {
6102 __ cmpl(temp, cls.AsRegister<Register>());
6103 } else {
6104 DCHECK(cls.IsStackSlot()) << cls;
6105 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6106 }
6107 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006108
6109 // Otherwise, we need to check that the object's class is a non-primitive array.
6110 Location temp2_loc =
6111 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6112 if (kEmitCompilerReadBarrier) {
6113 // Save the value of `temp` into `temp2` before overwriting it
6114 // in the following move operation, as we will need it for the
6115 // read barrier below.
6116 Register temp2 = temp2_loc.AsRegister<Register>();
6117 __ movl(temp2, temp);
6118 }
6119 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006120 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006121 codegen_->MaybeGenerateReadBarrier(
6122 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
6123
6124 // If the component type is not null (i.e. the object is indeed
6125 // an array), jump to label `check_non_primitive_component_type`
6126 // to further check that this component type is not a primitive
6127 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006128 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006129 __ j(kNotEqual, &check_non_primitive_component_type);
6130 // Otherwise, jump to the slow path to throw the exception.
6131 //
6132 // But before, move back the object's class into `temp` before
6133 // going into the slow path, as it has been overwritten in the
6134 // meantime.
6135 // /* HeapReference<Class> */ temp = obj->klass_
6136 __ movl(temp, Address(obj, class_offset));
6137 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6138 __ jmp(type_check_slow_path->GetEntryLabel());
6139
6140 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142 __ j(kEqual, &done);
6143 // Same comment as above regarding `temp` and the slow path.
6144 // /* HeapReference<Class> */ temp = obj->klass_
6145 __ movl(temp, Address(obj, class_offset));
6146 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6147 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006148 break;
6149 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006150
Calin Juravle98893e12015-10-02 21:05:03 +01006151 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153 // We always go into the type check slow path for the unresolved &
6154 // interface check cases.
6155 //
6156 // We cannot directly call the CheckCast runtime entry point
6157 // without resorting to a type checking slow path here (i.e. by
6158 // calling InvokeRuntime directly), as it would require to
6159 // assign fixed registers for the inputs of this HInstanceOf
6160 // instruction (following the runtime calling convention), which
6161 // might be cluttered by the potential first read barrier
6162 // emission at the beginning of this method.
6163 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 break;
6165 }
6166 __ Bind(&done);
6167
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006169}
6170
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006171void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6172 LocationSummary* locations =
6173 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6174 InvokeRuntimeCallingConvention calling_convention;
6175 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6176}
6177
6178void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006179 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6180 : QUICK_ENTRY_POINT(pUnlockObject),
6181 instruction,
6182 instruction->GetDexPc(),
6183 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006184 if (instruction->IsEnter()) {
6185 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6186 } else {
6187 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6188 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006189}
6190
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006191void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6192void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6193void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6194
6195void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6196 LocationSummary* locations =
6197 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6198 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6199 || instruction->GetResultType() == Primitive::kPrimLong);
6200 locations->SetInAt(0, Location::RequiresRegister());
6201 locations->SetInAt(1, Location::Any());
6202 locations->SetOut(Location::SameAsFirstInput());
6203}
6204
6205void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6206 HandleBitwiseOperation(instruction);
6207}
6208
6209void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6210 HandleBitwiseOperation(instruction);
6211}
6212
6213void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6214 HandleBitwiseOperation(instruction);
6215}
6216
6217void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6218 LocationSummary* locations = instruction->GetLocations();
6219 Location first = locations->InAt(0);
6220 Location second = locations->InAt(1);
6221 DCHECK(first.Equals(locations->Out()));
6222
6223 if (instruction->GetResultType() == Primitive::kPrimInt) {
6224 if (second.IsRegister()) {
6225 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006226 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006227 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006228 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006229 } else {
6230 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006231 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006232 }
6233 } else if (second.IsConstant()) {
6234 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006235 __ andl(first.AsRegister<Register>(),
6236 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006237 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006238 __ orl(first.AsRegister<Register>(),
6239 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006240 } else {
6241 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006242 __ xorl(first.AsRegister<Register>(),
6243 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006244 }
6245 } else {
6246 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006247 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006248 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006249 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006250 } else {
6251 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006252 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006253 }
6254 }
6255 } else {
6256 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6257 if (second.IsRegisterPair()) {
6258 if (instruction->IsAnd()) {
6259 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6260 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6261 } else if (instruction->IsOr()) {
6262 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6263 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6264 } else {
6265 DCHECK(instruction->IsXor());
6266 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6267 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6268 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006269 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006270 if (instruction->IsAnd()) {
6271 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6272 __ andl(first.AsRegisterPairHigh<Register>(),
6273 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6274 } else if (instruction->IsOr()) {
6275 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6276 __ orl(first.AsRegisterPairHigh<Register>(),
6277 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6278 } else {
6279 DCHECK(instruction->IsXor());
6280 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6281 __ xorl(first.AsRegisterPairHigh<Register>(),
6282 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6283 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006284 } else {
6285 DCHECK(second.IsConstant()) << second;
6286 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006287 int32_t low_value = Low32Bits(value);
6288 int32_t high_value = High32Bits(value);
6289 Immediate low(low_value);
6290 Immediate high(high_value);
6291 Register first_low = first.AsRegisterPairLow<Register>();
6292 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006293 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006294 if (low_value == 0) {
6295 __ xorl(first_low, first_low);
6296 } else if (low_value != -1) {
6297 __ andl(first_low, low);
6298 }
6299 if (high_value == 0) {
6300 __ xorl(first_high, first_high);
6301 } else if (high_value != -1) {
6302 __ andl(first_high, high);
6303 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006304 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006305 if (low_value != 0) {
6306 __ orl(first_low, low);
6307 }
6308 if (high_value != 0) {
6309 __ orl(first_high, high);
6310 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006311 } else {
6312 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006313 if (low_value != 0) {
6314 __ xorl(first_low, low);
6315 }
6316 if (high_value != 0) {
6317 __ xorl(first_high, high);
6318 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006319 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006320 }
6321 }
6322}
6323
Roland Levillain0d5a2812015-11-13 10:07:31 +00006324void CodeGeneratorX86::GenerateReadBarrier(HInstruction* instruction,
6325 Location out,
6326 Location ref,
6327 Location obj,
6328 uint32_t offset,
6329 Location index) {
6330 DCHECK(kEmitCompilerReadBarrier);
6331
6332 // If heap poisoning is enabled, the unpoisoning of the loaded
6333 // reference will be carried out by the runtime within the slow
6334 // path.
6335 //
6336 // Note that `ref` currently does not get unpoisoned (when heap
6337 // poisoning is enabled), which is alright as the `ref` argument is
6338 // not used by the artReadBarrierSlow entry point.
6339 //
6340 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6341 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6342 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6343 AddSlowPath(slow_path);
6344
6345 // TODO: When read barrier has a fast path, add it here.
6346 /* Currently the read barrier call is inserted after the original load.
6347 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
6348 * original load. This load-load ordering is required by the read barrier.
6349 * The fast path/slow path (for Baker's algorithm) should look like:
6350 *
6351 * bool isGray = obj.LockWord & kReadBarrierMask;
6352 * lfence; // load fence or artificial data dependence to prevent load-load reordering
6353 * ref = obj.field; // this is the original load
6354 * if (isGray) {
6355 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
6356 * }
6357 */
6358
6359 __ jmp(slow_path->GetEntryLabel());
6360 __ Bind(slow_path->GetExitLabel());
6361}
6362
6363void CodeGeneratorX86::MaybeGenerateReadBarrier(HInstruction* instruction,
6364 Location out,
6365 Location ref,
6366 Location obj,
6367 uint32_t offset,
6368 Location index) {
6369 if (kEmitCompilerReadBarrier) {
6370 // If heap poisoning is enabled, unpoisoning will be taken care of
6371 // by the runtime within the slow path.
6372 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
6373 } else if (kPoisonHeapReferences) {
6374 __ UnpoisonHeapReference(out.AsRegister<Register>());
6375 }
6376}
6377
6378void CodeGeneratorX86::GenerateReadBarrierForRoot(HInstruction* instruction,
6379 Location out,
6380 Location root) {
6381 DCHECK(kEmitCompilerReadBarrier);
6382
6383 // Note that GC roots are not affected by heap poisoning, so we do
6384 // not need to do anything special for this here.
6385 SlowPathCode* slow_path =
6386 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6387 AddSlowPath(slow_path);
6388
6389 // TODO: Implement a fast path for ReadBarrierForRoot, performing
6390 // the following operation (for Baker's algorithm):
6391 //
6392 // if (thread.tls32_.is_gc_marking) {
6393 // root = Mark(root);
6394 // }
6395
6396 __ jmp(slow_path->GetEntryLabel());
6397 __ Bind(slow_path->GetExitLabel());
6398}
6399
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006400void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006401 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006402 LOG(FATAL) << "Unreachable";
6403}
6404
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006405void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006406 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006407 LOG(FATAL) << "Unreachable";
6408}
6409
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006410void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6411 DCHECK(codegen_->IsBaseline());
6412 LocationSummary* locations =
6413 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6414 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6415}
6416
6417void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6418 DCHECK(codegen_->IsBaseline());
6419 // Will be generated at use site.
6420}
6421
Mark Mendellfe57faa2015-09-18 09:26:15 -04006422// Simple implementation of packed switch - generate cascaded compare/jumps.
6423void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6424 LocationSummary* locations =
6425 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6426 locations->SetInAt(0, Location::RequiresRegister());
6427}
6428
6429void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6430 int32_t lower_bound = switch_instr->GetStartValue();
6431 int32_t num_entries = switch_instr->GetNumEntries();
6432 LocationSummary* locations = switch_instr->GetLocations();
6433 Register value_reg = locations->InAt(0).AsRegister<Register>();
6434 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6435
6436 // Create a series of compare/jumps.
6437 const ArenaVector<HBasicBlock*>& successors = switch_instr->GetBlock()->GetSuccessors();
6438 for (int i = 0; i < num_entries; i++) {
6439 int32_t case_value = lower_bound + i;
6440 if (case_value == 0) {
6441 __ testl(value_reg, value_reg);
6442 } else {
6443 __ cmpl(value_reg, Immediate(case_value));
6444 }
Vladimir Markoec7802a2015-10-01 20:57:57 +01006445 __ j(kEqual, codegen_->GetLabelOf(successors[i]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006446 }
6447
6448 // And the default for any other value.
6449 if (!codegen_->GoesToNextBlock(switch_instr->GetBlock(), default_block)) {
6450 __ jmp(codegen_->GetLabelOf(default_block));
6451 }
6452}
6453
Mark Mendell805b3b52015-09-18 14:10:29 -04006454void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6455 LocationSummary* locations =
6456 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6457 locations->SetInAt(0, Location::RequiresRegister());
6458
6459 // Constant area pointer.
6460 locations->SetInAt(1, Location::RequiresRegister());
6461
6462 // And the temporary we need.
6463 locations->AddTemp(Location::RequiresRegister());
6464}
6465
6466void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6467 int32_t lower_bound = switch_instr->GetStartValue();
6468 int32_t num_entries = switch_instr->GetNumEntries();
6469 LocationSummary* locations = switch_instr->GetLocations();
6470 Register value_reg = locations->InAt(0).AsRegister<Register>();
6471 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6472
6473 // Optimizing has a jump area.
6474 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6475 Register constant_area = locations->InAt(1).AsRegister<Register>();
6476
6477 // Remove the bias, if needed.
6478 if (lower_bound != 0) {
6479 __ leal(temp_reg, Address(value_reg, -lower_bound));
6480 value_reg = temp_reg;
6481 }
6482
6483 // Is the value in range?
6484 DCHECK_GE(num_entries, 1);
6485 __ cmpl(value_reg, Immediate(num_entries - 1));
6486 __ j(kAbove, codegen_->GetLabelOf(default_block));
6487
6488 // We are in the range of the table.
6489 // Load (target-constant_area) from the jump table, indexing by the value.
6490 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6491
6492 // Compute the actual target address by adding in constant_area.
6493 __ addl(temp_reg, constant_area);
6494
6495 // And jump.
6496 __ jmp(temp_reg);
6497}
6498
Mark Mendell0616ae02015-04-17 12:49:27 -04006499void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6500 HX86ComputeBaseMethodAddress* insn) {
6501 LocationSummary* locations =
6502 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6503 locations->SetOut(Location::RequiresRegister());
6504}
6505
6506void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6507 HX86ComputeBaseMethodAddress* insn) {
6508 LocationSummary* locations = insn->GetLocations();
6509 Register reg = locations->Out().AsRegister<Register>();
6510
6511 // Generate call to next instruction.
6512 Label next_instruction;
6513 __ call(&next_instruction);
6514 __ Bind(&next_instruction);
6515
6516 // Remember this offset for later use with constant area.
6517 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6518
6519 // Grab the return address off the stack.
6520 __ popl(reg);
6521}
6522
6523void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6524 HX86LoadFromConstantTable* insn) {
6525 LocationSummary* locations =
6526 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6527
6528 locations->SetInAt(0, Location::RequiresRegister());
6529 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6530
6531 // If we don't need to be materialized, we only need the inputs to be set.
6532 if (!insn->NeedsMaterialization()) {
6533 return;
6534 }
6535
6536 switch (insn->GetType()) {
6537 case Primitive::kPrimFloat:
6538 case Primitive::kPrimDouble:
6539 locations->SetOut(Location::RequiresFpuRegister());
6540 break;
6541
6542 case Primitive::kPrimInt:
6543 locations->SetOut(Location::RequiresRegister());
6544 break;
6545
6546 default:
6547 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6548 }
6549}
6550
6551void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6552 if (!insn->NeedsMaterialization()) {
6553 return;
6554 }
6555
6556 LocationSummary* locations = insn->GetLocations();
6557 Location out = locations->Out();
6558 Register const_area = locations->InAt(0).AsRegister<Register>();
6559 HConstant *value = insn->GetConstant();
6560
6561 switch (insn->GetType()) {
6562 case Primitive::kPrimFloat:
6563 __ movss(out.AsFpuRegister<XmmRegister>(),
6564 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6565 break;
6566
6567 case Primitive::kPrimDouble:
6568 __ movsd(out.AsFpuRegister<XmmRegister>(),
6569 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6570 break;
6571
6572 case Primitive::kPrimInt:
6573 __ movl(out.AsRegister<Register>(),
6574 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6575 break;
6576
6577 default:
6578 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6579 }
6580}
6581
Mark Mendell0616ae02015-04-17 12:49:27 -04006582/**
6583 * Class to handle late fixup of offsets into constant area.
6584 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006585class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006586 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006587 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6588 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6589
6590 protected:
6591 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6592
6593 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006594
6595 private:
6596 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6597 // Patch the correct offset for the instruction. The place to patch is the
6598 // last 4 bytes of the instruction.
6599 // The value to patch is the distance from the offset in the constant area
6600 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006601 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6602 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006603
6604 // Patch in the right value.
6605 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6606 }
6607
Mark Mendell0616ae02015-04-17 12:49:27 -04006608 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006609 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006610};
6611
Mark Mendell805b3b52015-09-18 14:10:29 -04006612/**
6613 * Class to handle late fixup of offsets to a jump table that will be created in the
6614 * constant area.
6615 */
6616class JumpTableRIPFixup : public RIPFixup {
6617 public:
6618 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6619 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6620
6621 void CreateJumpTable() {
6622 X86Assembler* assembler = codegen_->GetAssembler();
6623
6624 // Ensure that the reference to the jump table has the correct offset.
6625 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6626 SetOffset(offset_in_constant_table);
6627
6628 // The label values in the jump table are computed relative to the
6629 // instruction addressing the constant area.
6630 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6631
6632 // Populate the jump table with the correct values for the jump table.
6633 int32_t num_entries = switch_instr_->GetNumEntries();
6634 HBasicBlock* block = switch_instr_->GetBlock();
6635 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6636 // The value that we want is the target offset - the position of the table.
6637 for (int32_t i = 0; i < num_entries; i++) {
6638 HBasicBlock* b = successors[i];
6639 Label* l = codegen_->GetLabelOf(b);
6640 DCHECK(l->IsBound());
6641 int32_t offset_to_block = l->Position() - relative_offset;
6642 assembler->AppendInt32(offset_to_block);
6643 }
6644 }
6645
6646 private:
6647 const HX86PackedSwitch* switch_instr_;
6648};
6649
6650void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
6651 // Generate the constant area if needed.
6652 X86Assembler* assembler = GetAssembler();
6653 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6654 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
6655 // byte values.
6656 assembler->Align(4, 0);
6657 constant_area_start_ = assembler->CodeSize();
6658
6659 // Populate any jump tables.
6660 for (auto jump_table : fixups_to_jump_tables_) {
6661 jump_table->CreateJumpTable();
6662 }
6663
6664 // And now add the constant area to the generated code.
6665 assembler->AddConstantArea();
6666 }
6667
6668 // And finish up.
6669 CodeGenerator::Finalize(allocator);
6670}
6671
Mark Mendell0616ae02015-04-17 12:49:27 -04006672Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
6673 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6674 return Address(reg, kDummy32BitOffset, fixup);
6675}
6676
6677Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
6678 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6679 return Address(reg, kDummy32BitOffset, fixup);
6680}
6681
6682Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
6683 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6684 return Address(reg, kDummy32BitOffset, fixup);
6685}
6686
6687Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
6688 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6689 return Address(reg, kDummy32BitOffset, fixup);
6690}
6691
Mark Mendell805b3b52015-09-18 14:10:29 -04006692Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
6693 Register reg,
6694 Register value) {
6695 // Create a fixup to be used to create and address the jump table.
6696 JumpTableRIPFixup* table_fixup =
6697 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6698
6699 // We have to populate the jump tables.
6700 fixups_to_jump_tables_.push_back(table_fixup);
6701
6702 // We want a scaled address, as we are extracting the correct offset from the table.
6703 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
6704}
6705
Andreas Gampe85b62f22015-09-09 13:15:38 -07006706// TODO: target as memory.
6707void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
6708 if (!target.IsValid()) {
6709 DCHECK(type == Primitive::kPrimVoid);
6710 return;
6711 }
6712
6713 DCHECK_NE(type, Primitive::kPrimVoid);
6714
6715 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
6716 if (target.Equals(return_loc)) {
6717 return;
6718 }
6719
6720 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6721 // with the else branch.
6722 if (type == Primitive::kPrimLong) {
6723 HParallelMove parallel_move(GetGraph()->GetArena());
6724 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
6725 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
6726 GetMoveResolver()->EmitNativeCode(&parallel_move);
6727 } else {
6728 // Let the parallel move resolver take care of all of this.
6729 HParallelMove parallel_move(GetGraph()->GetArena());
6730 parallel_move.AddMove(return_loc, target, type, nullptr);
6731 GetMoveResolver()->EmitNativeCode(&parallel_move);
6732 }
6733}
6734
Roland Levillain4d027112015-07-01 15:41:14 +01006735#undef __
6736
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006737} // namespace x86
6738} // namespace art