blob: 8c50650c2b6de8809a5615a20f391b56103d34ba [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;
Mark Mendell5f874182015-03-04 15:42:45 -050045static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010046
Mark Mendell24f2dfa2015-01-14 19:51:45 -050047static constexpr int kC2ConditionMask = 0x400;
48
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000049static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000050
Roland Levillain62a46b22015-06-01 18:24:13 +010051#define __ down_cast<X86Assembler*>(codegen->GetAssembler())->
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
Nicolas Geoffray39468442014-09-02 15:17:15 +010056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +010077 HNullCheck* const instruction_;
Nicolas Geoffraye5038322014-07-04 09:41:32 +010078 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
79};
80
Andreas Gampe85b62f22015-09-09 13:15:38 -070081class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000082 public:
83 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : instruction_(instruction) {}
84
Alexandre Rames2ed20af2015-03-06 13:55:35 +000085 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010086 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000087 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000088 if (instruction_->CanThrowIntoCatchBlock()) {
89 // Live registers will be restored in the catch block if caught.
90 SaveLiveRegisters(codegen, instruction_->GetLocations());
91 }
Alexandre Rames8158f282015-08-07 10:26:17 +010092 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
93 instruction_,
94 instruction_->GetDexPc(),
95 this);
Roland Levillain888d0672015-11-23 18:53:50 +000096 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000097 }
98
Alexandre Rames8158f282015-08-07 10:26:17 +010099 bool IsFatal() const OVERRIDE { return true; }
100
Alexandre Rames9931f312015-06-19 14:47:01 +0100101 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
102
Calin Juravled0d48522014-11-04 16:40:20 +0000103 private:
104 HDivZeroCheck* const instruction_;
105 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
106};
107
Andreas Gampe85b62f22015-09-09 13:15:38 -0700108class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000109 public:
Roland Levillain3887c462015-08-12 18:15:42 +0100110 DivRemMinusOneSlowPathX86(Register reg, bool is_div) : reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000111
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000112 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000113 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000114 if (is_div_) {
115 __ negl(reg_);
116 } else {
117 __ movl(reg_, Immediate(0));
118 }
Calin Juravled0d48522014-11-04 16:40:20 +0000119 __ jmp(GetExitLabel());
120 }
121
Alexandre Rames9931f312015-06-19 14:47:01 +0100122 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
123
Calin Juravled0d48522014-11-04 16:40:20 +0000124 private:
125 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000126 bool is_div_;
127 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000128};
129
Andreas Gampe85b62f22015-09-09 13:15:38 -0700130class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100131 public:
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100132 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : instruction_(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100133
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000134 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100135 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100136 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100137 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000138 // We're moving two locations to locations that could overlap, so we need a parallel
139 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000140 if (instruction_->CanThrowIntoCatchBlock()) {
141 // Live registers will be restored in the catch block if caught.
142 SaveLiveRegisters(codegen, instruction_->GetLocations());
143 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100144 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000145 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100146 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000147 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100148 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100149 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100150 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
151 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100152 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
153 instruction_,
154 instruction_->GetDexPc(),
155 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000156 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100157 }
158
Alexandre Rames8158f282015-08-07 10:26:17 +0100159 bool IsFatal() const OVERRIDE { return true; }
160
Alexandre Rames9931f312015-06-19 14:47:01 +0100161 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 private:
Nicolas Geoffray39468442014-09-02 15:17:15 +0100164 HBoundsCheck* const instruction_;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100165
166 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
167};
168
Andreas Gampe85b62f22015-09-09 13:15:38 -0700169class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000171 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100172 : instruction_(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000174 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100175 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000176 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000177 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100178 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
179 instruction_,
180 instruction_->GetDexPc(),
181 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000182 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000183 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100184 if (successor_ == nullptr) {
185 __ jmp(GetReturnLabel());
186 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100187 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100188 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000189 }
190
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100191 Label* GetReturnLabel() {
192 DCHECK(successor_ == nullptr);
193 return &return_label_;
194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100196 HBasicBlock* GetSuccessor() const {
197 return successor_;
198 }
199
Alexandre Rames9931f312015-06-19 14:47:01 +0100200 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
201
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000202 private:
203 HSuspendCheck* const instruction_;
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 Label return_label_;
206
207 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
208};
209
Andreas Gampe85b62f22015-09-09 13:15:38 -0700210class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 public:
212 explicit LoadStringSlowPathX86(HLoadString* instruction) : instruction_(instruction) {}
213
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000214 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215 LocationSummary* locations = instruction_->GetLocations();
216 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
217
218 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
219 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000220 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000221
222 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe1cc7dba2014-12-17 18:43:01 -0800223 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction_->GetStringIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100224 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
225 instruction_,
226 instruction_->GetDexPc(),
227 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000228 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000229 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000230 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000231
232 __ jmp(GetExitLabel());
233 }
234
Alexandre Rames9931f312015-06-19 14:47:01 +0100235 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
236
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000237 private:
238 HLoadString* const instruction_;
239
240 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
241};
242
Andreas Gampe85b62f22015-09-09 13:15:38 -0700243class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000244 public:
245 LoadClassSlowPathX86(HLoadClass* cls,
246 HInstruction* at,
247 uint32_t dex_pc,
248 bool do_clinit)
249 : cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
250 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
251 }
252
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000253 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000254 LocationSummary* locations = at_->GetLocations();
255 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
256 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000257 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258
259 InvokeRuntimeCallingConvention calling_convention;
260 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100261 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
262 : QUICK_ENTRY_POINT(pInitializeType),
263 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000264 if (do_clinit_) {
265 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
266 } else {
267 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
268 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000269
270 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271 Location out = locations->Out();
272 if (out.IsValid()) {
273 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
274 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000275 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000276
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000277 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 __ jmp(GetExitLabel());
279 }
280
Alexandre Rames9931f312015-06-19 14:47:01 +0100281 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
282
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 private:
284 // The class this slow path will load.
285 HLoadClass* const cls_;
286
287 // The instruction where this slow path is happening.
288 // (Might be the load class or an initialization check).
289 HInstruction* const at_;
290
291 // The dex PC of `at_`.
292 const uint32_t dex_pc_;
293
294 // Whether to initialize the class.
295 const bool do_clinit_;
296
297 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
298};
299
Andreas Gampe85b62f22015-09-09 13:15:38 -0700300class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000302 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
303 : instruction_(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000304
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000305 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100307 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
308 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000309 DCHECK(instruction_->IsCheckCast()
310 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000311
312 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
313 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000315 if (!is_fatal_) {
316 SaveLiveRegisters(codegen, locations);
317 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000318
319 // We're moving two locations to locations that could overlap, so we need a parallel
320 // move resolver.
321 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000322 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100323 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000324 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100325 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100326 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100327 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
328 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000329
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000330 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100331 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
332 instruction_,
333 instruction_->GetDexPc(),
334 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000335 CheckEntrypointTypes<
336 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000337 } else {
338 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100339 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
340 instruction_,
341 instruction_->GetDexPc(),
342 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000343 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000344 }
345
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000346 if (!is_fatal_) {
347 if (instruction_->IsInstanceOf()) {
348 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
349 }
350 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000351
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000352 __ jmp(GetExitLabel());
353 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354 }
355
Alexandre Rames9931f312015-06-19 14:47:01 +0100356 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000357 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100358
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000359 private:
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000360 HInstruction* const instruction_;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000361 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000362
363 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
364};
365
Andreas Gampe85b62f22015-09-09 13:15:38 -0700366class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 public:
368 explicit DeoptimizationSlowPathX86(HInstruction* instruction)
369 : instruction_(instruction) {}
370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames98596202015-08-19 11:33:36 +0100372 DCHECK(instruction_->IsDeoptimize());
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 __ Bind(GetEntryLabel());
375 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100376 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
377 instruction_,
378 instruction_->GetDexPc(),
379 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000380 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700381 }
382
Alexandre Rames9931f312015-06-19 14:47:01 +0100383 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
384
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700385 private:
386 HInstruction* const instruction_;
387 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
388};
389
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100390class ArraySetSlowPathX86 : public SlowPathCode {
391 public:
392 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
393
394 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
395 LocationSummary* locations = instruction_->GetLocations();
396 __ Bind(GetEntryLabel());
397 SaveLiveRegisters(codegen, locations);
398
399 InvokeRuntimeCallingConvention calling_convention;
400 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
401 parallel_move.AddMove(
402 locations->InAt(0),
403 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
404 Primitive::kPrimNot,
405 nullptr);
406 parallel_move.AddMove(
407 locations->InAt(1),
408 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
409 Primitive::kPrimInt,
410 nullptr);
411 parallel_move.AddMove(
412 locations->InAt(2),
413 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
414 Primitive::kPrimNot,
415 nullptr);
416 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
417
418 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
419 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
420 instruction_,
421 instruction_->GetDexPc(),
422 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000423 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 RestoreLiveRegisters(codegen, locations);
425 __ jmp(GetExitLabel());
426 }
427
428 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
429
430 private:
431 HInstruction* const instruction_;
432
433 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
434};
435
Roland Levillain0d5a2812015-11-13 10:07:31 +0000436// Slow path generating a read barrier for a heap reference.
437class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
438 public:
439 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
440 Location out,
441 Location ref,
442 Location obj,
443 uint32_t offset,
444 Location index)
445 : instruction_(instruction),
446 out_(out),
447 ref_(ref),
448 obj_(obj),
449 offset_(offset),
450 index_(index) {
451 DCHECK(kEmitCompilerReadBarrier);
452 // If `obj` is equal to `out` or `ref`, it means the initial object
453 // has been overwritten by (or after) the heap object reference load
454 // to be instrumented, e.g.:
455 //
456 // __ movl(out, Address(out, offset));
457 // codegen_->GenerateReadBarrier(instruction, out_loc, out_loc, out_loc, offset);
458 //
459 // In that case, we have lost the information about the original
460 // object, and the emitted read barrier cannot work properly.
461 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
462 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
463 }
464
465 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
466 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
467 LocationSummary* locations = instruction_->GetLocations();
468 Register reg_out = out_.AsRegister<Register>();
469 DCHECK(locations->CanCall());
470 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
471 DCHECK(!instruction_->IsInvoke() ||
472 (instruction_->IsInvokeStaticOrDirect() &&
473 instruction_->GetLocations()->Intrinsified()));
474
475 __ Bind(GetEntryLabel());
476 SaveLiveRegisters(codegen, locations);
477
478 // We may have to change the index's value, but as `index_` is a
479 // constant member (like other "inputs" of this slow path),
480 // introduce a copy of it, `index`.
481 Location index = index_;
482 if (index_.IsValid()) {
483 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
484 if (instruction_->IsArrayGet()) {
485 // Compute the actual memory offset and store it in `index`.
486 Register index_reg = index_.AsRegister<Register>();
487 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
488 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
489 // We are about to change the value of `index_reg` (see the
490 // calls to art::x86::X86Assembler::shll and
491 // art::x86::X86Assembler::AddImmediate below), but it has
492 // not been saved by the previous call to
493 // art::SlowPathCode::SaveLiveRegisters, as it is a
494 // callee-save register --
495 // art::SlowPathCode::SaveLiveRegisters does not consider
496 // callee-save registers, as it has been designed with the
497 // assumption that callee-save registers are supposed to be
498 // handled by the called function. So, as a callee-save
499 // register, `index_reg` _would_ eventually be saved onto
500 // the stack, but it would be too late: we would have
501 // changed its value earlier. Therefore, we manually save
502 // it here into another freely available register,
503 // `free_reg`, chosen of course among the caller-save
504 // registers (as a callee-save `free_reg` register would
505 // exhibit the same problem).
506 //
507 // Note we could have requested a temporary register from
508 // the register allocator instead; but we prefer not to, as
509 // this is a slow path, and we know we can find a
510 // caller-save register that is available.
511 Register free_reg = FindAvailableCallerSaveRegister(codegen);
512 __ movl(free_reg, index_reg);
513 index_reg = free_reg;
514 index = Location::RegisterLocation(index_reg);
515 } else {
516 // The initial register stored in `index_` has already been
517 // saved in the call to art::SlowPathCode::SaveLiveRegisters
518 // (as it is not a callee-save register), so we can freely
519 // use it.
520 }
521 // Shifting the index value contained in `index_reg` by the scale
522 // factor (2) cannot overflow in practice, as the runtime is
523 // unable to allocate object arrays with a size larger than
524 // 2^26 - 1 (that is, 2^28 - 4 bytes).
525 __ shll(index_reg, Immediate(TIMES_4));
526 static_assert(
527 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
528 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
529 __ AddImmediate(index_reg, Immediate(offset_));
530 } else {
531 DCHECK(instruction_->IsInvoke());
532 DCHECK(instruction_->GetLocations()->Intrinsified());
533 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
534 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
535 << instruction_->AsInvoke()->GetIntrinsic();
536 DCHECK_EQ(offset_, 0U);
537 DCHECK(index_.IsRegisterPair());
538 // UnsafeGet's offset location is a register pair, the low
539 // part contains the correct offset.
540 index = index_.ToLow();
541 }
542 }
543
544 // We're moving two or three locations to locations that could
545 // overlap, so we need a parallel move resolver.
546 InvokeRuntimeCallingConvention calling_convention;
547 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
548 parallel_move.AddMove(ref_,
549 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
550 Primitive::kPrimNot,
551 nullptr);
552 parallel_move.AddMove(obj_,
553 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
554 Primitive::kPrimNot,
555 nullptr);
556 if (index.IsValid()) {
557 parallel_move.AddMove(index,
558 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
559 Primitive::kPrimInt,
560 nullptr);
561 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
562 } else {
563 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
564 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
565 }
566 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
567 instruction_,
568 instruction_->GetDexPc(),
569 this);
570 CheckEntrypointTypes<
571 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
572 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
573
574 RestoreLiveRegisters(codegen, locations);
575 __ jmp(GetExitLabel());
576 }
577
578 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
579
580 private:
581 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
582 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
583 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
584 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
585 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
586 return static_cast<Register>(i);
587 }
588 }
589 // We shall never fail to find a free caller-save register, as
590 // there are more than two core caller-save registers on x86
591 // (meaning it is possible to find one which is different from
592 // `ref` and `obj`).
593 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
594 LOG(FATAL) << "Could not find a free caller-save register";
595 UNREACHABLE();
596 }
597
598 HInstruction* const instruction_;
599 const Location out_;
600 const Location ref_;
601 const Location obj_;
602 const uint32_t offset_;
603 // An additional location containing an index to an array.
604 // Only used for HArrayGet and the UnsafeGetObject &
605 // UnsafeGetObjectVolatile intrinsics.
606 const Location index_;
607
608 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
609};
610
611// Slow path generating a read barrier for a GC root.
612class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
613 public:
614 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
615 : instruction_(instruction), out_(out), root_(root) {}
616
617 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
618 LocationSummary* locations = instruction_->GetLocations();
619 Register reg_out = out_.AsRegister<Register>();
620 DCHECK(locations->CanCall());
621 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
622 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString());
623
624 __ Bind(GetEntryLabel());
625 SaveLiveRegisters(codegen, locations);
626
627 InvokeRuntimeCallingConvention calling_convention;
628 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
629 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
630 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
631 instruction_,
632 instruction_->GetDexPc(),
633 this);
634 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
635 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
636
637 RestoreLiveRegisters(codegen, locations);
638 __ jmp(GetExitLabel());
639 }
640
641 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
642
643 private:
644 HInstruction* const instruction_;
645 const Location out_;
646 const Location root_;
647
648 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
649};
650
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100651#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100652#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100653
Aart Bike9f37602015-10-09 11:15:55 -0700654inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700655 switch (cond) {
656 case kCondEQ: return kEqual;
657 case kCondNE: return kNotEqual;
658 case kCondLT: return kLess;
659 case kCondLE: return kLessEqual;
660 case kCondGT: return kGreater;
661 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700662 case kCondB: return kBelow;
663 case kCondBE: return kBelowEqual;
664 case kCondA: return kAbove;
665 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700666 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100667 LOG(FATAL) << "Unreachable";
668 UNREACHABLE();
669}
670
Aart Bike9f37602015-10-09 11:15:55 -0700671// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100672inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
673 switch (cond) {
674 case kCondEQ: return kEqual;
675 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700676 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100677 case kCondLT: return kBelow;
678 case kCondLE: return kBelowEqual;
679 case kCondGT: return kAbove;
680 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700681 // Unsigned remain unchanged.
682 case kCondB: return kBelow;
683 case kCondBE: return kBelowEqual;
684 case kCondA: return kAbove;
685 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100686 }
687 LOG(FATAL) << "Unreachable";
688 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700689}
690
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100691void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100692 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100693}
694
695void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100696 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100697}
698
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100699size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
700 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
701 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100702}
703
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100704size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
705 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
706 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100707}
708
Mark Mendell7c8d0092015-01-26 11:21:33 -0500709size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
710 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
711 return GetFloatingPointSpillSlotSize();
712}
713
714size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
715 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
716 return GetFloatingPointSpillSlotSize();
717}
718
Calin Juravle175dc732015-08-25 15:42:32 +0100719void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
720 HInstruction* instruction,
721 uint32_t dex_pc,
722 SlowPathCode* slow_path) {
723 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
724 instruction,
725 dex_pc,
726 slow_path);
727}
728
729void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100730 HInstruction* instruction,
731 uint32_t dex_pc,
732 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100733 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100734 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100735 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100736}
737
Mark Mendellfb8d2792015-03-31 22:16:59 -0400738CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000739 const X86InstructionSetFeatures& isa_features,
740 const CompilerOptions& compiler_options,
741 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500742 : CodeGenerator(graph,
743 kNumberOfCpuRegisters,
744 kNumberOfXmmRegisters,
745 kNumberOfRegisterPairs,
746 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
747 arraysize(kCoreCalleeSaves))
748 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100749 0,
750 compiler_options,
751 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100752 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100753 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100754 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400755 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000756 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100757 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400758 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000759 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400760 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000761 // Use a fake return address register to mimic Quick.
762 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100763}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100764
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100765Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100766 switch (type) {
767 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100768 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100769 X86ManagedRegister pair =
770 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100771 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
772 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100773 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
774 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100775 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100776 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100777 }
778
779 case Primitive::kPrimByte:
780 case Primitive::kPrimBoolean:
781 case Primitive::kPrimChar:
782 case Primitive::kPrimShort:
783 case Primitive::kPrimInt:
784 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100785 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100786 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100787 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100788 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
789 X86ManagedRegister current =
790 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
791 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100792 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100793 }
794 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100795 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100796 }
797
798 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100799 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100800 return Location::FpuRegisterLocation(
801 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100802 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100803
804 case Primitive::kPrimVoid:
805 LOG(FATAL) << "Unreachable type " << type;
806 }
807
Roland Levillain0d5a2812015-11-13 10:07:31 +0000808 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100809}
810
Mark Mendell5f874182015-03-04 15:42:45 -0500811void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100812 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100813 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100814
815 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100816 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100817
Mark Mendell5f874182015-03-04 15:42:45 -0500818 if (is_baseline) {
819 blocked_core_registers_[EBP] = true;
820 blocked_core_registers_[ESI] = true;
821 blocked_core_registers_[EDI] = true;
822 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100823
824 UpdateBlockedPairRegisters();
825}
826
827void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
828 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
829 X86ManagedRegister current =
830 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
831 if (blocked_core_registers_[current.AsRegisterPairLow()]
832 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
833 blocked_register_pairs_[i] = true;
834 }
835 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100836}
837
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100838InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
839 : HGraphVisitor(graph),
840 assembler_(codegen->GetAssembler()),
841 codegen_(codegen) {}
842
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100843static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100844 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100845}
846
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000847void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100848 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000849 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000850 bool skip_overflow_check =
851 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000852 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000853
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000854 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100855 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100856 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100857 }
858
Mark Mendell5f874182015-03-04 15:42:45 -0500859 if (HasEmptyFrame()) {
860 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000861 }
Mark Mendell5f874182015-03-04 15:42:45 -0500862
863 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
864 Register reg = kCoreCalleeSaves[i];
865 if (allocated_registers_.ContainsCoreRegister(reg)) {
866 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100867 __ cfi().AdjustCFAOffset(kX86WordSize);
868 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500869 }
870 }
871
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100872 int adjust = GetFrameSize() - FrameEntrySpillSize();
873 __ subl(ESP, Immediate(adjust));
874 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100875 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000876}
877
878void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100879 __ cfi().RememberState();
880 if (!HasEmptyFrame()) {
881 int adjust = GetFrameSize() - FrameEntrySpillSize();
882 __ addl(ESP, Immediate(adjust));
883 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500884
David Srbeckyc34dc932015-04-12 09:27:43 +0100885 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
886 Register reg = kCoreCalleeSaves[i];
887 if (allocated_registers_.ContainsCoreRegister(reg)) {
888 __ popl(reg);
889 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
890 __ cfi().Restore(DWARFReg(reg));
891 }
Mark Mendell5f874182015-03-04 15:42:45 -0500892 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000893 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100894 __ ret();
895 __ cfi().RestoreState();
896 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000897}
898
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100899void CodeGeneratorX86::Bind(HBasicBlock* block) {
900 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000901}
902
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100903Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
904 switch (load->GetType()) {
905 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100906 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100907 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100908
909 case Primitive::kPrimInt:
910 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100911 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100912 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100913
914 case Primitive::kPrimBoolean:
915 case Primitive::kPrimByte:
916 case Primitive::kPrimChar:
917 case Primitive::kPrimShort:
918 case Primitive::kPrimVoid:
919 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700920 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100921 }
922
923 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700924 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100925}
926
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100927Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
928 switch (type) {
929 case Primitive::kPrimBoolean:
930 case Primitive::kPrimByte:
931 case Primitive::kPrimChar:
932 case Primitive::kPrimShort:
933 case Primitive::kPrimInt:
934 case Primitive::kPrimNot:
935 return Location::RegisterLocation(EAX);
936
937 case Primitive::kPrimLong:
938 return Location::RegisterPairLocation(EAX, EDX);
939
940 case Primitive::kPrimVoid:
941 return Location::NoLocation();
942
943 case Primitive::kPrimDouble:
944 case Primitive::kPrimFloat:
945 return Location::FpuRegisterLocation(XMM0);
946 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100947
948 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100949}
950
951Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
952 return Location::RegisterLocation(kMethodRegisterArgument);
953}
954
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100955Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100956 switch (type) {
957 case Primitive::kPrimBoolean:
958 case Primitive::kPrimByte:
959 case Primitive::kPrimChar:
960 case Primitive::kPrimShort:
961 case Primitive::kPrimInt:
962 case Primitive::kPrimNot: {
963 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100965 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100966 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100967 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000968 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100969 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100970 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100971
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000972 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100973 uint32_t index = gp_index_;
974 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100977 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
978 calling_convention.GetRegisterPairAt(index));
979 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100980 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000981 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
982 }
983 }
984
985 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100986 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000987 stack_index_++;
988 if (index < calling_convention.GetNumberOfFpuRegisters()) {
989 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
990 } else {
991 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
992 }
993 }
994
995 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100996 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000997 stack_index_ += 2;
998 if (index < calling_convention.GetNumberOfFpuRegisters()) {
999 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1000 } else {
1001 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001002 }
1003 }
1004
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 case Primitive::kPrimVoid:
1006 LOG(FATAL) << "Unexpected parameter type " << type;
1007 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001008 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001009 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001010}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001011
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001012void CodeGeneratorX86::Move32(Location destination, Location source) {
1013 if (source.Equals(destination)) {
1014 return;
1015 }
1016 if (destination.IsRegister()) {
1017 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001018 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001019 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001021 } else {
1022 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001025 } else if (destination.IsFpuRegister()) {
1026 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001027 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001029 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001030 } else {
1031 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001034 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001035 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001036 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001037 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001038 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001039 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001040 } else if (source.IsConstant()) {
1041 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001042 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001043 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 } else {
1045 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001046 __ pushl(Address(ESP, source.GetStackIndex()));
1047 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 }
1049 }
1050}
1051
1052void CodeGeneratorX86::Move64(Location destination, Location source) {
1053 if (source.Equals(destination)) {
1054 return;
1055 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001056 if (destination.IsRegisterPair()) {
1057 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001058 EmitParallelMoves(
1059 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1060 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001061 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001062 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001063 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1064 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001066 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1067 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1068 __ psrlq(src_reg, Immediate(32));
1069 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001070 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001071 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001073 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1074 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1076 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001077 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001078 if (source.IsFpuRegister()) {
1079 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1080 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001081 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001082 } else if (source.IsRegisterPair()) {
1083 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1084 // Create stack space for 2 elements.
1085 __ subl(ESP, Immediate(2 * elem_size));
1086 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1087 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1088 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1089 // And remove the temporary stack space we allocated.
1090 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001091 } else {
1092 LOG(FATAL) << "Unimplemented";
1093 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001094 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001095 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001096 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001097 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001098 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001099 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001100 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001101 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001102 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001103 } else if (source.IsConstant()) {
1104 HConstant* constant = source.GetConstant();
1105 int64_t value;
1106 if (constant->IsLongConstant()) {
1107 value = constant->AsLongConstant()->GetValue();
1108 } else {
1109 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001110 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001111 }
1112 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1113 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001114 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001115 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001116 EmitParallelMoves(
1117 Location::StackSlot(source.GetStackIndex()),
1118 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001119 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001120 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001121 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1122 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001123 }
1124 }
1125}
1126
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001127void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001128 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001129 if (instruction->IsCurrentMethod()) {
1130 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1131 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001132 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001133 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001134 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001135 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1136 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001137 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001138 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001139 } else if (location.IsStackSlot()) {
1140 __ movl(Address(ESP, location.GetStackIndex()), imm);
1141 } else {
1142 DCHECK(location.IsConstant());
1143 DCHECK_EQ(location.GetConstant(), const_to_move);
1144 }
1145 } else if (const_to_move->IsLongConstant()) {
1146 int64_t value = const_to_move->AsLongConstant()->GetValue();
1147 if (location.IsRegisterPair()) {
1148 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1149 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1150 } else if (location.IsDoubleStackSlot()) {
1151 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001152 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1153 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001154 } else {
1155 DCHECK(location.IsConstant());
1156 DCHECK_EQ(location.GetConstant(), instruction);
1157 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001158 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001159 } else if (instruction->IsTemporary()) {
1160 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001161 if (temp_location.IsStackSlot()) {
1162 Move32(location, temp_location);
1163 } else {
1164 DCHECK(temp_location.IsDoubleStackSlot());
1165 Move64(location, temp_location);
1166 }
Roland Levillain476df552014-10-09 17:51:36 +01001167 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001168 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001169 switch (instruction->GetType()) {
1170 case Primitive::kPrimBoolean:
1171 case Primitive::kPrimByte:
1172 case Primitive::kPrimChar:
1173 case Primitive::kPrimShort:
1174 case Primitive::kPrimInt:
1175 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001176 case Primitive::kPrimFloat:
1177 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001178 break;
1179
1180 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001181 case Primitive::kPrimDouble:
1182 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001183 break;
1184
1185 default:
1186 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1187 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001188 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001189 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001190 switch (instruction->GetType()) {
1191 case Primitive::kPrimBoolean:
1192 case Primitive::kPrimByte:
1193 case Primitive::kPrimChar:
1194 case Primitive::kPrimShort:
1195 case Primitive::kPrimInt:
1196 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001197 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001198 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001199 break;
1200
1201 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001202 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001203 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001204 break;
1205
1206 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001208 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001209 }
1210}
1211
Calin Juravle175dc732015-08-25 15:42:32 +01001212void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1213 DCHECK(location.IsRegister());
1214 __ movl(location.AsRegister<Register>(), Immediate(value));
1215}
1216
Calin Juravlee460d1d2015-09-29 04:52:17 +01001217void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1218 if (Primitive::Is64BitType(dst_type)) {
1219 Move64(dst, src);
1220 } else {
1221 Move32(dst, src);
1222 }
1223}
1224
1225void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1226 if (location.IsRegister()) {
1227 locations->AddTemp(location);
1228 } else if (location.IsRegisterPair()) {
1229 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1230 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1231 } else {
1232 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1233 }
1234}
1235
David Brazdilfc6a86a2015-06-26 10:33:45 +00001236void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001237 DCHECK(!successor->IsExitBlock());
1238
1239 HBasicBlock* block = got->GetBlock();
1240 HInstruction* previous = got->GetPrevious();
1241
1242 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001243 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001244 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1245 return;
1246 }
1247
1248 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1249 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1250 }
1251 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001252 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001253 }
1254}
1255
David Brazdilfc6a86a2015-06-26 10:33:45 +00001256void LocationsBuilderX86::VisitGoto(HGoto* got) {
1257 got->SetLocations(nullptr);
1258}
1259
1260void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1261 HandleGoto(got, got->GetSuccessor());
1262}
1263
1264void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1265 try_boundary->SetLocations(nullptr);
1266}
1267
1268void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1269 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1270 if (!successor->IsExitBlock()) {
1271 HandleGoto(try_boundary, successor);
1272 }
1273}
1274
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001275void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001276 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001277}
1278
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001279void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001280}
1281
Mark Mendellc4701932015-04-10 13:18:51 -04001282void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1283 Label* true_label,
1284 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001285 if (cond->IsFPConditionTrueIfNaN()) {
1286 __ j(kUnordered, true_label);
1287 } else if (cond->IsFPConditionFalseIfNaN()) {
1288 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001289 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001290 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291}
1292
1293void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1294 Label* true_label,
1295 Label* false_label) {
1296 LocationSummary* locations = cond->GetLocations();
1297 Location left = locations->InAt(0);
1298 Location right = locations->InAt(1);
1299 IfCondition if_cond = cond->GetCondition();
1300
Mark Mendellc4701932015-04-10 13:18:51 -04001301 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001302 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001303 IfCondition true_high_cond = if_cond;
1304 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001305 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001306
1307 // Set the conditions for the test, remembering that == needs to be
1308 // decided using the low words.
1309 switch (if_cond) {
1310 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001311 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001312 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001313 break;
1314 case kCondLT:
1315 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001316 break;
1317 case kCondLE:
1318 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001319 break;
1320 case kCondGT:
1321 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001322 break;
1323 case kCondGE:
1324 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001325 break;
Aart Bike9f37602015-10-09 11:15:55 -07001326 case kCondB:
1327 false_high_cond = kCondA;
1328 break;
1329 case kCondBE:
1330 true_high_cond = kCondB;
1331 break;
1332 case kCondA:
1333 false_high_cond = kCondB;
1334 break;
1335 case kCondAE:
1336 true_high_cond = kCondA;
1337 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001338 }
1339
1340 if (right.IsConstant()) {
1341 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001342 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001343 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001344
1345 if (val_high == 0) {
1346 __ testl(left_high, left_high);
1347 } else {
1348 __ cmpl(left_high, Immediate(val_high));
1349 }
1350 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001351 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001352 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001353 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001355 __ j(X86Condition(true_high_cond), true_label);
1356 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001357 }
1358 // Must be equal high, so compare the lows.
1359 if (val_low == 0) {
1360 __ testl(left_low, left_low);
1361 } else {
1362 __ cmpl(left_low, Immediate(val_low));
1363 }
1364 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001365 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001366 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001367
1368 __ cmpl(left_high, right_high);
1369 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001370 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001371 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001372 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001373 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001374 __ j(X86Condition(true_high_cond), true_label);
1375 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001376 }
1377 // Must be equal high, so compare the lows.
1378 __ cmpl(left_low, right_low);
1379 }
1380 // The last comparison might be unsigned.
1381 __ j(final_condition, true_label);
1382}
1383
David Brazdil0debae72015-11-12 18:37:00 +00001384void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
1385 Label* true_target_in,
1386 Label* false_target_in) {
1387 // Generated branching requires both targets to be explicit. If either of the
1388 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1389 Label fallthrough_target;
1390 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1391 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1392
Mark Mendellc4701932015-04-10 13:18:51 -04001393 LocationSummary* locations = condition->GetLocations();
1394 Location left = locations->InAt(0);
1395 Location right = locations->InAt(1);
1396
Mark Mendellc4701932015-04-10 13:18:51 -04001397 Primitive::Type type = condition->InputAt(0)->GetType();
1398 switch (type) {
1399 case Primitive::kPrimLong:
1400 GenerateLongComparesAndJumps(condition, true_target, false_target);
1401 break;
1402 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001403 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1404 GenerateFPJumps(condition, true_target, false_target);
1405 break;
1406 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001407 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1408 GenerateFPJumps(condition, true_target, false_target);
1409 break;
1410 default:
1411 LOG(FATAL) << "Unexpected compare type " << type;
1412 }
1413
David Brazdil0debae72015-11-12 18:37:00 +00001414 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001415 __ jmp(false_target);
1416 }
David Brazdil0debae72015-11-12 18:37:00 +00001417
1418 if (fallthrough_target.IsLinked()) {
1419 __ Bind(&fallthrough_target);
1420 }
Mark Mendellc4701932015-04-10 13:18:51 -04001421}
1422
David Brazdil0debae72015-11-12 18:37:00 +00001423static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1424 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1425 // are set only strictly before `branch`. We can't use the eflags on long/FP
1426 // conditions if they are materialized due to the complex branching.
1427 return cond->IsCondition() &&
1428 cond->GetNext() == branch &&
1429 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1430 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1431}
1432
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001433void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001434 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001435 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001436 Label* false_target) {
1437 HInstruction* cond = instruction->InputAt(condition_input_index);
1438
1439 if (true_target == nullptr && false_target == nullptr) {
1440 // Nothing to do. The code always falls through.
1441 return;
1442 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001443 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001444 if (cond->AsIntConstant()->IsOne()) {
1445 if (true_target != nullptr) {
1446 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001447 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001448 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001449 DCHECK(cond->AsIntConstant()->IsZero());
1450 if (false_target != nullptr) {
1451 __ jmp(false_target);
1452 }
1453 }
1454 return;
1455 }
1456
1457 // The following code generates these patterns:
1458 // (1) true_target == nullptr && false_target != nullptr
1459 // - opposite condition true => branch to false_target
1460 // (2) true_target != nullptr && false_target == nullptr
1461 // - condition true => branch to true_target
1462 // (3) true_target != nullptr && false_target != nullptr
1463 // - condition true => branch to true_target
1464 // - branch to false_target
1465 if (IsBooleanValueOrMaterializedCondition(cond)) {
1466 if (AreEflagsSetFrom(cond, instruction)) {
1467 if (true_target == nullptr) {
1468 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1469 } else {
1470 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1471 }
1472 } else {
1473 // Materialized condition, compare against 0.
1474 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1475 if (lhs.IsRegister()) {
1476 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1477 } else {
1478 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1479 }
1480 if (true_target == nullptr) {
1481 __ j(kEqual, false_target);
1482 } else {
1483 __ j(kNotEqual, true_target);
1484 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001485 }
1486 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001487 // Condition has not been materialized, use its inputs as the comparison and
1488 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001489 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001490
1491 // If this is a long or FP comparison that has been folded into
1492 // the HCondition, generate the comparison directly.
1493 Primitive::Type type = condition->InputAt(0)->GetType();
1494 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1495 GenerateCompareTestAndBranch(condition, true_target, false_target);
1496 return;
1497 }
1498
1499 Location lhs = condition->GetLocations()->InAt(0);
1500 Location rhs = condition->GetLocations()->InAt(1);
1501 // LHS is guaranteed to be in a register (see LocationsBuilderX86::VisitCondition).
1502 if (rhs.IsRegister()) {
1503 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1504 } else if (rhs.IsConstant()) {
1505 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1506 if (constant == 0) {
1507 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001508 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001509 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001510 }
1511 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001512 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1513 }
1514 if (true_target == nullptr) {
1515 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1516 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001517 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001518 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001519 }
David Brazdil0debae72015-11-12 18:37:00 +00001520
1521 // If neither branch falls through (case 3), the conditional branch to `true_target`
1522 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1523 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001524 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001525 }
1526}
1527
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001528void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001529 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1530 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001531 locations->SetInAt(0, Location::Any());
1532 }
1533}
1534
1535void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001536 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1537 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1538 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1539 nullptr : codegen_->GetLabelOf(true_successor);
1540 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1541 nullptr : codegen_->GetLabelOf(false_successor);
1542 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001543}
1544
1545void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1546 LocationSummary* locations = new (GetGraph()->GetArena())
1547 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001548 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001549 locations->SetInAt(0, Location::Any());
1550 }
1551}
1552
1553void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001554 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001555 DeoptimizationSlowPathX86(deoptimize);
1556 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001557 GenerateTestAndBranch(deoptimize,
1558 /* condition_input_index */ 0,
1559 slow_path->GetEntryLabel(),
1560 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001561}
1562
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001563void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001564 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001565}
1566
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001567void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1568 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001569}
1570
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001571void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001572 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001573}
1574
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001575void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001576 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001577}
1578
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001579void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001580 LocationSummary* locations =
1581 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001582 switch (store->InputAt(1)->GetType()) {
1583 case Primitive::kPrimBoolean:
1584 case Primitive::kPrimByte:
1585 case Primitive::kPrimChar:
1586 case Primitive::kPrimShort:
1587 case Primitive::kPrimInt:
1588 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001589 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001590 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1591 break;
1592
1593 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001594 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001595 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1596 break;
1597
1598 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001599 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001600 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001601}
1602
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001603void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001604}
1605
Roland Levillain0d37cd02015-05-27 16:39:19 +01001606void LocationsBuilderX86::VisitCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001607 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001608 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001609 // Handle the long/FP comparisons made in instruction simplification.
1610 switch (cond->InputAt(0)->GetType()) {
1611 case Primitive::kPrimLong: {
1612 locations->SetInAt(0, Location::RequiresRegister());
1613 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1614 if (cond->NeedsMaterialization()) {
1615 locations->SetOut(Location::RequiresRegister());
1616 }
1617 break;
1618 }
1619 case Primitive::kPrimFloat:
1620 case Primitive::kPrimDouble: {
1621 locations->SetInAt(0, Location::RequiresFpuRegister());
1622 locations->SetInAt(1, Location::RequiresFpuRegister());
1623 if (cond->NeedsMaterialization()) {
1624 locations->SetOut(Location::RequiresRegister());
1625 }
1626 break;
1627 }
1628 default:
1629 locations->SetInAt(0, Location::RequiresRegister());
1630 locations->SetInAt(1, Location::Any());
1631 if (cond->NeedsMaterialization()) {
1632 // We need a byte register.
1633 locations->SetOut(Location::RegisterLocation(ECX));
1634 }
1635 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001636 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637}
1638
Roland Levillain0d37cd02015-05-27 16:39:19 +01001639void InstructionCodeGeneratorX86::VisitCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001640 if (!cond->NeedsMaterialization()) {
1641 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001642 }
Mark Mendellc4701932015-04-10 13:18:51 -04001643
1644 LocationSummary* locations = cond->GetLocations();
1645 Location lhs = locations->InAt(0);
1646 Location rhs = locations->InAt(1);
1647 Register reg = locations->Out().AsRegister<Register>();
1648 Label true_label, false_label;
1649
1650 switch (cond->InputAt(0)->GetType()) {
1651 default: {
1652 // Integer case.
1653
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001654 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001655 __ xorl(reg, reg);
1656
1657 if (rhs.IsRegister()) {
1658 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1659 } else if (rhs.IsConstant()) {
1660 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1661 if (constant == 0) {
1662 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1663 } else {
1664 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1665 }
1666 } else {
1667 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1668 }
Aart Bike9f37602015-10-09 11:15:55 -07001669 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001670 return;
1671 }
1672 case Primitive::kPrimLong:
1673 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1674 break;
1675 case Primitive::kPrimFloat:
1676 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1677 GenerateFPJumps(cond, &true_label, &false_label);
1678 break;
1679 case Primitive::kPrimDouble:
1680 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1681 GenerateFPJumps(cond, &true_label, &false_label);
1682 break;
1683 }
1684
1685 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001686 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001687
Roland Levillain4fa13f62015-07-06 18:11:54 +01001688 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001689 __ Bind(&false_label);
1690 __ xorl(reg, reg);
1691 __ jmp(&done_label);
1692
Roland Levillain4fa13f62015-07-06 18:11:54 +01001693 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001694 __ Bind(&true_label);
1695 __ movl(reg, Immediate(1));
1696 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001697}
1698
1699void LocationsBuilderX86::VisitEqual(HEqual* comp) {
1700 VisitCondition(comp);
1701}
1702
1703void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
1704 VisitCondition(comp);
1705}
1706
1707void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
1708 VisitCondition(comp);
1709}
1710
1711void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
1712 VisitCondition(comp);
1713}
1714
1715void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
1716 VisitCondition(comp);
1717}
1718
1719void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
1720 VisitCondition(comp);
1721}
1722
1723void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1724 VisitCondition(comp);
1725}
1726
1727void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
1728 VisitCondition(comp);
1729}
1730
1731void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
1732 VisitCondition(comp);
1733}
1734
1735void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
1736 VisitCondition(comp);
1737}
1738
1739void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1740 VisitCondition(comp);
1741}
1742
1743void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
1744 VisitCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001745}
1746
Aart Bike9f37602015-10-09 11:15:55 -07001747void LocationsBuilderX86::VisitBelow(HBelow* comp) {
1748 VisitCondition(comp);
1749}
1750
1751void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
1752 VisitCondition(comp);
1753}
1754
1755void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1756 VisitCondition(comp);
1757}
1758
1759void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
1760 VisitCondition(comp);
1761}
1762
1763void LocationsBuilderX86::VisitAbove(HAbove* comp) {
1764 VisitCondition(comp);
1765}
1766
1767void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
1768 VisitCondition(comp);
1769}
1770
1771void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1772 VisitCondition(comp);
1773}
1774
1775void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
1776 VisitCondition(comp);
1777}
1778
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001779void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001780 LocationSummary* locations =
1781 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001782 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001783}
1784
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001785void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001786 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001787}
1788
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001789void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1790 LocationSummary* locations =
1791 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1792 locations->SetOut(Location::ConstantLocation(constant));
1793}
1794
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001795void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001796 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001797}
1798
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001799void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001800 LocationSummary* locations =
1801 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001802 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001803}
1804
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001805void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001806 // Will be generated at use site.
1807}
1808
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001809void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1810 LocationSummary* locations =
1811 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1812 locations->SetOut(Location::ConstantLocation(constant));
1813}
1814
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001815void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001816 // Will be generated at use site.
1817}
1818
1819void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1820 LocationSummary* locations =
1821 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1822 locations->SetOut(Location::ConstantLocation(constant));
1823}
1824
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001825void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001826 // Will be generated at use site.
1827}
1828
Calin Juravle27df7582015-04-17 19:12:31 +01001829void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1830 memory_barrier->SetLocations(nullptr);
1831}
1832
1833void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1834 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
1835}
1836
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001837void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001838 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001839}
1840
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001841void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001842 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001843}
1844
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001845void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001846 LocationSummary* locations =
1847 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001848 switch (ret->InputAt(0)->GetType()) {
1849 case Primitive::kPrimBoolean:
1850 case Primitive::kPrimByte:
1851 case Primitive::kPrimChar:
1852 case Primitive::kPrimShort:
1853 case Primitive::kPrimInt:
1854 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001855 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001856 break;
1857
1858 case Primitive::kPrimLong:
1859 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001860 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001861 break;
1862
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001863 case Primitive::kPrimFloat:
1864 case Primitive::kPrimDouble:
1865 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001866 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001867 break;
1868
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001870 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001872}
1873
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001874void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 if (kIsDebugBuild) {
1876 switch (ret->InputAt(0)->GetType()) {
1877 case Primitive::kPrimBoolean:
1878 case Primitive::kPrimByte:
1879 case Primitive::kPrimChar:
1880 case Primitive::kPrimShort:
1881 case Primitive::kPrimInt:
1882 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001883 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 break;
1885
1886 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001887 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1888 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 break;
1890
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 case Primitive::kPrimFloat:
1892 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001893 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001894 break;
1895
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001896 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001897 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001898 }
1899 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001900 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901}
1902
Calin Juravle175dc732015-08-25 15:42:32 +01001903void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1904 // The trampoline uses the same calling convention as dex calling conventions,
1905 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1906 // the method_idx.
1907 HandleInvoke(invoke);
1908}
1909
1910void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1911 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1912}
1913
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001914void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001915 // When we do not run baseline, explicit clinit checks triggered by static
1916 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1917 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001918
Mark Mendellfb8d2792015-03-31 22:16:59 -04001919 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001920 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001921 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001922 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001923 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001924 return;
1925 }
1926
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001927 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001928
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001929 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1930 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001931 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001932 }
1933
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001934 if (codegen_->IsBaseline()) {
1935 // Baseline does not have enough registers if the current method also
1936 // needs a register. We therefore do not require a register for it, and let
1937 // the code generation of the invoke handle it.
1938 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00001939 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001940 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001941 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001942 }
1943 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001944}
1945
Mark Mendell09ed1a32015-03-25 08:30:06 -04001946static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1947 if (invoke->GetLocations()->Intrinsified()) {
1948 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1949 intrinsic.Dispatch(invoke);
1950 return true;
1951 }
1952 return false;
1953}
1954
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001955void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001956 // When we do not run baseline, explicit clinit checks triggered by static
1957 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1958 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001959
Mark Mendell09ed1a32015-03-25 08:30:06 -04001960 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1961 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001962 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001963
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001964 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001965 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001966 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001967 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001968}
1969
1970void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001971 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1972 if (intrinsic.TryDispatch(invoke)) {
1973 return;
1974 }
1975
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001976 HandleInvoke(invoke);
1977}
1978
1979void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001980 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001981 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001982}
1983
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001984void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001985 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1986 return;
1987 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001988
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001989 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001990 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001991 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001992}
1993
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001994void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001995 // This call to HandleInvoke allocates a temporary (core) register
1996 // which is also used to transfer the hidden argument from FP to
1997 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001998 HandleInvoke(invoke);
1999 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002000 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002001}
2002
2003void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2004 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002005 LocationSummary* locations = invoke->GetLocations();
2006 Register temp = locations->GetTemp(0).AsRegister<Register>();
2007 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002008 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2009 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002010 Location receiver = locations->InAt(0);
2011 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2012
Roland Levillain0d5a2812015-11-13 10:07:31 +00002013 // Set the hidden argument. This is safe to do this here, as XMM7
2014 // won't be modified thereafter, before the `call` instruction.
2015 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002017 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002018
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019 if (receiver.IsStackSlot()) {
2020 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002021 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002022 __ movl(temp, Address(temp, class_offset));
2023 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002024 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002025 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002026 }
Roland Levillain4d027112015-07-01 15:41:14 +01002027 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002028 // Instead of simply (possibly) unpoisoning `temp` here, we should
2029 // emit a read barrier for the previous class reference load.
2030 // However this is not required in practice, as this is an
2031 // intermediate/temporary reference and because the current
2032 // concurrent copying collector keeps the from-space memory
2033 // intact/accessible until the end of the marking phase (the
2034 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002035 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002036 // temp = temp->GetImtEntryAt(method_offset);
2037 __ movl(temp, Address(temp, method_offset));
2038 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002039 __ call(Address(temp,
2040 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002041
2042 DCHECK(!codegen_->IsLeafMethod());
2043 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2044}
2045
Roland Levillain88cb1752014-10-20 16:36:47 +01002046void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2047 LocationSummary* locations =
2048 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2049 switch (neg->GetResultType()) {
2050 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002051 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002052 locations->SetInAt(0, Location::RequiresRegister());
2053 locations->SetOut(Location::SameAsFirstInput());
2054 break;
2055
Roland Levillain88cb1752014-10-20 16:36:47 +01002056 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002057 locations->SetInAt(0, Location::RequiresFpuRegister());
2058 locations->SetOut(Location::SameAsFirstInput());
2059 locations->AddTemp(Location::RequiresRegister());
2060 locations->AddTemp(Location::RequiresFpuRegister());
2061 break;
2062
Roland Levillain88cb1752014-10-20 16:36:47 +01002063 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002064 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002065 locations->SetOut(Location::SameAsFirstInput());
2066 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002067 break;
2068
2069 default:
2070 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2071 }
2072}
2073
2074void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2075 LocationSummary* locations = neg->GetLocations();
2076 Location out = locations->Out();
2077 Location in = locations->InAt(0);
2078 switch (neg->GetResultType()) {
2079 case Primitive::kPrimInt:
2080 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002081 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002082 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002083 break;
2084
2085 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002086 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002087 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002088 __ negl(out.AsRegisterPairLow<Register>());
2089 // Negation is similar to subtraction from zero. The least
2090 // significant byte triggers a borrow when it is different from
2091 // zero; to take it into account, add 1 to the most significant
2092 // byte if the carry flag (CF) is set to 1 after the first NEGL
2093 // operation.
2094 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2095 __ negl(out.AsRegisterPairHigh<Register>());
2096 break;
2097
Roland Levillain5368c212014-11-27 15:03:41 +00002098 case Primitive::kPrimFloat: {
2099 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002100 Register constant = locations->GetTemp(0).AsRegister<Register>();
2101 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002102 // Implement float negation with an exclusive or with value
2103 // 0x80000000 (mask for bit 31, representing the sign of a
2104 // single-precision floating-point number).
2105 __ movl(constant, Immediate(INT32_C(0x80000000)));
2106 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002108 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002109 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002110
Roland Levillain5368c212014-11-27 15:03:41 +00002111 case Primitive::kPrimDouble: {
2112 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002114 // Implement double negation with an exclusive or with value
2115 // 0x8000000000000000 (mask for bit 63, representing the sign of
2116 // a double-precision floating-point number).
2117 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002119 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002120 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002121
2122 default:
2123 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2124 }
2125}
2126
Roland Levillaindff1f282014-11-05 14:15:05 +00002127void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002128 Primitive::Type result_type = conversion->GetResultType();
2129 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002130 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002131
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002132 // The float-to-long and double-to-long type conversions rely on a
2133 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002134 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002135 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2136 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002137 ? LocationSummary::kCall
2138 : LocationSummary::kNoCall;
2139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2141
David Brazdilb2bd1c52015-03-25 11:17:37 +00002142 // The Java language does not allow treating boolean as an integral type but
2143 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002144
Roland Levillaindff1f282014-11-05 14:15:05 +00002145 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002146 case Primitive::kPrimByte:
2147 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002148 case Primitive::kPrimBoolean:
2149 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002150 case Primitive::kPrimShort:
2151 case Primitive::kPrimInt:
2152 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002153 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002154 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2155 // Make the output overlap to please the register allocator. This greatly simplifies
2156 // the validation of the linear scan implementation
2157 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002158 break;
2159
2160 default:
2161 LOG(FATAL) << "Unexpected type conversion from " << input_type
2162 << " to " << result_type;
2163 }
2164 break;
2165
Roland Levillain01a8d712014-11-14 16:27:39 +00002166 case Primitive::kPrimShort:
2167 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002168 case Primitive::kPrimBoolean:
2169 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002170 case Primitive::kPrimByte:
2171 case Primitive::kPrimInt:
2172 case Primitive::kPrimChar:
2173 // Processing a Dex `int-to-short' instruction.
2174 locations->SetInAt(0, Location::Any());
2175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2176 break;
2177
2178 default:
2179 LOG(FATAL) << "Unexpected type conversion from " << input_type
2180 << " to " << result_type;
2181 }
2182 break;
2183
Roland Levillain946e1432014-11-11 17:35:19 +00002184 case Primitive::kPrimInt:
2185 switch (input_type) {
2186 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002187 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002188 locations->SetInAt(0, Location::Any());
2189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2190 break;
2191
2192 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002193 // Processing a Dex `float-to-int' instruction.
2194 locations->SetInAt(0, Location::RequiresFpuRegister());
2195 locations->SetOut(Location::RequiresRegister());
2196 locations->AddTemp(Location::RequiresFpuRegister());
2197 break;
2198
Roland Levillain946e1432014-11-11 17:35:19 +00002199 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002200 // Processing a Dex `double-to-int' instruction.
2201 locations->SetInAt(0, Location::RequiresFpuRegister());
2202 locations->SetOut(Location::RequiresRegister());
2203 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002204 break;
2205
2206 default:
2207 LOG(FATAL) << "Unexpected type conversion from " << input_type
2208 << " to " << result_type;
2209 }
2210 break;
2211
Roland Levillaindff1f282014-11-05 14:15:05 +00002212 case Primitive::kPrimLong:
2213 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002214 case Primitive::kPrimBoolean:
2215 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002216 case Primitive::kPrimByte:
2217 case Primitive::kPrimShort:
2218 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002219 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002220 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 locations->SetInAt(0, Location::RegisterLocation(EAX));
2222 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2223 break;
2224
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002225 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002226 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002227 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002228 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002229 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2230 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2231
Vladimir Marko949c91f2015-01-27 10:48:44 +00002232 // The runtime helper puts the result in EAX, EDX.
2233 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002234 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002235 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002236
2237 default:
2238 LOG(FATAL) << "Unexpected type conversion from " << input_type
2239 << " to " << result_type;
2240 }
2241 break;
2242
Roland Levillain981e4542014-11-14 11:47:14 +00002243 case Primitive::kPrimChar:
2244 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002245 case Primitive::kPrimBoolean:
2246 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002247 case Primitive::kPrimByte:
2248 case Primitive::kPrimShort:
2249 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002250 // Processing a Dex `int-to-char' instruction.
2251 locations->SetInAt(0, Location::Any());
2252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2253 break;
2254
2255 default:
2256 LOG(FATAL) << "Unexpected type conversion from " << input_type
2257 << " to " << result_type;
2258 }
2259 break;
2260
Roland Levillaindff1f282014-11-05 14:15:05 +00002261 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002262 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002263 case Primitive::kPrimBoolean:
2264 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002265 case Primitive::kPrimByte:
2266 case Primitive::kPrimShort:
2267 case Primitive::kPrimInt:
2268 case Primitive::kPrimChar:
2269 // Processing a Dex `int-to-float' instruction.
2270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetOut(Location::RequiresFpuRegister());
2272 break;
2273
2274 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002275 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002276 locations->SetInAt(0, Location::Any());
2277 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002278 break;
2279
Roland Levillaincff13742014-11-17 14:32:17 +00002280 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002281 // Processing a Dex `double-to-float' instruction.
2282 locations->SetInAt(0, Location::RequiresFpuRegister());
2283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002284 break;
2285
2286 default:
2287 LOG(FATAL) << "Unexpected type conversion from " << input_type
2288 << " to " << result_type;
2289 };
2290 break;
2291
Roland Levillaindff1f282014-11-05 14:15:05 +00002292 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002293 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002294 case Primitive::kPrimBoolean:
2295 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002296 case Primitive::kPrimByte:
2297 case Primitive::kPrimShort:
2298 case Primitive::kPrimInt:
2299 case Primitive::kPrimChar:
2300 // Processing a Dex `int-to-double' instruction.
2301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetOut(Location::RequiresFpuRegister());
2303 break;
2304
2305 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002306 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002307 locations->SetInAt(0, Location::Any());
2308 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002309 break;
2310
Roland Levillaincff13742014-11-17 14:32:17 +00002311 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002312 // Processing a Dex `float-to-double' instruction.
2313 locations->SetInAt(0, Location::RequiresFpuRegister());
2314 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 }
2327}
2328
2329void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2330 LocationSummary* locations = conversion->GetLocations();
2331 Location out = locations->Out();
2332 Location in = locations->InAt(0);
2333 Primitive::Type result_type = conversion->GetResultType();
2334 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002335 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002336 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002337 case Primitive::kPrimByte:
2338 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002339 case Primitive::kPrimBoolean:
2340 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002341 case Primitive::kPrimShort:
2342 case Primitive::kPrimInt:
2343 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002344 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002345 if (in.IsRegister()) {
2346 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002347 } else {
2348 DCHECK(in.GetConstant()->IsIntConstant());
2349 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2350 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2351 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 }
2358 break;
2359
Roland Levillain01a8d712014-11-14 16:27:39 +00002360 case Primitive::kPrimShort:
2361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimInt:
2366 case Primitive::kPrimChar:
2367 // Processing a Dex `int-to-short' instruction.
2368 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002370 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002371 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002372 } else {
2373 DCHECK(in.GetConstant()->IsIntConstant());
2374 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002376 }
2377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383 break;
2384
Roland Levillain946e1432014-11-11 17:35:19 +00002385 case Primitive::kPrimInt:
2386 switch (input_type) {
2387 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002388 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002389 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002390 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002391 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002392 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002393 } else {
2394 DCHECK(in.IsConstant());
2395 DCHECK(in.GetConstant()->IsLongConstant());
2396 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002397 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002398 }
2399 break;
2400
Roland Levillain3f8f9362014-12-02 17:45:01 +00002401 case Primitive::kPrimFloat: {
2402 // Processing a Dex `float-to-int' instruction.
2403 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2404 Register output = out.AsRegister<Register>();
2405 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002406 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002407
2408 __ movl(output, Immediate(kPrimIntMax));
2409 // temp = int-to-float(output)
2410 __ cvtsi2ss(temp, output);
2411 // if input >= temp goto done
2412 __ comiss(input, temp);
2413 __ j(kAboveEqual, &done);
2414 // if input == NaN goto nan
2415 __ j(kUnordered, &nan);
2416 // output = float-to-int-truncate(input)
2417 __ cvttss2si(output, input);
2418 __ jmp(&done);
2419 __ Bind(&nan);
2420 // output = 0
2421 __ xorl(output, output);
2422 __ Bind(&done);
2423 break;
2424 }
2425
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002426 case Primitive::kPrimDouble: {
2427 // Processing a Dex `double-to-int' instruction.
2428 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2429 Register output = out.AsRegister<Register>();
2430 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002431 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002432
2433 __ movl(output, Immediate(kPrimIntMax));
2434 // temp = int-to-double(output)
2435 __ cvtsi2sd(temp, output);
2436 // if input >= temp goto done
2437 __ comisd(input, temp);
2438 __ j(kAboveEqual, &done);
2439 // if input == NaN goto nan
2440 __ j(kUnordered, &nan);
2441 // output = double-to-int-truncate(input)
2442 __ cvttsd2si(output, input);
2443 __ jmp(&done);
2444 __ Bind(&nan);
2445 // output = 0
2446 __ xorl(output, output);
2447 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002448 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002449 }
Roland Levillain946e1432014-11-11 17:35:19 +00002450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
2455 break;
2456
Roland Levillaindff1f282014-11-05 14:15:05 +00002457 case Primitive::kPrimLong:
2458 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002459 case Primitive::kPrimBoolean:
2460 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002461 case Primitive::kPrimByte:
2462 case Primitive::kPrimShort:
2463 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002464 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002465 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002466 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2467 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002469 __ cdq();
2470 break;
2471
2472 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002473 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002474 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2475 conversion,
2476 conversion->GetDexPc(),
2477 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002478 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002479 break;
2480
Roland Levillaindff1f282014-11-05 14:15:05 +00002481 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002482 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002483 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2484 conversion,
2485 conversion->GetDexPc(),
2486 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002487 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 break;
2489
2490 default:
2491 LOG(FATAL) << "Unexpected type conversion from " << input_type
2492 << " to " << result_type;
2493 }
2494 break;
2495
Roland Levillain981e4542014-11-14 11:47:14 +00002496 case Primitive::kPrimChar:
2497 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002498 case Primitive::kPrimBoolean:
2499 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002500 case Primitive::kPrimByte:
2501 case Primitive::kPrimShort:
2502 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002503 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2504 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002506 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002507 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002508 } else {
2509 DCHECK(in.GetConstant()->IsIntConstant());
2510 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002512 }
2513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
2519 break;
2520
Roland Levillaindff1f282014-11-05 14:15:05 +00002521 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002522 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002523 case Primitive::kPrimBoolean:
2524 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002525 case Primitive::kPrimByte:
2526 case Primitive::kPrimShort:
2527 case Primitive::kPrimInt:
2528 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002529 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002531 break;
2532
Roland Levillain6d0e4832014-11-27 18:31:21 +00002533 case Primitive::kPrimLong: {
2534 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002535 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002536
Roland Levillain232ade02015-04-20 15:14:36 +01002537 // Create stack space for the call to
2538 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2539 // TODO: enhance register allocator to ask for stack temporaries.
2540 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2541 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2542 __ subl(ESP, Immediate(adjustment));
2543 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002544
Roland Levillain232ade02015-04-20 15:14:36 +01002545 // Load the value to the FP stack, using temporaries if needed.
2546 PushOntoFPStack(in, 0, adjustment, false, true);
2547
2548 if (out.IsStackSlot()) {
2549 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2550 } else {
2551 __ fstps(Address(ESP, 0));
2552 Location stack_temp = Location::StackSlot(0);
2553 codegen_->Move32(out, stack_temp);
2554 }
2555
2556 // Remove the temporary stack space we allocated.
2557 if (adjustment != 0) {
2558 __ addl(ESP, Immediate(adjustment));
2559 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002560 break;
2561 }
2562
Roland Levillaincff13742014-11-17 14:32:17 +00002563 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002564 // Processing a Dex `double-to-float' instruction.
2565 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002566 break;
2567
2568 default:
2569 LOG(FATAL) << "Unexpected type conversion from " << input_type
2570 << " to " << result_type;
2571 };
2572 break;
2573
Roland Levillaindff1f282014-11-05 14:15:05 +00002574 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002575 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002576 case Primitive::kPrimBoolean:
2577 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002578 case Primitive::kPrimByte:
2579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
2581 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002582 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002584 break;
2585
Roland Levillain647b9ed2014-11-27 12:06:00 +00002586 case Primitive::kPrimLong: {
2587 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002588 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002589
Roland Levillain232ade02015-04-20 15:14:36 +01002590 // Create stack space for the call to
2591 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2592 // TODO: enhance register allocator to ask for stack temporaries.
2593 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2594 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2595 __ subl(ESP, Immediate(adjustment));
2596 }
2597
2598 // Load the value to the FP stack, using temporaries if needed.
2599 PushOntoFPStack(in, 0, adjustment, false, true);
2600
2601 if (out.IsDoubleStackSlot()) {
2602 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2603 } else {
2604 __ fstpl(Address(ESP, 0));
2605 Location stack_temp = Location::DoubleStackSlot(0);
2606 codegen_->Move64(out, stack_temp);
2607 }
2608
2609 // Remove the temporary stack space we allocated.
2610 if (adjustment != 0) {
2611 __ addl(ESP, Immediate(adjustment));
2612 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002613 break;
2614 }
2615
Roland Levillaincff13742014-11-17 14:32:17 +00002616 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002617 // Processing a Dex `float-to-double' instruction.
2618 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002625 break;
2626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
2630 }
2631}
2632
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002634 LocationSummary* locations =
2635 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002636 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002637 case Primitive::kPrimInt: {
2638 locations->SetInAt(0, Location::RequiresRegister());
2639 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2640 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2641 break;
2642 }
2643
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002644 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::Any());
2647 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002648 break;
2649 }
2650
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002651 case Primitive::kPrimFloat:
2652 case Primitive::kPrimDouble: {
2653 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002654 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002655 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002656 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002657 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002658
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002659 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002660 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2661 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002662 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002663}
2664
2665void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2666 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002667 Location first = locations->InAt(0);
2668 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002669 Location out = locations->Out();
2670
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002671 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002672 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002673 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002674 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2675 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002676 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2677 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002678 } else {
2679 __ leal(out.AsRegister<Register>(), Address(
2680 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2681 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002682 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002683 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2684 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2685 __ addl(out.AsRegister<Register>(), Immediate(value));
2686 } else {
2687 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2688 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002689 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002690 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002691 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002692 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002693 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002694 }
2695
2696 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002697 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002698 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2699 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002700 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002701 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2702 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002703 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002704 } else {
2705 DCHECK(second.IsConstant()) << second;
2706 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2707 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2708 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002709 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002710 break;
2711 }
2712
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002713 case Primitive::kPrimFloat: {
2714 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002715 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002716 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2717 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2718 DCHECK(!const_area->NeedsMaterialization());
2719 __ addss(first.AsFpuRegister<XmmRegister>(),
2720 codegen_->LiteralFloatAddress(
2721 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2722 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2723 } else {
2724 DCHECK(second.IsStackSlot());
2725 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002726 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002727 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002728 }
2729
2730 case Primitive::kPrimDouble: {
2731 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002732 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002733 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2734 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2735 DCHECK(!const_area->NeedsMaterialization());
2736 __ addsd(first.AsFpuRegister<XmmRegister>(),
2737 codegen_->LiteralDoubleAddress(
2738 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2739 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2740 } else {
2741 DCHECK(second.IsDoubleStackSlot());
2742 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002743 }
2744 break;
2745 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002746
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002747 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002748 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002749 }
2750}
2751
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002752void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002753 LocationSummary* locations =
2754 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002755 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002756 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002757 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002758 locations->SetInAt(0, Location::RequiresRegister());
2759 locations->SetInAt(1, Location::Any());
2760 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002761 break;
2762 }
Calin Juravle11351682014-10-23 15:38:15 +01002763 case Primitive::kPrimFloat:
2764 case Primitive::kPrimDouble: {
2765 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002766 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002767 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768 break;
Calin Juravle11351682014-10-23 15:38:15 +01002769 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002770
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002771 default:
Calin Juravle11351682014-10-23 15:38:15 +01002772 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002773 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002774}
2775
2776void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2777 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002778 Location first = locations->InAt(0);
2779 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002780 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002781 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002782 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002783 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002785 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002786 __ subl(first.AsRegister<Register>(),
2787 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002788 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002789 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002790 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002791 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002792 }
2793
2794 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002795 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002796 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2797 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002798 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002799 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002800 __ sbbl(first.AsRegisterPairHigh<Register>(),
2801 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002802 } else {
2803 DCHECK(second.IsConstant()) << second;
2804 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2805 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2806 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002807 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002808 break;
2809 }
2810
Calin Juravle11351682014-10-23 15:38:15 +01002811 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002812 if (second.IsFpuRegister()) {
2813 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2814 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2815 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2816 DCHECK(!const_area->NeedsMaterialization());
2817 __ subss(first.AsFpuRegister<XmmRegister>(),
2818 codegen_->LiteralFloatAddress(
2819 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2820 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2821 } else {
2822 DCHECK(second.IsStackSlot());
2823 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2824 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002825 break;
Calin Juravle11351682014-10-23 15:38:15 +01002826 }
2827
2828 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002829 if (second.IsFpuRegister()) {
2830 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2831 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2832 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2833 DCHECK(!const_area->NeedsMaterialization());
2834 __ subsd(first.AsFpuRegister<XmmRegister>(),
2835 codegen_->LiteralDoubleAddress(
2836 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2837 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2838 } else {
2839 DCHECK(second.IsDoubleStackSlot());
2840 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2841 }
Calin Juravle11351682014-10-23 15:38:15 +01002842 break;
2843 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002844
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845 default:
Calin Juravle11351682014-10-23 15:38:15 +01002846 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002847 }
2848}
2849
Calin Juravle34bacdf2014-10-07 20:23:36 +01002850void LocationsBuilderX86::VisitMul(HMul* mul) {
2851 LocationSummary* locations =
2852 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2853 switch (mul->GetResultType()) {
2854 case Primitive::kPrimInt:
2855 locations->SetInAt(0, Location::RequiresRegister());
2856 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002857 if (mul->InputAt(1)->IsIntConstant()) {
2858 // Can use 3 operand multiply.
2859 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2860 } else {
2861 locations->SetOut(Location::SameAsFirstInput());
2862 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002863 break;
2864 case Primitive::kPrimLong: {
2865 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002866 locations->SetInAt(1, Location::Any());
2867 locations->SetOut(Location::SameAsFirstInput());
2868 // Needed for imul on 32bits with 64bits output.
2869 locations->AddTemp(Location::RegisterLocation(EAX));
2870 locations->AddTemp(Location::RegisterLocation(EDX));
2871 break;
2872 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002873 case Primitive::kPrimFloat:
2874 case Primitive::kPrimDouble: {
2875 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002876 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002877 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002878 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002879 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002880
2881 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002882 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002883 }
2884}
2885
2886void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2887 LocationSummary* locations = mul->GetLocations();
2888 Location first = locations->InAt(0);
2889 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002890 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002891
2892 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002893 case Primitive::kPrimInt:
2894 // The constant may have ended up in a register, so test explicitly to avoid
2895 // problems where the output may not be the same as the first operand.
2896 if (mul->InputAt(1)->IsIntConstant()) {
2897 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2898 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2899 } else if (second.IsRegister()) {
2900 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002901 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002902 } else {
2903 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002904 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002905 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002906 }
2907 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002908
2909 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002910 Register in1_hi = first.AsRegisterPairHigh<Register>();
2911 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002912 Register eax = locations->GetTemp(0).AsRegister<Register>();
2913 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002914
2915 DCHECK_EQ(EAX, eax);
2916 DCHECK_EQ(EDX, edx);
2917
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002918 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002919 // output: in1
2920 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2921 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2922 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002923 if (second.IsConstant()) {
2924 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002925
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002926 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2927 int32_t low_value = Low32Bits(value);
2928 int32_t high_value = High32Bits(value);
2929 Immediate low(low_value);
2930 Immediate high(high_value);
2931
2932 __ movl(eax, high);
2933 // eax <- in1.lo * in2.hi
2934 __ imull(eax, in1_lo);
2935 // in1.hi <- in1.hi * in2.lo
2936 __ imull(in1_hi, low);
2937 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2938 __ addl(in1_hi, eax);
2939 // move in2_lo to eax to prepare for double precision
2940 __ movl(eax, low);
2941 // edx:eax <- in1.lo * in2.lo
2942 __ mull(in1_lo);
2943 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2944 __ addl(in1_hi, edx);
2945 // in1.lo <- (in1.lo * in2.lo)[31:0];
2946 __ movl(in1_lo, eax);
2947 } else if (second.IsRegisterPair()) {
2948 Register in2_hi = second.AsRegisterPairHigh<Register>();
2949 Register in2_lo = second.AsRegisterPairLow<Register>();
2950
2951 __ movl(eax, in2_hi);
2952 // eax <- in1.lo * in2.hi
2953 __ imull(eax, in1_lo);
2954 // in1.hi <- in1.hi * in2.lo
2955 __ imull(in1_hi, in2_lo);
2956 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2957 __ addl(in1_hi, eax);
2958 // move in1_lo to eax to prepare for double precision
2959 __ movl(eax, in1_lo);
2960 // edx:eax <- in1.lo * in2.lo
2961 __ mull(in2_lo);
2962 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2963 __ addl(in1_hi, edx);
2964 // in1.lo <- (in1.lo * in2.lo)[31:0];
2965 __ movl(in1_lo, eax);
2966 } else {
2967 DCHECK(second.IsDoubleStackSlot()) << second;
2968 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2969 Address in2_lo(ESP, second.GetStackIndex());
2970
2971 __ movl(eax, in2_hi);
2972 // eax <- in1.lo * in2.hi
2973 __ imull(eax, in1_lo);
2974 // in1.hi <- in1.hi * in2.lo
2975 __ imull(in1_hi, in2_lo);
2976 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2977 __ addl(in1_hi, eax);
2978 // move in1_lo to eax to prepare for double precision
2979 __ movl(eax, in1_lo);
2980 // edx:eax <- in1.lo * in2.lo
2981 __ mull(in2_lo);
2982 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2983 __ addl(in1_hi, edx);
2984 // in1.lo <- (in1.lo * in2.lo)[31:0];
2985 __ movl(in1_lo, eax);
2986 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002987
2988 break;
2989 }
2990
Calin Juravleb5bfa962014-10-21 18:02:24 +01002991 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002992 DCHECK(first.Equals(locations->Out()));
2993 if (second.IsFpuRegister()) {
2994 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2995 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2996 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
2997 DCHECK(!const_area->NeedsMaterialization());
2998 __ mulss(first.AsFpuRegister<XmmRegister>(),
2999 codegen_->LiteralFloatAddress(
3000 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3001 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3002 } else {
3003 DCHECK(second.IsStackSlot());
3004 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3005 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003006 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003007 }
3008
3009 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003010 DCHECK(first.Equals(locations->Out()));
3011 if (second.IsFpuRegister()) {
3012 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3013 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3014 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3015 DCHECK(!const_area->NeedsMaterialization());
3016 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3017 codegen_->LiteralDoubleAddress(
3018 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3019 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3020 } else {
3021 DCHECK(second.IsDoubleStackSlot());
3022 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3023 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003024 break;
3025 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003026
3027 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003028 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029 }
3030}
3031
Roland Levillain232ade02015-04-20 15:14:36 +01003032void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3033 uint32_t temp_offset,
3034 uint32_t stack_adjustment,
3035 bool is_fp,
3036 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003037 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003038 DCHECK(!is_wide);
3039 if (is_fp) {
3040 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3041 } else {
3042 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3043 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003044 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003045 DCHECK(is_wide);
3046 if (is_fp) {
3047 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3048 } else {
3049 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3050 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003051 } else {
3052 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003053 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003054 Location stack_temp = Location::StackSlot(temp_offset);
3055 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003056 if (is_fp) {
3057 __ flds(Address(ESP, temp_offset));
3058 } else {
3059 __ filds(Address(ESP, temp_offset));
3060 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003061 } else {
3062 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3063 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003064 if (is_fp) {
3065 __ fldl(Address(ESP, temp_offset));
3066 } else {
3067 __ fildl(Address(ESP, temp_offset));
3068 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003069 }
3070 }
3071}
3072
3073void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3074 Primitive::Type type = rem->GetResultType();
3075 bool is_float = type == Primitive::kPrimFloat;
3076 size_t elem_size = Primitive::ComponentSize(type);
3077 LocationSummary* locations = rem->GetLocations();
3078 Location first = locations->InAt(0);
3079 Location second = locations->InAt(1);
3080 Location out = locations->Out();
3081
3082 // Create stack space for 2 elements.
3083 // TODO: enhance register allocator to ask for stack temporaries.
3084 __ subl(ESP, Immediate(2 * elem_size));
3085
3086 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003087 const bool is_wide = !is_float;
3088 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3089 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003090
3091 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003092 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003093 __ Bind(&retry);
3094 __ fprem();
3095
3096 // Move FP status to AX.
3097 __ fstsw();
3098
3099 // And see if the argument reduction is complete. This is signaled by the
3100 // C2 FPU flag bit set to 0.
3101 __ andl(EAX, Immediate(kC2ConditionMask));
3102 __ j(kNotEqual, &retry);
3103
3104 // We have settled on the final value. Retrieve it into an XMM register.
3105 // Store FP top of stack to real stack.
3106 if (is_float) {
3107 __ fsts(Address(ESP, 0));
3108 } else {
3109 __ fstl(Address(ESP, 0));
3110 }
3111
3112 // Pop the 2 items from the FP stack.
3113 __ fucompp();
3114
3115 // Load the value from the stack into an XMM register.
3116 DCHECK(out.IsFpuRegister()) << out;
3117 if (is_float) {
3118 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3119 } else {
3120 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3121 }
3122
3123 // And remove the temporary stack space we allocated.
3124 __ addl(ESP, Immediate(2 * elem_size));
3125}
3126
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003127
3128void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3129 DCHECK(instruction->IsDiv() || instruction->IsRem());
3130
3131 LocationSummary* locations = instruction->GetLocations();
3132 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003133 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003134
3135 Register out_register = locations->Out().AsRegister<Register>();
3136 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003137 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003138
3139 DCHECK(imm == 1 || imm == -1);
3140
3141 if (instruction->IsRem()) {
3142 __ xorl(out_register, out_register);
3143 } else {
3144 __ movl(out_register, input_register);
3145 if (imm == -1) {
3146 __ negl(out_register);
3147 }
3148 }
3149}
3150
3151
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003152void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003153 LocationSummary* locations = instruction->GetLocations();
3154
3155 Register out_register = locations->Out().AsRegister<Register>();
3156 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003157 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003158
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003159 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003160 Register num = locations->GetTemp(0).AsRegister<Register>();
3161
3162 __ leal(num, Address(input_register, std::abs(imm) - 1));
3163 __ testl(input_register, input_register);
3164 __ cmovl(kGreaterEqual, num, input_register);
3165 int shift = CTZ(imm);
3166 __ sarl(num, Immediate(shift));
3167
3168 if (imm < 0) {
3169 __ negl(num);
3170 }
3171
3172 __ movl(out_register, num);
3173}
3174
3175void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3176 DCHECK(instruction->IsDiv() || instruction->IsRem());
3177
3178 LocationSummary* locations = instruction->GetLocations();
3179 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3180
3181 Register eax = locations->InAt(0).AsRegister<Register>();
3182 Register out = locations->Out().AsRegister<Register>();
3183 Register num;
3184 Register edx;
3185
3186 if (instruction->IsDiv()) {
3187 edx = locations->GetTemp(0).AsRegister<Register>();
3188 num = locations->GetTemp(1).AsRegister<Register>();
3189 } else {
3190 edx = locations->Out().AsRegister<Register>();
3191 num = locations->GetTemp(0).AsRegister<Register>();
3192 }
3193
3194 DCHECK_EQ(EAX, eax);
3195 DCHECK_EQ(EDX, edx);
3196 if (instruction->IsDiv()) {
3197 DCHECK_EQ(EAX, out);
3198 } else {
3199 DCHECK_EQ(EDX, out);
3200 }
3201
3202 int64_t magic;
3203 int shift;
3204 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3205
Mark Mendell0c9497d2015-08-21 09:30:05 -04003206 NearLabel ndiv;
3207 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003208 // If numerator is 0, the result is 0, no computation needed.
3209 __ testl(eax, eax);
3210 __ j(kNotEqual, &ndiv);
3211
3212 __ xorl(out, out);
3213 __ jmp(&end);
3214
3215 __ Bind(&ndiv);
3216
3217 // Save the numerator.
3218 __ movl(num, eax);
3219
3220 // EAX = magic
3221 __ movl(eax, Immediate(magic));
3222
3223 // EDX:EAX = magic * numerator
3224 __ imull(num);
3225
3226 if (imm > 0 && magic < 0) {
3227 // EDX += num
3228 __ addl(edx, num);
3229 } else if (imm < 0 && magic > 0) {
3230 __ subl(edx, num);
3231 }
3232
3233 // Shift if needed.
3234 if (shift != 0) {
3235 __ sarl(edx, Immediate(shift));
3236 }
3237
3238 // EDX += 1 if EDX < 0
3239 __ movl(eax, edx);
3240 __ shrl(edx, Immediate(31));
3241 __ addl(edx, eax);
3242
3243 if (instruction->IsRem()) {
3244 __ movl(eax, num);
3245 __ imull(edx, Immediate(imm));
3246 __ subl(eax, edx);
3247 __ movl(edx, eax);
3248 } else {
3249 __ movl(eax, edx);
3250 }
3251 __ Bind(&end);
3252}
3253
Calin Juravlebacfec32014-11-14 15:54:36 +00003254void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3255 DCHECK(instruction->IsDiv() || instruction->IsRem());
3256
3257 LocationSummary* locations = instruction->GetLocations();
3258 Location out = locations->Out();
3259 Location first = locations->InAt(0);
3260 Location second = locations->InAt(1);
3261 bool is_div = instruction->IsDiv();
3262
3263 switch (instruction->GetResultType()) {
3264 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003265 DCHECK_EQ(EAX, first.AsRegister<Register>());
3266 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003267
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003268 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003269 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003270
3271 if (imm == 0) {
3272 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3273 } else if (imm == 1 || imm == -1) {
3274 DivRemOneOrMinusOne(instruction);
3275 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003276 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003277 } else {
3278 DCHECK(imm <= -2 || imm >= 2);
3279 GenerateDivRemWithAnyConstant(instruction);
3280 }
3281 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003282 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003283 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003284 is_div);
3285 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003286
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003287 Register second_reg = second.AsRegister<Register>();
3288 // 0x80000000/-1 triggers an arithmetic exception!
3289 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3290 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003291
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003292 __ cmpl(second_reg, Immediate(-1));
3293 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003294
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003295 // edx:eax <- sign-extended of eax
3296 __ cdq();
3297 // eax = quotient, edx = remainder
3298 __ idivl(second_reg);
3299 __ Bind(slow_path->GetExitLabel());
3300 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003301 break;
3302 }
3303
3304 case Primitive::kPrimLong: {
3305 InvokeRuntimeCallingConvention calling_convention;
3306 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3307 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3308 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3309 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3310 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3311 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3312
3313 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003314 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3315 instruction,
3316 instruction->GetDexPc(),
3317 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003318 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003319 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003320 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3321 instruction,
3322 instruction->GetDexPc(),
3323 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003324 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003325 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003326 break;
3327 }
3328
3329 default:
3330 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3331 }
3332}
3333
Calin Juravle7c4954d2014-10-28 16:57:40 +00003334void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003335 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003336 ? LocationSummary::kCall
3337 : LocationSummary::kNoCall;
3338 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3339
Calin Juravle7c4954d2014-10-28 16:57:40 +00003340 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003341 case Primitive::kPrimInt: {
3342 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003343 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003344 locations->SetOut(Location::SameAsFirstInput());
3345 // Intel uses edx:eax as the dividend.
3346 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003347 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3348 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3349 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003350 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351 locations->AddTemp(Location::RequiresRegister());
3352 }
Calin Juravled0d48522014-11-04 16:40:20 +00003353 break;
3354 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003355 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003356 InvokeRuntimeCallingConvention calling_convention;
3357 locations->SetInAt(0, Location::RegisterPairLocation(
3358 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3359 locations->SetInAt(1, Location::RegisterPairLocation(
3360 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3361 // Runtime helper puts the result in EAX, EDX.
3362 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003363 break;
3364 }
3365 case Primitive::kPrimFloat:
3366 case Primitive::kPrimDouble: {
3367 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003368 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003369 locations->SetOut(Location::SameAsFirstInput());
3370 break;
3371 }
3372
3373 default:
3374 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3375 }
3376}
3377
3378void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3379 LocationSummary* locations = div->GetLocations();
3380 Location first = locations->InAt(0);
3381 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003382
3383 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003384 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003385 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003386 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003387 break;
3388 }
3389
3390 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003391 if (second.IsFpuRegister()) {
3392 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3393 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3394 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3395 DCHECK(!const_area->NeedsMaterialization());
3396 __ divss(first.AsFpuRegister<XmmRegister>(),
3397 codegen_->LiteralFloatAddress(
3398 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3399 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3400 } else {
3401 DCHECK(second.IsStackSlot());
3402 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3403 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003404 break;
3405 }
3406
3407 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003408 if (second.IsFpuRegister()) {
3409 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3410 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3411 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3412 DCHECK(!const_area->NeedsMaterialization());
3413 __ divsd(first.AsFpuRegister<XmmRegister>(),
3414 codegen_->LiteralDoubleAddress(
3415 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3416 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3417 } else {
3418 DCHECK(second.IsDoubleStackSlot());
3419 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3420 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003421 break;
3422 }
3423
3424 default:
3425 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3426 }
3427}
3428
Calin Juravlebacfec32014-11-14 15:54:36 +00003429void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003430 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003431
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003432 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3433 ? LocationSummary::kCall
3434 : LocationSummary::kNoCall;
3435 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003436
Calin Juravled2ec87d2014-12-08 14:24:46 +00003437 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003438 case Primitive::kPrimInt: {
3439 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003441 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003442 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3443 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3444 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003445 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003446 locations->AddTemp(Location::RequiresRegister());
3447 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 break;
3449 }
3450 case Primitive::kPrimLong: {
3451 InvokeRuntimeCallingConvention calling_convention;
3452 locations->SetInAt(0, Location::RegisterPairLocation(
3453 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3454 locations->SetInAt(1, Location::RegisterPairLocation(
3455 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3456 // Runtime helper puts the result in EAX, EDX.
3457 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3458 break;
3459 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003460 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003461 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003462 locations->SetInAt(0, Location::Any());
3463 locations->SetInAt(1, Location::Any());
3464 locations->SetOut(Location::RequiresFpuRegister());
3465 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 break;
3467 }
3468
3469 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003470 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003471 }
3472}
3473
3474void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3475 Primitive::Type type = rem->GetResultType();
3476 switch (type) {
3477 case Primitive::kPrimInt:
3478 case Primitive::kPrimLong: {
3479 GenerateDivRemIntegral(rem);
3480 break;
3481 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003482 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003483 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003484 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003485 break;
3486 }
3487 default:
3488 LOG(FATAL) << "Unexpected rem type " << type;
3489 }
3490}
3491
Calin Juravled0d48522014-11-04 16:40:20 +00003492void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003493 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3494 ? LocationSummary::kCallOnSlowPath
3495 : LocationSummary::kNoCall;
3496 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003497 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003498 case Primitive::kPrimByte:
3499 case Primitive::kPrimChar:
3500 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003501 case Primitive::kPrimInt: {
3502 locations->SetInAt(0, Location::Any());
3503 break;
3504 }
3505 case Primitive::kPrimLong: {
3506 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3507 if (!instruction->IsConstant()) {
3508 locations->AddTemp(Location::RequiresRegister());
3509 }
3510 break;
3511 }
3512 default:
3513 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3514 }
Calin Juravled0d48522014-11-04 16:40:20 +00003515 if (instruction->HasUses()) {
3516 locations->SetOut(Location::SameAsFirstInput());
3517 }
3518}
3519
3520void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003521 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003522 codegen_->AddSlowPath(slow_path);
3523
3524 LocationSummary* locations = instruction->GetLocations();
3525 Location value = locations->InAt(0);
3526
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003527 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003528 case Primitive::kPrimByte:
3529 case Primitive::kPrimChar:
3530 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003531 case Primitive::kPrimInt: {
3532 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003533 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003534 __ j(kEqual, slow_path->GetEntryLabel());
3535 } else if (value.IsStackSlot()) {
3536 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3537 __ j(kEqual, slow_path->GetEntryLabel());
3538 } else {
3539 DCHECK(value.IsConstant()) << value;
3540 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3541 __ jmp(slow_path->GetEntryLabel());
3542 }
3543 }
3544 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003545 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003546 case Primitive::kPrimLong: {
3547 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003548 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003549 __ movl(temp, value.AsRegisterPairLow<Register>());
3550 __ orl(temp, value.AsRegisterPairHigh<Register>());
3551 __ j(kEqual, slow_path->GetEntryLabel());
3552 } else {
3553 DCHECK(value.IsConstant()) << value;
3554 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3555 __ jmp(slow_path->GetEntryLabel());
3556 }
3557 }
3558 break;
3559 }
3560 default:
3561 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003562 }
Calin Juravled0d48522014-11-04 16:40:20 +00003563}
3564
Calin Juravle9aec02f2014-11-18 23:06:35 +00003565void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3566 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3567
3568 LocationSummary* locations =
3569 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3570
3571 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003572 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003573 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003574 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003575 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003576 // The shift count needs to be in CL or a constant.
3577 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003578 locations->SetOut(Location::SameAsFirstInput());
3579 break;
3580 }
3581 default:
3582 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3583 }
3584}
3585
3586void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3587 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3588
3589 LocationSummary* locations = op->GetLocations();
3590 Location first = locations->InAt(0);
3591 Location second = locations->InAt(1);
3592 DCHECK(first.Equals(locations->Out()));
3593
3594 switch (op->GetResultType()) {
3595 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003596 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003597 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003598 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003599 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003600 DCHECK_EQ(ECX, second_reg);
3601 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003602 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003603 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003604 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003605 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003606 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003607 }
3608 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003609 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3610 if (shift == 0) {
3611 return;
3612 }
3613 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003614 if (op->IsShl()) {
3615 __ shll(first_reg, imm);
3616 } else if (op->IsShr()) {
3617 __ sarl(first_reg, imm);
3618 } else {
3619 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003620 }
3621 }
3622 break;
3623 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003624 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003625 if (second.IsRegister()) {
3626 Register second_reg = second.AsRegister<Register>();
3627 DCHECK_EQ(ECX, second_reg);
3628 if (op->IsShl()) {
3629 GenerateShlLong(first, second_reg);
3630 } else if (op->IsShr()) {
3631 GenerateShrLong(first, second_reg);
3632 } else {
3633 GenerateUShrLong(first, second_reg);
3634 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003635 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003636 // Shift by a constant.
3637 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3638 // Nothing to do if the shift is 0, as the input is already the output.
3639 if (shift != 0) {
3640 if (op->IsShl()) {
3641 GenerateShlLong(first, shift);
3642 } else if (op->IsShr()) {
3643 GenerateShrLong(first, shift);
3644 } else {
3645 GenerateUShrLong(first, shift);
3646 }
3647 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003648 }
3649 break;
3650 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003651 default:
3652 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3653 }
3654}
3655
Mark P Mendell73945692015-04-29 14:56:17 +00003656void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3657 Register low = loc.AsRegisterPairLow<Register>();
3658 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003659 if (shift == 1) {
3660 // This is just an addition.
3661 __ addl(low, low);
3662 __ adcl(high, high);
3663 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003664 // Shift by 32 is easy. High gets low, and low gets 0.
3665 codegen_->EmitParallelMoves(
3666 loc.ToLow(),
3667 loc.ToHigh(),
3668 Primitive::kPrimInt,
3669 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3670 loc.ToLow(),
3671 Primitive::kPrimInt);
3672 } else if (shift > 32) {
3673 // Low part becomes 0. High part is low part << (shift-32).
3674 __ movl(high, low);
3675 __ shll(high, Immediate(shift - 32));
3676 __ xorl(low, low);
3677 } else {
3678 // Between 1 and 31.
3679 __ shld(high, low, Immediate(shift));
3680 __ shll(low, Immediate(shift));
3681 }
3682}
3683
Calin Juravle9aec02f2014-11-18 23:06:35 +00003684void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003685 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003686 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3687 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3688 __ testl(shifter, Immediate(32));
3689 __ j(kEqual, &done);
3690 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3691 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3692 __ Bind(&done);
3693}
3694
Mark P Mendell73945692015-04-29 14:56:17 +00003695void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3696 Register low = loc.AsRegisterPairLow<Register>();
3697 Register high = loc.AsRegisterPairHigh<Register>();
3698 if (shift == 32) {
3699 // Need to copy the sign.
3700 DCHECK_NE(low, high);
3701 __ movl(low, high);
3702 __ sarl(high, Immediate(31));
3703 } else if (shift > 32) {
3704 DCHECK_NE(low, high);
3705 // High part becomes sign. Low part is shifted by shift - 32.
3706 __ movl(low, high);
3707 __ sarl(high, Immediate(31));
3708 __ sarl(low, Immediate(shift - 32));
3709 } else {
3710 // Between 1 and 31.
3711 __ shrd(low, high, Immediate(shift));
3712 __ sarl(high, Immediate(shift));
3713 }
3714}
3715
Calin Juravle9aec02f2014-11-18 23:06:35 +00003716void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003717 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003718 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3719 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3720 __ testl(shifter, Immediate(32));
3721 __ j(kEqual, &done);
3722 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3723 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3724 __ Bind(&done);
3725}
3726
Mark P Mendell73945692015-04-29 14:56:17 +00003727void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3728 Register low = loc.AsRegisterPairLow<Register>();
3729 Register high = loc.AsRegisterPairHigh<Register>();
3730 if (shift == 32) {
3731 // Shift by 32 is easy. Low gets high, and high gets 0.
3732 codegen_->EmitParallelMoves(
3733 loc.ToHigh(),
3734 loc.ToLow(),
3735 Primitive::kPrimInt,
3736 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3737 loc.ToHigh(),
3738 Primitive::kPrimInt);
3739 } else if (shift > 32) {
3740 // Low part is high >> (shift - 32). High part becomes 0.
3741 __ movl(low, high);
3742 __ shrl(low, Immediate(shift - 32));
3743 __ xorl(high, high);
3744 } else {
3745 // Between 1 and 31.
3746 __ shrd(low, high, Immediate(shift));
3747 __ shrl(high, Immediate(shift));
3748 }
3749}
3750
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003752 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003753 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3754 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3755 __ testl(shifter, Immediate(32));
3756 __ j(kEqual, &done);
3757 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3758 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3759 __ Bind(&done);
3760}
3761
3762void LocationsBuilderX86::VisitShl(HShl* shl) {
3763 HandleShift(shl);
3764}
3765
3766void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3767 HandleShift(shl);
3768}
3769
3770void LocationsBuilderX86::VisitShr(HShr* shr) {
3771 HandleShift(shr);
3772}
3773
3774void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3775 HandleShift(shr);
3776}
3777
3778void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3779 HandleShift(ushr);
3780}
3781
3782void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3783 HandleShift(ushr);
3784}
3785
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003786void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003787 LocationSummary* locations =
3788 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003789 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003790 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003791 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3792 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003793}
3794
3795void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003796 // Note: if heap poisoning is enabled, the entry point takes cares
3797 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003798 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3799 instruction,
3800 instruction->GetDexPc(),
3801 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003802 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003803 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003804}
3805
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003806void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3807 LocationSummary* locations =
3808 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3809 locations->SetOut(Location::RegisterLocation(EAX));
3810 InvokeRuntimeCallingConvention calling_convention;
3811 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003812 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003813 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003814}
3815
3816void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3817 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003818 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003819 // Note: if heap poisoning is enabled, the entry point takes cares
3820 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003821 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3822 instruction,
3823 instruction->GetDexPc(),
3824 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003825 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003826 DCHECK(!codegen_->IsLeafMethod());
3827}
3828
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003829void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003830 LocationSummary* locations =
3831 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003832 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3833 if (location.IsStackSlot()) {
3834 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3835 } else if (location.IsDoubleStackSlot()) {
3836 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003837 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003838 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003839}
3840
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003841void InstructionCodeGeneratorX86::VisitParameterValue(
3842 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3843}
3844
3845void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3846 LocationSummary* locations =
3847 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3848 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3849}
3850
3851void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003852}
3853
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003854void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003855 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003856 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003857 locations->SetInAt(0, Location::RequiresRegister());
3858 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003859}
3860
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003861void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3862 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003863 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003864 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003865 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003866 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003867 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003868 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003869 break;
3870
3871 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003872 __ notl(out.AsRegisterPairLow<Register>());
3873 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003874 break;
3875
3876 default:
3877 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
3878 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003879}
3880
David Brazdil66d126e2015-04-03 16:02:44 +01003881void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
3882 LocationSummary* locations =
3883 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
3884 locations->SetInAt(0, Location::RequiresRegister());
3885 locations->SetOut(Location::SameAsFirstInput());
3886}
3887
3888void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01003889 LocationSummary* locations = bool_not->GetLocations();
3890 Location in = locations->InAt(0);
3891 Location out = locations->Out();
3892 DCHECK(in.Equals(out));
3893 __ xorl(out.AsRegister<Register>(), Immediate(1));
3894}
3895
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003896void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003897 LocationSummary* locations =
3898 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00003899 switch (compare->InputAt(0)->GetType()) {
3900 case Primitive::kPrimLong: {
3901 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00003902 locations->SetInAt(1, Location::Any());
3903 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3904 break;
3905 }
3906 case Primitive::kPrimFloat:
3907 case Primitive::kPrimDouble: {
3908 locations->SetInAt(0, Location::RequiresFpuRegister());
3909 locations->SetInAt(1, Location::RequiresFpuRegister());
3910 locations->SetOut(Location::RequiresRegister());
3911 break;
3912 }
3913 default:
3914 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
3915 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003916}
3917
3918void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003919 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003920 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00003921 Location left = locations->InAt(0);
3922 Location right = locations->InAt(1);
3923
Mark Mendell0c9497d2015-08-21 09:30:05 -04003924 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003925 switch (compare->InputAt(0)->GetType()) {
3926 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003927 Register left_low = left.AsRegisterPairLow<Register>();
3928 Register left_high = left.AsRegisterPairHigh<Register>();
3929 int32_t val_low = 0;
3930 int32_t val_high = 0;
3931 bool right_is_const = false;
3932
3933 if (right.IsConstant()) {
3934 DCHECK(right.GetConstant()->IsLongConstant());
3935 right_is_const = true;
3936 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
3937 val_low = Low32Bits(val);
3938 val_high = High32Bits(val);
3939 }
3940
Calin Juravleddb7df22014-11-25 20:56:51 +00003941 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003942 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003943 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003944 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003945 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003946 DCHECK(right_is_const) << right;
3947 if (val_high == 0) {
3948 __ testl(left_high, left_high);
3949 } else {
3950 __ cmpl(left_high, Immediate(val_high));
3951 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003952 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003953 __ j(kLess, &less); // Signed compare.
3954 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003955 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003956 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003957 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003958 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003959 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04003960 DCHECK(right_is_const) << right;
3961 if (val_low == 0) {
3962 __ testl(left_low, left_low);
3963 } else {
3964 __ cmpl(left_low, Immediate(val_low));
3965 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01003966 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003967 break;
3968 }
3969 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003970 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003971 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
3972 break;
3973 }
3974 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003975 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00003976 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003977 break;
3978 }
3979 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00003980 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003981 }
Calin Juravleddb7df22014-11-25 20:56:51 +00003982 __ movl(out, Immediate(0));
3983 __ j(kEqual, &done);
3984 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
3985
3986 __ Bind(&greater);
3987 __ movl(out, Immediate(1));
3988 __ jmp(&done);
3989
3990 __ Bind(&less);
3991 __ movl(out, Immediate(-1));
3992
3993 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01003994}
3995
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01003996void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003997 LocationSummary* locations =
3998 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01003999 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4000 locations->SetInAt(i, Location::Any());
4001 }
4002 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004003}
4004
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004005void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004006 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004007}
4008
Calin Juravle52c48962014-12-16 17:02:57 +00004009void InstructionCodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
4010 /*
4011 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4012 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4013 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4014 */
4015 switch (kind) {
4016 case MemBarrierKind::kAnyAny: {
4017 __ mfence();
4018 break;
4019 }
4020 case MemBarrierKind::kAnyStore:
4021 case MemBarrierKind::kLoadAny:
4022 case MemBarrierKind::kStoreStore: {
4023 // nop
4024 break;
4025 }
4026 default:
4027 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004028 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004029}
4030
Vladimir Markodc151b22015-10-15 18:02:30 +01004031HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4032 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4033 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004034 switch (desired_dispatch_info.code_ptr_location) {
4035 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4036 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4037 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4038 // (Though the direct CALL ptr16:32 is available for consideration).
4039 return HInvokeStaticOrDirect::DispatchInfo {
4040 desired_dispatch_info.method_load_kind,
4041 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4042 desired_dispatch_info.method_load_data,
4043 0u
4044 };
4045 default:
4046 return desired_dispatch_info;
4047 }
4048}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004049
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004050Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4051 Register temp) {
4052 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004053 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004054 if (!invoke->GetLocations()->Intrinsified()) {
4055 return location.AsRegister<Register>();
4056 }
4057 // For intrinsics we allow any location, so it may be on the stack.
4058 if (!location.IsRegister()) {
4059 __ movl(temp, Address(ESP, location.GetStackIndex()));
4060 return temp;
4061 }
4062 // For register locations, check if the register was saved. If so, get it from the stack.
4063 // Note: There is a chance that the register was saved but not overwritten, so we could
4064 // save one load. However, since this is just an intrinsic slow path we prefer this
4065 // simple and more robust approach rather that trying to determine if that's the case.
4066 SlowPathCode* slow_path = GetCurrentSlowPath();
4067 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4068 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4069 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4070 __ movl(temp, Address(ESP, stack_offset));
4071 return temp;
4072 }
4073 return location.AsRegister<Register>();
4074}
4075
Vladimir Marko58155012015-08-19 12:49:41 +00004076void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4077 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4078 switch (invoke->GetMethodLoadKind()) {
4079 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4080 // temp = thread->string_init_entrypoint
4081 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4082 break;
4083 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004084 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004085 break;
4086 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4087 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4088 break;
4089 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4090 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4091 method_patches_.emplace_back(invoke->GetTargetMethod());
4092 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4093 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004094 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4095 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4096 temp.AsRegister<Register>());
4097 uint32_t offset = invoke->GetDexCacheArrayOffset();
4098 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4099 // Add the patch entry and bind its label at the end of the instruction.
4100 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4101 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4102 break;
4103 }
Vladimir Marko58155012015-08-19 12:49:41 +00004104 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004105 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004106 Register method_reg;
4107 Register reg = temp.AsRegister<Register>();
4108 if (current_method.IsRegister()) {
4109 method_reg = current_method.AsRegister<Register>();
4110 } else {
4111 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4112 DCHECK(!current_method.IsValid());
4113 method_reg = reg;
4114 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4115 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004116 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004117 __ movl(reg, Address(method_reg,
4118 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004119 // temp = temp[index_in_cache]
4120 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4121 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4122 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004123 }
Vladimir Marko58155012015-08-19 12:49:41 +00004124 }
4125
4126 switch (invoke->GetCodePtrLocation()) {
4127 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4128 __ call(GetFrameEntryLabel());
4129 break;
4130 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4131 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4132 Label* label = &relative_call_patches_.back().label;
4133 __ call(label); // Bind to the patch label, override at link time.
4134 __ Bind(label); // Bind the label at the end of the "call" insn.
4135 break;
4136 }
4137 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4138 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004139 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4140 LOG(FATAL) << "Unsupported";
4141 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004142 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4143 // (callee_method + offset_of_quick_compiled_code)()
4144 __ call(Address(callee_method.AsRegister<Register>(),
4145 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4146 kX86WordSize).Int32Value()));
4147 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004148 }
4149
4150 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004151}
4152
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004153void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4154 Register temp = temp_in.AsRegister<Register>();
4155 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4156 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004157
4158 // Use the calling convention instead of the location of the receiver, as
4159 // intrinsics may have put the receiver in a different register. In the intrinsics
4160 // slow path, the arguments have been moved to the right place, so here we are
4161 // guaranteed that the receiver is the first register of the calling convention.
4162 InvokeDexCallingConvention calling_convention;
4163 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004164 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004165 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004166 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004167 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004168 // Instead of simply (possibly) unpoisoning `temp` here, we should
4169 // emit a read barrier for the previous class reference load.
4170 // However this is not required in practice, as this is an
4171 // intermediate/temporary reference and because the current
4172 // concurrent copying collector keeps the from-space memory
4173 // intact/accessible until the end of the marking phase (the
4174 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004175 __ MaybeUnpoisonHeapReference(temp);
4176 // temp = temp->GetMethodAt(method_offset);
4177 __ movl(temp, Address(temp, method_offset));
4178 // call temp->GetEntryPoint();
4179 __ call(Address(
4180 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4181}
4182
Vladimir Marko58155012015-08-19 12:49:41 +00004183void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4184 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004185 size_t size =
4186 method_patches_.size() +
4187 relative_call_patches_.size() +
4188 pc_relative_dex_cache_patches_.size();
4189 linker_patches->reserve(size);
4190 // The label points to the end of the "movl" insn but the literal offset for method
4191 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4192 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004193 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004194 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004195 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4196 info.target_method.dex_file,
4197 info.target_method.dex_method_index));
4198 }
4199 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004200 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004201 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4202 info.target_method.dex_file,
4203 info.target_method.dex_method_index));
4204 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004205 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4206 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4207 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4208 &info.target_dex_file,
4209 GetMethodAddressOffset(),
4210 info.element_offset));
4211 }
Vladimir Marko58155012015-08-19 12:49:41 +00004212}
4213
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004214void CodeGeneratorX86::MarkGCCard(Register temp,
4215 Register card,
4216 Register object,
4217 Register value,
4218 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004219 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004220 if (value_can_be_null) {
4221 __ testl(value, value);
4222 __ j(kEqual, &is_null);
4223 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004224 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4225 __ movl(temp, object);
4226 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004227 __ movb(Address(temp, card, TIMES_1, 0),
4228 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004229 if (value_can_be_null) {
4230 __ Bind(&is_null);
4231 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004232}
4233
Calin Juravle52c48962014-12-16 17:02:57 +00004234void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4235 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004236
4237 bool object_field_get_with_read_barrier =
4238 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004239 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004240 new (GetGraph()->GetArena()) LocationSummary(instruction,
4241 kEmitCompilerReadBarrier ?
4242 LocationSummary::kCallOnSlowPath :
4243 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004244 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004245
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004246 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4247 locations->SetOut(Location::RequiresFpuRegister());
4248 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004249 // The output overlaps in case of long: we don't want the low move
4250 // to overwrite the object's location. Likewise, in the case of
4251 // an object field get with read barriers enabled, we do not want
4252 // the move to overwrite the object's location, as we need it to emit
4253 // the read barrier.
4254 locations->SetOut(
4255 Location::RequiresRegister(),
4256 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4257 Location::kOutputOverlap :
4258 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004259 }
Calin Juravle52c48962014-12-16 17:02:57 +00004260
4261 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4262 // Long values can be loaded atomically into an XMM using movsd.
4263 // So we use an XMM register as a temp to achieve atomicity (first load the temp into the XMM
4264 // and then copy the XMM into the output 32bits at a time).
4265 locations->AddTemp(Location::RequiresFpuRegister());
4266 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004267}
4268
Calin Juravle52c48962014-12-16 17:02:57 +00004269void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4270 const FieldInfo& field_info) {
4271 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272
Calin Juravle52c48962014-12-16 17:02:57 +00004273 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004274 Location base_loc = locations->InAt(0);
4275 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004276 Location out = locations->Out();
4277 bool is_volatile = field_info.IsVolatile();
4278 Primitive::Type field_type = field_info.GetFieldType();
4279 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4280
4281 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004282 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004283 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004284 break;
4285 }
4286
4287 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004288 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004289 break;
4290 }
4291
4292 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004293 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004294 break;
4295 }
4296
4297 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004298 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004299 break;
4300 }
4301
4302 case Primitive::kPrimInt:
4303 case Primitive::kPrimNot: {
Calin Juravle52c48962014-12-16 17:02:57 +00004304 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004305 break;
4306 }
4307
4308 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004309 if (is_volatile) {
4310 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4311 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004313 __ movd(out.AsRegisterPairLow<Register>(), temp);
4314 __ psrlq(temp, Immediate(32));
4315 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4316 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004317 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004318 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004319 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004320 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4321 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004322 break;
4323 }
4324
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004325 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004326 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004327 break;
4328 }
4329
4330 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004331 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004332 break;
4333 }
4334
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004335 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004336 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004337 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004338 }
Calin Juravle52c48962014-12-16 17:02:57 +00004339
Calin Juravle77520bc2015-01-12 18:45:46 +00004340 // Longs are handled in the switch.
4341 if (field_type != Primitive::kPrimLong) {
4342 codegen_->MaybeRecordImplicitNullCheck(instruction);
4343 }
4344
Calin Juravle52c48962014-12-16 17:02:57 +00004345 if (is_volatile) {
4346 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4347 }
Roland Levillain4d027112015-07-01 15:41:14 +01004348
4349 if (field_type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004350 codegen_->MaybeGenerateReadBarrier(instruction, out, out, base_loc, offset);
Roland Levillain4d027112015-07-01 15:41:14 +01004351 }
Calin Juravle52c48962014-12-16 17:02:57 +00004352}
4353
4354void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4355 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4356
4357 LocationSummary* locations =
4358 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4359 locations->SetInAt(0, Location::RequiresRegister());
4360 bool is_volatile = field_info.IsVolatile();
4361 Primitive::Type field_type = field_info.GetFieldType();
4362 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4363 || (field_type == Primitive::kPrimByte);
4364
4365 // The register allocator does not support multiple
4366 // inputs that die at entry with one in a specific register.
4367 if (is_byte_type) {
4368 // Ensure the value is in a byte register.
4369 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004370 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004371 if (is_volatile && field_type == Primitive::kPrimDouble) {
4372 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4373 locations->SetInAt(1, Location::RequiresFpuRegister());
4374 } else {
4375 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4376 }
4377 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4378 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004379 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004380
Calin Juravle52c48962014-12-16 17:02:57 +00004381 // 64bits value can be atomically written to an address with movsd and an XMM register.
4382 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4383 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4384 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4385 // isolated cases when we need this it isn't worth adding the extra complexity.
4386 locations->AddTemp(Location::RequiresFpuRegister());
4387 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004388 } else {
4389 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4390
4391 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4392 // Temporary registers for the write barrier.
4393 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4394 // Ensure the card is in a byte register.
4395 locations->AddTemp(Location::RegisterLocation(ECX));
4396 }
Calin Juravle52c48962014-12-16 17:02:57 +00004397 }
4398}
4399
4400void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004401 const FieldInfo& field_info,
4402 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004403 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4404
4405 LocationSummary* locations = instruction->GetLocations();
4406 Register base = locations->InAt(0).AsRegister<Register>();
4407 Location value = locations->InAt(1);
4408 bool is_volatile = field_info.IsVolatile();
4409 Primitive::Type field_type = field_info.GetFieldType();
4410 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004411 bool needs_write_barrier =
4412 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004413
4414 if (is_volatile) {
4415 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
4416 }
4417
Mark Mendell81489372015-11-04 11:30:41 -05004418 bool maybe_record_implicit_null_check_done = false;
4419
Calin Juravle52c48962014-12-16 17:02:57 +00004420 switch (field_type) {
4421 case Primitive::kPrimBoolean:
4422 case Primitive::kPrimByte: {
4423 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4424 break;
4425 }
4426
4427 case Primitive::kPrimShort:
4428 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004429 if (value.IsConstant()) {
4430 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4431 __ movw(Address(base, offset), Immediate(v));
4432 } else {
4433 __ movw(Address(base, offset), value.AsRegister<Register>());
4434 }
Calin Juravle52c48962014-12-16 17:02:57 +00004435 break;
4436 }
4437
4438 case Primitive::kPrimInt:
4439 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004440 if (kPoisonHeapReferences && needs_write_barrier) {
4441 // Note that in the case where `value` is a null reference,
4442 // we do not enter this block, as the reference does not
4443 // need poisoning.
4444 DCHECK_EQ(field_type, Primitive::kPrimNot);
4445 Register temp = locations->GetTemp(0).AsRegister<Register>();
4446 __ movl(temp, value.AsRegister<Register>());
4447 __ PoisonHeapReference(temp);
4448 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004449 } else if (value.IsConstant()) {
4450 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4451 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004452 } else {
4453 __ movl(Address(base, offset), value.AsRegister<Register>());
4454 }
Calin Juravle52c48962014-12-16 17:02:57 +00004455 break;
4456 }
4457
4458 case Primitive::kPrimLong: {
4459 if (is_volatile) {
4460 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4461 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4462 __ movd(temp1, value.AsRegisterPairLow<Register>());
4463 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4464 __ punpckldq(temp1, temp2);
4465 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004466 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004467 } else if (value.IsConstant()) {
4468 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4469 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4470 codegen_->MaybeRecordImplicitNullCheck(instruction);
4471 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004472 } else {
4473 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004474 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004475 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4476 }
Mark Mendell81489372015-11-04 11:30:41 -05004477 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004478 break;
4479 }
4480
4481 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004482 if (value.IsConstant()) {
4483 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4484 __ movl(Address(base, offset), Immediate(v));
4485 } else {
4486 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4487 }
Calin Juravle52c48962014-12-16 17:02:57 +00004488 break;
4489 }
4490
4491 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004492 if (value.IsConstant()) {
4493 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4494 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4495 codegen_->MaybeRecordImplicitNullCheck(instruction);
4496 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4497 maybe_record_implicit_null_check_done = true;
4498 } else {
4499 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4500 }
Calin Juravle52c48962014-12-16 17:02:57 +00004501 break;
4502 }
4503
4504 case Primitive::kPrimVoid:
4505 LOG(FATAL) << "Unreachable type " << field_type;
4506 UNREACHABLE();
4507 }
4508
Mark Mendell81489372015-11-04 11:30:41 -05004509 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004510 codegen_->MaybeRecordImplicitNullCheck(instruction);
4511 }
4512
Roland Levillain4d027112015-07-01 15:41:14 +01004513 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004514 Register temp = locations->GetTemp(0).AsRegister<Register>();
4515 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004516 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004517 }
4518
Calin Juravle52c48962014-12-16 17:02:57 +00004519 if (is_volatile) {
4520 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
4521 }
4522}
4523
4524void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4525 HandleFieldGet(instruction, instruction->GetFieldInfo());
4526}
4527
4528void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4529 HandleFieldGet(instruction, instruction->GetFieldInfo());
4530}
4531
4532void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4533 HandleFieldSet(instruction, instruction->GetFieldInfo());
4534}
4535
4536void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004537 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004538}
4539
4540void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4541 HandleFieldSet(instruction, instruction->GetFieldInfo());
4542}
4543
4544void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004545 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004546}
4547
4548void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4549 HandleFieldGet(instruction, instruction->GetFieldInfo());
4550}
4551
4552void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4553 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004554}
4555
Calin Juravlee460d1d2015-09-29 04:52:17 +01004556void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4557 HUnresolvedInstanceFieldGet* instruction) {
4558 FieldAccessCallingConventionX86 calling_convention;
4559 codegen_->CreateUnresolvedFieldLocationSummary(
4560 instruction, instruction->GetFieldType(), calling_convention);
4561}
4562
4563void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4564 HUnresolvedInstanceFieldGet* instruction) {
4565 FieldAccessCallingConventionX86 calling_convention;
4566 codegen_->GenerateUnresolvedFieldAccess(instruction,
4567 instruction->GetFieldType(),
4568 instruction->GetFieldIndex(),
4569 instruction->GetDexPc(),
4570 calling_convention);
4571}
4572
4573void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4574 HUnresolvedInstanceFieldSet* instruction) {
4575 FieldAccessCallingConventionX86 calling_convention;
4576 codegen_->CreateUnresolvedFieldLocationSummary(
4577 instruction, instruction->GetFieldType(), calling_convention);
4578}
4579
4580void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4581 HUnresolvedInstanceFieldSet* instruction) {
4582 FieldAccessCallingConventionX86 calling_convention;
4583 codegen_->GenerateUnresolvedFieldAccess(instruction,
4584 instruction->GetFieldType(),
4585 instruction->GetFieldIndex(),
4586 instruction->GetDexPc(),
4587 calling_convention);
4588}
4589
4590void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4591 HUnresolvedStaticFieldGet* instruction) {
4592 FieldAccessCallingConventionX86 calling_convention;
4593 codegen_->CreateUnresolvedFieldLocationSummary(
4594 instruction, instruction->GetFieldType(), calling_convention);
4595}
4596
4597void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4598 HUnresolvedStaticFieldGet* instruction) {
4599 FieldAccessCallingConventionX86 calling_convention;
4600 codegen_->GenerateUnresolvedFieldAccess(instruction,
4601 instruction->GetFieldType(),
4602 instruction->GetFieldIndex(),
4603 instruction->GetDexPc(),
4604 calling_convention);
4605}
4606
4607void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4608 HUnresolvedStaticFieldSet* instruction) {
4609 FieldAccessCallingConventionX86 calling_convention;
4610 codegen_->CreateUnresolvedFieldLocationSummary(
4611 instruction, instruction->GetFieldType(), calling_convention);
4612}
4613
4614void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4615 HUnresolvedStaticFieldSet* instruction) {
4616 FieldAccessCallingConventionX86 calling_convention;
4617 codegen_->GenerateUnresolvedFieldAccess(instruction,
4618 instruction->GetFieldType(),
4619 instruction->GetFieldIndex(),
4620 instruction->GetDexPc(),
4621 calling_convention);
4622}
4623
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004624void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004625 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4626 ? LocationSummary::kCallOnSlowPath
4627 : LocationSummary::kNoCall;
4628 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4629 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004630 ? Location::RequiresRegister()
4631 : Location::Any();
4632 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004633 if (instruction->HasUses()) {
4634 locations->SetOut(Location::SameAsFirstInput());
4635 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004636}
4637
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004638void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004639 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4640 return;
4641 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004642 LocationSummary* locations = instruction->GetLocations();
4643 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004644
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004645 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4646 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4647}
4648
4649void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004650 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004651 codegen_->AddSlowPath(slow_path);
4652
4653 LocationSummary* locations = instruction->GetLocations();
4654 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655
4656 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004657 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004658 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004659 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004660 } else {
4661 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004662 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004663 __ jmp(slow_path->GetEntryLabel());
4664 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004665 }
4666 __ j(kEqual, slow_path->GetEntryLabel());
4667}
4668
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004669void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004670 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004671 GenerateImplicitNullCheck(instruction);
4672 } else {
4673 GenerateExplicitNullCheck(instruction);
4674 }
4675}
4676
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004677void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004678 bool object_array_get_with_read_barrier =
4679 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004680 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004681 new (GetGraph()->GetArena()) LocationSummary(instruction,
4682 object_array_get_with_read_barrier ?
4683 LocationSummary::kCallOnSlowPath :
4684 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004685 locations->SetInAt(0, Location::RequiresRegister());
4686 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004687 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4688 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4689 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004690 // The output overlaps in case of long: we don't want the low move
4691 // to overwrite the array's location. Likewise, in the case of an
4692 // object array get with read barriers enabled, we do not want the
4693 // move to overwrite the array's location, as we need it to emit
4694 // the read barrier.
4695 locations->SetOut(
4696 Location::RequiresRegister(),
4697 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4698 Location::kOutputOverlap :
4699 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004700 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004701}
4702
4703void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4704 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004705 Location obj_loc = locations->InAt(0);
4706 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004707 Location index = locations->InAt(1);
4708
Calin Juravle77520bc2015-01-12 18:45:46 +00004709 Primitive::Type type = instruction->GetType();
4710 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004711 case Primitive::kPrimBoolean: {
4712 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004713 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004714 if (index.IsConstant()) {
4715 __ movzxb(out, Address(obj,
4716 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4717 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004718 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004719 }
4720 break;
4721 }
4722
4723 case Primitive::kPrimByte: {
4724 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004725 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004726 if (index.IsConstant()) {
4727 __ movsxb(out, Address(obj,
4728 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4729 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004730 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004731 }
4732 break;
4733 }
4734
4735 case Primitive::kPrimShort: {
4736 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004737 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004738 if (index.IsConstant()) {
4739 __ movsxw(out, Address(obj,
4740 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4741 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004742 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004743 }
4744 break;
4745 }
4746
4747 case Primitive::kPrimChar: {
4748 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004749 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004750 if (index.IsConstant()) {
4751 __ movzxw(out, Address(obj,
4752 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4753 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004754 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004755 }
4756 break;
4757 }
4758
4759 case Primitive::kPrimInt:
4760 case Primitive::kPrimNot: {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004761 static_assert(
4762 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4763 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004764 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004765 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004766 if (index.IsConstant()) {
4767 __ movl(out, Address(obj,
4768 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4769 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004770 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004771 }
4772 break;
4773 }
4774
4775 case Primitive::kPrimLong: {
4776 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004777 Location out = locations->Out();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004778 DCHECK_NE(obj, out.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004779 if (index.IsConstant()) {
4780 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004781 __ movl(out.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004782 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004783 __ movl(out.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004784 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004785 __ movl(out.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004786 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004787 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004788 __ movl(out.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004789 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004790 }
4791 break;
4792 }
4793
Mark Mendell7c8d0092015-01-26 11:21:33 -05004794 case Primitive::kPrimFloat: {
4795 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
4796 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4797 if (index.IsConstant()) {
4798 __ movss(out, Address(obj,
4799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4800 } else {
4801 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4802 }
4803 break;
4804 }
4805
4806 case Primitive::kPrimDouble: {
4807 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
4808 XmmRegister out = locations->Out().AsFpuRegister<XmmRegister>();
4809 if (index.IsConstant()) {
4810 __ movsd(out, Address(obj,
4811 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
4812 } else {
4813 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
4814 }
4815 break;
4816 }
4817
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004818 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00004819 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004820 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004821 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004822
4823 if (type != Primitive::kPrimLong) {
4824 codegen_->MaybeRecordImplicitNullCheck(instruction);
4825 }
Roland Levillain4d027112015-07-01 15:41:14 +01004826
4827 if (type == Primitive::kPrimNot) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004828 static_assert(
4829 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4830 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4831 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4832 Location out = locations->Out();
4833 if (index.IsConstant()) {
4834 uint32_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4835 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, offset);
4836 } else {
4837 codegen_->MaybeGenerateReadBarrier(instruction, out, out, obj_loc, data_offset, index);
4838 }
Roland Levillain4d027112015-07-01 15:41:14 +01004839 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004840}
4841
4842void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05004843 // This location builder might end up asking to up to four registers, which is
4844 // not currently possible for baseline. The situation in which we need four
4845 // registers cannot be met by baseline though, because it has not run any
4846 // optimization.
4847
Nicolas Geoffray39468442014-09-02 15:17:15 +01004848 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004849
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004850 bool needs_write_barrier =
4851 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004852 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
4853 bool object_array_set_with_read_barrier =
4854 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004855
Nicolas Geoffray39468442014-09-02 15:17:15 +01004856 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
4857 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00004858 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
4859 LocationSummary::kCallOnSlowPath :
4860 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004861
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004862 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
4863 || (value_type == Primitive::kPrimByte);
4864 // We need the inputs to be different than the output in case of long operation.
4865 // In case of a byte operation, the register allocator does not support multiple
4866 // inputs that die at entry with one in a specific register.
4867 locations->SetInAt(0, Location::RequiresRegister());
4868 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4869 if (is_byte_type) {
4870 // Ensure the value is in a byte register.
4871 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
4872 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004873 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004874 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004875 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
4876 }
4877 if (needs_write_barrier) {
4878 // Temporary registers for the write barrier.
4879 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
4880 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00004881 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004882 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004883}
4884
4885void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
4886 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004887 Location array_loc = locations->InAt(0);
4888 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004889 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004890 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004891 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004892 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
4893 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
4894 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004895 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004896 bool needs_write_barrier =
4897 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004898
4899 switch (value_type) {
4900 case Primitive::kPrimBoolean:
4901 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004902 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
4903 Address address = index.IsConstant()
4904 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
4905 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
4906 if (value.IsRegister()) {
4907 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004908 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004909 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004911 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004912 break;
4913 }
4914
4915 case Primitive::kPrimShort:
4916 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004917 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
4918 Address address = index.IsConstant()
4919 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
4920 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
4921 if (value.IsRegister()) {
4922 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004924 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 }
Calin Juravle77520bc2015-01-12 18:45:46 +00004926 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927 break;
4928 }
4929
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00004930 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004931 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4932 Address address = index.IsConstant()
4933 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
4934 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004935
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004936 if (!value.IsRegister()) {
4937 // Just setting null.
4938 DCHECK(instruction->InputAt(2)->IsNullConstant());
4939 DCHECK(value.IsConstant()) << value;
4940 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00004941 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004942 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004943 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004944 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004945 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004946
4947 DCHECK(needs_write_barrier);
4948 Register register_value = value.AsRegister<Register>();
4949 NearLabel done, not_null, do_put;
4950 SlowPathCode* slow_path = nullptr;
4951 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004952 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004953 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
4954 codegen_->AddSlowPath(slow_path);
4955 if (instruction->GetValueCanBeNull()) {
4956 __ testl(register_value, register_value);
4957 __ j(kNotEqual, &not_null);
4958 __ movl(address, Immediate(0));
4959 codegen_->MaybeRecordImplicitNullCheck(instruction);
4960 __ jmp(&done);
4961 __ Bind(&not_null);
4962 }
4963
Roland Levillain0d5a2812015-11-13 10:07:31 +00004964 if (kEmitCompilerReadBarrier) {
4965 // When read barriers are enabled, the type checking
4966 // instrumentation requires two read barriers:
4967 //
4968 // __ movl(temp2, temp);
4969 // // /* HeapReference<Class> */ temp = temp->component_type_
4970 // __ movl(temp, Address(temp, component_offset));
4971 // codegen_->GenerateReadBarrier(
4972 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
4973 //
4974 // // /* HeapReference<Class> */ temp2 = register_value->klass_
4975 // __ movl(temp2, Address(register_value, class_offset));
4976 // codegen_->GenerateReadBarrier(
4977 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
4978 //
4979 // __ cmpl(temp, temp2);
4980 //
4981 // However, the second read barrier may trash `temp`, as it
4982 // is a temporary register, and as such would not be saved
4983 // along with live registers before calling the runtime (nor
4984 // restored afterwards). So in this case, we bail out and
4985 // delegate the work to the array set slow path.
4986 //
4987 // TODO: Extend the register allocator to support a new
4988 // "(locally) live temp" location so as to avoid always
4989 // going into the slow path when read barriers are enabled.
4990 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01004991 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004992 // /* HeapReference<Class> */ temp = array->klass_
4993 __ movl(temp, Address(array, class_offset));
4994 codegen_->MaybeRecordImplicitNullCheck(instruction);
4995 __ MaybeUnpoisonHeapReference(temp);
4996
4997 // /* HeapReference<Class> */ temp = temp->component_type_
4998 __ movl(temp, Address(temp, component_offset));
4999 // If heap poisoning is enabled, no need to unpoison `temp`
5000 // nor the object reference in `register_value->klass`, as
5001 // we are comparing two poisoned references.
5002 __ cmpl(temp, Address(register_value, class_offset));
5003
5004 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5005 __ j(kEqual, &do_put);
5006 // If heap poisoning is enabled, the `temp` reference has
5007 // not been unpoisoned yet; unpoison it now.
5008 __ MaybeUnpoisonHeapReference(temp);
5009
5010 // /* HeapReference<Class> */ temp = temp->super_class_
5011 __ movl(temp, Address(temp, super_offset));
5012 // If heap poisoning is enabled, no need to unpoison
5013 // `temp`, as we are comparing against null below.
5014 __ testl(temp, temp);
5015 __ j(kNotEqual, slow_path->GetEntryLabel());
5016 __ Bind(&do_put);
5017 } else {
5018 __ j(kNotEqual, slow_path->GetEntryLabel());
5019 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005020 }
5021 }
5022
5023 if (kPoisonHeapReferences) {
5024 __ movl(temp, register_value);
5025 __ PoisonHeapReference(temp);
5026 __ movl(address, temp);
5027 } else {
5028 __ movl(address, register_value);
5029 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005030 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005031 codegen_->MaybeRecordImplicitNullCheck(instruction);
5032 }
5033
5034 Register card = locations->GetTemp(1).AsRegister<Register>();
5035 codegen_->MarkGCCard(
5036 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5037 __ Bind(&done);
5038
5039 if (slow_path != nullptr) {
5040 __ Bind(slow_path->GetExitLabel());
5041 }
5042
5043 break;
5044 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005045
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005046 case Primitive::kPrimInt: {
5047 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5048 Address address = index.IsConstant()
5049 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5050 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5051 if (value.IsRegister()) {
5052 __ movl(address, value.AsRegister<Register>());
5053 } else {
5054 DCHECK(value.IsConstant()) << value;
5055 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5056 __ movl(address, Immediate(v));
5057 }
5058 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005059 break;
5060 }
5061
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005062 case Primitive::kPrimLong: {
5063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 if (index.IsConstant()) {
5065 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005066 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005067 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005068 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005069 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005070 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005071 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005072 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005073 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005074 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005075 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005076 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005077 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005078 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005079 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005080 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005081 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005082 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005083 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005084 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005085 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005086 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005087 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005088 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005089 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005090 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005091 Immediate(High32Bits(val)));
5092 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 }
5094 break;
5095 }
5096
Mark Mendell7c8d0092015-01-26 11:21:33 -05005097 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005098 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5099 Address address = index.IsConstant()
5100 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5101 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005102 if (value.IsFpuRegister()) {
5103 __ movss(address, value.AsFpuRegister<XmmRegister>());
5104 } else {
5105 DCHECK(value.IsConstant());
5106 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5107 __ movl(address, Immediate(v));
5108 }
5109 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005110 break;
5111 }
5112
5113 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005114 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5115 Address address = index.IsConstant()
5116 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5117 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005118 if (value.IsFpuRegister()) {
5119 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5120 } else {
5121 DCHECK(value.IsConstant());
5122 Address address_hi = index.IsConstant() ?
5123 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5124 offset + kX86WordSize) :
5125 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5126 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5127 __ movl(address, Immediate(Low32Bits(v)));
5128 codegen_->MaybeRecordImplicitNullCheck(instruction);
5129 __ movl(address_hi, Immediate(High32Bits(v)));
5130 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005131 break;
5132 }
5133
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134 case Primitive::kPrimVoid:
5135 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005136 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 }
5138}
5139
5140void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5141 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005142 locations->SetInAt(0, Location::RequiresRegister());
5143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144}
5145
5146void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5147 LocationSummary* locations = instruction->GetLocations();
5148 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 Register obj = locations->InAt(0).AsRegister<Register>();
5150 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005152 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153}
5154
5155void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005156 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5157 ? LocationSummary::kCallOnSlowPath
5158 : LocationSummary::kNoCall;
5159 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005160 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005161 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005162 if (instruction->HasUses()) {
5163 locations->SetOut(Location::SameAsFirstInput());
5164 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005165}
5166
5167void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5168 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005169 Location index_loc = locations->InAt(0);
5170 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005171 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005172 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005173
Mark Mendell99dbd682015-04-22 16:18:52 -04005174 if (length_loc.IsConstant()) {
5175 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5176 if (index_loc.IsConstant()) {
5177 // BCE will remove the bounds check if we are guarenteed to pass.
5178 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5179 if (index < 0 || index >= length) {
5180 codegen_->AddSlowPath(slow_path);
5181 __ jmp(slow_path->GetEntryLabel());
5182 } else {
5183 // Some optimization after BCE may have generated this, and we should not
5184 // generate a bounds check if it is a valid range.
5185 }
5186 return;
5187 }
5188
5189 // We have to reverse the jump condition because the length is the constant.
5190 Register index_reg = index_loc.AsRegister<Register>();
5191 __ cmpl(index_reg, Immediate(length));
5192 codegen_->AddSlowPath(slow_path);
5193 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005194 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005195 Register length = length_loc.AsRegister<Register>();
5196 if (index_loc.IsConstant()) {
5197 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5198 __ cmpl(length, Immediate(value));
5199 } else {
5200 __ cmpl(length, index_loc.AsRegister<Register>());
5201 }
5202 codegen_->AddSlowPath(slow_path);
5203 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005204 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205}
5206
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005207void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5208 temp->SetLocations(nullptr);
5209}
5210
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005211void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005212 // Nothing to do, this is driven by the code generator.
5213}
5214
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005215void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005216 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005217}
5218
5219void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005220 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5221}
5222
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005223void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5224 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5225}
5226
5227void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005228 HBasicBlock* block = instruction->GetBlock();
5229 if (block->GetLoopInformation() != nullptr) {
5230 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5231 // The back edge will generate the suspend check.
5232 return;
5233 }
5234 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5235 // The goto will generate the suspend check.
5236 return;
5237 }
5238 GenerateSuspendCheck(instruction, nullptr);
5239}
5240
5241void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5242 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005243 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005244 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5245 if (slow_path == nullptr) {
5246 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5247 instruction->SetSlowPath(slow_path);
5248 codegen_->AddSlowPath(slow_path);
5249 if (successor != nullptr) {
5250 DCHECK(successor->IsLoopHeader());
5251 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5252 }
5253 } else {
5254 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5255 }
5256
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005257 __ fs()->cmpw(Address::Absolute(
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005258 Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()), Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005259 if (successor == nullptr) {
5260 __ j(kNotEqual, slow_path->GetEntryLabel());
5261 __ Bind(slow_path->GetReturnLabel());
5262 } else {
5263 __ j(kEqual, codegen_->GetLabelOf(successor));
5264 __ jmp(slow_path->GetEntryLabel());
5265 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005266}
5267
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005268X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5269 return codegen_->GetAssembler();
5270}
5271
Mark Mendell7c8d0092015-01-26 11:21:33 -05005272void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005273 ScratchRegisterScope ensure_scratch(
5274 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5275 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5276 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5277 __ movl(temp_reg, Address(ESP, src + stack_offset));
5278 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005279}
5280
5281void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005282 ScratchRegisterScope ensure_scratch(
5283 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5284 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5285 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5286 __ movl(temp_reg, Address(ESP, src + stack_offset));
5287 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5288 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5289 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005290}
5291
5292void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005293 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005294 Location source = move->GetSource();
5295 Location destination = move->GetDestination();
5296
5297 if (source.IsRegister()) {
5298 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005299 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005300 } else {
5301 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005302 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005303 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005304 } else if (source.IsFpuRegister()) {
5305 if (destination.IsFpuRegister()) {
5306 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5307 } else if (destination.IsStackSlot()) {
5308 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5309 } else {
5310 DCHECK(destination.IsDoubleStackSlot());
5311 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5312 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005313 } else if (source.IsStackSlot()) {
5314 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005315 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005316 } else if (destination.IsFpuRegister()) {
5317 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005318 } else {
5319 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005320 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5321 }
5322 } else if (source.IsDoubleStackSlot()) {
5323 if (destination.IsFpuRegister()) {
5324 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5325 } else {
5326 DCHECK(destination.IsDoubleStackSlot()) << destination;
5327 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005328 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005329 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005330 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005331 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005332 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005333 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005334 if (value == 0) {
5335 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5336 } else {
5337 __ movl(destination.AsRegister<Register>(), Immediate(value));
5338 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005339 } else {
5340 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005341 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005342 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005343 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005344 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005345 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005346 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005347 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005348 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5349 if (value == 0) {
5350 // Easy handling of 0.0.
5351 __ xorps(dest, dest);
5352 } else {
5353 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005354 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5355 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5356 __ movl(temp, Immediate(value));
5357 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005358 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005359 } else {
5360 DCHECK(destination.IsStackSlot()) << destination;
5361 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5362 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005363 } else if (constant->IsLongConstant()) {
5364 int64_t value = constant->AsLongConstant()->GetValue();
5365 int32_t low_value = Low32Bits(value);
5366 int32_t high_value = High32Bits(value);
5367 Immediate low(low_value);
5368 Immediate high(high_value);
5369 if (destination.IsDoubleStackSlot()) {
5370 __ movl(Address(ESP, destination.GetStackIndex()), low);
5371 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5372 } else {
5373 __ movl(destination.AsRegisterPairLow<Register>(), low);
5374 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5375 }
5376 } else {
5377 DCHECK(constant->IsDoubleConstant());
5378 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005379 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005380 int32_t low_value = Low32Bits(value);
5381 int32_t high_value = High32Bits(value);
5382 Immediate low(low_value);
5383 Immediate high(high_value);
5384 if (destination.IsFpuRegister()) {
5385 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5386 if (value == 0) {
5387 // Easy handling of 0.0.
5388 __ xorpd(dest, dest);
5389 } else {
5390 __ pushl(high);
5391 __ pushl(low);
5392 __ movsd(dest, Address(ESP, 0));
5393 __ addl(ESP, Immediate(8));
5394 }
5395 } else {
5396 DCHECK(destination.IsDoubleStackSlot()) << destination;
5397 __ movl(Address(ESP, destination.GetStackIndex()), low);
5398 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5399 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005400 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005401 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005402 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005403 }
5404}
5405
Mark Mendella5c19ce2015-04-01 12:51:05 -04005406void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005407 Register suggested_scratch = reg == EAX ? EBX : EAX;
5408 ScratchRegisterScope ensure_scratch(
5409 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5410
5411 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5412 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5413 __ movl(Address(ESP, mem + stack_offset), reg);
5414 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005415}
5416
Mark Mendell7c8d0092015-01-26 11:21:33 -05005417void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005418 ScratchRegisterScope ensure_scratch(
5419 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5420
5421 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5422 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5423 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5424 __ movss(Address(ESP, mem + stack_offset), reg);
5425 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005426}
5427
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005428void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005429 ScratchRegisterScope ensure_scratch1(
5430 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005431
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005432 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5433 ScratchRegisterScope ensure_scratch2(
5434 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005435
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005436 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5437 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5438 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5439 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5440 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5441 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005442}
5443
5444void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005445 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005446 Location source = move->GetSource();
5447 Location destination = move->GetDestination();
5448
5449 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005450 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5451 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5452 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5453 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5454 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005455 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005456 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005457 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005458 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005459 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5460 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005461 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5462 // Use XOR Swap algorithm to avoid a temporary.
5463 DCHECK_NE(source.reg(), destination.reg());
5464 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5465 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5466 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5467 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5468 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5469 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5470 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005471 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5472 // Take advantage of the 16 bytes in the XMM register.
5473 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5474 Address stack(ESP, destination.GetStackIndex());
5475 // Load the double into the high doubleword.
5476 __ movhpd(reg, stack);
5477
5478 // Store the low double into the destination.
5479 __ movsd(stack, reg);
5480
5481 // Move the high double to the low double.
5482 __ psrldq(reg, Immediate(8));
5483 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5484 // Take advantage of the 16 bytes in the XMM register.
5485 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5486 Address stack(ESP, source.GetStackIndex());
5487 // Load the double into the high doubleword.
5488 __ movhpd(reg, stack);
5489
5490 // Store the low double into the destination.
5491 __ movsd(stack, reg);
5492
5493 // Move the high double to the low double.
5494 __ psrldq(reg, Immediate(8));
5495 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5496 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5497 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005498 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005499 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005500 }
5501}
5502
5503void ParallelMoveResolverX86::SpillScratch(int reg) {
5504 __ pushl(static_cast<Register>(reg));
5505}
5506
5507void ParallelMoveResolverX86::RestoreScratch(int reg) {
5508 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005509}
5510
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005511void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005512 InvokeRuntimeCallingConvention calling_convention;
5513 CodeGenerator::CreateLoadClassLocationSummary(
5514 cls,
5515 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005516 Location::RegisterLocation(EAX),
5517 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005518}
5519
5520void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005521 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005522 if (cls->NeedsAccessCheck()) {
5523 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5524 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5525 cls,
5526 cls->GetDexPc(),
5527 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005528 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005529 return;
5530 }
5531
Roland Levillain0d5a2812015-11-13 10:07:31 +00005532 Location out_loc = locations->Out();
5533 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005534 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005535
Calin Juravle580b6092015-10-06 17:35:58 +01005536 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005537 DCHECK(!cls->CanCallRuntime());
5538 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005539 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5540 if (kEmitCompilerReadBarrier) {
5541 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5542 __ leal(out, Address(current_method, declaring_class_offset));
5543 // /* mirror::Class* */ out = out->Read()
5544 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5545 } else {
5546 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5547 __ movl(out, Address(current_method, declaring_class_offset));
5548 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005549 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005550 // /* GcRoot<mirror::Class>[] */ out =
5551 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5552 __ movl(out, Address(current_method,
5553 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
5554
5555 size_t cache_offset = CodeGenerator::GetCacheOffset(cls->GetTypeIndex());
5556 if (kEmitCompilerReadBarrier) {
5557 // /* GcRoot<mirror::Class>* */ out = &out[type_index]
5558 __ leal(out, Address(out, cache_offset));
5559 // /* mirror::Class* */ out = out->Read()
5560 codegen_->GenerateReadBarrierForRoot(cls, out_loc, out_loc);
5561 } else {
5562 // /* GcRoot<mirror::Class> */ out = out[type_index]
5563 __ movl(out, Address(out, cache_offset));
5564 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005565
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005566 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5567 DCHECK(cls->CanCallRuntime());
5568 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5569 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5570 codegen_->AddSlowPath(slow_path);
5571
5572 if (!cls->IsInDexCache()) {
5573 __ testl(out, out);
5574 __ j(kEqual, slow_path->GetEntryLabel());
5575 }
5576
5577 if (cls->MustGenerateClinitCheck()) {
5578 GenerateClassInitializationCheck(slow_path, out);
5579 } else {
5580 __ Bind(slow_path->GetExitLabel());
5581 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005582 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005583 }
5584}
5585
5586void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5587 LocationSummary* locations =
5588 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5589 locations->SetInAt(0, Location::RequiresRegister());
5590 if (check->HasUses()) {
5591 locations->SetOut(Location::SameAsFirstInput());
5592 }
5593}
5594
5595void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005596 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005597 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005598 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005599 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005600 GenerateClassInitializationCheck(slow_path,
5601 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005602}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005603
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005604void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005605 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005606 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5607 Immediate(mirror::Class::kStatusInitialized));
5608 __ j(kLess, slow_path->GetEntryLabel());
5609 __ Bind(slow_path->GetExitLabel());
5610 // No need for memory fence, thanks to the X86 memory model.
5611}
5612
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005613void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005614 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5615 ? LocationSummary::kCallOnSlowPath
5616 : LocationSummary::kNoCall;
5617 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005618 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005619 locations->SetOut(Location::RequiresRegister());
5620}
5621
5622void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005623 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005624 Location out_loc = locations->Out();
5625 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005626 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005627
5628 uint32_t declaring_class_offset = ArtMethod::DeclaringClassOffset().Int32Value();
5629 if (kEmitCompilerReadBarrier) {
5630 // /* GcRoot<mirror::Class>* */ out = &(current_method->declaring_class_)
5631 __ leal(out, Address(current_method, declaring_class_offset));
5632 // /* mirror::Class* */ out = out->Read()
5633 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5634 } else {
5635 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5636 __ movl(out, Address(current_method, declaring_class_offset));
5637 }
5638
5639 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005640 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005641
5642 size_t cache_offset = CodeGenerator::GetCacheOffset(load->GetStringIndex());
5643 if (kEmitCompilerReadBarrier) {
5644 // /* GcRoot<mirror::String>* */ out = &out[string_index]
5645 __ leal(out, Address(out, cache_offset));
5646 // /* mirror::String* */ out = out->Read()
5647 codegen_->GenerateReadBarrierForRoot(load, out_loc, out_loc);
5648 } else {
5649 // /* GcRoot<mirror::String> */ out = out[string_index]
5650 __ movl(out, Address(out, cache_offset));
5651 }
5652
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005653 if (!load->IsInDexCache()) {
5654 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5655 codegen_->AddSlowPath(slow_path);
5656 __ testl(out, out);
5657 __ j(kEqual, slow_path->GetEntryLabel());
5658 __ Bind(slow_path->GetExitLabel());
5659 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005660}
5661
David Brazdilcb1c0552015-08-04 16:22:25 +01005662static Address GetExceptionTlsAddress() {
5663 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5664}
5665
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005666void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5667 LocationSummary* locations =
5668 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5669 locations->SetOut(Location::RequiresRegister());
5670}
5671
5672void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005673 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5674}
5675
5676void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5677 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5678}
5679
5680void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5681 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005682}
5683
5684void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5685 LocationSummary* locations =
5686 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5687 InvokeRuntimeCallingConvention calling_convention;
5688 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5689}
5690
5691void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005692 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5693 instruction,
5694 instruction->GetDexPc(),
5695 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005696 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005697}
5698
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005699void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005700 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005701 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5702 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005703 case TypeCheckKind::kExactCheck:
5704 case TypeCheckKind::kAbstractClassCheck:
5705 case TypeCheckKind::kClassHierarchyCheck:
5706 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005707 call_kind =
5708 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005709 break;
5710 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005711 case TypeCheckKind::kUnresolvedCheck:
5712 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005713 call_kind = LocationSummary::kCallOnSlowPath;
5714 break;
5715 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005716
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005717 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005718 locations->SetInAt(0, Location::RequiresRegister());
5719 locations->SetInAt(1, Location::Any());
5720 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5721 locations->SetOut(Location::RequiresRegister());
5722 // When read barriers are enabled, we need a temporary register for
5723 // some cases.
5724 if (kEmitCompilerReadBarrier &&
5725 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5726 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5727 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
5728 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005729 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005730}
5731
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005732void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005733 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005734 Location obj_loc = locations->InAt(0);
5735 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005736 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005737 Location out_loc = locations->Out();
5738 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005739 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005740 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5741 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5742 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005743 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005744 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005745
5746 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005747 // Avoid null check if we know obj is not null.
5748 if (instruction->MustDoNullCheck()) {
5749 __ testl(obj, obj);
5750 __ j(kEqual, &zero);
5751 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005752
Roland Levillain0d5a2812015-11-13 10:07:31 +00005753 // /* HeapReference<Class> */ out = obj->klass_
5754 __ movl(out, Address(obj, class_offset));
5755 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005756
5757 switch (instruction->GetTypeCheckKind()) {
5758 case TypeCheckKind::kExactCheck: {
5759 if (cls.IsRegister()) {
5760 __ cmpl(out, cls.AsRegister<Register>());
5761 } else {
5762 DCHECK(cls.IsStackSlot()) << cls;
5763 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5764 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005765
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005766 // Classes must be equal for the instanceof to succeed.
5767 __ j(kNotEqual, &zero);
5768 __ movl(out, Immediate(1));
5769 __ jmp(&done);
5770 break;
5771 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005772
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005773 case TypeCheckKind::kAbstractClassCheck: {
5774 // If the class is abstract, we eagerly fetch the super class of the
5775 // object to avoid doing a comparison we know will fail.
5776 NearLabel loop;
5777 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005778 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5779 if (kEmitCompilerReadBarrier) {
5780 // Save the value of `out` into `temp` before overwriting it
5781 // in the following move operation, as we will need it for the
5782 // read barrier below.
5783 Register temp = temp_loc.AsRegister<Register>();
5784 __ movl(temp, out);
5785 }
5786 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005787 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005788 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005789 __ testl(out, out);
5790 // If `out` is null, we use it for the result, and jump to `done`.
5791 __ j(kEqual, &done);
5792 if (cls.IsRegister()) {
5793 __ cmpl(out, cls.AsRegister<Register>());
5794 } else {
5795 DCHECK(cls.IsStackSlot()) << cls;
5796 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5797 }
5798 __ j(kNotEqual, &loop);
5799 __ movl(out, Immediate(1));
5800 if (zero.IsLinked()) {
5801 __ jmp(&done);
5802 }
5803 break;
5804 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005805
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005806 case TypeCheckKind::kClassHierarchyCheck: {
5807 // Walk over the class hierarchy to find a match.
5808 NearLabel loop, success;
5809 __ Bind(&loop);
5810 if (cls.IsRegister()) {
5811 __ cmpl(out, cls.AsRegister<Register>());
5812 } else {
5813 DCHECK(cls.IsStackSlot()) << cls;
5814 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5815 }
5816 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005817 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5818 if (kEmitCompilerReadBarrier) {
5819 // Save the value of `out` into `temp` before overwriting it
5820 // in the following move operation, as we will need it for the
5821 // read barrier below.
5822 Register temp = temp_loc.AsRegister<Register>();
5823 __ movl(temp, out);
5824 }
5825 // /* HeapReference<Class> */ out = out->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005826 __ movl(out, Address(out, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005827 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, super_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005828 __ testl(out, out);
5829 __ j(kNotEqual, &loop);
5830 // If `out` is null, we use it for the result, and jump to `done`.
5831 __ jmp(&done);
5832 __ Bind(&success);
5833 __ movl(out, Immediate(1));
5834 if (zero.IsLinked()) {
5835 __ jmp(&done);
5836 }
5837 break;
5838 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005839
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005840 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005841 // Do an exact check.
5842 NearLabel exact_check;
5843 if (cls.IsRegister()) {
5844 __ cmpl(out, cls.AsRegister<Register>());
5845 } else {
5846 DCHECK(cls.IsStackSlot()) << cls;
5847 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5848 }
5849 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005850 // Otherwise, we need to check that the object's class is a non-primitive array.
5851 Location temp_loc = kEmitCompilerReadBarrier ? locations->GetTemp(0) : Location::NoLocation();
5852 if (kEmitCompilerReadBarrier) {
5853 // Save the value of `out` into `temp` before overwriting it
5854 // in the following move operation, as we will need it for the
5855 // read barrier below.
5856 Register temp = temp_loc.AsRegister<Register>();
5857 __ movl(temp, out);
5858 }
5859 // /* HeapReference<Class> */ out = out->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005860 __ movl(out, Address(out, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005861 codegen_->MaybeGenerateReadBarrier(instruction, out_loc, out_loc, temp_loc, component_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005862 __ testl(out, out);
5863 // If `out` is null, we use it for the result, and jump to `done`.
5864 __ j(kEqual, &done);
5865 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
5866 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005867 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005868 __ movl(out, Immediate(1));
5869 __ jmp(&done);
5870 break;
5871 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005873 case TypeCheckKind::kArrayCheck: {
5874 if (cls.IsRegister()) {
5875 __ cmpl(out, cls.AsRegister<Register>());
5876 } else {
5877 DCHECK(cls.IsStackSlot()) << cls;
5878 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5879 }
5880 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5882 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005883 codegen_->AddSlowPath(slow_path);
5884 __ j(kNotEqual, slow_path->GetEntryLabel());
5885 __ movl(out, Immediate(1));
5886 if (zero.IsLinked()) {
5887 __ jmp(&done);
5888 }
5889 break;
5890 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891
Calin Juravle98893e12015-10-02 21:05:03 +01005892 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893 case TypeCheckKind::kInterfaceCheck: {
5894 // Note that we indeed only call on slow path, but we always go
5895 // into the slow path for the unresolved & interface check
5896 // cases.
5897 //
5898 // We cannot directly call the InstanceofNonTrivial runtime
5899 // entry point without resorting to a type checking slow path
5900 // here (i.e. by calling InvokeRuntime directly), as it would
5901 // require to assign fixed registers for the inputs of this
5902 // HInstanceOf instruction (following the runtime calling
5903 // convention), which might be cluttered by the potential first
5904 // read barrier emission at the beginning of this method.
5905 DCHECK(locations->OnlyCallsOnSlowPath());
5906 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5907 /* is_fatal */ false);
5908 codegen_->AddSlowPath(slow_path);
5909 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 if (zero.IsLinked()) {
5911 __ jmp(&done);
5912 }
5913 break;
5914 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005915 }
5916
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005918 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005919 __ xorl(out, out);
5920 }
5921
5922 if (done.IsLinked()) {
5923 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005924 }
5925
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005926 if (slow_path != nullptr) {
5927 __ Bind(slow_path->GetExitLabel());
5928 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005929}
5930
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005931void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005932 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
5933 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005934 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5935 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005936 case TypeCheckKind::kExactCheck:
5937 case TypeCheckKind::kAbstractClassCheck:
5938 case TypeCheckKind::kClassHierarchyCheck:
5939 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005940 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
5941 LocationSummary::kCallOnSlowPath :
5942 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005943 break;
5944 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005945 case TypeCheckKind::kUnresolvedCheck:
5946 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005947 call_kind = LocationSummary::kCallOnSlowPath;
5948 break;
5949 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005950 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
5951 locations->SetInAt(0, Location::RequiresRegister());
5952 locations->SetInAt(1, Location::Any());
5953 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
5954 locations->AddTemp(Location::RequiresRegister());
5955 // When read barriers are enabled, we need an additional temporary
5956 // register for some cases.
5957 if (kEmitCompilerReadBarrier &&
5958 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5959 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5960 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005961 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005962 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005963}
5964
5965void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
5966 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005967 Location obj_loc = locations->InAt(0);
5968 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005969 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970 Location temp_loc = locations->GetTemp(0);
5971 Register temp = temp_loc.AsRegister<Register>();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5973 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5974 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5975 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005976
Roland Levillain0d5a2812015-11-13 10:07:31 +00005977 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5978 bool is_type_check_slow_path_fatal =
5979 (type_check_kind == TypeCheckKind::kExactCheck ||
5980 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5981 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5982 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
5983 !instruction->CanThrowIntoCatchBlock();
5984 SlowPathCode* type_check_slow_path =
5985 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
5986 is_type_check_slow_path_fatal);
5987 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005988
Roland Levillain0d5a2812015-11-13 10:07:31 +00005989 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005990 // Avoid null check if we know obj is not null.
5991 if (instruction->MustDoNullCheck()) {
5992 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005993 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005994 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005995
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996 // /* HeapReference<Class> */ temp = obj->klass_
5997 __ movl(temp, Address(obj, class_offset));
5998 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005999
Roland Levillain0d5a2812015-11-13 10:07:31 +00006000 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006001 case TypeCheckKind::kExactCheck:
6002 case TypeCheckKind::kArrayCheck: {
6003 if (cls.IsRegister()) {
6004 __ cmpl(temp, cls.AsRegister<Register>());
6005 } else {
6006 DCHECK(cls.IsStackSlot()) << cls;
6007 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6008 }
6009 // Jump to slow path for throwing the exception or doing a
6010 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006011 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006012 break;
6013 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006014
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006015 case TypeCheckKind::kAbstractClassCheck: {
6016 // If the class is abstract, we eagerly fetch the super class of the
6017 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006019 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006020 Location temp2_loc =
6021 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6022 if (kEmitCompilerReadBarrier) {
6023 // Save the value of `temp` into `temp2` before overwriting it
6024 // in the following move operation, as we will need it for the
6025 // read barrier below.
6026 Register temp2 = temp2_loc.AsRegister<Register>();
6027 __ movl(temp2, temp);
6028 }
6029 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006030 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6032
6033 // If the class reference currently in `temp` is not null, jump
6034 // to the `compare_classes` label to compare it with the checked
6035 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037 __ j(kNotEqual, &compare_classes);
6038 // Otherwise, jump to the slow path to throw the exception.
6039 //
6040 // But before, move back the object's class into `temp` before
6041 // going into the slow path, as it has been overwritten in the
6042 // meantime.
6043 // /* HeapReference<Class> */ temp = obj->klass_
6044 __ movl(temp, Address(obj, class_offset));
6045 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6046 __ jmp(type_check_slow_path->GetEntryLabel());
6047
6048 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006049 if (cls.IsRegister()) {
6050 __ cmpl(temp, cls.AsRegister<Register>());
6051 } else {
6052 DCHECK(cls.IsStackSlot()) << cls;
6053 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6054 }
6055 __ j(kNotEqual, &loop);
6056 break;
6057 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006058
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006059 case TypeCheckKind::kClassHierarchyCheck: {
6060 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006061 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 __ Bind(&loop);
6063 if (cls.IsRegister()) {
6064 __ cmpl(temp, cls.AsRegister<Register>());
6065 } else {
6066 DCHECK(cls.IsStackSlot()) << cls;
6067 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6068 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006069 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006070
6071 Location temp2_loc =
6072 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6073 if (kEmitCompilerReadBarrier) {
6074 // Save the value of `temp` into `temp2` before overwriting it
6075 // in the following move operation, as we will need it for the
6076 // read barrier below.
6077 Register temp2 = temp2_loc.AsRegister<Register>();
6078 __ movl(temp2, temp);
6079 }
6080 // /* HeapReference<Class> */ temp = temp->super_class_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 __ movl(temp, Address(temp, super_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006082 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, temp2_loc, super_offset);
6083
6084 // If the class reference currently in `temp` is not null, jump
6085 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006086 __ testl(temp, temp);
6087 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006088 // Otherwise, jump to the slow path to throw the exception.
6089 //
6090 // But before, move back the object's class into `temp` before
6091 // going into the slow path, as it has been overwritten in the
6092 // meantime.
6093 // /* HeapReference<Class> */ temp = obj->klass_
6094 __ movl(temp, Address(obj, class_offset));
6095 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6096 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006097 break;
6098 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006099
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006101 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006102 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006103 if (cls.IsRegister()) {
6104 __ cmpl(temp, cls.AsRegister<Register>());
6105 } else {
6106 DCHECK(cls.IsStackSlot()) << cls;
6107 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6108 }
6109 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006110
6111 // Otherwise, we need to check that the object's class is a non-primitive array.
6112 Location temp2_loc =
6113 kEmitCompilerReadBarrier ? locations->GetTemp(1) : Location::NoLocation();
6114 if (kEmitCompilerReadBarrier) {
6115 // Save the value of `temp` into `temp2` before overwriting it
6116 // in the following move operation, as we will need it for the
6117 // read barrier below.
6118 Register temp2 = temp2_loc.AsRegister<Register>();
6119 __ movl(temp2, temp);
6120 }
6121 // /* HeapReference<Class> */ temp = temp->component_type_
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 __ movl(temp, Address(temp, component_offset));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123 codegen_->MaybeGenerateReadBarrier(
6124 instruction, temp_loc, temp_loc, temp2_loc, component_offset);
6125
6126 // If the component type is not null (i.e. the object is indeed
6127 // an array), jump to label `check_non_primitive_component_type`
6128 // to further check that this component type is not a primitive
6129 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006131 __ j(kNotEqual, &check_non_primitive_component_type);
6132 // Otherwise, jump to the slow path to throw the exception.
6133 //
6134 // But before, move back the object's class into `temp` before
6135 // going into the slow path, as it has been overwritten in the
6136 // meantime.
6137 // /* HeapReference<Class> */ temp = obj->klass_
6138 __ movl(temp, Address(obj, class_offset));
6139 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6140 __ jmp(type_check_slow_path->GetEntryLabel());
6141
6142 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006143 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144 __ j(kEqual, &done);
6145 // Same comment as above regarding `temp` and the slow path.
6146 // /* HeapReference<Class> */ temp = obj->klass_
6147 __ movl(temp, Address(obj, class_offset));
6148 codegen_->MaybeGenerateReadBarrier(instruction, temp_loc, temp_loc, obj_loc, class_offset);
6149 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006150 break;
6151 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006152
Calin Juravle98893e12015-10-02 21:05:03 +01006153 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006154 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 // We always go into the type check slow path for the unresolved &
6156 // interface check cases.
6157 //
6158 // We cannot directly call the CheckCast runtime entry point
6159 // without resorting to a type checking slow path here (i.e. by
6160 // calling InvokeRuntime directly), as it would require to
6161 // assign fixed registers for the inputs of this HInstanceOf
6162 // instruction (following the runtime calling convention), which
6163 // might be cluttered by the potential first read barrier
6164 // emission at the beginning of this method.
6165 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166 break;
6167 }
6168 __ Bind(&done);
6169
Roland Levillain0d5a2812015-11-13 10:07:31 +00006170 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006171}
6172
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006173void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6174 LocationSummary* locations =
6175 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6176 InvokeRuntimeCallingConvention calling_convention;
6177 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6178}
6179
6180void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006181 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6182 : QUICK_ENTRY_POINT(pUnlockObject),
6183 instruction,
6184 instruction->GetDexPc(),
6185 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006186 if (instruction->IsEnter()) {
6187 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6188 } else {
6189 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6190 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006191}
6192
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006193void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6194void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6195void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6196
6197void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6198 LocationSummary* locations =
6199 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6200 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6201 || instruction->GetResultType() == Primitive::kPrimLong);
6202 locations->SetInAt(0, Location::RequiresRegister());
6203 locations->SetInAt(1, Location::Any());
6204 locations->SetOut(Location::SameAsFirstInput());
6205}
6206
6207void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6208 HandleBitwiseOperation(instruction);
6209}
6210
6211void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6212 HandleBitwiseOperation(instruction);
6213}
6214
6215void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6216 HandleBitwiseOperation(instruction);
6217}
6218
6219void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6220 LocationSummary* locations = instruction->GetLocations();
6221 Location first = locations->InAt(0);
6222 Location second = locations->InAt(1);
6223 DCHECK(first.Equals(locations->Out()));
6224
6225 if (instruction->GetResultType() == Primitive::kPrimInt) {
6226 if (second.IsRegister()) {
6227 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006228 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006229 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006230 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006231 } else {
6232 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006233 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006234 }
6235 } else if (second.IsConstant()) {
6236 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006237 __ andl(first.AsRegister<Register>(),
6238 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006239 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006240 __ orl(first.AsRegister<Register>(),
6241 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006242 } else {
6243 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006244 __ xorl(first.AsRegister<Register>(),
6245 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006246 }
6247 } else {
6248 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006249 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006250 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006251 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006252 } else {
6253 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006254 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006255 }
6256 }
6257 } else {
6258 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6259 if (second.IsRegisterPair()) {
6260 if (instruction->IsAnd()) {
6261 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6262 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6263 } else if (instruction->IsOr()) {
6264 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6265 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6266 } else {
6267 DCHECK(instruction->IsXor());
6268 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6269 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6270 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006271 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006272 if (instruction->IsAnd()) {
6273 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6274 __ andl(first.AsRegisterPairHigh<Register>(),
6275 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6276 } else if (instruction->IsOr()) {
6277 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6278 __ orl(first.AsRegisterPairHigh<Register>(),
6279 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6280 } else {
6281 DCHECK(instruction->IsXor());
6282 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6283 __ xorl(first.AsRegisterPairHigh<Register>(),
6284 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6285 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006286 } else {
6287 DCHECK(second.IsConstant()) << second;
6288 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006289 int32_t low_value = Low32Bits(value);
6290 int32_t high_value = High32Bits(value);
6291 Immediate low(low_value);
6292 Immediate high(high_value);
6293 Register first_low = first.AsRegisterPairLow<Register>();
6294 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006295 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006296 if (low_value == 0) {
6297 __ xorl(first_low, first_low);
6298 } else if (low_value != -1) {
6299 __ andl(first_low, low);
6300 }
6301 if (high_value == 0) {
6302 __ xorl(first_high, first_high);
6303 } else if (high_value != -1) {
6304 __ andl(first_high, high);
6305 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006306 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006307 if (low_value != 0) {
6308 __ orl(first_low, low);
6309 }
6310 if (high_value != 0) {
6311 __ orl(first_high, high);
6312 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006313 } else {
6314 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006315 if (low_value != 0) {
6316 __ xorl(first_low, low);
6317 }
6318 if (high_value != 0) {
6319 __ xorl(first_high, high);
6320 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006321 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006322 }
6323 }
6324}
6325
Roland Levillain0d5a2812015-11-13 10:07:31 +00006326void CodeGeneratorX86::GenerateReadBarrier(HInstruction* instruction,
6327 Location out,
6328 Location ref,
6329 Location obj,
6330 uint32_t offset,
6331 Location index) {
6332 DCHECK(kEmitCompilerReadBarrier);
6333
6334 // If heap poisoning is enabled, the unpoisoning of the loaded
6335 // reference will be carried out by the runtime within the slow
6336 // path.
6337 //
6338 // Note that `ref` currently does not get unpoisoned (when heap
6339 // poisoning is enabled), which is alright as the `ref` argument is
6340 // not used by the artReadBarrierSlow entry point.
6341 //
6342 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6343 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6344 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6345 AddSlowPath(slow_path);
6346
6347 // TODO: When read barrier has a fast path, add it here.
6348 /* Currently the read barrier call is inserted after the original load.
6349 * However, if we have a fast path, we need to perform the load of obj.LockWord *before* the
6350 * original load. This load-load ordering is required by the read barrier.
6351 * The fast path/slow path (for Baker's algorithm) should look like:
6352 *
6353 * bool isGray = obj.LockWord & kReadBarrierMask;
6354 * lfence; // load fence or artificial data dependence to prevent load-load reordering
6355 * ref = obj.field; // this is the original load
6356 * if (isGray) {
6357 * ref = Mark(ref); // ideally the slow path just does Mark(ref)
6358 * }
6359 */
6360
6361 __ jmp(slow_path->GetEntryLabel());
6362 __ Bind(slow_path->GetExitLabel());
6363}
6364
6365void CodeGeneratorX86::MaybeGenerateReadBarrier(HInstruction* instruction,
6366 Location out,
6367 Location ref,
6368 Location obj,
6369 uint32_t offset,
6370 Location index) {
6371 if (kEmitCompilerReadBarrier) {
6372 // If heap poisoning is enabled, unpoisoning will be taken care of
6373 // by the runtime within the slow path.
6374 GenerateReadBarrier(instruction, out, ref, obj, offset, index);
6375 } else if (kPoisonHeapReferences) {
6376 __ UnpoisonHeapReference(out.AsRegister<Register>());
6377 }
6378}
6379
6380void CodeGeneratorX86::GenerateReadBarrierForRoot(HInstruction* instruction,
6381 Location out,
6382 Location root) {
6383 DCHECK(kEmitCompilerReadBarrier);
6384
6385 // Note that GC roots are not affected by heap poisoning, so we do
6386 // not need to do anything special for this here.
6387 SlowPathCode* slow_path =
6388 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6389 AddSlowPath(slow_path);
6390
6391 // TODO: Implement a fast path for ReadBarrierForRoot, performing
6392 // the following operation (for Baker's algorithm):
6393 //
6394 // if (thread.tls32_.is_gc_marking) {
6395 // root = Mark(root);
6396 // }
6397
6398 __ jmp(slow_path->GetEntryLabel());
6399 __ Bind(slow_path->GetExitLabel());
6400}
6401
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006402void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006403 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006404 LOG(FATAL) << "Unreachable";
6405}
6406
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006407void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006408 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006409 LOG(FATAL) << "Unreachable";
6410}
6411
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006412void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6413 DCHECK(codegen_->IsBaseline());
6414 LocationSummary* locations =
6415 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6416 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6417}
6418
6419void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6420 DCHECK(codegen_->IsBaseline());
6421 // Will be generated at use site.
6422}
6423
Mark Mendellfe57faa2015-09-18 09:26:15 -04006424// Simple implementation of packed switch - generate cascaded compare/jumps.
6425void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6426 LocationSummary* locations =
6427 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6428 locations->SetInAt(0, Location::RequiresRegister());
6429}
6430
Zheng Xu59f054d2015-12-07 17:17:03 +08006431void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6432 int32_t lower_bound,
6433 uint32_t num_entries,
6434 HBasicBlock* switch_block,
6435 HBasicBlock* default_block) {
6436 // Figure out the correct compare values and jump conditions.
6437 // Handle the first compare/branch as a special case because it might
6438 // jump to the default case.
6439 DCHECK_GT(num_entries, 2u);
6440 Condition first_condition;
6441 uint32_t index;
6442 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6443 if (lower_bound != 0) {
6444 first_condition = kLess;
6445 __ cmpl(value_reg, Immediate(lower_bound));
6446 __ j(first_condition, codegen_->GetLabelOf(default_block));
6447 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006448
Zheng Xu59f054d2015-12-07 17:17:03 +08006449 index = 1;
6450 } else {
6451 // Handle all the compare/jumps below.
6452 first_condition = kBelow;
6453 index = 0;
6454 }
6455
6456 // Handle the rest of the compare/jumps.
6457 for (; index + 1 < num_entries; index += 2) {
6458 int32_t compare_to_value = lower_bound + index + 1;
6459 __ cmpl(value_reg, Immediate(compare_to_value));
6460 // Jump to successors[index] if value < case_value[index].
6461 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6462 // Jump to successors[index + 1] if value == case_value[index + 1].
6463 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6464 }
6465
6466 if (index != num_entries) {
6467 // There are an odd number of entries. Handle the last one.
6468 DCHECK_EQ(index + 1, num_entries);
6469 __ cmpl(value_reg, Immediate(lower_bound + index));
6470 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006471 }
6472
6473 // And the default for any other value.
Zheng Xu59f054d2015-12-07 17:17:03 +08006474 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6475 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006476 }
6477}
6478
Zheng Xu59f054d2015-12-07 17:17:03 +08006479void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6480 int32_t lower_bound = switch_instr->GetStartValue();
6481 uint32_t num_entries = switch_instr->GetNumEntries();
6482 LocationSummary* locations = switch_instr->GetLocations();
6483 Register value_reg = locations->InAt(0).AsRegister<Register>();
6484
6485 GenPackedSwitchWithCompares(value_reg,
6486 lower_bound,
6487 num_entries,
6488 switch_instr->GetBlock(),
6489 switch_instr->GetDefaultBlock());
6490}
6491
Mark Mendell805b3b52015-09-18 14:10:29 -04006492void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6493 LocationSummary* locations =
6494 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6495 locations->SetInAt(0, Location::RequiresRegister());
6496
6497 // Constant area pointer.
6498 locations->SetInAt(1, Location::RequiresRegister());
6499
6500 // And the temporary we need.
6501 locations->AddTemp(Location::RequiresRegister());
6502}
6503
6504void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6505 int32_t lower_bound = switch_instr->GetStartValue();
Zheng Xu59f054d2015-12-07 17:17:03 +08006506 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006507 LocationSummary* locations = switch_instr->GetLocations();
6508 Register value_reg = locations->InAt(0).AsRegister<Register>();
6509 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6510
Zheng Xu59f054d2015-12-07 17:17:03 +08006511 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6512 GenPackedSwitchWithCompares(value_reg,
6513 lower_bound,
6514 num_entries,
6515 switch_instr->GetBlock(),
6516 default_block);
6517 return;
6518 }
6519
Mark Mendell805b3b52015-09-18 14:10:29 -04006520 // Optimizing has a jump area.
6521 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6522 Register constant_area = locations->InAt(1).AsRegister<Register>();
6523
6524 // Remove the bias, if needed.
6525 if (lower_bound != 0) {
6526 __ leal(temp_reg, Address(value_reg, -lower_bound));
6527 value_reg = temp_reg;
6528 }
6529
6530 // Is the value in range?
Zheng Xu59f054d2015-12-07 17:17:03 +08006531 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006532 __ cmpl(value_reg, Immediate(num_entries - 1));
6533 __ j(kAbove, codegen_->GetLabelOf(default_block));
6534
6535 // We are in the range of the table.
6536 // Load (target-constant_area) from the jump table, indexing by the value.
6537 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6538
6539 // Compute the actual target address by adding in constant_area.
6540 __ addl(temp_reg, constant_area);
6541
6542 // And jump.
6543 __ jmp(temp_reg);
6544}
6545
Mark Mendell0616ae02015-04-17 12:49:27 -04006546void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6547 HX86ComputeBaseMethodAddress* insn) {
6548 LocationSummary* locations =
6549 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6550 locations->SetOut(Location::RequiresRegister());
6551}
6552
6553void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6554 HX86ComputeBaseMethodAddress* insn) {
6555 LocationSummary* locations = insn->GetLocations();
6556 Register reg = locations->Out().AsRegister<Register>();
6557
6558 // Generate call to next instruction.
6559 Label next_instruction;
6560 __ call(&next_instruction);
6561 __ Bind(&next_instruction);
6562
6563 // Remember this offset for later use with constant area.
6564 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6565
6566 // Grab the return address off the stack.
6567 __ popl(reg);
6568}
6569
6570void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6571 HX86LoadFromConstantTable* insn) {
6572 LocationSummary* locations =
6573 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6574
6575 locations->SetInAt(0, Location::RequiresRegister());
6576 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6577
6578 // If we don't need to be materialized, we only need the inputs to be set.
6579 if (!insn->NeedsMaterialization()) {
6580 return;
6581 }
6582
6583 switch (insn->GetType()) {
6584 case Primitive::kPrimFloat:
6585 case Primitive::kPrimDouble:
6586 locations->SetOut(Location::RequiresFpuRegister());
6587 break;
6588
6589 case Primitive::kPrimInt:
6590 locations->SetOut(Location::RequiresRegister());
6591 break;
6592
6593 default:
6594 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6595 }
6596}
6597
6598void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6599 if (!insn->NeedsMaterialization()) {
6600 return;
6601 }
6602
6603 LocationSummary* locations = insn->GetLocations();
6604 Location out = locations->Out();
6605 Register const_area = locations->InAt(0).AsRegister<Register>();
6606 HConstant *value = insn->GetConstant();
6607
6608 switch (insn->GetType()) {
6609 case Primitive::kPrimFloat:
6610 __ movss(out.AsFpuRegister<XmmRegister>(),
6611 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6612 break;
6613
6614 case Primitive::kPrimDouble:
6615 __ movsd(out.AsFpuRegister<XmmRegister>(),
6616 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6617 break;
6618
6619 case Primitive::kPrimInt:
6620 __ movl(out.AsRegister<Register>(),
6621 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6622 break;
6623
6624 default:
6625 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6626 }
6627}
6628
Mark Mendell0616ae02015-04-17 12:49:27 -04006629/**
6630 * Class to handle late fixup of offsets into constant area.
6631 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006632class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006633 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006634 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6635 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6636
6637 protected:
6638 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6639
6640 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006641
6642 private:
6643 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6644 // Patch the correct offset for the instruction. The place to patch is the
6645 // last 4 bytes of the instruction.
6646 // The value to patch is the distance from the offset in the constant area
6647 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006648 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6649 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006650
6651 // Patch in the right value.
6652 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6653 }
6654
Mark Mendell0616ae02015-04-17 12:49:27 -04006655 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006656 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006657};
6658
Mark Mendell805b3b52015-09-18 14:10:29 -04006659/**
6660 * Class to handle late fixup of offsets to a jump table that will be created in the
6661 * constant area.
6662 */
6663class JumpTableRIPFixup : public RIPFixup {
6664 public:
6665 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6666 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6667
6668 void CreateJumpTable() {
6669 X86Assembler* assembler = codegen_->GetAssembler();
6670
6671 // Ensure that the reference to the jump table has the correct offset.
6672 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6673 SetOffset(offset_in_constant_table);
6674
6675 // The label values in the jump table are computed relative to the
6676 // instruction addressing the constant area.
6677 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6678
6679 // Populate the jump table with the correct values for the jump table.
6680 int32_t num_entries = switch_instr_->GetNumEntries();
6681 HBasicBlock* block = switch_instr_->GetBlock();
6682 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6683 // The value that we want is the target offset - the position of the table.
6684 for (int32_t i = 0; i < num_entries; i++) {
6685 HBasicBlock* b = successors[i];
6686 Label* l = codegen_->GetLabelOf(b);
6687 DCHECK(l->IsBound());
6688 int32_t offset_to_block = l->Position() - relative_offset;
6689 assembler->AppendInt32(offset_to_block);
6690 }
6691 }
6692
6693 private:
6694 const HX86PackedSwitch* switch_instr_;
6695};
6696
6697void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
6698 // Generate the constant area if needed.
6699 X86Assembler* assembler = GetAssembler();
6700 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
6701 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
6702 // byte values.
6703 assembler->Align(4, 0);
6704 constant_area_start_ = assembler->CodeSize();
6705
6706 // Populate any jump tables.
6707 for (auto jump_table : fixups_to_jump_tables_) {
6708 jump_table->CreateJumpTable();
6709 }
6710
6711 // And now add the constant area to the generated code.
6712 assembler->AddConstantArea();
6713 }
6714
6715 // And finish up.
6716 CodeGenerator::Finalize(allocator);
6717}
6718
Mark Mendell0616ae02015-04-17 12:49:27 -04006719Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
6720 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
6721 return Address(reg, kDummy32BitOffset, fixup);
6722}
6723
6724Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
6725 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
6726 return Address(reg, kDummy32BitOffset, fixup);
6727}
6728
6729Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
6730 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
6731 return Address(reg, kDummy32BitOffset, fixup);
6732}
6733
6734Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
6735 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
6736 return Address(reg, kDummy32BitOffset, fixup);
6737}
6738
Mark Mendell805b3b52015-09-18 14:10:29 -04006739Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
6740 Register reg,
6741 Register value) {
6742 // Create a fixup to be used to create and address the jump table.
6743 JumpTableRIPFixup* table_fixup =
6744 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
6745
6746 // We have to populate the jump tables.
6747 fixups_to_jump_tables_.push_back(table_fixup);
6748
6749 // We want a scaled address, as we are extracting the correct offset from the table.
6750 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
6751}
6752
Andreas Gampe85b62f22015-09-09 13:15:38 -07006753// TODO: target as memory.
6754void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
6755 if (!target.IsValid()) {
6756 DCHECK(type == Primitive::kPrimVoid);
6757 return;
6758 }
6759
6760 DCHECK_NE(type, Primitive::kPrimVoid);
6761
6762 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
6763 if (target.Equals(return_loc)) {
6764 return;
6765 }
6766
6767 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
6768 // with the else branch.
6769 if (type == Primitive::kPrimLong) {
6770 HParallelMove parallel_move(GetGraph()->GetArena());
6771 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
6772 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
6773 GetMoveResolver()->EmitNativeCode(&parallel_move);
6774 } else {
6775 // Let the parallel move resolver take care of all of this.
6776 HParallelMove parallel_move(GetGraph()->GetArena());
6777 parallel_move.AddMove(return_loc, target, type, nullptr);
6778 GetMoveResolver()->EmitNativeCode(&parallel_move);
6779 }
6780}
6781
Roland Levillain4d027112015-07-01 15:41:14 +01006782#undef __
6783
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00006784} // namespace x86
6785} // namespace art