blob: fd18917842946da8cd5aa4af92be149e627cd317 [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 Levillain7c1559a2015-12-15 10:55:36 +0000436// Slow path marking an object during a read barrier.
437class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
438 public:
439 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
440 : instruction_(instruction), out_(out), obj_(obj) {
441 DCHECK(kEmitCompilerReadBarrier);
442 }
443
444 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
445
446 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
447 LocationSummary* locations = instruction_->GetLocations();
448 Register reg_out = out_.AsRegister<Register>();
449 DCHECK(locations->CanCall());
450 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
451 DCHECK(instruction_->IsInstanceFieldGet() ||
452 instruction_->IsStaticFieldGet() ||
453 instruction_->IsArrayGet() ||
454 instruction_->IsLoadClass() ||
455 instruction_->IsLoadString() ||
456 instruction_->IsInstanceOf() ||
457 instruction_->IsCheckCast())
458 << "Unexpected instruction in read barrier marking slow path: "
459 << instruction_->DebugName();
460
461 __ Bind(GetEntryLabel());
462 SaveLiveRegisters(codegen, locations);
463
464 InvokeRuntimeCallingConvention calling_convention;
465 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
466 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
467 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
468 instruction_,
469 instruction_->GetDexPc(),
470 this);
471 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
472 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
473
474 RestoreLiveRegisters(codegen, locations);
475 __ jmp(GetExitLabel());
476 }
477
478 private:
479 HInstruction* const instruction_;
480 const Location out_;
481 const Location obj_;
482
483 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
484};
485
Roland Levillain0d5a2812015-11-13 10:07:31 +0000486// Slow path generating a read barrier for a heap reference.
487class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
488 public:
489 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
490 Location out,
491 Location ref,
492 Location obj,
493 uint32_t offset,
494 Location index)
495 : instruction_(instruction),
496 out_(out),
497 ref_(ref),
498 obj_(obj),
499 offset_(offset),
500 index_(index) {
501 DCHECK(kEmitCompilerReadBarrier);
502 // If `obj` is equal to `out` or `ref`, it means the initial object
503 // has been overwritten by (or after) the heap object reference load
504 // to be instrumented, e.g.:
505 //
506 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000507 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000508 //
509 // In that case, we have lost the information about the original
510 // object, and the emitted read barrier cannot work properly.
511 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
512 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
513 }
514
515 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
516 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
517 LocationSummary* locations = instruction_->GetLocations();
518 Register reg_out = out_.AsRegister<Register>();
519 DCHECK(locations->CanCall());
520 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
521 DCHECK(!instruction_->IsInvoke() ||
522 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000523 instruction_->GetLocations()->Intrinsified()))
524 << "Unexpected instruction in read barrier for heap reference slow path: "
525 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000526
527 __ Bind(GetEntryLabel());
528 SaveLiveRegisters(codegen, locations);
529
530 // We may have to change the index's value, but as `index_` is a
531 // constant member (like other "inputs" of this slow path),
532 // introduce a copy of it, `index`.
533 Location index = index_;
534 if (index_.IsValid()) {
535 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
536 if (instruction_->IsArrayGet()) {
537 // Compute the actual memory offset and store it in `index`.
538 Register index_reg = index_.AsRegister<Register>();
539 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
540 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
541 // We are about to change the value of `index_reg` (see the
542 // calls to art::x86::X86Assembler::shll and
543 // art::x86::X86Assembler::AddImmediate below), but it has
544 // not been saved by the previous call to
545 // art::SlowPathCode::SaveLiveRegisters, as it is a
546 // callee-save register --
547 // art::SlowPathCode::SaveLiveRegisters does not consider
548 // callee-save registers, as it has been designed with the
549 // assumption that callee-save registers are supposed to be
550 // handled by the called function. So, as a callee-save
551 // register, `index_reg` _would_ eventually be saved onto
552 // the stack, but it would be too late: we would have
553 // changed its value earlier. Therefore, we manually save
554 // it here into another freely available register,
555 // `free_reg`, chosen of course among the caller-save
556 // registers (as a callee-save `free_reg` register would
557 // exhibit the same problem).
558 //
559 // Note we could have requested a temporary register from
560 // the register allocator instead; but we prefer not to, as
561 // this is a slow path, and we know we can find a
562 // caller-save register that is available.
563 Register free_reg = FindAvailableCallerSaveRegister(codegen);
564 __ movl(free_reg, index_reg);
565 index_reg = free_reg;
566 index = Location::RegisterLocation(index_reg);
567 } else {
568 // The initial register stored in `index_` has already been
569 // saved in the call to art::SlowPathCode::SaveLiveRegisters
570 // (as it is not a callee-save register), so we can freely
571 // use it.
572 }
573 // Shifting the index value contained in `index_reg` by the scale
574 // factor (2) cannot overflow in practice, as the runtime is
575 // unable to allocate object arrays with a size larger than
576 // 2^26 - 1 (that is, 2^28 - 4 bytes).
577 __ shll(index_reg, Immediate(TIMES_4));
578 static_assert(
579 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
580 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
581 __ AddImmediate(index_reg, Immediate(offset_));
582 } else {
583 DCHECK(instruction_->IsInvoke());
584 DCHECK(instruction_->GetLocations()->Intrinsified());
585 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
586 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
587 << instruction_->AsInvoke()->GetIntrinsic();
588 DCHECK_EQ(offset_, 0U);
589 DCHECK(index_.IsRegisterPair());
590 // UnsafeGet's offset location is a register pair, the low
591 // part contains the correct offset.
592 index = index_.ToLow();
593 }
594 }
595
596 // We're moving two or three locations to locations that could
597 // overlap, so we need a parallel move resolver.
598 InvokeRuntimeCallingConvention calling_convention;
599 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
600 parallel_move.AddMove(ref_,
601 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
602 Primitive::kPrimNot,
603 nullptr);
604 parallel_move.AddMove(obj_,
605 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
606 Primitive::kPrimNot,
607 nullptr);
608 if (index.IsValid()) {
609 parallel_move.AddMove(index,
610 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
611 Primitive::kPrimInt,
612 nullptr);
613 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
614 } else {
615 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
616 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
617 }
618 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
619 instruction_,
620 instruction_->GetDexPc(),
621 this);
622 CheckEntrypointTypes<
623 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
624 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
625
626 RestoreLiveRegisters(codegen, locations);
627 __ jmp(GetExitLabel());
628 }
629
630 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
631
632 private:
633 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
634 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
635 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
636 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
637 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
638 return static_cast<Register>(i);
639 }
640 }
641 // We shall never fail to find a free caller-save register, as
642 // there are more than two core caller-save registers on x86
643 // (meaning it is possible to find one which is different from
644 // `ref` and `obj`).
645 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
646 LOG(FATAL) << "Could not find a free caller-save register";
647 UNREACHABLE();
648 }
649
650 HInstruction* const instruction_;
651 const Location out_;
652 const Location ref_;
653 const Location obj_;
654 const uint32_t offset_;
655 // An additional location containing an index to an array.
656 // Only used for HArrayGet and the UnsafeGetObject &
657 // UnsafeGetObjectVolatile intrinsics.
658 const Location index_;
659
660 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
661};
662
663// Slow path generating a read barrier for a GC root.
664class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
665 public:
666 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000667 : instruction_(instruction), out_(out), root_(root) {
668 DCHECK(kEmitCompilerReadBarrier);
669 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000670
671 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
672 LocationSummary* locations = instruction_->GetLocations();
673 Register reg_out = out_.AsRegister<Register>();
674 DCHECK(locations->CanCall());
675 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000676 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
677 << "Unexpected instruction in read barrier for GC root slow path: "
678 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000679
680 __ Bind(GetEntryLabel());
681 SaveLiveRegisters(codegen, locations);
682
683 InvokeRuntimeCallingConvention calling_convention;
684 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
685 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
686 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
687 instruction_,
688 instruction_->GetDexPc(),
689 this);
690 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
691 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
692
693 RestoreLiveRegisters(codegen, locations);
694 __ jmp(GetExitLabel());
695 }
696
697 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
698
699 private:
700 HInstruction* const instruction_;
701 const Location out_;
702 const Location root_;
703
704 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
705};
706
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100707#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100708#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100709
Aart Bike9f37602015-10-09 11:15:55 -0700710inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700711 switch (cond) {
712 case kCondEQ: return kEqual;
713 case kCondNE: return kNotEqual;
714 case kCondLT: return kLess;
715 case kCondLE: return kLessEqual;
716 case kCondGT: return kGreater;
717 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700718 case kCondB: return kBelow;
719 case kCondBE: return kBelowEqual;
720 case kCondA: return kAbove;
721 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700722 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100723 LOG(FATAL) << "Unreachable";
724 UNREACHABLE();
725}
726
Aart Bike9f37602015-10-09 11:15:55 -0700727// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100728inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
729 switch (cond) {
730 case kCondEQ: return kEqual;
731 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700732 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100733 case kCondLT: return kBelow;
734 case kCondLE: return kBelowEqual;
735 case kCondGT: return kAbove;
736 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700737 // Unsigned remain unchanged.
738 case kCondB: return kBelow;
739 case kCondBE: return kBelowEqual;
740 case kCondA: return kAbove;
741 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100742 }
743 LOG(FATAL) << "Unreachable";
744 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700745}
746
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100747void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100748 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100749}
750
751void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100752 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100753}
754
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100755size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
756 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
757 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100758}
759
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100760size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
761 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
762 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100763}
764
Mark Mendell7c8d0092015-01-26 11:21:33 -0500765size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
766 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
767 return GetFloatingPointSpillSlotSize();
768}
769
770size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
771 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
772 return GetFloatingPointSpillSlotSize();
773}
774
Calin Juravle175dc732015-08-25 15:42:32 +0100775void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
776 HInstruction* instruction,
777 uint32_t dex_pc,
778 SlowPathCode* slow_path) {
779 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
780 instruction,
781 dex_pc,
782 slow_path);
783}
784
785void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100786 HInstruction* instruction,
787 uint32_t dex_pc,
788 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100789 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100790 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100791 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100792}
793
Mark Mendellfb8d2792015-03-31 22:16:59 -0400794CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000795 const X86InstructionSetFeatures& isa_features,
796 const CompilerOptions& compiler_options,
797 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500798 : CodeGenerator(graph,
799 kNumberOfCpuRegisters,
800 kNumberOfXmmRegisters,
801 kNumberOfRegisterPairs,
802 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
803 arraysize(kCoreCalleeSaves))
804 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100805 0,
806 compiler_options,
807 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100808 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100809 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100810 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400811 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000812 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100813 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400814 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000815 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400816 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000817 // Use a fake return address register to mimic Quick.
818 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100819}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100820
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100821Location CodeGeneratorX86::AllocateFreeRegister(Primitive::Type type) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100822 switch (type) {
823 case Primitive::kPrimLong: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100824 size_t reg = FindFreeEntry(blocked_register_pairs_, kNumberOfRegisterPairs);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825 X86ManagedRegister pair =
826 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(reg));
Calin Juravle34bacdf2014-10-07 20:23:36 +0100827 DCHECK(!blocked_core_registers_[pair.AsRegisterPairLow()]);
828 DCHECK(!blocked_core_registers_[pair.AsRegisterPairHigh()]);
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100829 blocked_core_registers_[pair.AsRegisterPairLow()] = true;
830 blocked_core_registers_[pair.AsRegisterPairHigh()] = true;
Calin Juravle34bacdf2014-10-07 20:23:36 +0100831 UpdateBlockedPairRegisters();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100832 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100833 }
834
835 case Primitive::kPrimByte:
836 case Primitive::kPrimBoolean:
837 case Primitive::kPrimChar:
838 case Primitive::kPrimShort:
839 case Primitive::kPrimInt:
840 case Primitive::kPrimNot: {
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100841 Register reg = static_cast<Register>(
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100842 FindFreeEntry(blocked_core_registers_, kNumberOfCpuRegisters));
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100843 // Block all register pairs that contain `reg`.
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100844 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
845 X86ManagedRegister current =
846 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
847 if (current.AsRegisterPairLow() == reg || current.AsRegisterPairHigh() == reg) {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100848 blocked_register_pairs_[i] = true;
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100849 }
850 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100851 return Location::RegisterLocation(reg);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100852 }
853
854 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100855 case Primitive::kPrimDouble: {
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100856 return Location::FpuRegisterLocation(
857 FindFreeEntry(blocked_fpu_registers_, kNumberOfXmmRegisters));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100858 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100859
860 case Primitive::kPrimVoid:
861 LOG(FATAL) << "Unreachable type " << type;
862 }
863
Roland Levillain0d5a2812015-11-13 10:07:31 +0000864 return Location::NoLocation();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100865}
866
Mark Mendell5f874182015-03-04 15:42:45 -0500867void CodeGeneratorX86::SetupBlockedRegisters(bool is_baseline) const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100868 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100869 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100870
871 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100872 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100873
Mark Mendell5f874182015-03-04 15:42:45 -0500874 if (is_baseline) {
875 blocked_core_registers_[EBP] = true;
876 blocked_core_registers_[ESI] = true;
877 blocked_core_registers_[EDI] = true;
878 }
Calin Juravle34bacdf2014-10-07 20:23:36 +0100879
880 UpdateBlockedPairRegisters();
881}
882
883void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
884 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
885 X86ManagedRegister current =
886 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
887 if (blocked_core_registers_[current.AsRegisterPairLow()]
888 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
889 blocked_register_pairs_[i] = true;
890 }
891 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100892}
893
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100894InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
895 : HGraphVisitor(graph),
896 assembler_(codegen->GetAssembler()),
897 codegen_(codegen) {}
898
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100899static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100900 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100901}
902
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000903void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100904 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000905 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000906 bool skip_overflow_check =
907 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000908 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000909
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000910 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100911 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100912 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100913 }
914
Mark Mendell5f874182015-03-04 15:42:45 -0500915 if (HasEmptyFrame()) {
916 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000917 }
Mark Mendell5f874182015-03-04 15:42:45 -0500918
919 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
920 Register reg = kCoreCalleeSaves[i];
921 if (allocated_registers_.ContainsCoreRegister(reg)) {
922 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100923 __ cfi().AdjustCFAOffset(kX86WordSize);
924 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500925 }
926 }
927
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100928 int adjust = GetFrameSize() - FrameEntrySpillSize();
929 __ subl(ESP, Immediate(adjust));
930 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100931 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000932}
933
934void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100935 __ cfi().RememberState();
936 if (!HasEmptyFrame()) {
937 int adjust = GetFrameSize() - FrameEntrySpillSize();
938 __ addl(ESP, Immediate(adjust));
939 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500940
David Srbeckyc34dc932015-04-12 09:27:43 +0100941 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
942 Register reg = kCoreCalleeSaves[i];
943 if (allocated_registers_.ContainsCoreRegister(reg)) {
944 __ popl(reg);
945 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
946 __ cfi().Restore(DWARFReg(reg));
947 }
Mark Mendell5f874182015-03-04 15:42:45 -0500948 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000949 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100950 __ ret();
951 __ cfi().RestoreState();
952 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000953}
954
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100955void CodeGeneratorX86::Bind(HBasicBlock* block) {
956 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000957}
958
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100959Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
960 switch (load->GetType()) {
961 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100962 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100963 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100964
965 case Primitive::kPrimInt:
966 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100967 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100968 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100969
970 case Primitive::kPrimBoolean:
971 case Primitive::kPrimByte:
972 case Primitive::kPrimChar:
973 case Primitive::kPrimShort:
974 case Primitive::kPrimVoid:
975 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700976 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100977 }
978
979 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700980 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100981}
982
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100983Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
984 switch (type) {
985 case Primitive::kPrimBoolean:
986 case Primitive::kPrimByte:
987 case Primitive::kPrimChar:
988 case Primitive::kPrimShort:
989 case Primitive::kPrimInt:
990 case Primitive::kPrimNot:
991 return Location::RegisterLocation(EAX);
992
993 case Primitive::kPrimLong:
994 return Location::RegisterPairLocation(EAX, EDX);
995
996 case Primitive::kPrimVoid:
997 return Location::NoLocation();
998
999 case Primitive::kPrimDouble:
1000 case Primitive::kPrimFloat:
1001 return Location::FpuRegisterLocation(XMM0);
1002 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001003
1004 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001005}
1006
1007Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1008 return Location::RegisterLocation(kMethodRegisterArgument);
1009}
1010
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001011Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001012 switch (type) {
1013 case Primitive::kPrimBoolean:
1014 case Primitive::kPrimByte:
1015 case Primitive::kPrimChar:
1016 case Primitive::kPrimShort:
1017 case Primitive::kPrimInt:
1018 case Primitive::kPrimNot: {
1019 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001020 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001021 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001022 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001023 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001024 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001025 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001026 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001027
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001028 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001029 uint32_t index = gp_index_;
1030 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001031 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001032 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001033 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1034 calling_convention.GetRegisterPairAt(index));
1035 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001036 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001037 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1038 }
1039 }
1040
1041 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001042 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001043 stack_index_++;
1044 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1045 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1046 } else {
1047 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1048 }
1049 }
1050
1051 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001052 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001053 stack_index_ += 2;
1054 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1055 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1056 } else {
1057 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001058 }
1059 }
1060
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001061 case Primitive::kPrimVoid:
1062 LOG(FATAL) << "Unexpected parameter type " << type;
1063 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001064 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001065 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001066}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001067
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068void CodeGeneratorX86::Move32(Location destination, Location source) {
1069 if (source.Equals(destination)) {
1070 return;
1071 }
1072 if (destination.IsRegister()) {
1073 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001074 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001076 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 } else {
1078 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001079 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001080 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001081 } else if (destination.IsFpuRegister()) {
1082 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001083 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001085 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001086 } else {
1087 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001088 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001089 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001090 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001091 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001092 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001093 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001095 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001096 } else if (source.IsConstant()) {
1097 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001098 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001099 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 } else {
1101 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001102 __ pushl(Address(ESP, source.GetStackIndex()));
1103 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001104 }
1105 }
1106}
1107
1108void CodeGeneratorX86::Move64(Location destination, Location source) {
1109 if (source.Equals(destination)) {
1110 return;
1111 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001112 if (destination.IsRegisterPair()) {
1113 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001114 EmitParallelMoves(
1115 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1116 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001117 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001118 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001119 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1120 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001121 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001122 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1123 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1124 __ psrlq(src_reg, Immediate(32));
1125 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001127 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001128 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001129 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1130 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001131 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1132 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001133 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001134 if (source.IsFpuRegister()) {
1135 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1136 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001137 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001138 } else if (source.IsRegisterPair()) {
1139 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1140 // Create stack space for 2 elements.
1141 __ subl(ESP, Immediate(2 * elem_size));
1142 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1143 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1144 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1145 // And remove the temporary stack space we allocated.
1146 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001147 } else {
1148 LOG(FATAL) << "Unimplemented";
1149 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001150 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001151 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001152 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001153 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001155 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001156 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001157 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001158 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001159 } else if (source.IsConstant()) {
1160 HConstant* constant = source.GetConstant();
1161 int64_t value;
1162 if (constant->IsLongConstant()) {
1163 value = constant->AsLongConstant()->GetValue();
1164 } else {
1165 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001166 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001167 }
1168 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1169 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001170 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001171 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001172 EmitParallelMoves(
1173 Location::StackSlot(source.GetStackIndex()),
1174 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001175 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001176 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001177 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1178 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001179 }
1180 }
1181}
1182
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001183void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001184 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001185 if (instruction->IsCurrentMethod()) {
1186 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1187 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001188 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001189 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001190 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001191 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1192 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001193 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001195 } else if (location.IsStackSlot()) {
1196 __ movl(Address(ESP, location.GetStackIndex()), imm);
1197 } else {
1198 DCHECK(location.IsConstant());
1199 DCHECK_EQ(location.GetConstant(), const_to_move);
1200 }
1201 } else if (const_to_move->IsLongConstant()) {
1202 int64_t value = const_to_move->AsLongConstant()->GetValue();
1203 if (location.IsRegisterPair()) {
1204 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1205 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1206 } else if (location.IsDoubleStackSlot()) {
1207 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001208 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1209 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001210 } else {
1211 DCHECK(location.IsConstant());
1212 DCHECK_EQ(location.GetConstant(), instruction);
1213 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001214 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001215 } else if (instruction->IsTemporary()) {
1216 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001217 if (temp_location.IsStackSlot()) {
1218 Move32(location, temp_location);
1219 } else {
1220 DCHECK(temp_location.IsDoubleStackSlot());
1221 Move64(location, temp_location);
1222 }
Roland Levillain476df552014-10-09 17:51:36 +01001223 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001224 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 switch (instruction->GetType()) {
1226 case Primitive::kPrimBoolean:
1227 case Primitive::kPrimByte:
1228 case Primitive::kPrimChar:
1229 case Primitive::kPrimShort:
1230 case Primitive::kPrimInt:
1231 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001232 case Primitive::kPrimFloat:
1233 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001234 break;
1235
1236 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001237 case Primitive::kPrimDouble:
1238 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001239 break;
1240
1241 default:
1242 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1243 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001244 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001245 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 switch (instruction->GetType()) {
1247 case Primitive::kPrimBoolean:
1248 case Primitive::kPrimByte:
1249 case Primitive::kPrimChar:
1250 case Primitive::kPrimShort:
1251 case Primitive::kPrimInt:
1252 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001254 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001255 break;
1256
1257 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001258 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001259 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001260 break;
1261
1262 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001263 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001264 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001265 }
1266}
1267
Calin Juravle175dc732015-08-25 15:42:32 +01001268void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1269 DCHECK(location.IsRegister());
1270 __ movl(location.AsRegister<Register>(), Immediate(value));
1271}
1272
Calin Juravlee460d1d2015-09-29 04:52:17 +01001273void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1274 if (Primitive::Is64BitType(dst_type)) {
1275 Move64(dst, src);
1276 } else {
1277 Move32(dst, src);
1278 }
1279}
1280
1281void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1282 if (location.IsRegister()) {
1283 locations->AddTemp(location);
1284 } else if (location.IsRegisterPair()) {
1285 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1286 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1287 } else {
1288 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1289 }
1290}
1291
David Brazdilfc6a86a2015-06-26 10:33:45 +00001292void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001293 DCHECK(!successor->IsExitBlock());
1294
1295 HBasicBlock* block = got->GetBlock();
1296 HInstruction* previous = got->GetPrevious();
1297
1298 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001299 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001300 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1301 return;
1302 }
1303
1304 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1305 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1306 }
1307 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001308 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001309 }
1310}
1311
David Brazdilfc6a86a2015-06-26 10:33:45 +00001312void LocationsBuilderX86::VisitGoto(HGoto* got) {
1313 got->SetLocations(nullptr);
1314}
1315
1316void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1317 HandleGoto(got, got->GetSuccessor());
1318}
1319
1320void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1321 try_boundary->SetLocations(nullptr);
1322}
1323
1324void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1325 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1326 if (!successor->IsExitBlock()) {
1327 HandleGoto(try_boundary, successor);
1328 }
1329}
1330
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001331void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001332 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001333}
1334
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001335void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001336}
1337
Mark Mendellc4701932015-04-10 13:18:51 -04001338void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
1339 Label* true_label,
1340 Label* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001341 if (cond->IsFPConditionTrueIfNaN()) {
1342 __ j(kUnordered, true_label);
1343 } else if (cond->IsFPConditionFalseIfNaN()) {
1344 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001345 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001346 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001347}
1348
1349void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
1350 Label* true_label,
1351 Label* false_label) {
1352 LocationSummary* locations = cond->GetLocations();
1353 Location left = locations->InAt(0);
1354 Location right = locations->InAt(1);
1355 IfCondition if_cond = cond->GetCondition();
1356
Mark Mendellc4701932015-04-10 13:18:51 -04001357 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001358 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001359 IfCondition true_high_cond = if_cond;
1360 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001361 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001362
1363 // Set the conditions for the test, remembering that == needs to be
1364 // decided using the low words.
1365 switch (if_cond) {
1366 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001367 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001368 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001369 break;
1370 case kCondLT:
1371 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001372 break;
1373 case kCondLE:
1374 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001375 break;
1376 case kCondGT:
1377 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001378 break;
1379 case kCondGE:
1380 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001381 break;
Aart Bike9f37602015-10-09 11:15:55 -07001382 case kCondB:
1383 false_high_cond = kCondA;
1384 break;
1385 case kCondBE:
1386 true_high_cond = kCondB;
1387 break;
1388 case kCondA:
1389 false_high_cond = kCondB;
1390 break;
1391 case kCondAE:
1392 true_high_cond = kCondA;
1393 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001394 }
1395
1396 if (right.IsConstant()) {
1397 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001398 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001399 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001400
1401 if (val_high == 0) {
1402 __ testl(left_high, left_high);
1403 } else {
1404 __ cmpl(left_high, Immediate(val_high));
1405 }
1406 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001407 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001408 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001409 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001410 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001411 __ j(X86Condition(true_high_cond), true_label);
1412 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001413 }
1414 // Must be equal high, so compare the lows.
1415 if (val_low == 0) {
1416 __ testl(left_low, left_low);
1417 } else {
1418 __ cmpl(left_low, Immediate(val_low));
1419 }
1420 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001421 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001422 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001423
1424 __ cmpl(left_high, right_high);
1425 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001426 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001427 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001428 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001429 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001430 __ j(X86Condition(true_high_cond), true_label);
1431 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001432 }
1433 // Must be equal high, so compare the lows.
1434 __ cmpl(left_low, right_low);
1435 }
1436 // The last comparison might be unsigned.
1437 __ j(final_condition, true_label);
1438}
1439
David Brazdil0debae72015-11-12 18:37:00 +00001440void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
1441 Label* true_target_in,
1442 Label* false_target_in) {
1443 // Generated branching requires both targets to be explicit. If either of the
1444 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
1445 Label fallthrough_target;
1446 Label* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1447 Label* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
1448
Mark Mendellc4701932015-04-10 13:18:51 -04001449 LocationSummary* locations = condition->GetLocations();
1450 Location left = locations->InAt(0);
1451 Location right = locations->InAt(1);
1452
Mark Mendellc4701932015-04-10 13:18:51 -04001453 Primitive::Type type = condition->InputAt(0)->GetType();
1454 switch (type) {
1455 case Primitive::kPrimLong:
1456 GenerateLongComparesAndJumps(condition, true_target, false_target);
1457 break;
1458 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001459 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1460 GenerateFPJumps(condition, true_target, false_target);
1461 break;
1462 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001463 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1464 GenerateFPJumps(condition, true_target, false_target);
1465 break;
1466 default:
1467 LOG(FATAL) << "Unexpected compare type " << type;
1468 }
1469
David Brazdil0debae72015-11-12 18:37:00 +00001470 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001471 __ jmp(false_target);
1472 }
David Brazdil0debae72015-11-12 18:37:00 +00001473
1474 if (fallthrough_target.IsLinked()) {
1475 __ Bind(&fallthrough_target);
1476 }
Mark Mendellc4701932015-04-10 13:18:51 -04001477}
1478
David Brazdil0debae72015-11-12 18:37:00 +00001479static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1480 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1481 // are set only strictly before `branch`. We can't use the eflags on long/FP
1482 // conditions if they are materialized due to the complex branching.
1483 return cond->IsCondition() &&
1484 cond->GetNext() == branch &&
1485 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1486 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1487}
1488
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001489void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001490 size_t condition_input_index,
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001491 Label* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00001492 Label* false_target) {
1493 HInstruction* cond = instruction->InputAt(condition_input_index);
1494
1495 if (true_target == nullptr && false_target == nullptr) {
1496 // Nothing to do. The code always falls through.
1497 return;
1498 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001499 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001500 if (cond->AsIntConstant()->IsOne()) {
1501 if (true_target != nullptr) {
1502 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001503 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001504 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001505 DCHECK(cond->AsIntConstant()->IsZero());
1506 if (false_target != nullptr) {
1507 __ jmp(false_target);
1508 }
1509 }
1510 return;
1511 }
1512
1513 // The following code generates these patterns:
1514 // (1) true_target == nullptr && false_target != nullptr
1515 // - opposite condition true => branch to false_target
1516 // (2) true_target != nullptr && false_target == nullptr
1517 // - condition true => branch to true_target
1518 // (3) true_target != nullptr && false_target != nullptr
1519 // - condition true => branch to true_target
1520 // - branch to false_target
1521 if (IsBooleanValueOrMaterializedCondition(cond)) {
1522 if (AreEflagsSetFrom(cond, instruction)) {
1523 if (true_target == nullptr) {
1524 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1525 } else {
1526 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1527 }
1528 } else {
1529 // Materialized condition, compare against 0.
1530 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1531 if (lhs.IsRegister()) {
1532 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1533 } else {
1534 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1535 }
1536 if (true_target == nullptr) {
1537 __ j(kEqual, false_target);
1538 } else {
1539 __ j(kNotEqual, true_target);
1540 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001541 }
1542 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001543 // Condition has not been materialized, use its inputs as the comparison and
1544 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001545 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001546
1547 // If this is a long or FP comparison that has been folded into
1548 // the HCondition, generate the comparison directly.
1549 Primitive::Type type = condition->InputAt(0)->GetType();
1550 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1551 GenerateCompareTestAndBranch(condition, true_target, false_target);
1552 return;
1553 }
1554
1555 Location lhs = condition->GetLocations()->InAt(0);
1556 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001557 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001558 if (rhs.IsRegister()) {
1559 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1560 } else if (rhs.IsConstant()) {
1561 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1562 if (constant == 0) {
1563 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001564 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001565 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001566 }
1567 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001568 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1569 }
1570 if (true_target == nullptr) {
1571 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1572 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001573 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001574 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001575 }
David Brazdil0debae72015-11-12 18:37:00 +00001576
1577 // If neither branch falls through (case 3), the conditional branch to `true_target`
1578 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1579 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001580 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001581 }
1582}
1583
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001584void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001585 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1586 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001587 locations->SetInAt(0, Location::Any());
1588 }
1589}
1590
1591void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001592 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1593 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1594 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1595 nullptr : codegen_->GetLabelOf(true_successor);
1596 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1597 nullptr : codegen_->GetLabelOf(false_successor);
1598 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001599}
1600
1601void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1602 LocationSummary* locations = new (GetGraph()->GetArena())
1603 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001604 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001605 locations->SetInAt(0, Location::Any());
1606 }
1607}
1608
1609void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001610 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001611 DeoptimizationSlowPathX86(deoptimize);
1612 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001613 GenerateTestAndBranch(deoptimize,
1614 /* condition_input_index */ 0,
1615 slow_path->GetEntryLabel(),
1616 /* false_target */ nullptr);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001617}
1618
David Srbecky0cf44932015-12-09 14:09:59 +00001619void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1620 new (GetGraph()->GetArena()) LocationSummary(info);
1621}
1622
1623void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1624 codegen_->RecordPcInfo(info, info->GetDexPc());
1625}
1626
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001627void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001628 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001629}
1630
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001631void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1632 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001633}
1634
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001635void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001636 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001637}
1638
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001639void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001640 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001641}
1642
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001643void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001644 LocationSummary* locations =
1645 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001646 switch (store->InputAt(1)->GetType()) {
1647 case Primitive::kPrimBoolean:
1648 case Primitive::kPrimByte:
1649 case Primitive::kPrimChar:
1650 case Primitive::kPrimShort:
1651 case Primitive::kPrimInt:
1652 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001653 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001654 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1655 break;
1656
1657 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001658 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001659 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1660 break;
1661
1662 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001663 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001664 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001665}
1666
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001667void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001668}
1669
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001670void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001671 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001672 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001673 // Handle the long/FP comparisons made in instruction simplification.
1674 switch (cond->InputAt(0)->GetType()) {
1675 case Primitive::kPrimLong: {
1676 locations->SetInAt(0, Location::RequiresRegister());
1677 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1678 if (cond->NeedsMaterialization()) {
1679 locations->SetOut(Location::RequiresRegister());
1680 }
1681 break;
1682 }
1683 case Primitive::kPrimFloat:
1684 case Primitive::kPrimDouble: {
1685 locations->SetInAt(0, Location::RequiresFpuRegister());
1686 locations->SetInAt(1, Location::RequiresFpuRegister());
1687 if (cond->NeedsMaterialization()) {
1688 locations->SetOut(Location::RequiresRegister());
1689 }
1690 break;
1691 }
1692 default:
1693 locations->SetInAt(0, Location::RequiresRegister());
1694 locations->SetInAt(1, Location::Any());
1695 if (cond->NeedsMaterialization()) {
1696 // We need a byte register.
1697 locations->SetOut(Location::RegisterLocation(ECX));
1698 }
1699 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001700 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001701}
1702
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001703void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001704 if (!cond->NeedsMaterialization()) {
1705 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001706 }
Mark Mendellc4701932015-04-10 13:18:51 -04001707
1708 LocationSummary* locations = cond->GetLocations();
1709 Location lhs = locations->InAt(0);
1710 Location rhs = locations->InAt(1);
1711 Register reg = locations->Out().AsRegister<Register>();
1712 Label true_label, false_label;
1713
1714 switch (cond->InputAt(0)->GetType()) {
1715 default: {
1716 // Integer case.
1717
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001718 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001719 __ xorl(reg, reg);
1720
1721 if (rhs.IsRegister()) {
1722 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1723 } else if (rhs.IsConstant()) {
1724 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1725 if (constant == 0) {
1726 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1727 } else {
1728 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1729 }
1730 } else {
1731 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1732 }
Aart Bike9f37602015-10-09 11:15:55 -07001733 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001734 return;
1735 }
1736 case Primitive::kPrimLong:
1737 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1738 break;
1739 case Primitive::kPrimFloat:
1740 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1741 GenerateFPJumps(cond, &true_label, &false_label);
1742 break;
1743 case Primitive::kPrimDouble:
1744 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1745 GenerateFPJumps(cond, &true_label, &false_label);
1746 break;
1747 }
1748
1749 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001750 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001751
Roland Levillain4fa13f62015-07-06 18:11:54 +01001752 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001753 __ Bind(&false_label);
1754 __ xorl(reg, reg);
1755 __ jmp(&done_label);
1756
Roland Levillain4fa13f62015-07-06 18:11:54 +01001757 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001758 __ Bind(&true_label);
1759 __ movl(reg, Immediate(1));
1760 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001761}
1762
1763void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001764 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809}
1810
Aart Bike9f37602015-10-09 11:15:55 -07001811void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001813}
1814
1815void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001843void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001844 LocationSummary* locations =
1845 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001846 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001847}
1848
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001849void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001850 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851}
1852
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001853void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1854 LocationSummary* locations =
1855 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1856 locations->SetOut(Location::ConstantLocation(constant));
1857}
1858
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001859void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001860 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001861}
1862
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001863void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001864 LocationSummary* locations =
1865 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001866 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867}
1868
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001869void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 // Will be generated at use site.
1871}
1872
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001873void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1874 LocationSummary* locations =
1875 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1876 locations->SetOut(Location::ConstantLocation(constant));
1877}
1878
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001879void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001880 // Will be generated at use site.
1881}
1882
1883void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1884 LocationSummary* locations =
1885 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1886 locations->SetOut(Location::ConstantLocation(constant));
1887}
1888
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001889void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001890 // Will be generated at use site.
1891}
1892
Calin Juravle27df7582015-04-17 19:12:31 +01001893void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1894 memory_barrier->SetLocations(nullptr);
1895}
1896
1897void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001898 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001899}
1900
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001901void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001902 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001903}
1904
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001905void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001907}
1908
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001909void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001910 LocationSummary* locations =
1911 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 switch (ret->InputAt(0)->GetType()) {
1913 case Primitive::kPrimBoolean:
1914 case Primitive::kPrimByte:
1915 case Primitive::kPrimChar:
1916 case Primitive::kPrimShort:
1917 case Primitive::kPrimInt:
1918 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001919 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 break;
1921
1922 case Primitive::kPrimLong:
1923 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001924 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001925 break;
1926
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001927 case Primitive::kPrimFloat:
1928 case Primitive::kPrimDouble:
1929 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001930 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001931 break;
1932
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001933 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001934 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001935 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001936}
1937
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001938void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 if (kIsDebugBuild) {
1940 switch (ret->InputAt(0)->GetType()) {
1941 case Primitive::kPrimBoolean:
1942 case Primitive::kPrimByte:
1943 case Primitive::kPrimChar:
1944 case Primitive::kPrimShort:
1945 case Primitive::kPrimInt:
1946 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001947 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001948 break;
1949
1950 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001951 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1952 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001953 break;
1954
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001955 case Primitive::kPrimFloat:
1956 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001957 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001958 break;
1959
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001960 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001961 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001962 }
1963 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001964 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001965}
1966
Calin Juravle175dc732015-08-25 15:42:32 +01001967void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1968 // The trampoline uses the same calling convention as dex calling conventions,
1969 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1970 // the method_idx.
1971 HandleInvoke(invoke);
1972}
1973
1974void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1975 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1976}
1977
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001978void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001979 // When we do not run baseline, explicit clinit checks triggered by static
1980 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1981 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001982
Mark Mendellfb8d2792015-03-31 22:16:59 -04001983 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001984 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001985 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001986 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001987 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001988 return;
1989 }
1990
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001991 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001992
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001993 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1994 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001995 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001996 }
1997
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001998 if (codegen_->IsBaseline()) {
1999 // Baseline does not have enough registers if the current method also
2000 // needs a register. We therefore do not require a register for it, and let
2001 // the code generation of the invoke handle it.
2002 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002003 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002004 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002005 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002006 }
2007 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002008}
2009
Mark Mendell09ed1a32015-03-25 08:30:06 -04002010static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2011 if (invoke->GetLocations()->Intrinsified()) {
2012 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2013 intrinsic.Dispatch(invoke);
2014 return true;
2015 }
2016 return false;
2017}
2018
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002019void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002020 // When we do not run baseline, explicit clinit checks triggered by static
2021 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2022 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002023
Mark Mendell09ed1a32015-03-25 08:30:06 -04002024 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2025 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002026 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002027
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002028 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002029 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002030 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002031 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002032}
2033
2034void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002035 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2036 if (intrinsic.TryDispatch(invoke)) {
2037 return;
2038 }
2039
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002040 HandleInvoke(invoke);
2041}
2042
2043void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002044 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002045 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002046}
2047
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002048void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002049 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2050 return;
2051 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002052
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002053 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002054 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002055 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002056}
2057
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002058void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002059 // This call to HandleInvoke allocates a temporary (core) register
2060 // which is also used to transfer the hidden argument from FP to
2061 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002062 HandleInvoke(invoke);
2063 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002064 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002065}
2066
2067void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2068 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002069 LocationSummary* locations = invoke->GetLocations();
2070 Register temp = locations->GetTemp(0).AsRegister<Register>();
2071 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002072 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2073 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002074 Location receiver = locations->InAt(0);
2075 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2076
Roland Levillain0d5a2812015-11-13 10:07:31 +00002077 // Set the hidden argument. This is safe to do this here, as XMM7
2078 // won't be modified thereafter, before the `call` instruction.
2079 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002080 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002081 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002083 if (receiver.IsStackSlot()) {
2084 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002085 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002086 __ movl(temp, Address(temp, class_offset));
2087 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002088 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002089 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002090 }
Roland Levillain4d027112015-07-01 15:41:14 +01002091 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002092 // Instead of simply (possibly) unpoisoning `temp` here, we should
2093 // emit a read barrier for the previous class reference load.
2094 // However this is not required in practice, as this is an
2095 // intermediate/temporary reference and because the current
2096 // concurrent copying collector keeps the from-space memory
2097 // intact/accessible until the end of the marking phase (the
2098 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002099 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002100 // temp = temp->GetImtEntryAt(method_offset);
2101 __ movl(temp, Address(temp, method_offset));
2102 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002103 __ call(Address(temp,
2104 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002105
2106 DCHECK(!codegen_->IsLeafMethod());
2107 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2108}
2109
Roland Levillain88cb1752014-10-20 16:36:47 +01002110void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2111 LocationSummary* locations =
2112 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2113 switch (neg->GetResultType()) {
2114 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002115 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002116 locations->SetInAt(0, Location::RequiresRegister());
2117 locations->SetOut(Location::SameAsFirstInput());
2118 break;
2119
Roland Levillain88cb1752014-10-20 16:36:47 +01002120 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002121 locations->SetInAt(0, Location::RequiresFpuRegister());
2122 locations->SetOut(Location::SameAsFirstInput());
2123 locations->AddTemp(Location::RequiresRegister());
2124 locations->AddTemp(Location::RequiresFpuRegister());
2125 break;
2126
Roland Levillain88cb1752014-10-20 16:36:47 +01002127 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002128 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002129 locations->SetOut(Location::SameAsFirstInput());
2130 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002131 break;
2132
2133 default:
2134 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2135 }
2136}
2137
2138void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2139 LocationSummary* locations = neg->GetLocations();
2140 Location out = locations->Out();
2141 Location in = locations->InAt(0);
2142 switch (neg->GetResultType()) {
2143 case Primitive::kPrimInt:
2144 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002145 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002146 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002147 break;
2148
2149 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002150 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002151 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002152 __ negl(out.AsRegisterPairLow<Register>());
2153 // Negation is similar to subtraction from zero. The least
2154 // significant byte triggers a borrow when it is different from
2155 // zero; to take it into account, add 1 to the most significant
2156 // byte if the carry flag (CF) is set to 1 after the first NEGL
2157 // operation.
2158 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2159 __ negl(out.AsRegisterPairHigh<Register>());
2160 break;
2161
Roland Levillain5368c212014-11-27 15:03:41 +00002162 case Primitive::kPrimFloat: {
2163 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002164 Register constant = locations->GetTemp(0).AsRegister<Register>();
2165 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002166 // Implement float negation with an exclusive or with value
2167 // 0x80000000 (mask for bit 31, representing the sign of a
2168 // single-precision floating-point number).
2169 __ movl(constant, Immediate(INT32_C(0x80000000)));
2170 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002171 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002172 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002173 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002174
Roland Levillain5368c212014-11-27 15:03:41 +00002175 case Primitive::kPrimDouble: {
2176 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002177 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002178 // Implement double negation with an exclusive or with value
2179 // 0x8000000000000000 (mask for bit 63, representing the sign of
2180 // a double-precision floating-point number).
2181 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002182 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002183 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002184 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002185
2186 default:
2187 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2188 }
2189}
2190
Roland Levillaindff1f282014-11-05 14:15:05 +00002191void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002192 Primitive::Type result_type = conversion->GetResultType();
2193 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002194 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002195
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002196 // The float-to-long and double-to-long type conversions rely on a
2197 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002198 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002199 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2200 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002201 ? LocationSummary::kCall
2202 : LocationSummary::kNoCall;
2203 LocationSummary* locations =
2204 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2205
David Brazdilb2bd1c52015-03-25 11:17:37 +00002206 // The Java language does not allow treating boolean as an integral type but
2207 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002208
Roland Levillaindff1f282014-11-05 14:15:05 +00002209 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 case Primitive::kPrimByte:
2211 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002212 case Primitive::kPrimBoolean:
2213 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002214 case Primitive::kPrimShort:
2215 case Primitive::kPrimInt:
2216 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002217 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002218 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2219 // Make the output overlap to please the register allocator. This greatly simplifies
2220 // the validation of the linear scan implementation
2221 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002222 break;
2223
2224 default:
2225 LOG(FATAL) << "Unexpected type conversion from " << input_type
2226 << " to " << result_type;
2227 }
2228 break;
2229
Roland Levillain01a8d712014-11-14 16:27:39 +00002230 case Primitive::kPrimShort:
2231 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002232 case Primitive::kPrimBoolean:
2233 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002234 case Primitive::kPrimByte:
2235 case Primitive::kPrimInt:
2236 case Primitive::kPrimChar:
2237 // Processing a Dex `int-to-short' instruction.
2238 locations->SetInAt(0, Location::Any());
2239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2240 break;
2241
2242 default:
2243 LOG(FATAL) << "Unexpected type conversion from " << input_type
2244 << " to " << result_type;
2245 }
2246 break;
2247
Roland Levillain946e1432014-11-11 17:35:19 +00002248 case Primitive::kPrimInt:
2249 switch (input_type) {
2250 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002251 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002252 locations->SetInAt(0, Location::Any());
2253 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2254 break;
2255
2256 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002257 // Processing a Dex `float-to-int' instruction.
2258 locations->SetInAt(0, Location::RequiresFpuRegister());
2259 locations->SetOut(Location::RequiresRegister());
2260 locations->AddTemp(Location::RequiresFpuRegister());
2261 break;
2262
Roland Levillain946e1432014-11-11 17:35:19 +00002263 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002264 // Processing a Dex `double-to-int' instruction.
2265 locations->SetInAt(0, Location::RequiresFpuRegister());
2266 locations->SetOut(Location::RequiresRegister());
2267 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002268 break;
2269
2270 default:
2271 LOG(FATAL) << "Unexpected type conversion from " << input_type
2272 << " to " << result_type;
2273 }
2274 break;
2275
Roland Levillaindff1f282014-11-05 14:15:05 +00002276 case Primitive::kPrimLong:
2277 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002278 case Primitive::kPrimBoolean:
2279 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002280 case Primitive::kPrimByte:
2281 case Primitive::kPrimShort:
2282 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002283 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002284 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002285 locations->SetInAt(0, Location::RegisterLocation(EAX));
2286 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2287 break;
2288
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002289 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002290 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002291 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002292 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002293 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2294 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2295
Vladimir Marko949c91f2015-01-27 10:48:44 +00002296 // The runtime helper puts the result in EAX, EDX.
2297 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002298 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002299 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002300
2301 default:
2302 LOG(FATAL) << "Unexpected type conversion from " << input_type
2303 << " to " << result_type;
2304 }
2305 break;
2306
Roland Levillain981e4542014-11-14 11:47:14 +00002307 case Primitive::kPrimChar:
2308 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002309 case Primitive::kPrimBoolean:
2310 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002311 case Primitive::kPrimByte:
2312 case Primitive::kPrimShort:
2313 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002314 // Processing a Dex `int-to-char' instruction.
2315 locations->SetInAt(0, Location::Any());
2316 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2317 break;
2318
2319 default:
2320 LOG(FATAL) << "Unexpected type conversion from " << input_type
2321 << " to " << result_type;
2322 }
2323 break;
2324
Roland Levillaindff1f282014-11-05 14:15:05 +00002325 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002326 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002327 case Primitive::kPrimBoolean:
2328 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002329 case Primitive::kPrimByte:
2330 case Primitive::kPrimShort:
2331 case Primitive::kPrimInt:
2332 case Primitive::kPrimChar:
2333 // Processing a Dex `int-to-float' instruction.
2334 locations->SetInAt(0, Location::RequiresRegister());
2335 locations->SetOut(Location::RequiresFpuRegister());
2336 break;
2337
2338 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002339 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002340 locations->SetInAt(0, Location::Any());
2341 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002342 break;
2343
Roland Levillaincff13742014-11-17 14:32:17 +00002344 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002345 // Processing a Dex `double-to-float' instruction.
2346 locations->SetInAt(0, Location::RequiresFpuRegister());
2347 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002348 break;
2349
2350 default:
2351 LOG(FATAL) << "Unexpected type conversion from " << input_type
2352 << " to " << result_type;
2353 };
2354 break;
2355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002357 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002358 case Primitive::kPrimBoolean:
2359 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002360 case Primitive::kPrimByte:
2361 case Primitive::kPrimShort:
2362 case Primitive::kPrimInt:
2363 case Primitive::kPrimChar:
2364 // Processing a Dex `int-to-double' instruction.
2365 locations->SetInAt(0, Location::RequiresRegister());
2366 locations->SetOut(Location::RequiresFpuRegister());
2367 break;
2368
2369 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002370 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002371 locations->SetInAt(0, Location::Any());
2372 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002373 break;
2374
Roland Levillaincff13742014-11-17 14:32:17 +00002375 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002376 // Processing a Dex `float-to-double' instruction.
2377 locations->SetInAt(0, Location::RequiresFpuRegister());
2378 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002379 break;
2380
2381 default:
2382 LOG(FATAL) << "Unexpected type conversion from " << input_type
2383 << " to " << result_type;
2384 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002385 break;
2386
2387 default:
2388 LOG(FATAL) << "Unexpected type conversion from " << input_type
2389 << " to " << result_type;
2390 }
2391}
2392
2393void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2394 LocationSummary* locations = conversion->GetLocations();
2395 Location out = locations->Out();
2396 Location in = locations->InAt(0);
2397 Primitive::Type result_type = conversion->GetResultType();
2398 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002399 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002400 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002401 case Primitive::kPrimByte:
2402 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002403 case Primitive::kPrimBoolean:
2404 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002405 case Primitive::kPrimShort:
2406 case Primitive::kPrimInt:
2407 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002408 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002409 if (in.IsRegister()) {
2410 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002411 } else {
2412 DCHECK(in.GetConstant()->IsIntConstant());
2413 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2414 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2415 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002416 break;
2417
2418 default:
2419 LOG(FATAL) << "Unexpected type conversion from " << input_type
2420 << " to " << result_type;
2421 }
2422 break;
2423
Roland Levillain01a8d712014-11-14 16:27:39 +00002424 case Primitive::kPrimShort:
2425 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002426 case Primitive::kPrimBoolean:
2427 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002428 case Primitive::kPrimByte:
2429 case Primitive::kPrimInt:
2430 case Primitive::kPrimChar:
2431 // Processing a Dex `int-to-short' instruction.
2432 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002433 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002434 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002435 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002436 } else {
2437 DCHECK(in.GetConstant()->IsIntConstant());
2438 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002439 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002440 }
2441 break;
2442
2443 default:
2444 LOG(FATAL) << "Unexpected type conversion from " << input_type
2445 << " to " << result_type;
2446 }
2447 break;
2448
Roland Levillain946e1432014-11-11 17:35:19 +00002449 case Primitive::kPrimInt:
2450 switch (input_type) {
2451 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002452 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002453 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002454 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002455 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002456 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002457 } else {
2458 DCHECK(in.IsConstant());
2459 DCHECK(in.GetConstant()->IsLongConstant());
2460 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002462 }
2463 break;
2464
Roland Levillain3f8f9362014-12-02 17:45:01 +00002465 case Primitive::kPrimFloat: {
2466 // Processing a Dex `float-to-int' instruction.
2467 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2468 Register output = out.AsRegister<Register>();
2469 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002470 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002471
2472 __ movl(output, Immediate(kPrimIntMax));
2473 // temp = int-to-float(output)
2474 __ cvtsi2ss(temp, output);
2475 // if input >= temp goto done
2476 __ comiss(input, temp);
2477 __ j(kAboveEqual, &done);
2478 // if input == NaN goto nan
2479 __ j(kUnordered, &nan);
2480 // output = float-to-int-truncate(input)
2481 __ cvttss2si(output, input);
2482 __ jmp(&done);
2483 __ Bind(&nan);
2484 // output = 0
2485 __ xorl(output, output);
2486 __ Bind(&done);
2487 break;
2488 }
2489
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002490 case Primitive::kPrimDouble: {
2491 // Processing a Dex `double-to-int' instruction.
2492 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2493 Register output = out.AsRegister<Register>();
2494 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002495 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002496
2497 __ movl(output, Immediate(kPrimIntMax));
2498 // temp = int-to-double(output)
2499 __ cvtsi2sd(temp, output);
2500 // if input >= temp goto done
2501 __ comisd(input, temp);
2502 __ j(kAboveEqual, &done);
2503 // if input == NaN goto nan
2504 __ j(kUnordered, &nan);
2505 // output = double-to-int-truncate(input)
2506 __ cvttsd2si(output, input);
2507 __ jmp(&done);
2508 __ Bind(&nan);
2509 // output = 0
2510 __ xorl(output, output);
2511 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002512 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002513 }
Roland Levillain946e1432014-11-11 17:35:19 +00002514
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::kPrimLong:
2522 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002523 case Primitive::kPrimBoolean:
2524 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002525 case Primitive::kPrimByte:
2526 case Primitive::kPrimShort:
2527 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002528 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002529 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002530 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2531 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002532 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002533 __ cdq();
2534 break;
2535
2536 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002537 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002538 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2539 conversion,
2540 conversion->GetDexPc(),
2541 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002542 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002543 break;
2544
Roland Levillaindff1f282014-11-05 14:15:05 +00002545 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002546 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002547 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2548 conversion,
2549 conversion->GetDexPc(),
2550 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002551 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002552 break;
2553
2554 default:
2555 LOG(FATAL) << "Unexpected type conversion from " << input_type
2556 << " to " << result_type;
2557 }
2558 break;
2559
Roland Levillain981e4542014-11-14 11:47:14 +00002560 case Primitive::kPrimChar:
2561 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002562 case Primitive::kPrimBoolean:
2563 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002564 case Primitive::kPrimByte:
2565 case Primitive::kPrimShort:
2566 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002567 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2568 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002569 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002570 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002571 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002572 } else {
2573 DCHECK(in.GetConstant()->IsIntConstant());
2574 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002575 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002576 }
2577 break;
2578
2579 default:
2580 LOG(FATAL) << "Unexpected type conversion from " << input_type
2581 << " to " << result_type;
2582 }
2583 break;
2584
Roland Levillaindff1f282014-11-05 14:15:05 +00002585 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002586 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002587 case Primitive::kPrimBoolean:
2588 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002589 case Primitive::kPrimByte:
2590 case Primitive::kPrimShort:
2591 case Primitive::kPrimInt:
2592 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002593 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002595 break;
2596
Roland Levillain6d0e4832014-11-27 18:31:21 +00002597 case Primitive::kPrimLong: {
2598 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002599 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002600
Roland Levillain232ade02015-04-20 15:14:36 +01002601 // Create stack space for the call to
2602 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2603 // TODO: enhance register allocator to ask for stack temporaries.
2604 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2605 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2606 __ subl(ESP, Immediate(adjustment));
2607 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002608
Roland Levillain232ade02015-04-20 15:14:36 +01002609 // Load the value to the FP stack, using temporaries if needed.
2610 PushOntoFPStack(in, 0, adjustment, false, true);
2611
2612 if (out.IsStackSlot()) {
2613 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2614 } else {
2615 __ fstps(Address(ESP, 0));
2616 Location stack_temp = Location::StackSlot(0);
2617 codegen_->Move32(out, stack_temp);
2618 }
2619
2620 // Remove the temporary stack space we allocated.
2621 if (adjustment != 0) {
2622 __ addl(ESP, Immediate(adjustment));
2623 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002624 break;
2625 }
2626
Roland Levillaincff13742014-11-17 14:32:17 +00002627 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002628 // Processing a Dex `double-to-float' instruction.
2629 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002630 break;
2631
2632 default:
2633 LOG(FATAL) << "Unexpected type conversion from " << input_type
2634 << " to " << result_type;
2635 };
2636 break;
2637
Roland Levillaindff1f282014-11-05 14:15:05 +00002638 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002639 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002640 case Primitive::kPrimBoolean:
2641 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002642 case Primitive::kPrimByte:
2643 case Primitive::kPrimShort:
2644 case Primitive::kPrimInt:
2645 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002646 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002647 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002648 break;
2649
Roland Levillain647b9ed2014-11-27 12:06:00 +00002650 case Primitive::kPrimLong: {
2651 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002652 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002653
Roland Levillain232ade02015-04-20 15:14:36 +01002654 // Create stack space for the call to
2655 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2656 // TODO: enhance register allocator to ask for stack temporaries.
2657 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2658 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2659 __ subl(ESP, Immediate(adjustment));
2660 }
2661
2662 // Load the value to the FP stack, using temporaries if needed.
2663 PushOntoFPStack(in, 0, adjustment, false, true);
2664
2665 if (out.IsDoubleStackSlot()) {
2666 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2667 } else {
2668 __ fstpl(Address(ESP, 0));
2669 Location stack_temp = Location::DoubleStackSlot(0);
2670 codegen_->Move64(out, stack_temp);
2671 }
2672
2673 // Remove the temporary stack space we allocated.
2674 if (adjustment != 0) {
2675 __ addl(ESP, Immediate(adjustment));
2676 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002677 break;
2678 }
2679
Roland Levillaincff13742014-11-17 14:32:17 +00002680 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002681 // Processing a Dex `float-to-double' instruction.
2682 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002683 break;
2684
2685 default:
2686 LOG(FATAL) << "Unexpected type conversion from " << input_type
2687 << " to " << result_type;
2688 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002689 break;
2690
2691 default:
2692 LOG(FATAL) << "Unexpected type conversion from " << input_type
2693 << " to " << result_type;
2694 }
2695}
2696
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002697void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002698 LocationSummary* locations =
2699 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002700 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002701 case Primitive::kPrimInt: {
2702 locations->SetInAt(0, Location::RequiresRegister());
2703 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2704 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2705 break;
2706 }
2707
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002708 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002709 locations->SetInAt(0, Location::RequiresRegister());
2710 locations->SetInAt(1, Location::Any());
2711 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002712 break;
2713 }
2714
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002715 case Primitive::kPrimFloat:
2716 case Primitive::kPrimDouble: {
2717 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002718 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002719 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002720 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002721 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002722
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002723 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002724 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2725 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002726 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002727}
2728
2729void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2730 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002731 Location first = locations->InAt(0);
2732 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002733 Location out = locations->Out();
2734
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002735 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002736 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002737 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002738 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2739 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002740 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2741 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002742 } else {
2743 __ leal(out.AsRegister<Register>(), Address(
2744 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2745 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002746 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002747 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2748 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2749 __ addl(out.AsRegister<Register>(), Immediate(value));
2750 } else {
2751 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2752 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002753 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002754 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002755 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002756 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002757 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002758 }
2759
2760 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002761 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002762 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2763 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002764 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002765 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2766 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002767 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002768 } else {
2769 DCHECK(second.IsConstant()) << second;
2770 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2771 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2772 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002773 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002774 break;
2775 }
2776
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002777 case Primitive::kPrimFloat: {
2778 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002779 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002780 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2781 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2782 DCHECK(!const_area->NeedsMaterialization());
2783 __ addss(first.AsFpuRegister<XmmRegister>(),
2784 codegen_->LiteralFloatAddress(
2785 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2786 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2787 } else {
2788 DCHECK(second.IsStackSlot());
2789 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002790 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002791 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002792 }
2793
2794 case Primitive::kPrimDouble: {
2795 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002797 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2798 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2799 DCHECK(!const_area->NeedsMaterialization());
2800 __ addsd(first.AsFpuRegister<XmmRegister>(),
2801 codegen_->LiteralDoubleAddress(
2802 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2803 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2804 } else {
2805 DCHECK(second.IsDoubleStackSlot());
2806 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002807 }
2808 break;
2809 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002810
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002811 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002812 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002813 }
2814}
2815
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002816void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002817 LocationSummary* locations =
2818 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002819 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002820 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002821 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002822 locations->SetInAt(0, Location::RequiresRegister());
2823 locations->SetInAt(1, Location::Any());
2824 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002825 break;
2826 }
Calin Juravle11351682014-10-23 15:38:15 +01002827 case Primitive::kPrimFloat:
2828 case Primitive::kPrimDouble: {
2829 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002830 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002831 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002832 break;
Calin Juravle11351682014-10-23 15:38:15 +01002833 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002834
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002835 default:
Calin Juravle11351682014-10-23 15:38:15 +01002836 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002837 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002838}
2839
2840void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2841 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002842 Location first = locations->InAt(0);
2843 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002844 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002846 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002847 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002848 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002849 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002850 __ subl(first.AsRegister<Register>(),
2851 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002852 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002853 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002854 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002855 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002856 }
2857
2858 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002859 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002860 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2861 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002862 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002863 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002864 __ sbbl(first.AsRegisterPairHigh<Register>(),
2865 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002866 } else {
2867 DCHECK(second.IsConstant()) << second;
2868 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2869 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2870 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002871 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002872 break;
2873 }
2874
Calin Juravle11351682014-10-23 15:38:15 +01002875 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002876 if (second.IsFpuRegister()) {
2877 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2878 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2879 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2880 DCHECK(!const_area->NeedsMaterialization());
2881 __ subss(first.AsFpuRegister<XmmRegister>(),
2882 codegen_->LiteralFloatAddress(
2883 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2884 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2885 } else {
2886 DCHECK(second.IsStackSlot());
2887 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2888 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002889 break;
Calin Juravle11351682014-10-23 15:38:15 +01002890 }
2891
2892 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002893 if (second.IsFpuRegister()) {
2894 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2895 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2896 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2897 DCHECK(!const_area->NeedsMaterialization());
2898 __ subsd(first.AsFpuRegister<XmmRegister>(),
2899 codegen_->LiteralDoubleAddress(
2900 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2901 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2902 } else {
2903 DCHECK(second.IsDoubleStackSlot());
2904 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2905 }
Calin Juravle11351682014-10-23 15:38:15 +01002906 break;
2907 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002908
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002909 default:
Calin Juravle11351682014-10-23 15:38:15 +01002910 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002911 }
2912}
2913
Calin Juravle34bacdf2014-10-07 20:23:36 +01002914void LocationsBuilderX86::VisitMul(HMul* mul) {
2915 LocationSummary* locations =
2916 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2917 switch (mul->GetResultType()) {
2918 case Primitive::kPrimInt:
2919 locations->SetInAt(0, Location::RequiresRegister());
2920 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002921 if (mul->InputAt(1)->IsIntConstant()) {
2922 // Can use 3 operand multiply.
2923 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2924 } else {
2925 locations->SetOut(Location::SameAsFirstInput());
2926 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002927 break;
2928 case Primitive::kPrimLong: {
2929 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002930 locations->SetInAt(1, Location::Any());
2931 locations->SetOut(Location::SameAsFirstInput());
2932 // Needed for imul on 32bits with 64bits output.
2933 locations->AddTemp(Location::RegisterLocation(EAX));
2934 locations->AddTemp(Location::RegisterLocation(EDX));
2935 break;
2936 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002937 case Primitive::kPrimFloat:
2938 case Primitive::kPrimDouble: {
2939 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002940 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002941 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002942 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002943 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002944
2945 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002946 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002947 }
2948}
2949
2950void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2951 LocationSummary* locations = mul->GetLocations();
2952 Location first = locations->InAt(0);
2953 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002954 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002955
2956 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002957 case Primitive::kPrimInt:
2958 // The constant may have ended up in a register, so test explicitly to avoid
2959 // problems where the output may not be the same as the first operand.
2960 if (mul->InputAt(1)->IsIntConstant()) {
2961 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2962 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2963 } else if (second.IsRegister()) {
2964 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002965 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002966 } else {
2967 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002968 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002970 }
2971 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002972
2973 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002974 Register in1_hi = first.AsRegisterPairHigh<Register>();
2975 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002976 Register eax = locations->GetTemp(0).AsRegister<Register>();
2977 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002978
2979 DCHECK_EQ(EAX, eax);
2980 DCHECK_EQ(EDX, edx);
2981
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002982 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002983 // output: in1
2984 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2985 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2986 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002987 if (second.IsConstant()) {
2988 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002989
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002990 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2991 int32_t low_value = Low32Bits(value);
2992 int32_t high_value = High32Bits(value);
2993 Immediate low(low_value);
2994 Immediate high(high_value);
2995
2996 __ movl(eax, high);
2997 // eax <- in1.lo * in2.hi
2998 __ imull(eax, in1_lo);
2999 // in1.hi <- in1.hi * in2.lo
3000 __ imull(in1_hi, low);
3001 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3002 __ addl(in1_hi, eax);
3003 // move in2_lo to eax to prepare for double precision
3004 __ movl(eax, low);
3005 // edx:eax <- in1.lo * in2.lo
3006 __ mull(in1_lo);
3007 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3008 __ addl(in1_hi, edx);
3009 // in1.lo <- (in1.lo * in2.lo)[31:0];
3010 __ movl(in1_lo, eax);
3011 } else if (second.IsRegisterPair()) {
3012 Register in2_hi = second.AsRegisterPairHigh<Register>();
3013 Register in2_lo = second.AsRegisterPairLow<Register>();
3014
3015 __ movl(eax, in2_hi);
3016 // eax <- in1.lo * in2.hi
3017 __ imull(eax, in1_lo);
3018 // in1.hi <- in1.hi * in2.lo
3019 __ imull(in1_hi, in2_lo);
3020 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3021 __ addl(in1_hi, eax);
3022 // move in1_lo to eax to prepare for double precision
3023 __ movl(eax, in1_lo);
3024 // edx:eax <- in1.lo * in2.lo
3025 __ mull(in2_lo);
3026 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3027 __ addl(in1_hi, edx);
3028 // in1.lo <- (in1.lo * in2.lo)[31:0];
3029 __ movl(in1_lo, eax);
3030 } else {
3031 DCHECK(second.IsDoubleStackSlot()) << second;
3032 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3033 Address in2_lo(ESP, second.GetStackIndex());
3034
3035 __ movl(eax, in2_hi);
3036 // eax <- in1.lo * in2.hi
3037 __ imull(eax, in1_lo);
3038 // in1.hi <- in1.hi * in2.lo
3039 __ imull(in1_hi, in2_lo);
3040 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3041 __ addl(in1_hi, eax);
3042 // move in1_lo to eax to prepare for double precision
3043 __ movl(eax, in1_lo);
3044 // edx:eax <- in1.lo * in2.lo
3045 __ mull(in2_lo);
3046 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3047 __ addl(in1_hi, edx);
3048 // in1.lo <- (in1.lo * in2.lo)[31:0];
3049 __ movl(in1_lo, eax);
3050 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003051
3052 break;
3053 }
3054
Calin Juravleb5bfa962014-10-21 18:02:24 +01003055 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003056 DCHECK(first.Equals(locations->Out()));
3057 if (second.IsFpuRegister()) {
3058 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3059 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3060 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3061 DCHECK(!const_area->NeedsMaterialization());
3062 __ mulss(first.AsFpuRegister<XmmRegister>(),
3063 codegen_->LiteralFloatAddress(
3064 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3065 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3066 } else {
3067 DCHECK(second.IsStackSlot());
3068 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3069 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003070 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003071 }
3072
3073 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003074 DCHECK(first.Equals(locations->Out()));
3075 if (second.IsFpuRegister()) {
3076 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3077 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3078 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3079 DCHECK(!const_area->NeedsMaterialization());
3080 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3081 codegen_->LiteralDoubleAddress(
3082 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3083 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3084 } else {
3085 DCHECK(second.IsDoubleStackSlot());
3086 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3087 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003088 break;
3089 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003090
3091 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003092 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003093 }
3094}
3095
Roland Levillain232ade02015-04-20 15:14:36 +01003096void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3097 uint32_t temp_offset,
3098 uint32_t stack_adjustment,
3099 bool is_fp,
3100 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003101 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003102 DCHECK(!is_wide);
3103 if (is_fp) {
3104 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3105 } else {
3106 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3107 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003108 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003109 DCHECK(is_wide);
3110 if (is_fp) {
3111 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3112 } else {
3113 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3114 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003115 } else {
3116 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003117 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003118 Location stack_temp = Location::StackSlot(temp_offset);
3119 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003120 if (is_fp) {
3121 __ flds(Address(ESP, temp_offset));
3122 } else {
3123 __ filds(Address(ESP, temp_offset));
3124 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003125 } else {
3126 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3127 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003128 if (is_fp) {
3129 __ fldl(Address(ESP, temp_offset));
3130 } else {
3131 __ fildl(Address(ESP, temp_offset));
3132 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003133 }
3134 }
3135}
3136
3137void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3138 Primitive::Type type = rem->GetResultType();
3139 bool is_float = type == Primitive::kPrimFloat;
3140 size_t elem_size = Primitive::ComponentSize(type);
3141 LocationSummary* locations = rem->GetLocations();
3142 Location first = locations->InAt(0);
3143 Location second = locations->InAt(1);
3144 Location out = locations->Out();
3145
3146 // Create stack space for 2 elements.
3147 // TODO: enhance register allocator to ask for stack temporaries.
3148 __ subl(ESP, Immediate(2 * elem_size));
3149
3150 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003151 const bool is_wide = !is_float;
3152 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3153 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003154
3155 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003156 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003157 __ Bind(&retry);
3158 __ fprem();
3159
3160 // Move FP status to AX.
3161 __ fstsw();
3162
3163 // And see if the argument reduction is complete. This is signaled by the
3164 // C2 FPU flag bit set to 0.
3165 __ andl(EAX, Immediate(kC2ConditionMask));
3166 __ j(kNotEqual, &retry);
3167
3168 // We have settled on the final value. Retrieve it into an XMM register.
3169 // Store FP top of stack to real stack.
3170 if (is_float) {
3171 __ fsts(Address(ESP, 0));
3172 } else {
3173 __ fstl(Address(ESP, 0));
3174 }
3175
3176 // Pop the 2 items from the FP stack.
3177 __ fucompp();
3178
3179 // Load the value from the stack into an XMM register.
3180 DCHECK(out.IsFpuRegister()) << out;
3181 if (is_float) {
3182 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3183 } else {
3184 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3185 }
3186
3187 // And remove the temporary stack space we allocated.
3188 __ addl(ESP, Immediate(2 * elem_size));
3189}
3190
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003191
3192void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3193 DCHECK(instruction->IsDiv() || instruction->IsRem());
3194
3195 LocationSummary* locations = instruction->GetLocations();
3196 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003197 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003198
3199 Register out_register = locations->Out().AsRegister<Register>();
3200 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003201 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003202
3203 DCHECK(imm == 1 || imm == -1);
3204
3205 if (instruction->IsRem()) {
3206 __ xorl(out_register, out_register);
3207 } else {
3208 __ movl(out_register, input_register);
3209 if (imm == -1) {
3210 __ negl(out_register);
3211 }
3212 }
3213}
3214
3215
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003216void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003217 LocationSummary* locations = instruction->GetLocations();
3218
3219 Register out_register = locations->Out().AsRegister<Register>();
3220 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003221 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003222
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003223 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003224 Register num = locations->GetTemp(0).AsRegister<Register>();
3225
3226 __ leal(num, Address(input_register, std::abs(imm) - 1));
3227 __ testl(input_register, input_register);
3228 __ cmovl(kGreaterEqual, num, input_register);
3229 int shift = CTZ(imm);
3230 __ sarl(num, Immediate(shift));
3231
3232 if (imm < 0) {
3233 __ negl(num);
3234 }
3235
3236 __ movl(out_register, num);
3237}
3238
3239void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3240 DCHECK(instruction->IsDiv() || instruction->IsRem());
3241
3242 LocationSummary* locations = instruction->GetLocations();
3243 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3244
3245 Register eax = locations->InAt(0).AsRegister<Register>();
3246 Register out = locations->Out().AsRegister<Register>();
3247 Register num;
3248 Register edx;
3249
3250 if (instruction->IsDiv()) {
3251 edx = locations->GetTemp(0).AsRegister<Register>();
3252 num = locations->GetTemp(1).AsRegister<Register>();
3253 } else {
3254 edx = locations->Out().AsRegister<Register>();
3255 num = locations->GetTemp(0).AsRegister<Register>();
3256 }
3257
3258 DCHECK_EQ(EAX, eax);
3259 DCHECK_EQ(EDX, edx);
3260 if (instruction->IsDiv()) {
3261 DCHECK_EQ(EAX, out);
3262 } else {
3263 DCHECK_EQ(EDX, out);
3264 }
3265
3266 int64_t magic;
3267 int shift;
3268 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3269
Mark Mendell0c9497d2015-08-21 09:30:05 -04003270 NearLabel ndiv;
3271 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003272 // If numerator is 0, the result is 0, no computation needed.
3273 __ testl(eax, eax);
3274 __ j(kNotEqual, &ndiv);
3275
3276 __ xorl(out, out);
3277 __ jmp(&end);
3278
3279 __ Bind(&ndiv);
3280
3281 // Save the numerator.
3282 __ movl(num, eax);
3283
3284 // EAX = magic
3285 __ movl(eax, Immediate(magic));
3286
3287 // EDX:EAX = magic * numerator
3288 __ imull(num);
3289
3290 if (imm > 0 && magic < 0) {
3291 // EDX += num
3292 __ addl(edx, num);
3293 } else if (imm < 0 && magic > 0) {
3294 __ subl(edx, num);
3295 }
3296
3297 // Shift if needed.
3298 if (shift != 0) {
3299 __ sarl(edx, Immediate(shift));
3300 }
3301
3302 // EDX += 1 if EDX < 0
3303 __ movl(eax, edx);
3304 __ shrl(edx, Immediate(31));
3305 __ addl(edx, eax);
3306
3307 if (instruction->IsRem()) {
3308 __ movl(eax, num);
3309 __ imull(edx, Immediate(imm));
3310 __ subl(eax, edx);
3311 __ movl(edx, eax);
3312 } else {
3313 __ movl(eax, edx);
3314 }
3315 __ Bind(&end);
3316}
3317
Calin Juravlebacfec32014-11-14 15:54:36 +00003318void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3319 DCHECK(instruction->IsDiv() || instruction->IsRem());
3320
3321 LocationSummary* locations = instruction->GetLocations();
3322 Location out = locations->Out();
3323 Location first = locations->InAt(0);
3324 Location second = locations->InAt(1);
3325 bool is_div = instruction->IsDiv();
3326
3327 switch (instruction->GetResultType()) {
3328 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003329 DCHECK_EQ(EAX, first.AsRegister<Register>());
3330 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003331
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003332 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003333 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003334
3335 if (imm == 0) {
3336 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3337 } else if (imm == 1 || imm == -1) {
3338 DivRemOneOrMinusOne(instruction);
3339 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003340 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003341 } else {
3342 DCHECK(imm <= -2 || imm >= 2);
3343 GenerateDivRemWithAnyConstant(instruction);
3344 }
3345 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003346 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003347 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003348 is_div);
3349 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003350
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003351 Register second_reg = second.AsRegister<Register>();
3352 // 0x80000000/-1 triggers an arithmetic exception!
3353 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3354 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003355
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003356 __ cmpl(second_reg, Immediate(-1));
3357 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003358
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003359 // edx:eax <- sign-extended of eax
3360 __ cdq();
3361 // eax = quotient, edx = remainder
3362 __ idivl(second_reg);
3363 __ Bind(slow_path->GetExitLabel());
3364 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003365 break;
3366 }
3367
3368 case Primitive::kPrimLong: {
3369 InvokeRuntimeCallingConvention calling_convention;
3370 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3371 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3372 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3373 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3374 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3375 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3376
3377 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003378 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3379 instruction,
3380 instruction->GetDexPc(),
3381 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003382 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003383 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003384 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3385 instruction,
3386 instruction->GetDexPc(),
3387 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003388 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003389 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003390 break;
3391 }
3392
3393 default:
3394 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3395 }
3396}
3397
Calin Juravle7c4954d2014-10-28 16:57:40 +00003398void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003399 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003400 ? LocationSummary::kCall
3401 : LocationSummary::kNoCall;
3402 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3403
Calin Juravle7c4954d2014-10-28 16:57:40 +00003404 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003405 case Primitive::kPrimInt: {
3406 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003407 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003408 locations->SetOut(Location::SameAsFirstInput());
3409 // Intel uses edx:eax as the dividend.
3410 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003411 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3412 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3413 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003414 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415 locations->AddTemp(Location::RequiresRegister());
3416 }
Calin Juravled0d48522014-11-04 16:40:20 +00003417 break;
3418 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003419 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003420 InvokeRuntimeCallingConvention calling_convention;
3421 locations->SetInAt(0, Location::RegisterPairLocation(
3422 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3423 locations->SetInAt(1, Location::RegisterPairLocation(
3424 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3425 // Runtime helper puts the result in EAX, EDX.
3426 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003427 break;
3428 }
3429 case Primitive::kPrimFloat:
3430 case Primitive::kPrimDouble: {
3431 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003432 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003433 locations->SetOut(Location::SameAsFirstInput());
3434 break;
3435 }
3436
3437 default:
3438 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3439 }
3440}
3441
3442void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3443 LocationSummary* locations = div->GetLocations();
3444 Location first = locations->InAt(0);
3445 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003446
3447 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003449 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003450 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003451 break;
3452 }
3453
3454 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003455 if (second.IsFpuRegister()) {
3456 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3457 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3458 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3459 DCHECK(!const_area->NeedsMaterialization());
3460 __ divss(first.AsFpuRegister<XmmRegister>(),
3461 codegen_->LiteralFloatAddress(
3462 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3463 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3464 } else {
3465 DCHECK(second.IsStackSlot());
3466 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3467 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003468 break;
3469 }
3470
3471 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003472 if (second.IsFpuRegister()) {
3473 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3474 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3475 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3476 DCHECK(!const_area->NeedsMaterialization());
3477 __ divsd(first.AsFpuRegister<XmmRegister>(),
3478 codegen_->LiteralDoubleAddress(
3479 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3480 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3481 } else {
3482 DCHECK(second.IsDoubleStackSlot());
3483 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3484 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003485 break;
3486 }
3487
3488 default:
3489 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3490 }
3491}
3492
Calin Juravlebacfec32014-11-14 15:54:36 +00003493void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003494 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003495
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003496 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3497 ? LocationSummary::kCall
3498 : LocationSummary::kNoCall;
3499 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003500
Calin Juravled2ec87d2014-12-08 14:24:46 +00003501 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003502 case Primitive::kPrimInt: {
3503 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003504 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003505 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003506 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3507 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3508 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003509 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 locations->AddTemp(Location::RequiresRegister());
3511 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003512 break;
3513 }
3514 case Primitive::kPrimLong: {
3515 InvokeRuntimeCallingConvention calling_convention;
3516 locations->SetInAt(0, Location::RegisterPairLocation(
3517 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3518 locations->SetInAt(1, Location::RegisterPairLocation(
3519 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3520 // Runtime helper puts the result in EAX, EDX.
3521 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3522 break;
3523 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003524 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003525 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003526 locations->SetInAt(0, Location::Any());
3527 locations->SetInAt(1, Location::Any());
3528 locations->SetOut(Location::RequiresFpuRegister());
3529 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003530 break;
3531 }
3532
3533 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003534 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003535 }
3536}
3537
3538void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3539 Primitive::Type type = rem->GetResultType();
3540 switch (type) {
3541 case Primitive::kPrimInt:
3542 case Primitive::kPrimLong: {
3543 GenerateDivRemIntegral(rem);
3544 break;
3545 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003546 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003547 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003548 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003549 break;
3550 }
3551 default:
3552 LOG(FATAL) << "Unexpected rem type " << type;
3553 }
3554}
3555
Calin Juravled0d48522014-11-04 16:40:20 +00003556void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003557 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3558 ? LocationSummary::kCallOnSlowPath
3559 : LocationSummary::kNoCall;
3560 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003561 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003562 case Primitive::kPrimByte:
3563 case Primitive::kPrimChar:
3564 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003565 case Primitive::kPrimInt: {
3566 locations->SetInAt(0, Location::Any());
3567 break;
3568 }
3569 case Primitive::kPrimLong: {
3570 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3571 if (!instruction->IsConstant()) {
3572 locations->AddTemp(Location::RequiresRegister());
3573 }
3574 break;
3575 }
3576 default:
3577 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3578 }
Calin Juravled0d48522014-11-04 16:40:20 +00003579 if (instruction->HasUses()) {
3580 locations->SetOut(Location::SameAsFirstInput());
3581 }
3582}
3583
3584void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003585 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003586 codegen_->AddSlowPath(slow_path);
3587
3588 LocationSummary* locations = instruction->GetLocations();
3589 Location value = locations->InAt(0);
3590
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003591 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003592 case Primitive::kPrimByte:
3593 case Primitive::kPrimChar:
3594 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003595 case Primitive::kPrimInt: {
3596 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003597 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003598 __ j(kEqual, slow_path->GetEntryLabel());
3599 } else if (value.IsStackSlot()) {
3600 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3601 __ j(kEqual, slow_path->GetEntryLabel());
3602 } else {
3603 DCHECK(value.IsConstant()) << value;
3604 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3605 __ jmp(slow_path->GetEntryLabel());
3606 }
3607 }
3608 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003609 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003610 case Primitive::kPrimLong: {
3611 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003612 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003613 __ movl(temp, value.AsRegisterPairLow<Register>());
3614 __ orl(temp, value.AsRegisterPairHigh<Register>());
3615 __ j(kEqual, slow_path->GetEntryLabel());
3616 } else {
3617 DCHECK(value.IsConstant()) << value;
3618 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3619 __ jmp(slow_path->GetEntryLabel());
3620 }
3621 }
3622 break;
3623 }
3624 default:
3625 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003626 }
Calin Juravled0d48522014-11-04 16:40:20 +00003627}
3628
Calin Juravle9aec02f2014-11-18 23:06:35 +00003629void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3630 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3631
3632 LocationSummary* locations =
3633 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3634
3635 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003636 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003637 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003638 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003639 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003640 // The shift count needs to be in CL or a constant.
3641 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003642 locations->SetOut(Location::SameAsFirstInput());
3643 break;
3644 }
3645 default:
3646 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3647 }
3648}
3649
3650void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3651 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3652
3653 LocationSummary* locations = op->GetLocations();
3654 Location first = locations->InAt(0);
3655 Location second = locations->InAt(1);
3656 DCHECK(first.Equals(locations->Out()));
3657
3658 switch (op->GetResultType()) {
3659 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003660 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003661 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003662 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003663 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003664 DCHECK_EQ(ECX, second_reg);
3665 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003666 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003667 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003668 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003669 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003670 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003671 }
3672 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003673 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3674 if (shift == 0) {
3675 return;
3676 }
3677 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003678 if (op->IsShl()) {
3679 __ shll(first_reg, imm);
3680 } else if (op->IsShr()) {
3681 __ sarl(first_reg, imm);
3682 } else {
3683 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003684 }
3685 }
3686 break;
3687 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003688 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003689 if (second.IsRegister()) {
3690 Register second_reg = second.AsRegister<Register>();
3691 DCHECK_EQ(ECX, second_reg);
3692 if (op->IsShl()) {
3693 GenerateShlLong(first, second_reg);
3694 } else if (op->IsShr()) {
3695 GenerateShrLong(first, second_reg);
3696 } else {
3697 GenerateUShrLong(first, second_reg);
3698 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003699 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003700 // Shift by a constant.
3701 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3702 // Nothing to do if the shift is 0, as the input is already the output.
3703 if (shift != 0) {
3704 if (op->IsShl()) {
3705 GenerateShlLong(first, shift);
3706 } else if (op->IsShr()) {
3707 GenerateShrLong(first, shift);
3708 } else {
3709 GenerateUShrLong(first, shift);
3710 }
3711 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003712 }
3713 break;
3714 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715 default:
3716 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3717 }
3718}
3719
Mark P Mendell73945692015-04-29 14:56:17 +00003720void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3721 Register low = loc.AsRegisterPairLow<Register>();
3722 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003723 if (shift == 1) {
3724 // This is just an addition.
3725 __ addl(low, low);
3726 __ adcl(high, high);
3727 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003728 // Shift by 32 is easy. High gets low, and low gets 0.
3729 codegen_->EmitParallelMoves(
3730 loc.ToLow(),
3731 loc.ToHigh(),
3732 Primitive::kPrimInt,
3733 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3734 loc.ToLow(),
3735 Primitive::kPrimInt);
3736 } else if (shift > 32) {
3737 // Low part becomes 0. High part is low part << (shift-32).
3738 __ movl(high, low);
3739 __ shll(high, Immediate(shift - 32));
3740 __ xorl(low, low);
3741 } else {
3742 // Between 1 and 31.
3743 __ shld(high, low, Immediate(shift));
3744 __ shll(low, Immediate(shift));
3745 }
3746}
3747
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003749 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003750 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3751 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3752 __ testl(shifter, Immediate(32));
3753 __ j(kEqual, &done);
3754 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3755 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3756 __ Bind(&done);
3757}
3758
Mark P Mendell73945692015-04-29 14:56:17 +00003759void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3760 Register low = loc.AsRegisterPairLow<Register>();
3761 Register high = loc.AsRegisterPairHigh<Register>();
3762 if (shift == 32) {
3763 // Need to copy the sign.
3764 DCHECK_NE(low, high);
3765 __ movl(low, high);
3766 __ sarl(high, Immediate(31));
3767 } else if (shift > 32) {
3768 DCHECK_NE(low, high);
3769 // High part becomes sign. Low part is shifted by shift - 32.
3770 __ movl(low, high);
3771 __ sarl(high, Immediate(31));
3772 __ sarl(low, Immediate(shift - 32));
3773 } else {
3774 // Between 1 and 31.
3775 __ shrd(low, high, Immediate(shift));
3776 __ sarl(high, Immediate(shift));
3777 }
3778}
3779
Calin Juravle9aec02f2014-11-18 23:06:35 +00003780void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003781 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003782 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3783 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3784 __ testl(shifter, Immediate(32));
3785 __ j(kEqual, &done);
3786 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3787 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3788 __ Bind(&done);
3789}
3790
Mark P Mendell73945692015-04-29 14:56:17 +00003791void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3792 Register low = loc.AsRegisterPairLow<Register>();
3793 Register high = loc.AsRegisterPairHigh<Register>();
3794 if (shift == 32) {
3795 // Shift by 32 is easy. Low gets high, and high gets 0.
3796 codegen_->EmitParallelMoves(
3797 loc.ToHigh(),
3798 loc.ToLow(),
3799 Primitive::kPrimInt,
3800 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3801 loc.ToHigh(),
3802 Primitive::kPrimInt);
3803 } else if (shift > 32) {
3804 // Low part is high >> (shift - 32). High part becomes 0.
3805 __ movl(low, high);
3806 __ shrl(low, Immediate(shift - 32));
3807 __ xorl(high, high);
3808 } else {
3809 // Between 1 and 31.
3810 __ shrd(low, high, Immediate(shift));
3811 __ shrl(high, Immediate(shift));
3812 }
3813}
3814
Calin Juravle9aec02f2014-11-18 23:06:35 +00003815void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003816 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003817 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3818 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3819 __ testl(shifter, Immediate(32));
3820 __ j(kEqual, &done);
3821 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3822 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3823 __ Bind(&done);
3824}
3825
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003826void LocationsBuilderX86::VisitRor(HRor* ror) {
3827 LocationSummary* locations =
3828 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3829
3830 switch (ror->GetResultType()) {
3831 case Primitive::kPrimLong:
3832 // Add the temporary needed.
3833 locations->AddTemp(Location::RequiresRegister());
3834 FALLTHROUGH_INTENDED;
3835 case Primitive::kPrimInt:
3836 locations->SetInAt(0, Location::RequiresRegister());
3837 // The shift count needs to be in CL (unless it is a constant).
3838 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3839 locations->SetOut(Location::SameAsFirstInput());
3840 break;
3841 default:
3842 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3843 UNREACHABLE();
3844 }
3845}
3846
3847void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3848 LocationSummary* locations = ror->GetLocations();
3849 Location first = locations->InAt(0);
3850 Location second = locations->InAt(1);
3851
3852 if (ror->GetResultType() == Primitive::kPrimInt) {
3853 Register first_reg = first.AsRegister<Register>();
3854 if (second.IsRegister()) {
3855 Register second_reg = second.AsRegister<Register>();
3856 __ rorl(first_reg, second_reg);
3857 } else {
3858 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3859 __ rorl(first_reg, imm);
3860 }
3861 return;
3862 }
3863
3864 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3865 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3866 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3867 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3868 if (second.IsRegister()) {
3869 Register second_reg = second.AsRegister<Register>();
3870 DCHECK_EQ(second_reg, ECX);
3871 __ movl(temp_reg, first_reg_hi);
3872 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3873 __ shrd(first_reg_lo, temp_reg, second_reg);
3874 __ movl(temp_reg, first_reg_hi);
3875 __ testl(second_reg, Immediate(32));
3876 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3877 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3878 } else {
3879 int32_t shift_amt =
3880 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3881 if (shift_amt == 0) {
3882 // Already fine.
3883 return;
3884 }
3885 if (shift_amt == 32) {
3886 // Just swap.
3887 __ movl(temp_reg, first_reg_lo);
3888 __ movl(first_reg_lo, first_reg_hi);
3889 __ movl(first_reg_hi, temp_reg);
3890 return;
3891 }
3892
3893 Immediate imm(shift_amt);
3894 // Save the constents of the low value.
3895 __ movl(temp_reg, first_reg_lo);
3896
3897 // Shift right into low, feeding bits from high.
3898 __ shrd(first_reg_lo, first_reg_hi, imm);
3899
3900 // Shift right into high, feeding bits from the original low.
3901 __ shrd(first_reg_hi, temp_reg, imm);
3902
3903 // Swap if needed.
3904 if (shift_amt > 32) {
3905 __ movl(temp_reg, first_reg_lo);
3906 __ movl(first_reg_lo, first_reg_hi);
3907 __ movl(first_reg_hi, temp_reg);
3908 }
3909 }
3910}
3911
Calin Juravle9aec02f2014-11-18 23:06:35 +00003912void LocationsBuilderX86::VisitShl(HShl* shl) {
3913 HandleShift(shl);
3914}
3915
3916void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3917 HandleShift(shl);
3918}
3919
3920void LocationsBuilderX86::VisitShr(HShr* shr) {
3921 HandleShift(shr);
3922}
3923
3924void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3925 HandleShift(shr);
3926}
3927
3928void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3929 HandleShift(ushr);
3930}
3931
3932void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3933 HandleShift(ushr);
3934}
3935
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003936void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003937 LocationSummary* locations =
3938 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003939 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003940 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003941 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3942 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003943}
3944
3945void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003946 // Note: if heap poisoning is enabled, the entry point takes cares
3947 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003948 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3949 instruction,
3950 instruction->GetDexPc(),
3951 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003952 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003953 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003954}
3955
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003956void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3957 LocationSummary* locations =
3958 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3959 locations->SetOut(Location::RegisterLocation(EAX));
3960 InvokeRuntimeCallingConvention calling_convention;
3961 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003962 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003963 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003964}
3965
3966void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3967 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003968 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003969 // Note: if heap poisoning is enabled, the entry point takes cares
3970 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003971 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3972 instruction,
3973 instruction->GetDexPc(),
3974 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003975 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003976 DCHECK(!codegen_->IsLeafMethod());
3977}
3978
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003979void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003980 LocationSummary* locations =
3981 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003982 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3983 if (location.IsStackSlot()) {
3984 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3985 } else if (location.IsDoubleStackSlot()) {
3986 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003987 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003988 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003989}
3990
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003991void InstructionCodeGeneratorX86::VisitParameterValue(
3992 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3993}
3994
3995void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3996 LocationSummary* locations =
3997 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3998 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3999}
4000
4001void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004002}
4003
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004004void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004005 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004006 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004007 locations->SetInAt(0, Location::RequiresRegister());
4008 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004009}
4010
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004011void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4012 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004013 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004014 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004015 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004016 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004017 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004018 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004019 break;
4020
4021 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004022 __ notl(out.AsRegisterPairLow<Register>());
4023 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004024 break;
4025
4026 default:
4027 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4028 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004029}
4030
David Brazdil66d126e2015-04-03 16:02:44 +01004031void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4034 locations->SetInAt(0, Location::RequiresRegister());
4035 locations->SetOut(Location::SameAsFirstInput());
4036}
4037
4038void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004039 LocationSummary* locations = bool_not->GetLocations();
4040 Location in = locations->InAt(0);
4041 Location out = locations->Out();
4042 DCHECK(in.Equals(out));
4043 __ xorl(out.AsRegister<Register>(), Immediate(1));
4044}
4045
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004046void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004047 LocationSummary* locations =
4048 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004049 switch (compare->InputAt(0)->GetType()) {
4050 case Primitive::kPrimLong: {
4051 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004052 locations->SetInAt(1, Location::Any());
4053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4054 break;
4055 }
4056 case Primitive::kPrimFloat:
4057 case Primitive::kPrimDouble: {
4058 locations->SetInAt(0, Location::RequiresFpuRegister());
4059 locations->SetInAt(1, Location::RequiresFpuRegister());
4060 locations->SetOut(Location::RequiresRegister());
4061 break;
4062 }
4063 default:
4064 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4065 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004066}
4067
4068void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004069 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004070 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004071 Location left = locations->InAt(0);
4072 Location right = locations->InAt(1);
4073
Mark Mendell0c9497d2015-08-21 09:30:05 -04004074 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004075 switch (compare->InputAt(0)->GetType()) {
4076 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004077 Register left_low = left.AsRegisterPairLow<Register>();
4078 Register left_high = left.AsRegisterPairHigh<Register>();
4079 int32_t val_low = 0;
4080 int32_t val_high = 0;
4081 bool right_is_const = false;
4082
4083 if (right.IsConstant()) {
4084 DCHECK(right.GetConstant()->IsLongConstant());
4085 right_is_const = true;
4086 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4087 val_low = Low32Bits(val);
4088 val_high = High32Bits(val);
4089 }
4090
Calin Juravleddb7df22014-11-25 20:56:51 +00004091 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004092 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004093 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004094 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004095 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004096 DCHECK(right_is_const) << right;
4097 if (val_high == 0) {
4098 __ testl(left_high, left_high);
4099 } else {
4100 __ cmpl(left_high, Immediate(val_high));
4101 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004102 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004103 __ j(kLess, &less); // Signed compare.
4104 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004105 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004106 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004107 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004108 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004109 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004110 DCHECK(right_is_const) << right;
4111 if (val_low == 0) {
4112 __ testl(left_low, left_low);
4113 } else {
4114 __ cmpl(left_low, Immediate(val_low));
4115 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004116 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004117 break;
4118 }
4119 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004120 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004121 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4122 break;
4123 }
4124 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004125 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004126 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004127 break;
4128 }
4129 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004130 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004131 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004132 __ movl(out, Immediate(0));
4133 __ j(kEqual, &done);
4134 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4135
4136 __ Bind(&greater);
4137 __ movl(out, Immediate(1));
4138 __ jmp(&done);
4139
4140 __ Bind(&less);
4141 __ movl(out, Immediate(-1));
4142
4143 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004144}
4145
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004146void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004147 LocationSummary* locations =
4148 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004149 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4150 locations->SetInAt(i, Location::Any());
4151 }
4152 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004153}
4154
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004155void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004156 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004157}
4158
Roland Levillain7c1559a2015-12-15 10:55:36 +00004159void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004160 /*
4161 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4162 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4163 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4164 */
4165 switch (kind) {
4166 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004167 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004168 break;
4169 }
4170 case MemBarrierKind::kAnyStore:
4171 case MemBarrierKind::kLoadAny:
4172 case MemBarrierKind::kStoreStore: {
4173 // nop
4174 break;
4175 }
4176 default:
4177 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004178 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004179}
4180
Vladimir Markodc151b22015-10-15 18:02:30 +01004181HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4182 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4183 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004184 switch (desired_dispatch_info.code_ptr_location) {
4185 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4186 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4187 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4188 // (Though the direct CALL ptr16:32 is available for consideration).
4189 return HInvokeStaticOrDirect::DispatchInfo {
4190 desired_dispatch_info.method_load_kind,
4191 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4192 desired_dispatch_info.method_load_data,
4193 0u
4194 };
4195 default:
4196 return desired_dispatch_info;
4197 }
4198}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004199
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004200Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4201 Register temp) {
4202 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004203 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004204 if (!invoke->GetLocations()->Intrinsified()) {
4205 return location.AsRegister<Register>();
4206 }
4207 // For intrinsics we allow any location, so it may be on the stack.
4208 if (!location.IsRegister()) {
4209 __ movl(temp, Address(ESP, location.GetStackIndex()));
4210 return temp;
4211 }
4212 // For register locations, check if the register was saved. If so, get it from the stack.
4213 // Note: There is a chance that the register was saved but not overwritten, so we could
4214 // save one load. However, since this is just an intrinsic slow path we prefer this
4215 // simple and more robust approach rather that trying to determine if that's the case.
4216 SlowPathCode* slow_path = GetCurrentSlowPath();
4217 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4218 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4219 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4220 __ movl(temp, Address(ESP, stack_offset));
4221 return temp;
4222 }
4223 return location.AsRegister<Register>();
4224}
4225
Vladimir Marko58155012015-08-19 12:49:41 +00004226void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4227 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4228 switch (invoke->GetMethodLoadKind()) {
4229 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4230 // temp = thread->string_init_entrypoint
4231 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4232 break;
4233 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004234 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004235 break;
4236 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4237 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4238 break;
4239 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4240 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4241 method_patches_.emplace_back(invoke->GetTargetMethod());
4242 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4243 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004244 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4245 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4246 temp.AsRegister<Register>());
4247 uint32_t offset = invoke->GetDexCacheArrayOffset();
4248 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4249 // Add the patch entry and bind its label at the end of the instruction.
4250 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4251 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4252 break;
4253 }
Vladimir Marko58155012015-08-19 12:49:41 +00004254 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004255 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004256 Register method_reg;
4257 Register reg = temp.AsRegister<Register>();
4258 if (current_method.IsRegister()) {
4259 method_reg = current_method.AsRegister<Register>();
4260 } else {
4261 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4262 DCHECK(!current_method.IsValid());
4263 method_reg = reg;
4264 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4265 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004266 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004267 __ movl(reg, Address(method_reg,
4268 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004269 // temp = temp[index_in_cache]
4270 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4271 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4272 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004273 }
Vladimir Marko58155012015-08-19 12:49:41 +00004274 }
4275
4276 switch (invoke->GetCodePtrLocation()) {
4277 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4278 __ call(GetFrameEntryLabel());
4279 break;
4280 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4281 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4282 Label* label = &relative_call_patches_.back().label;
4283 __ call(label); // Bind to the patch label, override at link time.
4284 __ Bind(label); // Bind the label at the end of the "call" insn.
4285 break;
4286 }
4287 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4288 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004289 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4290 LOG(FATAL) << "Unsupported";
4291 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004292 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4293 // (callee_method + offset_of_quick_compiled_code)()
4294 __ call(Address(callee_method.AsRegister<Register>(),
4295 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4296 kX86WordSize).Int32Value()));
4297 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004298 }
4299
4300 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004301}
4302
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004303void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4304 Register temp = temp_in.AsRegister<Register>();
4305 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4306 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004307
4308 // Use the calling convention instead of the location of the receiver, as
4309 // intrinsics may have put the receiver in a different register. In the intrinsics
4310 // slow path, the arguments have been moved to the right place, so here we are
4311 // guaranteed that the receiver is the first register of the calling convention.
4312 InvokeDexCallingConvention calling_convention;
4313 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004314 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004315 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004316 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004317 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004318 // Instead of simply (possibly) unpoisoning `temp` here, we should
4319 // emit a read barrier for the previous class reference load.
4320 // However this is not required in practice, as this is an
4321 // intermediate/temporary reference and because the current
4322 // concurrent copying collector keeps the from-space memory
4323 // intact/accessible until the end of the marking phase (the
4324 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004325 __ MaybeUnpoisonHeapReference(temp);
4326 // temp = temp->GetMethodAt(method_offset);
4327 __ movl(temp, Address(temp, method_offset));
4328 // call temp->GetEntryPoint();
4329 __ call(Address(
4330 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4331}
4332
Vladimir Marko58155012015-08-19 12:49:41 +00004333void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4334 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004335 size_t size =
4336 method_patches_.size() +
4337 relative_call_patches_.size() +
4338 pc_relative_dex_cache_patches_.size();
4339 linker_patches->reserve(size);
4340 // The label points to the end of the "movl" insn but the literal offset for method
4341 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4342 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004343 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004344 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004345 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4346 info.target_method.dex_file,
4347 info.target_method.dex_method_index));
4348 }
4349 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004350 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004351 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4352 info.target_method.dex_file,
4353 info.target_method.dex_method_index));
4354 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004355 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4356 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4357 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4358 &info.target_dex_file,
4359 GetMethodAddressOffset(),
4360 info.element_offset));
4361 }
Vladimir Marko58155012015-08-19 12:49:41 +00004362}
4363
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004364void CodeGeneratorX86::MarkGCCard(Register temp,
4365 Register card,
4366 Register object,
4367 Register value,
4368 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004369 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004370 if (value_can_be_null) {
4371 __ testl(value, value);
4372 __ j(kEqual, &is_null);
4373 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004374 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4375 __ movl(temp, object);
4376 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004377 __ movb(Address(temp, card, TIMES_1, 0),
4378 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004379 if (value_can_be_null) {
4380 __ Bind(&is_null);
4381 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382}
4383
Calin Juravle52c48962014-12-16 17:02:57 +00004384void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4385 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004386
4387 bool object_field_get_with_read_barrier =
4388 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004389 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004390 new (GetGraph()->GetArena()) LocationSummary(instruction,
4391 kEmitCompilerReadBarrier ?
4392 LocationSummary::kCallOnSlowPath :
4393 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004394 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004395
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004396 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4397 locations->SetOut(Location::RequiresFpuRegister());
4398 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004399 // The output overlaps in case of long: we don't want the low move
4400 // to overwrite the object's location. Likewise, in the case of
4401 // an object field get with read barriers enabled, we do not want
4402 // the move to overwrite the object's location, as we need it to emit
4403 // the read barrier.
4404 locations->SetOut(
4405 Location::RequiresRegister(),
4406 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4407 Location::kOutputOverlap :
4408 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004409 }
Calin Juravle52c48962014-12-16 17:02:57 +00004410
4411 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4412 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004413 // So we use an XMM register as a temp to achieve atomicity (first
4414 // load the temp into the XMM and then copy the XMM into the
4415 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004416 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004417 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4418 // We need a temporary register for the read barrier marking slow
4419 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4420 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004421 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004422}
4423
Calin Juravle52c48962014-12-16 17:02:57 +00004424void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4425 const FieldInfo& field_info) {
4426 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004427
Calin Juravle52c48962014-12-16 17:02:57 +00004428 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004429 Location base_loc = locations->InAt(0);
4430 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004431 Location out = locations->Out();
4432 bool is_volatile = field_info.IsVolatile();
4433 Primitive::Type field_type = field_info.GetFieldType();
4434 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4435
4436 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004437 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004438 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004439 break;
4440 }
4441
4442 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004443 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004444 break;
4445 }
4446
4447 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004448 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004449 break;
4450 }
4451
4452 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004453 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004454 break;
4455 }
4456
4457 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004458 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004459 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004460
4461 case Primitive::kPrimNot: {
4462 // /* HeapReference<Object> */ out = *(base + offset)
4463 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4464 Location temp_loc = locations->GetTemp(0);
4465 // Note that a potential implicit null check is handled in this
4466 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4467 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4468 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4469 if (is_volatile) {
4470 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4471 }
4472 } else {
4473 __ movl(out.AsRegister<Register>(), Address(base, offset));
4474 codegen_->MaybeRecordImplicitNullCheck(instruction);
4475 if (is_volatile) {
4476 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4477 }
4478 // If read barriers are enabled, emit read barriers other than
4479 // Baker's using a slow path (and also unpoison the loaded
4480 // reference, if heap poisoning is enabled).
4481 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4482 }
4483 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004484 }
4485
4486 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004487 if (is_volatile) {
4488 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4489 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004490 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004491 __ movd(out.AsRegisterPairLow<Register>(), temp);
4492 __ psrlq(temp, Immediate(32));
4493 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4494 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004495 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004496 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004497 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004498 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4499 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004500 break;
4501 }
4502
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004503 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004504 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004505 break;
4506 }
4507
4508 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004509 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004510 break;
4511 }
4512
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004513 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004514 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004515 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004516 }
Calin Juravle52c48962014-12-16 17:02:57 +00004517
Roland Levillain7c1559a2015-12-15 10:55:36 +00004518 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4519 // Potential implicit null checks, in the case of reference or
4520 // long fields, are handled in the previous switch statement.
4521 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004522 codegen_->MaybeRecordImplicitNullCheck(instruction);
4523 }
4524
Calin Juravle52c48962014-12-16 17:02:57 +00004525 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004526 if (field_type == Primitive::kPrimNot) {
4527 // Memory barriers, in the case of references, are also handled
4528 // in the previous switch statement.
4529 } else {
4530 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4531 }
Roland Levillain4d027112015-07-01 15:41:14 +01004532 }
Calin Juravle52c48962014-12-16 17:02:57 +00004533}
4534
4535void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4536 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4537
4538 LocationSummary* locations =
4539 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4540 locations->SetInAt(0, Location::RequiresRegister());
4541 bool is_volatile = field_info.IsVolatile();
4542 Primitive::Type field_type = field_info.GetFieldType();
4543 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4544 || (field_type == Primitive::kPrimByte);
4545
4546 // The register allocator does not support multiple
4547 // inputs that die at entry with one in a specific register.
4548 if (is_byte_type) {
4549 // Ensure the value is in a byte register.
4550 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004551 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004552 if (is_volatile && field_type == Primitive::kPrimDouble) {
4553 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4554 locations->SetInAt(1, Location::RequiresFpuRegister());
4555 } else {
4556 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4557 }
4558 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4559 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004560 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004561
Calin Juravle52c48962014-12-16 17:02:57 +00004562 // 64bits value can be atomically written to an address with movsd and an XMM register.
4563 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4564 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4565 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4566 // isolated cases when we need this it isn't worth adding the extra complexity.
4567 locations->AddTemp(Location::RequiresFpuRegister());
4568 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004569 } else {
4570 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4571
4572 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4573 // Temporary registers for the write barrier.
4574 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4575 // Ensure the card is in a byte register.
4576 locations->AddTemp(Location::RegisterLocation(ECX));
4577 }
Calin Juravle52c48962014-12-16 17:02:57 +00004578 }
4579}
4580
4581void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004582 const FieldInfo& field_info,
4583 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004584 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4585
4586 LocationSummary* locations = instruction->GetLocations();
4587 Register base = locations->InAt(0).AsRegister<Register>();
4588 Location value = locations->InAt(1);
4589 bool is_volatile = field_info.IsVolatile();
4590 Primitive::Type field_type = field_info.GetFieldType();
4591 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004592 bool needs_write_barrier =
4593 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004594
4595 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004596 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004597 }
4598
Mark Mendell81489372015-11-04 11:30:41 -05004599 bool maybe_record_implicit_null_check_done = false;
4600
Calin Juravle52c48962014-12-16 17:02:57 +00004601 switch (field_type) {
4602 case Primitive::kPrimBoolean:
4603 case Primitive::kPrimByte: {
4604 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4605 break;
4606 }
4607
4608 case Primitive::kPrimShort:
4609 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004610 if (value.IsConstant()) {
4611 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4612 __ movw(Address(base, offset), Immediate(v));
4613 } else {
4614 __ movw(Address(base, offset), value.AsRegister<Register>());
4615 }
Calin Juravle52c48962014-12-16 17:02:57 +00004616 break;
4617 }
4618
4619 case Primitive::kPrimInt:
4620 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004621 if (kPoisonHeapReferences && needs_write_barrier) {
4622 // Note that in the case where `value` is a null reference,
4623 // we do not enter this block, as the reference does not
4624 // need poisoning.
4625 DCHECK_EQ(field_type, Primitive::kPrimNot);
4626 Register temp = locations->GetTemp(0).AsRegister<Register>();
4627 __ movl(temp, value.AsRegister<Register>());
4628 __ PoisonHeapReference(temp);
4629 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004630 } else if (value.IsConstant()) {
4631 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4632 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004633 } else {
4634 __ movl(Address(base, offset), value.AsRegister<Register>());
4635 }
Calin Juravle52c48962014-12-16 17:02:57 +00004636 break;
4637 }
4638
4639 case Primitive::kPrimLong: {
4640 if (is_volatile) {
4641 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4642 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4643 __ movd(temp1, value.AsRegisterPairLow<Register>());
4644 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4645 __ punpckldq(temp1, temp2);
4646 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004647 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004648 } else if (value.IsConstant()) {
4649 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4650 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4651 codegen_->MaybeRecordImplicitNullCheck(instruction);
4652 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004653 } else {
4654 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004655 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004656 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4657 }
Mark Mendell81489372015-11-04 11:30:41 -05004658 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004659 break;
4660 }
4661
4662 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004663 if (value.IsConstant()) {
4664 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4665 __ movl(Address(base, offset), Immediate(v));
4666 } else {
4667 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4668 }
Calin Juravle52c48962014-12-16 17:02:57 +00004669 break;
4670 }
4671
4672 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004673 if (value.IsConstant()) {
4674 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4675 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4676 codegen_->MaybeRecordImplicitNullCheck(instruction);
4677 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4678 maybe_record_implicit_null_check_done = true;
4679 } else {
4680 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4681 }
Calin Juravle52c48962014-12-16 17:02:57 +00004682 break;
4683 }
4684
4685 case Primitive::kPrimVoid:
4686 LOG(FATAL) << "Unreachable type " << field_type;
4687 UNREACHABLE();
4688 }
4689
Mark Mendell81489372015-11-04 11:30:41 -05004690 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004691 codegen_->MaybeRecordImplicitNullCheck(instruction);
4692 }
4693
Roland Levillain4d027112015-07-01 15:41:14 +01004694 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004695 Register temp = locations->GetTemp(0).AsRegister<Register>();
4696 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004697 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004698 }
4699
Calin Juravle52c48962014-12-16 17:02:57 +00004700 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004701 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004702 }
4703}
4704
4705void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4706 HandleFieldGet(instruction, instruction->GetFieldInfo());
4707}
4708
4709void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4710 HandleFieldGet(instruction, instruction->GetFieldInfo());
4711}
4712
4713void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4714 HandleFieldSet(instruction, instruction->GetFieldInfo());
4715}
4716
4717void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004718 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004719}
4720
4721void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4722 HandleFieldSet(instruction, instruction->GetFieldInfo());
4723}
4724
4725void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004726 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004727}
4728
4729void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4730 HandleFieldGet(instruction, instruction->GetFieldInfo());
4731}
4732
4733void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4734 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004735}
4736
Calin Juravlee460d1d2015-09-29 04:52:17 +01004737void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4738 HUnresolvedInstanceFieldGet* instruction) {
4739 FieldAccessCallingConventionX86 calling_convention;
4740 codegen_->CreateUnresolvedFieldLocationSummary(
4741 instruction, instruction->GetFieldType(), calling_convention);
4742}
4743
4744void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4745 HUnresolvedInstanceFieldGet* instruction) {
4746 FieldAccessCallingConventionX86 calling_convention;
4747 codegen_->GenerateUnresolvedFieldAccess(instruction,
4748 instruction->GetFieldType(),
4749 instruction->GetFieldIndex(),
4750 instruction->GetDexPc(),
4751 calling_convention);
4752}
4753
4754void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4755 HUnresolvedInstanceFieldSet* instruction) {
4756 FieldAccessCallingConventionX86 calling_convention;
4757 codegen_->CreateUnresolvedFieldLocationSummary(
4758 instruction, instruction->GetFieldType(), calling_convention);
4759}
4760
4761void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4762 HUnresolvedInstanceFieldSet* instruction) {
4763 FieldAccessCallingConventionX86 calling_convention;
4764 codegen_->GenerateUnresolvedFieldAccess(instruction,
4765 instruction->GetFieldType(),
4766 instruction->GetFieldIndex(),
4767 instruction->GetDexPc(),
4768 calling_convention);
4769}
4770
4771void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4772 HUnresolvedStaticFieldGet* instruction) {
4773 FieldAccessCallingConventionX86 calling_convention;
4774 codegen_->CreateUnresolvedFieldLocationSummary(
4775 instruction, instruction->GetFieldType(), calling_convention);
4776}
4777
4778void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4779 HUnresolvedStaticFieldGet* instruction) {
4780 FieldAccessCallingConventionX86 calling_convention;
4781 codegen_->GenerateUnresolvedFieldAccess(instruction,
4782 instruction->GetFieldType(),
4783 instruction->GetFieldIndex(),
4784 instruction->GetDexPc(),
4785 calling_convention);
4786}
4787
4788void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4789 HUnresolvedStaticFieldSet* instruction) {
4790 FieldAccessCallingConventionX86 calling_convention;
4791 codegen_->CreateUnresolvedFieldLocationSummary(
4792 instruction, instruction->GetFieldType(), calling_convention);
4793}
4794
4795void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4796 HUnresolvedStaticFieldSet* instruction) {
4797 FieldAccessCallingConventionX86 calling_convention;
4798 codegen_->GenerateUnresolvedFieldAccess(instruction,
4799 instruction->GetFieldType(),
4800 instruction->GetFieldIndex(),
4801 instruction->GetDexPc(),
4802 calling_convention);
4803}
4804
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004805void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004806 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4807 ? LocationSummary::kCallOnSlowPath
4808 : LocationSummary::kNoCall;
4809 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4810 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004811 ? Location::RequiresRegister()
4812 : Location::Any();
4813 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004814 if (instruction->HasUses()) {
4815 locations->SetOut(Location::SameAsFirstInput());
4816 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004817}
4818
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004819void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004820 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4821 return;
4822 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004823 LocationSummary* locations = instruction->GetLocations();
4824 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004825
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004826 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4827 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4828}
4829
4830void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004831 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004832 codegen_->AddSlowPath(slow_path);
4833
4834 LocationSummary* locations = instruction->GetLocations();
4835 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004836
4837 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004838 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004839 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004840 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004841 } else {
4842 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004843 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004844 __ jmp(slow_path->GetEntryLabel());
4845 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004846 }
4847 __ j(kEqual, slow_path->GetEntryLabel());
4848}
4849
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004850void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004851 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004852 GenerateImplicitNullCheck(instruction);
4853 } else {
4854 GenerateExplicitNullCheck(instruction);
4855 }
4856}
4857
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004858void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004859 bool object_array_get_with_read_barrier =
4860 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004861 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004862 new (GetGraph()->GetArena()) LocationSummary(instruction,
4863 object_array_get_with_read_barrier ?
4864 LocationSummary::kCallOnSlowPath :
4865 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004866 locations->SetInAt(0, Location::RequiresRegister());
4867 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004868 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4869 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4870 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004871 // The output overlaps in case of long: we don't want the low move
4872 // to overwrite the array's location. Likewise, in the case of an
4873 // object array get with read barriers enabled, we do not want the
4874 // move to overwrite the array's location, as we need it to emit
4875 // the read barrier.
4876 locations->SetOut(
4877 Location::RequiresRegister(),
4878 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4879 Location::kOutputOverlap :
4880 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004881 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004882 // We need a temporary register for the read barrier marking slow
4883 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4884 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4885 locations->AddTemp(Location::RequiresRegister());
4886 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887}
4888
4889void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4890 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004891 Location obj_loc = locations->InAt(0);
4892 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004893 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004894 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004895
Calin Juravle77520bc2015-01-12 18:45:46 +00004896 Primitive::Type type = instruction->GetType();
4897 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004898 case Primitive::kPrimBoolean: {
4899 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004900 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004901 if (index.IsConstant()) {
4902 __ movzxb(out, Address(obj,
4903 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4904 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004905 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004906 }
4907 break;
4908 }
4909
4910 case Primitive::kPrimByte: {
4911 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004912 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004913 if (index.IsConstant()) {
4914 __ movsxb(out, Address(obj,
4915 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4916 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004917 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004918 }
4919 break;
4920 }
4921
4922 case Primitive::kPrimShort: {
4923 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004924 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004925 if (index.IsConstant()) {
4926 __ movsxw(out, Address(obj,
4927 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4928 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004929 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004930 }
4931 break;
4932 }
4933
4934 case Primitive::kPrimChar: {
4935 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004936 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004937 if (index.IsConstant()) {
4938 __ movzxw(out, Address(obj,
4939 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4940 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004941 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004942 }
4943 break;
4944 }
4945
Roland Levillain7c1559a2015-12-15 10:55:36 +00004946 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004947 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004948 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004949 if (index.IsConstant()) {
4950 __ movl(out, Address(obj,
4951 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4952 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004953 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004954 }
4955 break;
4956 }
4957
Roland Levillain7c1559a2015-12-15 10:55:36 +00004958 case Primitive::kPrimNot: {
4959 static_assert(
4960 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4961 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4962 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4963 // /* HeapReference<Object> */ out =
4964 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4965 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4966 Location temp = locations->GetTemp(0);
4967 // Note that a potential implicit null check is handled in this
4968 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4969 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4970 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4971 } else {
4972 Register out = out_loc.AsRegister<Register>();
4973 if (index.IsConstant()) {
4974 uint32_t offset =
4975 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4976 __ movl(out, Address(obj, offset));
4977 codegen_->MaybeRecordImplicitNullCheck(instruction);
4978 // If read barriers are enabled, emit read barriers other than
4979 // Baker's using a slow path (and also unpoison the loaded
4980 // reference, if heap poisoning is enabled).
4981 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4982 } else {
4983 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4984 codegen_->MaybeRecordImplicitNullCheck(instruction);
4985 // If read barriers are enabled, emit read barriers other than
4986 // Baker's using a slow path (and also unpoison the loaded
4987 // reference, if heap poisoning is enabled).
4988 codegen_->MaybeGenerateReadBarrierSlow(
4989 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4990 }
4991 }
4992 break;
4993 }
4994
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995 case Primitive::kPrimLong: {
4996 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004997 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004998 if (index.IsConstant()) {
4999 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005000 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005001 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005002 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005004 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005005 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005006 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005007 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005008 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005009 }
5010 break;
5011 }
5012
Mark Mendell7c8d0092015-01-26 11:21:33 -05005013 case Primitive::kPrimFloat: {
5014 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005015 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005016 if (index.IsConstant()) {
5017 __ movss(out, Address(obj,
5018 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5019 } else {
5020 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5021 }
5022 break;
5023 }
5024
5025 case Primitive::kPrimDouble: {
5026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005027 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005028 if (index.IsConstant()) {
5029 __ movsd(out, Address(obj,
5030 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5031 } else {
5032 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5033 }
5034 break;
5035 }
5036
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005037 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005038 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005039 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005040 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005041
Roland Levillain7c1559a2015-12-15 10:55:36 +00005042 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5043 // Potential implicit null checks, in the case of reference or
5044 // long arrays, are handled in the previous switch statement.
5045 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005046 codegen_->MaybeRecordImplicitNullCheck(instruction);
5047 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048}
5049
5050void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05005051 // This location builder might end up asking to up to four registers, which is
5052 // not currently possible for baseline. The situation in which we need four
5053 // registers cannot be met by baseline though, because it has not run any
5054 // optimization.
5055
Nicolas Geoffray39468442014-09-02 15:17:15 +01005056 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005057
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005058 bool needs_write_barrier =
5059 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005060 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5061 bool object_array_set_with_read_barrier =
5062 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005063
Nicolas Geoffray39468442014-09-02 15:17:15 +01005064 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5065 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005066 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5067 LocationSummary::kCallOnSlowPath :
5068 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005069
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005070 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5071 || (value_type == Primitive::kPrimByte);
5072 // We need the inputs to be different than the output in case of long operation.
5073 // In case of a byte operation, the register allocator does not support multiple
5074 // inputs that die at entry with one in a specific register.
5075 locations->SetInAt(0, Location::RequiresRegister());
5076 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5077 if (is_byte_type) {
5078 // Ensure the value is in a byte register.
5079 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5080 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005081 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005082 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005083 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5084 }
5085 if (needs_write_barrier) {
5086 // Temporary registers for the write barrier.
5087 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5088 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005089 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091}
5092
5093void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5094 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005095 Location array_loc = locations->InAt(0);
5096 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005098 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005099 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005100 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5101 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5102 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005103 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005104 bool needs_write_barrier =
5105 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106
5107 switch (value_type) {
5108 case Primitive::kPrimBoolean:
5109 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005110 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5111 Address address = index.IsConstant()
5112 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5113 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5114 if (value.IsRegister()) {
5115 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005117 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005118 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005119 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120 break;
5121 }
5122
5123 case Primitive::kPrimShort:
5124 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5126 Address address = index.IsConstant()
5127 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5128 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5129 if (value.IsRegister()) {
5130 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005131 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005132 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005133 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005134 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 break;
5136 }
5137
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005138 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005139 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5140 Address address = index.IsConstant()
5141 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5142 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005143
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005144 if (!value.IsRegister()) {
5145 // Just setting null.
5146 DCHECK(instruction->InputAt(2)->IsNullConstant());
5147 DCHECK(value.IsConstant()) << value;
5148 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005149 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005150 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005151 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005152 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005154
5155 DCHECK(needs_write_barrier);
5156 Register register_value = value.AsRegister<Register>();
5157 NearLabel done, not_null, do_put;
5158 SlowPathCode* slow_path = nullptr;
5159 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005160 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005161 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5162 codegen_->AddSlowPath(slow_path);
5163 if (instruction->GetValueCanBeNull()) {
5164 __ testl(register_value, register_value);
5165 __ j(kNotEqual, &not_null);
5166 __ movl(address, Immediate(0));
5167 codegen_->MaybeRecordImplicitNullCheck(instruction);
5168 __ jmp(&done);
5169 __ Bind(&not_null);
5170 }
5171
Roland Levillain0d5a2812015-11-13 10:07:31 +00005172 if (kEmitCompilerReadBarrier) {
5173 // When read barriers are enabled, the type checking
5174 // instrumentation requires two read barriers:
5175 //
5176 // __ movl(temp2, temp);
5177 // // /* HeapReference<Class> */ temp = temp->component_type_
5178 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005179 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005180 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5181 //
5182 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5183 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005184 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005185 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5186 //
5187 // __ cmpl(temp, temp2);
5188 //
5189 // However, the second read barrier may trash `temp`, as it
5190 // is a temporary register, and as such would not be saved
5191 // along with live registers before calling the runtime (nor
5192 // restored afterwards). So in this case, we bail out and
5193 // delegate the work to the array set slow path.
5194 //
5195 // TODO: Extend the register allocator to support a new
5196 // "(locally) live temp" location so as to avoid always
5197 // going into the slow path when read barriers are enabled.
5198 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005199 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005200 // /* HeapReference<Class> */ temp = array->klass_
5201 __ movl(temp, Address(array, class_offset));
5202 codegen_->MaybeRecordImplicitNullCheck(instruction);
5203 __ MaybeUnpoisonHeapReference(temp);
5204
5205 // /* HeapReference<Class> */ temp = temp->component_type_
5206 __ movl(temp, Address(temp, component_offset));
5207 // If heap poisoning is enabled, no need to unpoison `temp`
5208 // nor the object reference in `register_value->klass`, as
5209 // we are comparing two poisoned references.
5210 __ cmpl(temp, Address(register_value, class_offset));
5211
5212 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5213 __ j(kEqual, &do_put);
5214 // If heap poisoning is enabled, the `temp` reference has
5215 // not been unpoisoned yet; unpoison it now.
5216 __ MaybeUnpoisonHeapReference(temp);
5217
5218 // /* HeapReference<Class> */ temp = temp->super_class_
5219 __ movl(temp, Address(temp, super_offset));
5220 // If heap poisoning is enabled, no need to unpoison
5221 // `temp`, as we are comparing against null below.
5222 __ testl(temp, temp);
5223 __ j(kNotEqual, slow_path->GetEntryLabel());
5224 __ Bind(&do_put);
5225 } else {
5226 __ j(kNotEqual, slow_path->GetEntryLabel());
5227 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005228 }
5229 }
5230
5231 if (kPoisonHeapReferences) {
5232 __ movl(temp, register_value);
5233 __ PoisonHeapReference(temp);
5234 __ movl(address, temp);
5235 } else {
5236 __ movl(address, register_value);
5237 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005238 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005239 codegen_->MaybeRecordImplicitNullCheck(instruction);
5240 }
5241
5242 Register card = locations->GetTemp(1).AsRegister<Register>();
5243 codegen_->MarkGCCard(
5244 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5245 __ Bind(&done);
5246
5247 if (slow_path != nullptr) {
5248 __ Bind(slow_path->GetExitLabel());
5249 }
5250
5251 break;
5252 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005253
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005254 case Primitive::kPrimInt: {
5255 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5256 Address address = index.IsConstant()
5257 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5258 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5259 if (value.IsRegister()) {
5260 __ movl(address, value.AsRegister<Register>());
5261 } else {
5262 DCHECK(value.IsConstant()) << value;
5263 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5264 __ movl(address, Immediate(v));
5265 }
5266 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 break;
5268 }
5269
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 case Primitive::kPrimLong: {
5271 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005272 if (index.IsConstant()) {
5273 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005274 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005276 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005277 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005278 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005279 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005280 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005281 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005284 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005285 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005286 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005288 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005289 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005291 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005292 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005293 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005294 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005296 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005297 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005298 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005299 Immediate(High32Bits(val)));
5300 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 }
5302 break;
5303 }
5304
Mark Mendell7c8d0092015-01-26 11:21:33 -05005305 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005306 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5307 Address address = index.IsConstant()
5308 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5309 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005310 if (value.IsFpuRegister()) {
5311 __ movss(address, value.AsFpuRegister<XmmRegister>());
5312 } else {
5313 DCHECK(value.IsConstant());
5314 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5315 __ movl(address, Immediate(v));
5316 }
5317 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005318 break;
5319 }
5320
5321 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005322 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5323 Address address = index.IsConstant()
5324 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5325 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005326 if (value.IsFpuRegister()) {
5327 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5328 } else {
5329 DCHECK(value.IsConstant());
5330 Address address_hi = index.IsConstant() ?
5331 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5332 offset + kX86WordSize) :
5333 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5334 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5335 __ movl(address, Immediate(Low32Bits(v)));
5336 codegen_->MaybeRecordImplicitNullCheck(instruction);
5337 __ movl(address_hi, Immediate(High32Bits(v)));
5338 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005339 break;
5340 }
5341
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342 case Primitive::kPrimVoid:
5343 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005344 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005345 }
5346}
5347
5348void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5349 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005350 locations->SetInAt(0, Location::RequiresRegister());
5351 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005352}
5353
5354void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5355 LocationSummary* locations = instruction->GetLocations();
5356 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005357 Register obj = locations->InAt(0).AsRegister<Register>();
5358 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005359 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005360 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361}
5362
5363void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005364 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5365 ? LocationSummary::kCallOnSlowPath
5366 : LocationSummary::kNoCall;
5367 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005368 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005369 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005370 if (instruction->HasUses()) {
5371 locations->SetOut(Location::SameAsFirstInput());
5372 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005373}
5374
5375void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5376 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005377 Location index_loc = locations->InAt(0);
5378 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005379 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005380 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005381
Mark Mendell99dbd682015-04-22 16:18:52 -04005382 if (length_loc.IsConstant()) {
5383 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5384 if (index_loc.IsConstant()) {
5385 // BCE will remove the bounds check if we are guarenteed to pass.
5386 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5387 if (index < 0 || index >= length) {
5388 codegen_->AddSlowPath(slow_path);
5389 __ jmp(slow_path->GetEntryLabel());
5390 } else {
5391 // Some optimization after BCE may have generated this, and we should not
5392 // generate a bounds check if it is a valid range.
5393 }
5394 return;
5395 }
5396
5397 // We have to reverse the jump condition because the length is the constant.
5398 Register index_reg = index_loc.AsRegister<Register>();
5399 __ cmpl(index_reg, Immediate(length));
5400 codegen_->AddSlowPath(slow_path);
5401 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005402 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005403 Register length = length_loc.AsRegister<Register>();
5404 if (index_loc.IsConstant()) {
5405 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5406 __ cmpl(length, Immediate(value));
5407 } else {
5408 __ cmpl(length, index_loc.AsRegister<Register>());
5409 }
5410 codegen_->AddSlowPath(slow_path);
5411 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005412 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413}
5414
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005415void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5416 temp->SetLocations(nullptr);
5417}
5418
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005419void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005420 // Nothing to do, this is driven by the code generator.
5421}
5422
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005423void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005424 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005425}
5426
5427void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005428 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5429}
5430
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005431void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5432 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5433}
5434
5435void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005436 HBasicBlock* block = instruction->GetBlock();
5437 if (block->GetLoopInformation() != nullptr) {
5438 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5439 // The back edge will generate the suspend check.
5440 return;
5441 }
5442 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5443 // The goto will generate the suspend check.
5444 return;
5445 }
5446 GenerateSuspendCheck(instruction, nullptr);
5447}
5448
5449void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5450 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005451 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005452 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5453 if (slow_path == nullptr) {
5454 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5455 instruction->SetSlowPath(slow_path);
5456 codegen_->AddSlowPath(slow_path);
5457 if (successor != nullptr) {
5458 DCHECK(successor->IsLoopHeader());
5459 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5460 }
5461 } else {
5462 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5463 }
5464
Roland Levillain7c1559a2015-12-15 10:55:36 +00005465 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5466 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005467 if (successor == nullptr) {
5468 __ j(kNotEqual, slow_path->GetEntryLabel());
5469 __ Bind(slow_path->GetReturnLabel());
5470 } else {
5471 __ j(kEqual, codegen_->GetLabelOf(successor));
5472 __ jmp(slow_path->GetEntryLabel());
5473 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005474}
5475
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005476X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5477 return codegen_->GetAssembler();
5478}
5479
Mark Mendell7c8d0092015-01-26 11:21:33 -05005480void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005481 ScratchRegisterScope ensure_scratch(
5482 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5483 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5484 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5485 __ movl(temp_reg, Address(ESP, src + stack_offset));
5486 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005487}
5488
5489void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005490 ScratchRegisterScope ensure_scratch(
5491 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5492 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5493 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5494 __ movl(temp_reg, Address(ESP, src + stack_offset));
5495 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5496 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5497 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005498}
5499
5500void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005501 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005502 Location source = move->GetSource();
5503 Location destination = move->GetDestination();
5504
5505 if (source.IsRegister()) {
5506 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005507 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005508 } else {
5509 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005510 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005511 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005512 } else if (source.IsFpuRegister()) {
5513 if (destination.IsFpuRegister()) {
5514 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5515 } else if (destination.IsStackSlot()) {
5516 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5517 } else {
5518 DCHECK(destination.IsDoubleStackSlot());
5519 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5520 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005521 } else if (source.IsStackSlot()) {
5522 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005523 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005524 } else if (destination.IsFpuRegister()) {
5525 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005526 } else {
5527 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005528 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5529 }
5530 } else if (source.IsDoubleStackSlot()) {
5531 if (destination.IsFpuRegister()) {
5532 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5533 } else {
5534 DCHECK(destination.IsDoubleStackSlot()) << destination;
5535 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005536 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005537 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005538 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005539 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005540 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005541 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005542 if (value == 0) {
5543 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5544 } else {
5545 __ movl(destination.AsRegister<Register>(), Immediate(value));
5546 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005547 } else {
5548 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005549 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005550 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005551 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005552 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005553 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005554 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005555 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005556 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5557 if (value == 0) {
5558 // Easy handling of 0.0.
5559 __ xorps(dest, dest);
5560 } else {
5561 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005562 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5563 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5564 __ movl(temp, Immediate(value));
5565 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005566 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005567 } else {
5568 DCHECK(destination.IsStackSlot()) << destination;
5569 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5570 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005571 } else if (constant->IsLongConstant()) {
5572 int64_t value = constant->AsLongConstant()->GetValue();
5573 int32_t low_value = Low32Bits(value);
5574 int32_t high_value = High32Bits(value);
5575 Immediate low(low_value);
5576 Immediate high(high_value);
5577 if (destination.IsDoubleStackSlot()) {
5578 __ movl(Address(ESP, destination.GetStackIndex()), low);
5579 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5580 } else {
5581 __ movl(destination.AsRegisterPairLow<Register>(), low);
5582 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5583 }
5584 } else {
5585 DCHECK(constant->IsDoubleConstant());
5586 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005587 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005588 int32_t low_value = Low32Bits(value);
5589 int32_t high_value = High32Bits(value);
5590 Immediate low(low_value);
5591 Immediate high(high_value);
5592 if (destination.IsFpuRegister()) {
5593 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5594 if (value == 0) {
5595 // Easy handling of 0.0.
5596 __ xorpd(dest, dest);
5597 } else {
5598 __ pushl(high);
5599 __ pushl(low);
5600 __ movsd(dest, Address(ESP, 0));
5601 __ addl(ESP, Immediate(8));
5602 }
5603 } else {
5604 DCHECK(destination.IsDoubleStackSlot()) << destination;
5605 __ movl(Address(ESP, destination.GetStackIndex()), low);
5606 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5607 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005608 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005609 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005610 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005611 }
5612}
5613
Mark Mendella5c19ce2015-04-01 12:51:05 -04005614void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005615 Register suggested_scratch = reg == EAX ? EBX : EAX;
5616 ScratchRegisterScope ensure_scratch(
5617 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5618
5619 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5620 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5621 __ movl(Address(ESP, mem + stack_offset), reg);
5622 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005623}
5624
Mark Mendell7c8d0092015-01-26 11:21:33 -05005625void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005626 ScratchRegisterScope ensure_scratch(
5627 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5628
5629 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5630 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5631 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5632 __ movss(Address(ESP, mem + stack_offset), reg);
5633 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005634}
5635
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005636void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005637 ScratchRegisterScope ensure_scratch1(
5638 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005639
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005640 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5641 ScratchRegisterScope ensure_scratch2(
5642 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005643
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005644 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5645 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5646 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5647 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5648 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5649 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005650}
5651
5652void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005653 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005654 Location source = move->GetSource();
5655 Location destination = move->GetDestination();
5656
5657 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005658 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5659 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5660 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5661 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5662 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005663 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005664 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005665 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005666 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005667 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5668 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005669 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5670 // Use XOR Swap algorithm to avoid a temporary.
5671 DCHECK_NE(source.reg(), destination.reg());
5672 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5673 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5674 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5675 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5676 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5677 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5678 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005679 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5680 // Take advantage of the 16 bytes in the XMM register.
5681 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5682 Address stack(ESP, destination.GetStackIndex());
5683 // Load the double into the high doubleword.
5684 __ movhpd(reg, stack);
5685
5686 // Store the low double into the destination.
5687 __ movsd(stack, reg);
5688
5689 // Move the high double to the low double.
5690 __ psrldq(reg, Immediate(8));
5691 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5692 // Take advantage of the 16 bytes in the XMM register.
5693 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5694 Address stack(ESP, source.GetStackIndex());
5695 // Load the double into the high doubleword.
5696 __ movhpd(reg, stack);
5697
5698 // Store the low double into the destination.
5699 __ movsd(stack, reg);
5700
5701 // Move the high double to the low double.
5702 __ psrldq(reg, Immediate(8));
5703 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5704 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5705 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005706 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005707 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005708 }
5709}
5710
5711void ParallelMoveResolverX86::SpillScratch(int reg) {
5712 __ pushl(static_cast<Register>(reg));
5713}
5714
5715void ParallelMoveResolverX86::RestoreScratch(int reg) {
5716 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005717}
5718
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005719void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005720 InvokeRuntimeCallingConvention calling_convention;
5721 CodeGenerator::CreateLoadClassLocationSummary(
5722 cls,
5723 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005724 Location::RegisterLocation(EAX),
5725 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005726}
5727
5728void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005729 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005730 if (cls->NeedsAccessCheck()) {
5731 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5732 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5733 cls,
5734 cls->GetDexPc(),
5735 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005736 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005737 return;
5738 }
5739
Roland Levillain0d5a2812015-11-13 10:07:31 +00005740 Location out_loc = locations->Out();
5741 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005742 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005743
Calin Juravle580b6092015-10-06 17:35:58 +01005744 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005745 DCHECK(!cls->CanCallRuntime());
5746 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005747 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5748 GenerateGcRootFieldLoad(
5749 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005750 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005751 // /* GcRoot<mirror::Class>[] */ out =
5752 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5753 __ movl(out, Address(current_method,
5754 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005755 // /* GcRoot<mirror::Class> */ out = out[type_index]
5756 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005757
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005758 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5759 DCHECK(cls->CanCallRuntime());
5760 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5761 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5762 codegen_->AddSlowPath(slow_path);
5763
5764 if (!cls->IsInDexCache()) {
5765 __ testl(out, out);
5766 __ j(kEqual, slow_path->GetEntryLabel());
5767 }
5768
5769 if (cls->MustGenerateClinitCheck()) {
5770 GenerateClassInitializationCheck(slow_path, out);
5771 } else {
5772 __ Bind(slow_path->GetExitLabel());
5773 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005774 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005775 }
5776}
5777
5778void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5779 LocationSummary* locations =
5780 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5781 locations->SetInAt(0, Location::RequiresRegister());
5782 if (check->HasUses()) {
5783 locations->SetOut(Location::SameAsFirstInput());
5784 }
5785}
5786
5787void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005788 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005789 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005790 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005791 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005792 GenerateClassInitializationCheck(slow_path,
5793 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005794}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005795
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005796void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005797 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005798 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5799 Immediate(mirror::Class::kStatusInitialized));
5800 __ j(kLess, slow_path->GetEntryLabel());
5801 __ Bind(slow_path->GetExitLabel());
5802 // No need for memory fence, thanks to the X86 memory model.
5803}
5804
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005805void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005806 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5807 ? LocationSummary::kCallOnSlowPath
5808 : LocationSummary::kNoCall;
5809 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005810 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005811 locations->SetOut(Location::RequiresRegister());
5812}
5813
5814void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005815 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005816 Location out_loc = locations->Out();
5817 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005818 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005819
Roland Levillain7c1559a2015-12-15 10:55:36 +00005820 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5821 GenerateGcRootFieldLoad(
5822 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005824 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005825 // /* GcRoot<mirror::String> */ out = out[string_index]
5826 GenerateGcRootFieldLoad(
5827 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005828
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005829 if (!load->IsInDexCache()) {
5830 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5831 codegen_->AddSlowPath(slow_path);
5832 __ testl(out, out);
5833 __ j(kEqual, slow_path->GetEntryLabel());
5834 __ Bind(slow_path->GetExitLabel());
5835 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005836}
5837
David Brazdilcb1c0552015-08-04 16:22:25 +01005838static Address GetExceptionTlsAddress() {
5839 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5840}
5841
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005842void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5843 LocationSummary* locations =
5844 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5845 locations->SetOut(Location::RequiresRegister());
5846}
5847
5848void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005849 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5850}
5851
5852void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5853 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5854}
5855
5856void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5857 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005858}
5859
5860void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5861 LocationSummary* locations =
5862 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5863 InvokeRuntimeCallingConvention calling_convention;
5864 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5865}
5866
5867void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005868 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5869 instruction,
5870 instruction->GetDexPc(),
5871 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005872 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005873}
5874
Roland Levillain7c1559a2015-12-15 10:55:36 +00005875static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5876 return kEmitCompilerReadBarrier &&
5877 (kUseBakerReadBarrier ||
5878 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5879 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5880 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5881}
5882
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005883void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005884 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005885 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5886 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005887 case TypeCheckKind::kExactCheck:
5888 case TypeCheckKind::kAbstractClassCheck:
5889 case TypeCheckKind::kClassHierarchyCheck:
5890 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891 call_kind =
5892 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005893 break;
5894 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005895 case TypeCheckKind::kUnresolvedCheck:
5896 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005897 call_kind = LocationSummary::kCallOnSlowPath;
5898 break;
5899 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005900
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005901 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005902 locations->SetInAt(0, Location::RequiresRegister());
5903 locations->SetInAt(1, Location::Any());
5904 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5905 locations->SetOut(Location::RequiresRegister());
5906 // When read barriers are enabled, we need a temporary register for
5907 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005908 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005909 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005911}
5912
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005913void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005914 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005915 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005916 Location obj_loc = locations->InAt(0);
5917 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005918 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919 Location out_loc = locations->Out();
5920 Register out = out_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005921 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5922 locations->GetTemp(0) :
5923 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005924 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005925 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5926 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5927 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005928 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005930
5931 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005932 // Avoid null check if we know obj is not null.
5933 if (instruction->MustDoNullCheck()) {
5934 __ testl(obj, obj);
5935 __ j(kEqual, &zero);
5936 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937
Roland Levillain0d5a2812015-11-13 10:07:31 +00005938 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005939 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940
Roland Levillain7c1559a2015-12-15 10:55:36 +00005941 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005942 case TypeCheckKind::kExactCheck: {
5943 if (cls.IsRegister()) {
5944 __ cmpl(out, cls.AsRegister<Register>());
5945 } else {
5946 DCHECK(cls.IsStackSlot()) << cls;
5947 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5948 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005949
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 // Classes must be equal for the instanceof to succeed.
5951 __ j(kNotEqual, &zero);
5952 __ movl(out, Immediate(1));
5953 __ jmp(&done);
5954 break;
5955 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005956
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005957 case TypeCheckKind::kAbstractClassCheck: {
5958 // If the class is abstract, we eagerly fetch the super class of the
5959 // object to avoid doing a comparison we know will fail.
5960 NearLabel loop;
5961 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005962 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005963 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005964 __ testl(out, out);
5965 // If `out` is null, we use it for the result, and jump to `done`.
5966 __ j(kEqual, &done);
5967 if (cls.IsRegister()) {
5968 __ cmpl(out, cls.AsRegister<Register>());
5969 } else {
5970 DCHECK(cls.IsStackSlot()) << cls;
5971 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5972 }
5973 __ j(kNotEqual, &loop);
5974 __ movl(out, Immediate(1));
5975 if (zero.IsLinked()) {
5976 __ jmp(&done);
5977 }
5978 break;
5979 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005980
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005981 case TypeCheckKind::kClassHierarchyCheck: {
5982 // Walk over the class hierarchy to find a match.
5983 NearLabel loop, success;
5984 __ Bind(&loop);
5985 if (cls.IsRegister()) {
5986 __ cmpl(out, cls.AsRegister<Register>());
5987 } else {
5988 DCHECK(cls.IsStackSlot()) << cls;
5989 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5990 }
5991 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005992 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005993 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005994 __ testl(out, out);
5995 __ j(kNotEqual, &loop);
5996 // If `out` is null, we use it for the result, and jump to `done`.
5997 __ jmp(&done);
5998 __ Bind(&success);
5999 __ movl(out, Immediate(1));
6000 if (zero.IsLinked()) {
6001 __ jmp(&done);
6002 }
6003 break;
6004 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006005
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006007 // Do an exact check.
6008 NearLabel exact_check;
6009 if (cls.IsRegister()) {
6010 __ cmpl(out, cls.AsRegister<Register>());
6011 } else {
6012 DCHECK(cls.IsStackSlot()) << cls;
6013 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6014 }
6015 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006016 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006017 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006018 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006019 __ testl(out, out);
6020 // If `out` is null, we use it for the result, and jump to `done`.
6021 __ j(kEqual, &done);
6022 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6023 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006024 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006025 __ movl(out, Immediate(1));
6026 __ jmp(&done);
6027 break;
6028 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006029
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006030 case TypeCheckKind::kArrayCheck: {
6031 if (cls.IsRegister()) {
6032 __ cmpl(out, cls.AsRegister<Register>());
6033 } else {
6034 DCHECK(cls.IsStackSlot()) << cls;
6035 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6036 }
6037 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006038 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6039 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006040 codegen_->AddSlowPath(slow_path);
6041 __ j(kNotEqual, slow_path->GetEntryLabel());
6042 __ movl(out, Immediate(1));
6043 if (zero.IsLinked()) {
6044 __ jmp(&done);
6045 }
6046 break;
6047 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006048
Calin Juravle98893e12015-10-02 21:05:03 +01006049 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006050 case TypeCheckKind::kInterfaceCheck: {
6051 // Note that we indeed only call on slow path, but we always go
6052 // into the slow path for the unresolved & interface check
6053 // cases.
6054 //
6055 // We cannot directly call the InstanceofNonTrivial runtime
6056 // entry point without resorting to a type checking slow path
6057 // here (i.e. by calling InvokeRuntime directly), as it would
6058 // require to assign fixed registers for the inputs of this
6059 // HInstanceOf instruction (following the runtime calling
6060 // convention), which might be cluttered by the potential first
6061 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006062 //
6063 // TODO: Introduce a new runtime entry point taking the object
6064 // to test (instead of its class) as argument, and let it deal
6065 // with the read barrier issues. This will let us refactor this
6066 // case of the `switch` code as it was previously (with a direct
6067 // call to the runtime not using a type checking slow path).
6068 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006069 DCHECK(locations->OnlyCallsOnSlowPath());
6070 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6071 /* is_fatal */ false);
6072 codegen_->AddSlowPath(slow_path);
6073 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006074 if (zero.IsLinked()) {
6075 __ jmp(&done);
6076 }
6077 break;
6078 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006079 }
6080
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006082 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006083 __ xorl(out, out);
6084 }
6085
6086 if (done.IsLinked()) {
6087 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006088 }
6089
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006090 if (slow_path != nullptr) {
6091 __ Bind(slow_path->GetExitLabel());
6092 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006093}
6094
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006095void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006096 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6097 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006098 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6099 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100 case TypeCheckKind::kExactCheck:
6101 case TypeCheckKind::kAbstractClassCheck:
6102 case TypeCheckKind::kClassHierarchyCheck:
6103 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006104 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6105 LocationSummary::kCallOnSlowPath :
6106 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006107 break;
6108 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006109 case TypeCheckKind::kUnresolvedCheck:
6110 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 call_kind = LocationSummary::kCallOnSlowPath;
6112 break;
6113 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006114 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6115 locations->SetInAt(0, Location::RequiresRegister());
6116 locations->SetInAt(1, Location::Any());
6117 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6118 locations->AddTemp(Location::RequiresRegister());
6119 // When read barriers are enabled, we need an additional temporary
6120 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006121 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006123 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006124}
6125
6126void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006127 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006128 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006129 Location obj_loc = locations->InAt(0);
6130 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006131 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006132 Location temp_loc = locations->GetTemp(0);
6133 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006134 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
6135 locations->GetTemp(1) :
6136 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6138 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6139 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6140 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142 bool is_type_check_slow_path_fatal =
6143 (type_check_kind == TypeCheckKind::kExactCheck ||
6144 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6145 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6146 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6147 !instruction->CanThrowIntoCatchBlock();
6148 SlowPathCode* type_check_slow_path =
6149 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6150 is_type_check_slow_path_fatal);
6151 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152
Roland Levillain0d5a2812015-11-13 10:07:31 +00006153 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006154 // Avoid null check if we know obj is not null.
6155 if (instruction->MustDoNullCheck()) {
6156 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006157 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006158 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159
Roland Levillain0d5a2812015-11-13 10:07:31 +00006160 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006161 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006162
Roland Levillain0d5a2812015-11-13 10:07:31 +00006163 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006164 case TypeCheckKind::kExactCheck:
6165 case TypeCheckKind::kArrayCheck: {
6166 if (cls.IsRegister()) {
6167 __ cmpl(temp, cls.AsRegister<Register>());
6168 } else {
6169 DCHECK(cls.IsStackSlot()) << cls;
6170 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6171 }
6172 // Jump to slow path for throwing the exception or doing a
6173 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006174 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006175 break;
6176 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006177
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 case TypeCheckKind::kAbstractClassCheck: {
6179 // If the class is abstract, we eagerly fetch the super class of the
6180 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006181 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006184 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185
6186 // If the class reference currently in `temp` is not null, jump
6187 // to the `compare_classes` label to compare it with the checked
6188 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006190 __ j(kNotEqual, &compare_classes);
6191 // Otherwise, jump to the slow path to throw the exception.
6192 //
6193 // But before, move back the object's class into `temp` before
6194 // going into the slow path, as it has been overwritten in the
6195 // meantime.
6196 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006197 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006198 __ jmp(type_check_slow_path->GetEntryLabel());
6199
6200 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 if (cls.IsRegister()) {
6202 __ cmpl(temp, cls.AsRegister<Register>());
6203 } else {
6204 DCHECK(cls.IsStackSlot()) << cls;
6205 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6206 }
6207 __ j(kNotEqual, &loop);
6208 break;
6209 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006210
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006211 case TypeCheckKind::kClassHierarchyCheck: {
6212 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006213 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006214 __ Bind(&loop);
6215 if (cls.IsRegister()) {
6216 __ cmpl(temp, cls.AsRegister<Register>());
6217 } else {
6218 DCHECK(cls.IsStackSlot()) << cls;
6219 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6220 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006221 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222
Roland Levillain0d5a2812015-11-13 10:07:31 +00006223 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006224 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225
6226 // If the class reference currently in `temp` is not null, jump
6227 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006228 __ testl(temp, temp);
6229 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230 // Otherwise, jump to the slow path to throw the exception.
6231 //
6232 // But before, move back the object's class into `temp` before
6233 // going into the slow path, as it has been overwritten in the
6234 // meantime.
6235 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006236 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006237 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006238 break;
6239 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006240
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006241 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006242 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006243 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006244 if (cls.IsRegister()) {
6245 __ cmpl(temp, cls.AsRegister<Register>());
6246 } else {
6247 DCHECK(cls.IsStackSlot()) << cls;
6248 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6249 }
6250 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006251
6252 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006254 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255
6256 // If the component type is not null (i.e. the object is indeed
6257 // an array), jump to label `check_non_primitive_component_type`
6258 // to further check that this component type is not a primitive
6259 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006260 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261 __ j(kNotEqual, &check_non_primitive_component_type);
6262 // Otherwise, jump to the slow path to throw the exception.
6263 //
6264 // But before, move back the object's class into `temp` before
6265 // going into the slow path, as it has been overwritten in the
6266 // meantime.
6267 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006268 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269 __ jmp(type_check_slow_path->GetEntryLabel());
6270
6271 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006273 __ j(kEqual, &done);
6274 // Same comment as above regarding `temp` and the slow path.
6275 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006276 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006277 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006278 break;
6279 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280
Calin Juravle98893e12015-10-02 21:05:03 +01006281 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 // We always go into the type check slow path for the unresolved &
6284 // interface check cases.
6285 //
6286 // We cannot directly call the CheckCast runtime entry point
6287 // without resorting to a type checking slow path here (i.e. by
6288 // calling InvokeRuntime directly), as it would require to
6289 // assign fixed registers for the inputs of this HInstanceOf
6290 // instruction (following the runtime calling convention), which
6291 // might be cluttered by the potential first read barrier
6292 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006293 //
6294 // TODO: Introduce a new runtime entry point taking the object
6295 // to test (instead of its class) as argument, and let it deal
6296 // with the read barrier issues. This will let us refactor this
6297 // case of the `switch` code as it was previously (with a direct
6298 // call to the runtime not using a type checking slow path).
6299 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006300 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006301 break;
6302 }
6303 __ Bind(&done);
6304
Roland Levillain0d5a2812015-11-13 10:07:31 +00006305 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006306}
6307
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006308void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6309 LocationSummary* locations =
6310 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6311 InvokeRuntimeCallingConvention calling_convention;
6312 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6313}
6314
6315void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006316 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6317 : QUICK_ENTRY_POINT(pUnlockObject),
6318 instruction,
6319 instruction->GetDexPc(),
6320 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006321 if (instruction->IsEnter()) {
6322 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6323 } else {
6324 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6325 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006326}
6327
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006328void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6329void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6330void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6331
6332void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6333 LocationSummary* locations =
6334 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6335 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6336 || instruction->GetResultType() == Primitive::kPrimLong);
6337 locations->SetInAt(0, Location::RequiresRegister());
6338 locations->SetInAt(1, Location::Any());
6339 locations->SetOut(Location::SameAsFirstInput());
6340}
6341
6342void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6343 HandleBitwiseOperation(instruction);
6344}
6345
6346void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6347 HandleBitwiseOperation(instruction);
6348}
6349
6350void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6351 HandleBitwiseOperation(instruction);
6352}
6353
6354void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6355 LocationSummary* locations = instruction->GetLocations();
6356 Location first = locations->InAt(0);
6357 Location second = locations->InAt(1);
6358 DCHECK(first.Equals(locations->Out()));
6359
6360 if (instruction->GetResultType() == Primitive::kPrimInt) {
6361 if (second.IsRegister()) {
6362 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006363 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006364 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006365 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006366 } else {
6367 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006368 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006369 }
6370 } else if (second.IsConstant()) {
6371 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006372 __ andl(first.AsRegister<Register>(),
6373 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006374 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006375 __ orl(first.AsRegister<Register>(),
6376 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006377 } else {
6378 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006379 __ xorl(first.AsRegister<Register>(),
6380 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006381 }
6382 } else {
6383 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006384 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006385 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006386 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006387 } else {
6388 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006389 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006390 }
6391 }
6392 } else {
6393 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6394 if (second.IsRegisterPair()) {
6395 if (instruction->IsAnd()) {
6396 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6397 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6398 } else if (instruction->IsOr()) {
6399 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6400 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6401 } else {
6402 DCHECK(instruction->IsXor());
6403 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6404 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6405 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006406 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006407 if (instruction->IsAnd()) {
6408 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6409 __ andl(first.AsRegisterPairHigh<Register>(),
6410 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6411 } else if (instruction->IsOr()) {
6412 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6413 __ orl(first.AsRegisterPairHigh<Register>(),
6414 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6415 } else {
6416 DCHECK(instruction->IsXor());
6417 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6418 __ xorl(first.AsRegisterPairHigh<Register>(),
6419 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6420 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006421 } else {
6422 DCHECK(second.IsConstant()) << second;
6423 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006424 int32_t low_value = Low32Bits(value);
6425 int32_t high_value = High32Bits(value);
6426 Immediate low(low_value);
6427 Immediate high(high_value);
6428 Register first_low = first.AsRegisterPairLow<Register>();
6429 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006430 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006431 if (low_value == 0) {
6432 __ xorl(first_low, first_low);
6433 } else if (low_value != -1) {
6434 __ andl(first_low, low);
6435 }
6436 if (high_value == 0) {
6437 __ xorl(first_high, first_high);
6438 } else if (high_value != -1) {
6439 __ andl(first_high, high);
6440 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006441 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006442 if (low_value != 0) {
6443 __ orl(first_low, low);
6444 }
6445 if (high_value != 0) {
6446 __ orl(first_high, high);
6447 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006448 } else {
6449 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006450 if (low_value != 0) {
6451 __ xorl(first_low, low);
6452 }
6453 if (high_value != 0) {
6454 __ xorl(first_high, high);
6455 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006456 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006457 }
6458 }
6459}
6460
Roland Levillain7c1559a2015-12-15 10:55:36 +00006461void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6462 Location out,
6463 uint32_t offset,
6464 Location temp) {
6465 Register out_reg = out.AsRegister<Register>();
6466 if (kEmitCompilerReadBarrier) {
6467 if (kUseBakerReadBarrier) {
6468 // Load with fast path based Baker's read barrier.
6469 // /* HeapReference<Object> */ out = *(out + offset)
6470 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6471 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6472 } else {
6473 // Load with slow path based read barrier.
6474 // Save the value of `out` into `temp` before overwriting it
6475 // in the following move operation, as we will need it for the
6476 // read barrier below.
6477 __ movl(temp.AsRegister<Register>(), out_reg);
6478 // /* HeapReference<Object> */ out = *(out + offset)
6479 __ movl(out_reg, Address(out_reg, offset));
6480 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6481 }
6482 } else {
6483 // Plain load with no read barrier.
6484 // /* HeapReference<Object> */ out = *(out + offset)
6485 __ movl(out_reg, Address(out_reg, offset));
6486 __ MaybeUnpoisonHeapReference(out_reg);
6487 }
6488}
6489
6490void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6491 Location out,
6492 Location obj,
6493 uint32_t offset,
6494 Location temp) {
6495 Register out_reg = out.AsRegister<Register>();
6496 Register obj_reg = obj.AsRegister<Register>();
6497 if (kEmitCompilerReadBarrier) {
6498 if (kUseBakerReadBarrier) {
6499 // Load with fast path based Baker's read barrier.
6500 // /* HeapReference<Object> */ out = *(obj + offset)
6501 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6502 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6503 } else {
6504 // Load with slow path based read barrier.
6505 // /* HeapReference<Object> */ out = *(obj + offset)
6506 __ movl(out_reg, Address(obj_reg, offset));
6507 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6508 }
6509 } else {
6510 // Plain load with no read barrier.
6511 // /* HeapReference<Object> */ out = *(obj + offset)
6512 __ movl(out_reg, Address(obj_reg, offset));
6513 __ MaybeUnpoisonHeapReference(out_reg);
6514 }
6515}
6516
6517void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6518 Location root,
6519 Register obj,
6520 uint32_t offset) {
6521 Register root_reg = root.AsRegister<Register>();
6522 if (kEmitCompilerReadBarrier) {
6523 if (kUseBakerReadBarrier) {
6524 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6525 // Baker's read barrier are used:
6526 //
6527 // root = obj.field;
6528 // if (Thread::Current()->GetIsGcMarking()) {
6529 // root = ReadBarrier::Mark(root)
6530 // }
6531
6532 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6533 __ movl(root_reg, Address(obj, offset));
6534 static_assert(
6535 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6536 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6537 "have different sizes.");
6538 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6539 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6540 "have different sizes.");
6541
6542 // Slow path used to mark the GC root `root`.
6543 SlowPathCode* slow_path =
6544 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6545 codegen_->AddSlowPath(slow_path);
6546
6547 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6548 Immediate(0));
6549 __ j(kNotEqual, slow_path->GetEntryLabel());
6550 __ Bind(slow_path->GetExitLabel());
6551 } else {
6552 // GC root loaded through a slow path for read barriers other
6553 // than Baker's.
6554 // /* GcRoot<mirror::Object>* */ root = obj + offset
6555 __ leal(root_reg, Address(obj, offset));
6556 // /* mirror::Object* */ root = root->Read()
6557 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6558 }
6559 } else {
6560 // Plain GC root load with no read barrier.
6561 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6562 __ movl(root_reg, Address(obj, offset));
6563 }
6564}
6565
6566void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6567 Location ref,
6568 Register obj,
6569 uint32_t offset,
6570 Location temp,
6571 bool needs_null_check) {
6572 DCHECK(kEmitCompilerReadBarrier);
6573 DCHECK(kUseBakerReadBarrier);
6574
6575 // /* HeapReference<Object> */ ref = *(obj + offset)
6576 Address src(obj, offset);
6577 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6578}
6579
6580void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6581 Location ref,
6582 Register obj,
6583 uint32_t data_offset,
6584 Location index,
6585 Location temp,
6586 bool needs_null_check) {
6587 DCHECK(kEmitCompilerReadBarrier);
6588 DCHECK(kUseBakerReadBarrier);
6589
6590 // /* HeapReference<Object> */ ref =
6591 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6592 Address src = index.IsConstant() ?
6593 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6594 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6595 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6596}
6597
6598void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6599 Location ref,
6600 Register obj,
6601 const Address& src,
6602 Location temp,
6603 bool needs_null_check) {
6604 DCHECK(kEmitCompilerReadBarrier);
6605 DCHECK(kUseBakerReadBarrier);
6606
6607 // In slow path based read barriers, the read barrier call is
6608 // inserted after the original load. However, in fast path based
6609 // Baker's read barriers, we need to perform the load of
6610 // mirror::Object::monitor_ *before* the original reference load.
6611 // This load-load ordering is required by the read barrier.
6612 // The fast path/slow path (for Baker's algorithm) should look like:
6613 //
6614 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6615 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6616 // HeapReference<Object> ref = *src; // Original reference load.
6617 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6618 // if (is_gray) {
6619 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6620 // }
6621 //
6622 // Note: the original implementation in ReadBarrier::Barrier is
6623 // slightly more complex as:
6624 // - it implements the load-load fence using a data dependency on
6625 // the high-bits of rb_state, which are expected to be all zeroes;
6626 // - it performs additional checks that we do not do here for
6627 // performance reasons.
6628
6629 Register ref_reg = ref.AsRegister<Register>();
6630 Register temp_reg = temp.AsRegister<Register>();
6631 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6632
6633 // /* int32_t */ monitor = obj->monitor_
6634 __ movl(temp_reg, Address(obj, monitor_offset));
6635 if (needs_null_check) {
6636 MaybeRecordImplicitNullCheck(instruction);
6637 }
6638 // /* LockWord */ lock_word = LockWord(monitor)
6639 static_assert(sizeof(LockWord) == sizeof(int32_t),
6640 "art::LockWord and int32_t have different sizes.");
6641 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6642 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6643 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6644 static_assert(
6645 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6646 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6647
6648 // Load fence to prevent load-load reordering.
6649 // Note that this is a no-op, thanks to the x86 memory model.
6650 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6651
6652 // The actual reference load.
6653 // /* HeapReference<Object> */ ref = *src
6654 __ movl(ref_reg, src);
6655
6656 // Object* ref = ref_addr->AsMirrorPtr()
6657 __ MaybeUnpoisonHeapReference(ref_reg);
6658
6659 // Slow path used to mark the object `ref` when it is gray.
6660 SlowPathCode* slow_path =
6661 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6662 AddSlowPath(slow_path);
6663
6664 // if (rb_state == ReadBarrier::gray_ptr_)
6665 // ref = ReadBarrier::Mark(ref);
6666 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6667 __ j(kEqual, slow_path->GetEntryLabel());
6668 __ Bind(slow_path->GetExitLabel());
6669}
6670
6671void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6672 Location out,
6673 Location ref,
6674 Location obj,
6675 uint32_t offset,
6676 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006677 DCHECK(kEmitCompilerReadBarrier);
6678
Roland Levillain7c1559a2015-12-15 10:55:36 +00006679 // Insert a slow path based read barrier *after* the reference load.
6680 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 // If heap poisoning is enabled, the unpoisoning of the loaded
6682 // reference will be carried out by the runtime within the slow
6683 // path.
6684 //
6685 // Note that `ref` currently does not get unpoisoned (when heap
6686 // poisoning is enabled), which is alright as the `ref` argument is
6687 // not used by the artReadBarrierSlow entry point.
6688 //
6689 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6690 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6691 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6692 AddSlowPath(slow_path);
6693
Roland Levillain0d5a2812015-11-13 10:07:31 +00006694 __ jmp(slow_path->GetEntryLabel());
6695 __ Bind(slow_path->GetExitLabel());
6696}
6697
Roland Levillain7c1559a2015-12-15 10:55:36 +00006698void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6699 Location out,
6700 Location ref,
6701 Location obj,
6702 uint32_t offset,
6703 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006704 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006705 // Baker's read barriers shall be handled by the fast path
6706 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6707 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006708 // If heap poisoning is enabled, unpoisoning will be taken care of
6709 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006710 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 } else if (kPoisonHeapReferences) {
6712 __ UnpoisonHeapReference(out.AsRegister<Register>());
6713 }
6714}
6715
Roland Levillain7c1559a2015-12-15 10:55:36 +00006716void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6717 Location out,
6718 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006719 DCHECK(kEmitCompilerReadBarrier);
6720
Roland Levillain7c1559a2015-12-15 10:55:36 +00006721 // Insert a slow path based read barrier *after* the GC root load.
6722 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006723 // Note that GC roots are not affected by heap poisoning, so we do
6724 // not need to do anything special for this here.
6725 SlowPathCode* slow_path =
6726 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6727 AddSlowPath(slow_path);
6728
Roland Levillain0d5a2812015-11-13 10:07:31 +00006729 __ jmp(slow_path->GetEntryLabel());
6730 __ Bind(slow_path->GetExitLabel());
6731}
6732
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006733void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006734 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006735 LOG(FATAL) << "Unreachable";
6736}
6737
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006738void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006739 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006740 LOG(FATAL) << "Unreachable";
6741}
6742
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006743void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6744 DCHECK(codegen_->IsBaseline());
6745 LocationSummary* locations =
6746 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6747 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6748}
6749
6750void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6751 DCHECK(codegen_->IsBaseline());
6752 // Will be generated at use site.
6753}
6754
Mark Mendellfe57faa2015-09-18 09:26:15 -04006755// Simple implementation of packed switch - generate cascaded compare/jumps.
6756void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6757 LocationSummary* locations =
6758 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6759 locations->SetInAt(0, Location::RequiresRegister());
6760}
6761
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006762void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6763 int32_t lower_bound,
6764 uint32_t num_entries,
6765 HBasicBlock* switch_block,
6766 HBasicBlock* default_block) {
6767 // Figure out the correct compare values and jump conditions.
6768 // Handle the first compare/branch as a special case because it might
6769 // jump to the default case.
6770 DCHECK_GT(num_entries, 2u);
6771 Condition first_condition;
6772 uint32_t index;
6773 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6774 if (lower_bound != 0) {
6775 first_condition = kLess;
6776 __ cmpl(value_reg, Immediate(lower_bound));
6777 __ j(first_condition, codegen_->GetLabelOf(default_block));
6778 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006779
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006780 index = 1;
6781 } else {
6782 // Handle all the compare/jumps below.
6783 first_condition = kBelow;
6784 index = 0;
6785 }
6786
6787 // Handle the rest of the compare/jumps.
6788 for (; index + 1 < num_entries; index += 2) {
6789 int32_t compare_to_value = lower_bound + index + 1;
6790 __ cmpl(value_reg, Immediate(compare_to_value));
6791 // Jump to successors[index] if value < case_value[index].
6792 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6793 // Jump to successors[index + 1] if value == case_value[index + 1].
6794 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6795 }
6796
6797 if (index != num_entries) {
6798 // There are an odd number of entries. Handle the last one.
6799 DCHECK_EQ(index + 1, num_entries);
6800 __ cmpl(value_reg, Immediate(lower_bound + index));
6801 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006802 }
6803
6804 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006805 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6806 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006807 }
6808}
6809
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006810void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6811 int32_t lower_bound = switch_instr->GetStartValue();
6812 uint32_t num_entries = switch_instr->GetNumEntries();
6813 LocationSummary* locations = switch_instr->GetLocations();
6814 Register value_reg = locations->InAt(0).AsRegister<Register>();
6815
6816 GenPackedSwitchWithCompares(value_reg,
6817 lower_bound,
6818 num_entries,
6819 switch_instr->GetBlock(),
6820 switch_instr->GetDefaultBlock());
6821}
6822
Mark Mendell805b3b52015-09-18 14:10:29 -04006823void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6824 LocationSummary* locations =
6825 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6826 locations->SetInAt(0, Location::RequiresRegister());
6827
6828 // Constant area pointer.
6829 locations->SetInAt(1, Location::RequiresRegister());
6830
6831 // And the temporary we need.
6832 locations->AddTemp(Location::RequiresRegister());
6833}
6834
6835void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6836 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006837 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006838 LocationSummary* locations = switch_instr->GetLocations();
6839 Register value_reg = locations->InAt(0).AsRegister<Register>();
6840 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6841
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006842 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6843 GenPackedSwitchWithCompares(value_reg,
6844 lower_bound,
6845 num_entries,
6846 switch_instr->GetBlock(),
6847 default_block);
6848 return;
6849 }
6850
Mark Mendell805b3b52015-09-18 14:10:29 -04006851 // Optimizing has a jump area.
6852 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6853 Register constant_area = locations->InAt(1).AsRegister<Register>();
6854
6855 // Remove the bias, if needed.
6856 if (lower_bound != 0) {
6857 __ leal(temp_reg, Address(value_reg, -lower_bound));
6858 value_reg = temp_reg;
6859 }
6860
6861 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006862 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006863 __ cmpl(value_reg, Immediate(num_entries - 1));
6864 __ j(kAbove, codegen_->GetLabelOf(default_block));
6865
6866 // We are in the range of the table.
6867 // Load (target-constant_area) from the jump table, indexing by the value.
6868 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6869
6870 // Compute the actual target address by adding in constant_area.
6871 __ addl(temp_reg, constant_area);
6872
6873 // And jump.
6874 __ jmp(temp_reg);
6875}
6876
Mark Mendell0616ae02015-04-17 12:49:27 -04006877void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6878 HX86ComputeBaseMethodAddress* insn) {
6879 LocationSummary* locations =
6880 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6881 locations->SetOut(Location::RequiresRegister());
6882}
6883
6884void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6885 HX86ComputeBaseMethodAddress* insn) {
6886 LocationSummary* locations = insn->GetLocations();
6887 Register reg = locations->Out().AsRegister<Register>();
6888
6889 // Generate call to next instruction.
6890 Label next_instruction;
6891 __ call(&next_instruction);
6892 __ Bind(&next_instruction);
6893
6894 // Remember this offset for later use with constant area.
6895 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6896
6897 // Grab the return address off the stack.
6898 __ popl(reg);
6899}
6900
6901void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6902 HX86LoadFromConstantTable* insn) {
6903 LocationSummary* locations =
6904 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6905
6906 locations->SetInAt(0, Location::RequiresRegister());
6907 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6908
6909 // If we don't need to be materialized, we only need the inputs to be set.
6910 if (!insn->NeedsMaterialization()) {
6911 return;
6912 }
6913
6914 switch (insn->GetType()) {
6915 case Primitive::kPrimFloat:
6916 case Primitive::kPrimDouble:
6917 locations->SetOut(Location::RequiresFpuRegister());
6918 break;
6919
6920 case Primitive::kPrimInt:
6921 locations->SetOut(Location::RequiresRegister());
6922 break;
6923
6924 default:
6925 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6926 }
6927}
6928
6929void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6930 if (!insn->NeedsMaterialization()) {
6931 return;
6932 }
6933
6934 LocationSummary* locations = insn->GetLocations();
6935 Location out = locations->Out();
6936 Register const_area = locations->InAt(0).AsRegister<Register>();
6937 HConstant *value = insn->GetConstant();
6938
6939 switch (insn->GetType()) {
6940 case Primitive::kPrimFloat:
6941 __ movss(out.AsFpuRegister<XmmRegister>(),
6942 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6943 break;
6944
6945 case Primitive::kPrimDouble:
6946 __ movsd(out.AsFpuRegister<XmmRegister>(),
6947 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6948 break;
6949
6950 case Primitive::kPrimInt:
6951 __ movl(out.AsRegister<Register>(),
6952 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6953 break;
6954
6955 default:
6956 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6957 }
6958}
6959
Mark Mendell0616ae02015-04-17 12:49:27 -04006960/**
6961 * Class to handle late fixup of offsets into constant area.
6962 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006963class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006964 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006965 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6966 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6967
6968 protected:
6969 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6970
6971 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006972
6973 private:
6974 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6975 // Patch the correct offset for the instruction. The place to patch is the
6976 // last 4 bytes of the instruction.
6977 // The value to patch is the distance from the offset in the constant area
6978 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006979 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6980 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006981
6982 // Patch in the right value.
6983 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6984 }
6985
Mark Mendell0616ae02015-04-17 12:49:27 -04006986 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006987 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006988};
6989
Mark Mendell805b3b52015-09-18 14:10:29 -04006990/**
6991 * Class to handle late fixup of offsets to a jump table that will be created in the
6992 * constant area.
6993 */
6994class JumpTableRIPFixup : public RIPFixup {
6995 public:
6996 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6997 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6998
6999 void CreateJumpTable() {
7000 X86Assembler* assembler = codegen_->GetAssembler();
7001
7002 // Ensure that the reference to the jump table has the correct offset.
7003 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7004 SetOffset(offset_in_constant_table);
7005
7006 // The label values in the jump table are computed relative to the
7007 // instruction addressing the constant area.
7008 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7009
7010 // Populate the jump table with the correct values for the jump table.
7011 int32_t num_entries = switch_instr_->GetNumEntries();
7012 HBasicBlock* block = switch_instr_->GetBlock();
7013 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7014 // The value that we want is the target offset - the position of the table.
7015 for (int32_t i = 0; i < num_entries; i++) {
7016 HBasicBlock* b = successors[i];
7017 Label* l = codegen_->GetLabelOf(b);
7018 DCHECK(l->IsBound());
7019 int32_t offset_to_block = l->Position() - relative_offset;
7020 assembler->AppendInt32(offset_to_block);
7021 }
7022 }
7023
7024 private:
7025 const HX86PackedSwitch* switch_instr_;
7026};
7027
7028void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7029 // Generate the constant area if needed.
7030 X86Assembler* assembler = GetAssembler();
7031 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7032 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7033 // byte values.
7034 assembler->Align(4, 0);
7035 constant_area_start_ = assembler->CodeSize();
7036
7037 // Populate any jump tables.
7038 for (auto jump_table : fixups_to_jump_tables_) {
7039 jump_table->CreateJumpTable();
7040 }
7041
7042 // And now add the constant area to the generated code.
7043 assembler->AddConstantArea();
7044 }
7045
7046 // And finish up.
7047 CodeGenerator::Finalize(allocator);
7048}
7049
Mark Mendell0616ae02015-04-17 12:49:27 -04007050Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7051 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7052 return Address(reg, kDummy32BitOffset, fixup);
7053}
7054
7055Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7056 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7057 return Address(reg, kDummy32BitOffset, fixup);
7058}
7059
7060Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7061 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7062 return Address(reg, kDummy32BitOffset, fixup);
7063}
7064
7065Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7066 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7067 return Address(reg, kDummy32BitOffset, fixup);
7068}
7069
Mark Mendell805b3b52015-09-18 14:10:29 -04007070Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7071 Register reg,
7072 Register value) {
7073 // Create a fixup to be used to create and address the jump table.
7074 JumpTableRIPFixup* table_fixup =
7075 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7076
7077 // We have to populate the jump tables.
7078 fixups_to_jump_tables_.push_back(table_fixup);
7079
7080 // We want a scaled address, as we are extracting the correct offset from the table.
7081 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7082}
7083
Andreas Gampe85b62f22015-09-09 13:15:38 -07007084// TODO: target as memory.
7085void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7086 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007087 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007088 return;
7089 }
7090
7091 DCHECK_NE(type, Primitive::kPrimVoid);
7092
7093 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7094 if (target.Equals(return_loc)) {
7095 return;
7096 }
7097
7098 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7099 // with the else branch.
7100 if (type == Primitive::kPrimLong) {
7101 HParallelMove parallel_move(GetGraph()->GetArena());
7102 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7103 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7104 GetMoveResolver()->EmitNativeCode(&parallel_move);
7105 } else {
7106 // Let the parallel move resolver take care of all of this.
7107 HParallelMove parallel_move(GetGraph()->GetArena());
7108 parallel_move.AddMove(return_loc, target, type, nullptr);
7109 GetMoveResolver()->EmitNativeCode(&parallel_move);
7110 }
7111}
7112
Roland Levillain4d027112015-07-01 15:41:14 +01007113#undef __
7114
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007115} // namespace x86
7116} // namespace art