blob: a808c27313d38fdd34ed2ed2d87fbb576b1cbbad [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 Mendell152408f2015-12-31 12:28:50 -05001338template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001339void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001340 LabelType* true_label,
1341 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001342 if (cond->IsFPConditionTrueIfNaN()) {
1343 __ j(kUnordered, true_label);
1344 } else if (cond->IsFPConditionFalseIfNaN()) {
1345 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001346 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001347 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001348}
1349
Mark Mendell152408f2015-12-31 12:28:50 -05001350template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001351void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001352 LabelType* true_label,
1353 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001354 LocationSummary* locations = cond->GetLocations();
1355 Location left = locations->InAt(0);
1356 Location right = locations->InAt(1);
1357 IfCondition if_cond = cond->GetCondition();
1358
Mark Mendellc4701932015-04-10 13:18:51 -04001359 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001360 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001361 IfCondition true_high_cond = if_cond;
1362 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001363 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001364
1365 // Set the conditions for the test, remembering that == needs to be
1366 // decided using the low words.
1367 switch (if_cond) {
1368 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001369 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001370 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001371 break;
1372 case kCondLT:
1373 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001374 break;
1375 case kCondLE:
1376 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001377 break;
1378 case kCondGT:
1379 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001380 break;
1381 case kCondGE:
1382 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001383 break;
Aart Bike9f37602015-10-09 11:15:55 -07001384 case kCondB:
1385 false_high_cond = kCondA;
1386 break;
1387 case kCondBE:
1388 true_high_cond = kCondB;
1389 break;
1390 case kCondA:
1391 false_high_cond = kCondB;
1392 break;
1393 case kCondAE:
1394 true_high_cond = kCondA;
1395 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001396 }
1397
1398 if (right.IsConstant()) {
1399 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001400 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001401 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001402
1403 if (val_high == 0) {
1404 __ testl(left_high, left_high);
1405 } else {
1406 __ cmpl(left_high, Immediate(val_high));
1407 }
1408 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001409 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001410 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001411 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001412 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001413 __ j(X86Condition(true_high_cond), true_label);
1414 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001415 }
1416 // Must be equal high, so compare the lows.
1417 if (val_low == 0) {
1418 __ testl(left_low, left_low);
1419 } else {
1420 __ cmpl(left_low, Immediate(val_low));
1421 }
1422 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001423 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001424 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001425
1426 __ cmpl(left_high, right_high);
1427 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001428 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001429 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001430 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001431 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001432 __ j(X86Condition(true_high_cond), true_label);
1433 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001434 }
1435 // Must be equal high, so compare the lows.
1436 __ cmpl(left_low, right_low);
1437 }
1438 // The last comparison might be unsigned.
1439 __ j(final_condition, true_label);
1440}
1441
Mark Mendell152408f2015-12-31 12:28:50 -05001442template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001443void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001444 LabelType* true_target_in,
1445 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001446 // Generated branching requires both targets to be explicit. If either of the
1447 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001448 LabelType fallthrough_target;
1449 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1450 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001451
Mark Mendellc4701932015-04-10 13:18:51 -04001452 LocationSummary* locations = condition->GetLocations();
1453 Location left = locations->InAt(0);
1454 Location right = locations->InAt(1);
1455
Mark Mendellc4701932015-04-10 13:18:51 -04001456 Primitive::Type type = condition->InputAt(0)->GetType();
1457 switch (type) {
1458 case Primitive::kPrimLong:
1459 GenerateLongComparesAndJumps(condition, true_target, false_target);
1460 break;
1461 case Primitive::kPrimFloat:
Mark Mendellc4701932015-04-10 13:18:51 -04001462 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1463 GenerateFPJumps(condition, true_target, false_target);
1464 break;
1465 case Primitive::kPrimDouble:
Mark Mendellc4701932015-04-10 13:18:51 -04001466 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
1467 GenerateFPJumps(condition, true_target, false_target);
1468 break;
1469 default:
1470 LOG(FATAL) << "Unexpected compare type " << type;
1471 }
1472
David Brazdil0debae72015-11-12 18:37:00 +00001473 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001474 __ jmp(false_target);
1475 }
David Brazdil0debae72015-11-12 18:37:00 +00001476
1477 if (fallthrough_target.IsLinked()) {
1478 __ Bind(&fallthrough_target);
1479 }
Mark Mendellc4701932015-04-10 13:18:51 -04001480}
1481
David Brazdil0debae72015-11-12 18:37:00 +00001482static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1483 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1484 // are set only strictly before `branch`. We can't use the eflags on long/FP
1485 // conditions if they are materialized due to the complex branching.
1486 return cond->IsCondition() &&
1487 cond->GetNext() == branch &&
1488 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1489 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1490}
1491
Mark Mendell152408f2015-12-31 12:28:50 -05001492template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001493void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001494 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001495 LabelType* true_target,
1496 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001497 HInstruction* cond = instruction->InputAt(condition_input_index);
1498
1499 if (true_target == nullptr && false_target == nullptr) {
1500 // Nothing to do. The code always falls through.
1501 return;
1502 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001503 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001504 if (cond->AsIntConstant()->IsOne()) {
1505 if (true_target != nullptr) {
1506 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001507 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001508 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001509 DCHECK(cond->AsIntConstant()->IsZero());
1510 if (false_target != nullptr) {
1511 __ jmp(false_target);
1512 }
1513 }
1514 return;
1515 }
1516
1517 // The following code generates these patterns:
1518 // (1) true_target == nullptr && false_target != nullptr
1519 // - opposite condition true => branch to false_target
1520 // (2) true_target != nullptr && false_target == nullptr
1521 // - condition true => branch to true_target
1522 // (3) true_target != nullptr && false_target != nullptr
1523 // - condition true => branch to true_target
1524 // - branch to false_target
1525 if (IsBooleanValueOrMaterializedCondition(cond)) {
1526 if (AreEflagsSetFrom(cond, instruction)) {
1527 if (true_target == nullptr) {
1528 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1529 } else {
1530 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1531 }
1532 } else {
1533 // Materialized condition, compare against 0.
1534 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1535 if (lhs.IsRegister()) {
1536 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1537 } else {
1538 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1539 }
1540 if (true_target == nullptr) {
1541 __ j(kEqual, false_target);
1542 } else {
1543 __ j(kNotEqual, true_target);
1544 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001545 }
1546 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001547 // Condition has not been materialized, use its inputs as the comparison and
1548 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001549 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001550
1551 // If this is a long or FP comparison that has been folded into
1552 // the HCondition, generate the comparison directly.
1553 Primitive::Type type = condition->InputAt(0)->GetType();
1554 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1555 GenerateCompareTestAndBranch(condition, true_target, false_target);
1556 return;
1557 }
1558
1559 Location lhs = condition->GetLocations()->InAt(0);
1560 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001561 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001562 if (rhs.IsRegister()) {
1563 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1564 } else if (rhs.IsConstant()) {
1565 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1566 if (constant == 0) {
1567 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001568 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001569 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001570 }
1571 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001572 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1573 }
1574 if (true_target == nullptr) {
1575 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1576 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001577 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001578 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001579 }
David Brazdil0debae72015-11-12 18:37:00 +00001580
1581 // If neither branch falls through (case 3), the conditional branch to `true_target`
1582 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1583 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001584 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001585 }
1586}
1587
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001588void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1590 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001591 locations->SetInAt(0, Location::Any());
1592 }
1593}
1594
1595void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001596 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1597 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1598 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1599 nullptr : codegen_->GetLabelOf(true_successor);
1600 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1601 nullptr : codegen_->GetLabelOf(false_successor);
1602 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001603}
1604
1605void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1606 LocationSummary* locations = new (GetGraph()->GetArena())
1607 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001608 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001609 locations->SetInAt(0, Location::Any());
1610 }
1611}
1612
1613void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07001614 SlowPathCode* slow_path = new (GetGraph()->GetArena())
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001615 DeoptimizationSlowPathX86(deoptimize);
1616 codegen_->AddSlowPath(slow_path);
David Brazdil0debae72015-11-12 18:37:00 +00001617 GenerateTestAndBranch(deoptimize,
1618 /* condition_input_index */ 0,
1619 slow_path->GetEntryLabel(),
Mark Mendell152408f2015-12-31 12:28:50 -05001620 /* false_target */ static_cast<Label*>(nullptr));
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001621}
1622
David Srbecky0cf44932015-12-09 14:09:59 +00001623void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1624 new (GetGraph()->GetArena()) LocationSummary(info);
1625}
1626
1627void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1628 codegen_->RecordPcInfo(info, info->GetDexPc());
1629}
1630
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001631void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001632 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001633}
1634
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001635void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1636 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001637}
1638
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001639void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001640 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001641}
1642
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001643void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001644 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001645}
1646
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001647void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001648 LocationSummary* locations =
1649 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001650 switch (store->InputAt(1)->GetType()) {
1651 case Primitive::kPrimBoolean:
1652 case Primitive::kPrimByte:
1653 case Primitive::kPrimChar:
1654 case Primitive::kPrimShort:
1655 case Primitive::kPrimInt:
1656 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001657 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001658 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1659 break;
1660
1661 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001662 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001663 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1664 break;
1665
1666 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001667 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001668 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001669}
1670
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001671void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001672}
1673
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001674void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001675 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001676 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001677 // Handle the long/FP comparisons made in instruction simplification.
1678 switch (cond->InputAt(0)->GetType()) {
1679 case Primitive::kPrimLong: {
1680 locations->SetInAt(0, Location::RequiresRegister());
1681 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1682 if (cond->NeedsMaterialization()) {
1683 locations->SetOut(Location::RequiresRegister());
1684 }
1685 break;
1686 }
1687 case Primitive::kPrimFloat:
1688 case Primitive::kPrimDouble: {
1689 locations->SetInAt(0, Location::RequiresFpuRegister());
1690 locations->SetInAt(1, Location::RequiresFpuRegister());
1691 if (cond->NeedsMaterialization()) {
1692 locations->SetOut(Location::RequiresRegister());
1693 }
1694 break;
1695 }
1696 default:
1697 locations->SetInAt(0, Location::RequiresRegister());
1698 locations->SetInAt(1, Location::Any());
1699 if (cond->NeedsMaterialization()) {
1700 // We need a byte register.
1701 locations->SetOut(Location::RegisterLocation(ECX));
1702 }
1703 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001704 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001705}
1706
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001707void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001708 if (!cond->NeedsMaterialization()) {
1709 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001710 }
Mark Mendellc4701932015-04-10 13:18:51 -04001711
1712 LocationSummary* locations = cond->GetLocations();
1713 Location lhs = locations->InAt(0);
1714 Location rhs = locations->InAt(1);
1715 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001716 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001717
1718 switch (cond->InputAt(0)->GetType()) {
1719 default: {
1720 // Integer case.
1721
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001722 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001723 __ xorl(reg, reg);
1724
1725 if (rhs.IsRegister()) {
1726 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1727 } else if (rhs.IsConstant()) {
1728 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1729 if (constant == 0) {
1730 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1731 } else {
1732 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1733 }
1734 } else {
1735 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1736 }
Aart Bike9f37602015-10-09 11:15:55 -07001737 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001738 return;
1739 }
1740 case Primitive::kPrimLong:
1741 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1742 break;
1743 case Primitive::kPrimFloat:
1744 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1745 GenerateFPJumps(cond, &true_label, &false_label);
1746 break;
1747 case Primitive::kPrimDouble:
1748 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1749 GenerateFPJumps(cond, &true_label, &false_label);
1750 break;
1751 }
1752
1753 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001754 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001755
Roland Levillain4fa13f62015-07-06 18:11:54 +01001756 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001757 __ Bind(&false_label);
1758 __ xorl(reg, reg);
1759 __ jmp(&done_label);
1760
Roland Levillain4fa13f62015-07-06 18:11:54 +01001761 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001762 __ Bind(&true_label);
1763 __ movl(reg, Immediate(1));
1764 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001813}
1814
Aart Bike9f37602015-10-09 11:15:55 -07001815void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001817}
1818
1819void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001847void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001850 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001851}
1852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001853void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001854 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001855}
1856
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001857void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1858 LocationSummary* locations =
1859 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1860 locations->SetOut(Location::ConstantLocation(constant));
1861}
1862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001864 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001865}
1866
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001870 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871}
1872
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001873void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 // Will be generated at use site.
1875}
1876
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001877void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1878 LocationSummary* locations =
1879 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1880 locations->SetOut(Location::ConstantLocation(constant));
1881}
1882
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001883void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001884 // Will be generated at use site.
1885}
1886
1887void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1888 LocationSummary* locations =
1889 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1890 locations->SetOut(Location::ConstantLocation(constant));
1891}
1892
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001893void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001894 // Will be generated at use site.
1895}
1896
Calin Juravle27df7582015-04-17 19:12:31 +01001897void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1898 memory_barrier->SetLocations(nullptr);
1899}
1900
1901void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001902 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001903}
1904
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001905void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001906 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001907}
1908
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001909void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001911}
1912
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001913void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001914 LocationSummary* locations =
1915 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 switch (ret->InputAt(0)->GetType()) {
1917 case Primitive::kPrimBoolean:
1918 case Primitive::kPrimByte:
1919 case Primitive::kPrimChar:
1920 case Primitive::kPrimShort:
1921 case Primitive::kPrimInt:
1922 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001923 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 break;
1925
1926 case Primitive::kPrimLong:
1927 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001928 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001929 break;
1930
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001931 case Primitive::kPrimFloat:
1932 case Primitive::kPrimDouble:
1933 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001934 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 break;
1936
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001937 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001938 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001939 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001940}
1941
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001942void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 if (kIsDebugBuild) {
1944 switch (ret->InputAt(0)->GetType()) {
1945 case Primitive::kPrimBoolean:
1946 case Primitive::kPrimByte:
1947 case Primitive::kPrimChar:
1948 case Primitive::kPrimShort:
1949 case Primitive::kPrimInt:
1950 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001951 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001952 break;
1953
1954 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001955 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1956 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001957 break;
1958
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001959 case Primitive::kPrimFloat:
1960 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001961 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001962 break;
1963
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001964 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001965 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001966 }
1967 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001968 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001969}
1970
Calin Juravle175dc732015-08-25 15:42:32 +01001971void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1972 // The trampoline uses the same calling convention as dex calling conventions,
1973 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1974 // the method_idx.
1975 HandleInvoke(invoke);
1976}
1977
1978void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1979 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1980}
1981
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001982void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001983 // When we do not run baseline, explicit clinit checks triggered by static
1984 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1985 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001986
Mark Mendellfb8d2792015-03-31 22:16:59 -04001987 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001988 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001989 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001990 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001991 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001992 return;
1993 }
1994
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001996
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001997 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1998 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001999 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002000 }
2001
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002002 if (codegen_->IsBaseline()) {
2003 // Baseline does not have enough registers if the current method also
2004 // needs a register. We therefore do not require a register for it, and let
2005 // the code generation of the invoke handle it.
2006 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002007 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002008 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002009 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002010 }
2011 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002012}
2013
Mark Mendell09ed1a32015-03-25 08:30:06 -04002014static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2015 if (invoke->GetLocations()->Intrinsified()) {
2016 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2017 intrinsic.Dispatch(invoke);
2018 return true;
2019 }
2020 return false;
2021}
2022
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002023void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002024 // When we do not run baseline, explicit clinit checks triggered by static
2025 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2026 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002027
Mark Mendell09ed1a32015-03-25 08:30:06 -04002028 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2029 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002030 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002031
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002032 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002033 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002034 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002035 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002036}
2037
2038void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002039 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2040 if (intrinsic.TryDispatch(invoke)) {
2041 return;
2042 }
2043
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002044 HandleInvoke(invoke);
2045}
2046
2047void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002048 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002049 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002050}
2051
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002052void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002053 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2054 return;
2055 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002056
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002057 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002058 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002059 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002060}
2061
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002062void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002063 // This call to HandleInvoke allocates a temporary (core) register
2064 // which is also used to transfer the hidden argument from FP to
2065 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066 HandleInvoke(invoke);
2067 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002068 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002069}
2070
2071void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2072 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002073 LocationSummary* locations = invoke->GetLocations();
2074 Register temp = locations->GetTemp(0).AsRegister<Register>();
2075 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002076 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2077 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002078 Location receiver = locations->InAt(0);
2079 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2080
Roland Levillain0d5a2812015-11-13 10:07:31 +00002081 // Set the hidden argument. This is safe to do this here, as XMM7
2082 // won't be modified thereafter, before the `call` instruction.
2083 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002084 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002085 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002086
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002087 if (receiver.IsStackSlot()) {
2088 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002089 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002090 __ movl(temp, Address(temp, class_offset));
2091 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002092 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002094 }
Roland Levillain4d027112015-07-01 15:41:14 +01002095 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002096 // Instead of simply (possibly) unpoisoning `temp` here, we should
2097 // emit a read barrier for the previous class reference load.
2098 // However this is not required in practice, as this is an
2099 // intermediate/temporary reference and because the current
2100 // concurrent copying collector keeps the from-space memory
2101 // intact/accessible until the end of the marking phase (the
2102 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002103 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002104 // temp = temp->GetImtEntryAt(method_offset);
2105 __ movl(temp, Address(temp, method_offset));
2106 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002107 __ call(Address(temp,
2108 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002109
2110 DCHECK(!codegen_->IsLeafMethod());
2111 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2112}
2113
Roland Levillain88cb1752014-10-20 16:36:47 +01002114void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2115 LocationSummary* locations =
2116 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2117 switch (neg->GetResultType()) {
2118 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002119 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002120 locations->SetInAt(0, Location::RequiresRegister());
2121 locations->SetOut(Location::SameAsFirstInput());
2122 break;
2123
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002125 locations->SetInAt(0, Location::RequiresFpuRegister());
2126 locations->SetOut(Location::SameAsFirstInput());
2127 locations->AddTemp(Location::RequiresRegister());
2128 locations->AddTemp(Location::RequiresFpuRegister());
2129 break;
2130
Roland Levillain88cb1752014-10-20 16:36:47 +01002131 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002132 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002133 locations->SetOut(Location::SameAsFirstInput());
2134 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002135 break;
2136
2137 default:
2138 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2139 }
2140}
2141
2142void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2143 LocationSummary* locations = neg->GetLocations();
2144 Location out = locations->Out();
2145 Location in = locations->InAt(0);
2146 switch (neg->GetResultType()) {
2147 case Primitive::kPrimInt:
2148 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002149 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002150 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002151 break;
2152
2153 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002154 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002155 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002156 __ negl(out.AsRegisterPairLow<Register>());
2157 // Negation is similar to subtraction from zero. The least
2158 // significant byte triggers a borrow when it is different from
2159 // zero; to take it into account, add 1 to the most significant
2160 // byte if the carry flag (CF) is set to 1 after the first NEGL
2161 // operation.
2162 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2163 __ negl(out.AsRegisterPairHigh<Register>());
2164 break;
2165
Roland Levillain5368c212014-11-27 15:03:41 +00002166 case Primitive::kPrimFloat: {
2167 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002168 Register constant = locations->GetTemp(0).AsRegister<Register>();
2169 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002170 // Implement float negation with an exclusive or with value
2171 // 0x80000000 (mask for bit 31, representing the sign of a
2172 // single-precision floating-point number).
2173 __ movl(constant, Immediate(INT32_C(0x80000000)));
2174 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002175 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002176 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002177 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002178
Roland Levillain5368c212014-11-27 15:03:41 +00002179 case Primitive::kPrimDouble: {
2180 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002181 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002182 // Implement double negation with an exclusive or with value
2183 // 0x8000000000000000 (mask for bit 63, representing the sign of
2184 // a double-precision floating-point number).
2185 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002186 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002187 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002188 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002189
2190 default:
2191 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2192 }
2193}
2194
Roland Levillaindff1f282014-11-05 14:15:05 +00002195void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002196 Primitive::Type result_type = conversion->GetResultType();
2197 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002198 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002199
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002200 // The float-to-long and double-to-long type conversions rely on a
2201 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002202 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002203 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2204 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002205 ? LocationSummary::kCall
2206 : LocationSummary::kNoCall;
2207 LocationSummary* locations =
2208 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2209
David Brazdilb2bd1c52015-03-25 11:17:37 +00002210 // The Java language does not allow treating boolean as an integral type but
2211 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002212
Roland Levillaindff1f282014-11-05 14:15:05 +00002213 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002214 case Primitive::kPrimByte:
2215 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002216 case Primitive::kPrimBoolean:
2217 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002218 case Primitive::kPrimShort:
2219 case Primitive::kPrimInt:
2220 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002221 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002222 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2223 // Make the output overlap to please the register allocator. This greatly simplifies
2224 // the validation of the linear scan implementation
2225 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002226 break;
2227
2228 default:
2229 LOG(FATAL) << "Unexpected type conversion from " << input_type
2230 << " to " << result_type;
2231 }
2232 break;
2233
Roland Levillain01a8d712014-11-14 16:27:39 +00002234 case Primitive::kPrimShort:
2235 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002236 case Primitive::kPrimBoolean:
2237 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002238 case Primitive::kPrimByte:
2239 case Primitive::kPrimInt:
2240 case Primitive::kPrimChar:
2241 // Processing a Dex `int-to-short' instruction.
2242 locations->SetInAt(0, Location::Any());
2243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2244 break;
2245
2246 default:
2247 LOG(FATAL) << "Unexpected type conversion from " << input_type
2248 << " to " << result_type;
2249 }
2250 break;
2251
Roland Levillain946e1432014-11-11 17:35:19 +00002252 case Primitive::kPrimInt:
2253 switch (input_type) {
2254 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002255 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002256 locations->SetInAt(0, Location::Any());
2257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2258 break;
2259
2260 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002261 // Processing a Dex `float-to-int' instruction.
2262 locations->SetInAt(0, Location::RequiresFpuRegister());
2263 locations->SetOut(Location::RequiresRegister());
2264 locations->AddTemp(Location::RequiresFpuRegister());
2265 break;
2266
Roland Levillain946e1432014-11-11 17:35:19 +00002267 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002268 // Processing a Dex `double-to-int' instruction.
2269 locations->SetInAt(0, Location::RequiresFpuRegister());
2270 locations->SetOut(Location::RequiresRegister());
2271 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002272 break;
2273
2274 default:
2275 LOG(FATAL) << "Unexpected type conversion from " << input_type
2276 << " to " << result_type;
2277 }
2278 break;
2279
Roland Levillaindff1f282014-11-05 14:15:05 +00002280 case Primitive::kPrimLong:
2281 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002282 case Primitive::kPrimBoolean:
2283 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002284 case Primitive::kPrimByte:
2285 case Primitive::kPrimShort:
2286 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002287 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002288 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002289 locations->SetInAt(0, Location::RegisterLocation(EAX));
2290 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2291 break;
2292
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002293 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002294 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002295 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002296 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002297 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2298 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2299
Vladimir Marko949c91f2015-01-27 10:48:44 +00002300 // The runtime helper puts the result in EAX, EDX.
2301 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002302 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002303 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002304
2305 default:
2306 LOG(FATAL) << "Unexpected type conversion from " << input_type
2307 << " to " << result_type;
2308 }
2309 break;
2310
Roland Levillain981e4542014-11-14 11:47:14 +00002311 case Primitive::kPrimChar:
2312 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002313 case Primitive::kPrimBoolean:
2314 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002315 case Primitive::kPrimByte:
2316 case Primitive::kPrimShort:
2317 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002318 // Processing a Dex `int-to-char' instruction.
2319 locations->SetInAt(0, Location::Any());
2320 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 }
2327 break;
2328
Roland Levillaindff1f282014-11-05 14:15:05 +00002329 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002330 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002331 case Primitive::kPrimBoolean:
2332 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002333 case Primitive::kPrimByte:
2334 case Primitive::kPrimShort:
2335 case Primitive::kPrimInt:
2336 case Primitive::kPrimChar:
2337 // Processing a Dex `int-to-float' instruction.
2338 locations->SetInAt(0, Location::RequiresRegister());
2339 locations->SetOut(Location::RequiresFpuRegister());
2340 break;
2341
2342 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002343 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002344 locations->SetInAt(0, Location::Any());
2345 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002346 break;
2347
Roland Levillaincff13742014-11-17 14:32:17 +00002348 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002349 // Processing a Dex `double-to-float' instruction.
2350 locations->SetInAt(0, Location::RequiresFpuRegister());
2351 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 };
2358 break;
2359
Roland Levillaindff1f282014-11-05 14:15:05 +00002360 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimShort:
2366 case Primitive::kPrimInt:
2367 case Primitive::kPrimChar:
2368 // Processing a Dex `int-to-double' instruction.
2369 locations->SetInAt(0, Location::RequiresRegister());
2370 locations->SetOut(Location::RequiresFpuRegister());
2371 break;
2372
2373 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002374 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002375 locations->SetInAt(0, Location::Any());
2376 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002377 break;
2378
Roland Levillaincff13742014-11-17 14:32:17 +00002379 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002380 // Processing a Dex `float-to-double' instruction.
2381 locations->SetInAt(0, Location::RequiresFpuRegister());
2382 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002383 break;
2384
2385 default:
2386 LOG(FATAL) << "Unexpected type conversion from " << input_type
2387 << " to " << result_type;
2388 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002389 break;
2390
2391 default:
2392 LOG(FATAL) << "Unexpected type conversion from " << input_type
2393 << " to " << result_type;
2394 }
2395}
2396
2397void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2398 LocationSummary* locations = conversion->GetLocations();
2399 Location out = locations->Out();
2400 Location in = locations->InAt(0);
2401 Primitive::Type result_type = conversion->GetResultType();
2402 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002403 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002404 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002405 case Primitive::kPrimByte:
2406 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002407 case Primitive::kPrimBoolean:
2408 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002409 case Primitive::kPrimShort:
2410 case Primitive::kPrimInt:
2411 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002412 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002413 if (in.IsRegister()) {
2414 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002415 } else {
2416 DCHECK(in.GetConstant()->IsIntConstant());
2417 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2418 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2419 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002420 break;
2421
2422 default:
2423 LOG(FATAL) << "Unexpected type conversion from " << input_type
2424 << " to " << result_type;
2425 }
2426 break;
2427
Roland Levillain01a8d712014-11-14 16:27:39 +00002428 case Primitive::kPrimShort:
2429 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002430 case Primitive::kPrimBoolean:
2431 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002432 case Primitive::kPrimByte:
2433 case Primitive::kPrimInt:
2434 case Primitive::kPrimChar:
2435 // Processing a Dex `int-to-short' instruction.
2436 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002437 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002438 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002439 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002440 } else {
2441 DCHECK(in.GetConstant()->IsIntConstant());
2442 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002443 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002444 }
2445 break;
2446
2447 default:
2448 LOG(FATAL) << "Unexpected type conversion from " << input_type
2449 << " to " << result_type;
2450 }
2451 break;
2452
Roland Levillain946e1432014-11-11 17:35:19 +00002453 case Primitive::kPrimInt:
2454 switch (input_type) {
2455 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002456 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002457 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002458 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002459 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002460 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002461 } else {
2462 DCHECK(in.IsConstant());
2463 DCHECK(in.GetConstant()->IsLongConstant());
2464 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002465 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002466 }
2467 break;
2468
Roland Levillain3f8f9362014-12-02 17:45:01 +00002469 case Primitive::kPrimFloat: {
2470 // Processing a Dex `float-to-int' instruction.
2471 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2472 Register output = out.AsRegister<Register>();
2473 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002474 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002475
2476 __ movl(output, Immediate(kPrimIntMax));
2477 // temp = int-to-float(output)
2478 __ cvtsi2ss(temp, output);
2479 // if input >= temp goto done
2480 __ comiss(input, temp);
2481 __ j(kAboveEqual, &done);
2482 // if input == NaN goto nan
2483 __ j(kUnordered, &nan);
2484 // output = float-to-int-truncate(input)
2485 __ cvttss2si(output, input);
2486 __ jmp(&done);
2487 __ Bind(&nan);
2488 // output = 0
2489 __ xorl(output, output);
2490 __ Bind(&done);
2491 break;
2492 }
2493
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002494 case Primitive::kPrimDouble: {
2495 // Processing a Dex `double-to-int' instruction.
2496 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2497 Register output = out.AsRegister<Register>();
2498 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002499 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002500
2501 __ movl(output, Immediate(kPrimIntMax));
2502 // temp = int-to-double(output)
2503 __ cvtsi2sd(temp, output);
2504 // if input >= temp goto done
2505 __ comisd(input, temp);
2506 __ j(kAboveEqual, &done);
2507 // if input == NaN goto nan
2508 __ j(kUnordered, &nan);
2509 // output = double-to-int-truncate(input)
2510 __ cvttsd2si(output, input);
2511 __ jmp(&done);
2512 __ Bind(&nan);
2513 // output = 0
2514 __ xorl(output, output);
2515 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002516 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002517 }
Roland Levillain946e1432014-11-11 17:35:19 +00002518
2519 default:
2520 LOG(FATAL) << "Unexpected type conversion from " << input_type
2521 << " to " << result_type;
2522 }
2523 break;
2524
Roland Levillaindff1f282014-11-05 14:15:05 +00002525 case Primitive::kPrimLong:
2526 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002527 case Primitive::kPrimBoolean:
2528 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002529 case Primitive::kPrimByte:
2530 case Primitive::kPrimShort:
2531 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002532 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002533 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002534 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2535 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002536 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 __ cdq();
2538 break;
2539
2540 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002541 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002542 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2543 conversion,
2544 conversion->GetDexPc(),
2545 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002546 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002547 break;
2548
Roland Levillaindff1f282014-11-05 14:15:05 +00002549 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002550 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002551 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2552 conversion,
2553 conversion->GetDexPc(),
2554 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002555 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 break;
2557
2558 default:
2559 LOG(FATAL) << "Unexpected type conversion from " << input_type
2560 << " to " << result_type;
2561 }
2562 break;
2563
Roland Levillain981e4542014-11-14 11:47:14 +00002564 case Primitive::kPrimChar:
2565 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002566 case Primitive::kPrimBoolean:
2567 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002568 case Primitive::kPrimByte:
2569 case Primitive::kPrimShort:
2570 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002571 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2572 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002573 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002574 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002575 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002576 } else {
2577 DCHECK(in.GetConstant()->IsIntConstant());
2578 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002579 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002580 }
2581 break;
2582
2583 default:
2584 LOG(FATAL) << "Unexpected type conversion from " << input_type
2585 << " to " << result_type;
2586 }
2587 break;
2588
Roland Levillaindff1f282014-11-05 14:15:05 +00002589 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002590 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002591 case Primitive::kPrimBoolean:
2592 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002593 case Primitive::kPrimByte:
2594 case Primitive::kPrimShort:
2595 case Primitive::kPrimInt:
2596 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002597 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002598 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002599 break;
2600
Roland Levillain6d0e4832014-11-27 18:31:21 +00002601 case Primitive::kPrimLong: {
2602 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002603 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002604
Roland Levillain232ade02015-04-20 15:14:36 +01002605 // Create stack space for the call to
2606 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2607 // TODO: enhance register allocator to ask for stack temporaries.
2608 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2609 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2610 __ subl(ESP, Immediate(adjustment));
2611 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002612
Roland Levillain232ade02015-04-20 15:14:36 +01002613 // Load the value to the FP stack, using temporaries if needed.
2614 PushOntoFPStack(in, 0, adjustment, false, true);
2615
2616 if (out.IsStackSlot()) {
2617 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2618 } else {
2619 __ fstps(Address(ESP, 0));
2620 Location stack_temp = Location::StackSlot(0);
2621 codegen_->Move32(out, stack_temp);
2622 }
2623
2624 // Remove the temporary stack space we allocated.
2625 if (adjustment != 0) {
2626 __ addl(ESP, Immediate(adjustment));
2627 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002628 break;
2629 }
2630
Roland Levillaincff13742014-11-17 14:32:17 +00002631 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002632 // Processing a Dex `double-to-float' instruction.
2633 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002634 break;
2635
2636 default:
2637 LOG(FATAL) << "Unexpected type conversion from " << input_type
2638 << " to " << result_type;
2639 };
2640 break;
2641
Roland Levillaindff1f282014-11-05 14:15:05 +00002642 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002643 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002644 case Primitive::kPrimBoolean:
2645 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002646 case Primitive::kPrimByte:
2647 case Primitive::kPrimShort:
2648 case Primitive::kPrimInt:
2649 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002650 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002651 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002652 break;
2653
Roland Levillain647b9ed2014-11-27 12:06:00 +00002654 case Primitive::kPrimLong: {
2655 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002656 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002657
Roland Levillain232ade02015-04-20 15:14:36 +01002658 // Create stack space for the call to
2659 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2660 // TODO: enhance register allocator to ask for stack temporaries.
2661 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2662 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2663 __ subl(ESP, Immediate(adjustment));
2664 }
2665
2666 // Load the value to the FP stack, using temporaries if needed.
2667 PushOntoFPStack(in, 0, adjustment, false, true);
2668
2669 if (out.IsDoubleStackSlot()) {
2670 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2671 } else {
2672 __ fstpl(Address(ESP, 0));
2673 Location stack_temp = Location::DoubleStackSlot(0);
2674 codegen_->Move64(out, stack_temp);
2675 }
2676
2677 // Remove the temporary stack space we allocated.
2678 if (adjustment != 0) {
2679 __ addl(ESP, Immediate(adjustment));
2680 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002681 break;
2682 }
2683
Roland Levillaincff13742014-11-17 14:32:17 +00002684 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002685 // Processing a Dex `float-to-double' instruction.
2686 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002687 break;
2688
2689 default:
2690 LOG(FATAL) << "Unexpected type conversion from " << input_type
2691 << " to " << result_type;
2692 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002693 break;
2694
2695 default:
2696 LOG(FATAL) << "Unexpected type conversion from " << input_type
2697 << " to " << result_type;
2698 }
2699}
2700
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002701void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002702 LocationSummary* locations =
2703 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002704 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002705 case Primitive::kPrimInt: {
2706 locations->SetInAt(0, Location::RequiresRegister());
2707 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2708 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2709 break;
2710 }
2711
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002712 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002713 locations->SetInAt(0, Location::RequiresRegister());
2714 locations->SetInAt(1, Location::Any());
2715 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002716 break;
2717 }
2718
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002719 case Primitive::kPrimFloat:
2720 case Primitive::kPrimDouble: {
2721 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002722 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002723 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002724 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002725 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002726
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002727 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002728 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2729 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002730 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002731}
2732
2733void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2734 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002735 Location first = locations->InAt(0);
2736 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002737 Location out = locations->Out();
2738
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002739 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002740 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002741 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002742 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2743 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002744 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2745 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002746 } else {
2747 __ leal(out.AsRegister<Register>(), Address(
2748 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2749 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002750 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002751 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2752 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2753 __ addl(out.AsRegister<Register>(), Immediate(value));
2754 } else {
2755 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2756 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002757 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002758 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002759 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002760 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002761 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002762 }
2763
2764 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002765 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002766 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2767 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002768 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002769 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2770 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002771 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002772 } else {
2773 DCHECK(second.IsConstant()) << second;
2774 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2775 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2776 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002777 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002778 break;
2779 }
2780
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002781 case Primitive::kPrimFloat: {
2782 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002784 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2785 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2786 DCHECK(!const_area->NeedsMaterialization());
2787 __ addss(first.AsFpuRegister<XmmRegister>(),
2788 codegen_->LiteralFloatAddress(
2789 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2790 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2791 } else {
2792 DCHECK(second.IsStackSlot());
2793 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002794 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002795 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002796 }
2797
2798 case Primitive::kPrimDouble: {
2799 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002800 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002801 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2802 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2803 DCHECK(!const_area->NeedsMaterialization());
2804 __ addsd(first.AsFpuRegister<XmmRegister>(),
2805 codegen_->LiteralDoubleAddress(
2806 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2807 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2808 } else {
2809 DCHECK(second.IsDoubleStackSlot());
2810 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002811 }
2812 break;
2813 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002814
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002815 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002816 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002817 }
2818}
2819
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002820void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002821 LocationSummary* locations =
2822 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002823 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002824 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002825 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002826 locations->SetInAt(0, Location::RequiresRegister());
2827 locations->SetInAt(1, Location::Any());
2828 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002829 break;
2830 }
Calin Juravle11351682014-10-23 15:38:15 +01002831 case Primitive::kPrimFloat:
2832 case Primitive::kPrimDouble: {
2833 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002834 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002835 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002836 break;
Calin Juravle11351682014-10-23 15:38:15 +01002837 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002838
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002839 default:
Calin Juravle11351682014-10-23 15:38:15 +01002840 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002841 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002842}
2843
2844void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2845 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002846 Location first = locations->InAt(0);
2847 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002848 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002849 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002850 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002851 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002852 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002853 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002854 __ subl(first.AsRegister<Register>(),
2855 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002856 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002857 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002858 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002859 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002860 }
2861
2862 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002863 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002864 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2865 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002866 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002867 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002868 __ sbbl(first.AsRegisterPairHigh<Register>(),
2869 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002870 } else {
2871 DCHECK(second.IsConstant()) << second;
2872 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2873 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2874 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002875 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002876 break;
2877 }
2878
Calin Juravle11351682014-10-23 15:38:15 +01002879 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002880 if (second.IsFpuRegister()) {
2881 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2882 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2883 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2884 DCHECK(!const_area->NeedsMaterialization());
2885 __ subss(first.AsFpuRegister<XmmRegister>(),
2886 codegen_->LiteralFloatAddress(
2887 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2888 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2889 } else {
2890 DCHECK(second.IsStackSlot());
2891 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2892 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002893 break;
Calin Juravle11351682014-10-23 15:38:15 +01002894 }
2895
2896 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002897 if (second.IsFpuRegister()) {
2898 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2899 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2900 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2901 DCHECK(!const_area->NeedsMaterialization());
2902 __ subsd(first.AsFpuRegister<XmmRegister>(),
2903 codegen_->LiteralDoubleAddress(
2904 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2905 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2906 } else {
2907 DCHECK(second.IsDoubleStackSlot());
2908 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2909 }
Calin Juravle11351682014-10-23 15:38:15 +01002910 break;
2911 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002912
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002913 default:
Calin Juravle11351682014-10-23 15:38:15 +01002914 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002915 }
2916}
2917
Calin Juravle34bacdf2014-10-07 20:23:36 +01002918void LocationsBuilderX86::VisitMul(HMul* mul) {
2919 LocationSummary* locations =
2920 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2921 switch (mul->GetResultType()) {
2922 case Primitive::kPrimInt:
2923 locations->SetInAt(0, Location::RequiresRegister());
2924 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002925 if (mul->InputAt(1)->IsIntConstant()) {
2926 // Can use 3 operand multiply.
2927 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2928 } else {
2929 locations->SetOut(Location::SameAsFirstInput());
2930 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002931 break;
2932 case Primitive::kPrimLong: {
2933 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002934 locations->SetInAt(1, Location::Any());
2935 locations->SetOut(Location::SameAsFirstInput());
2936 // Needed for imul on 32bits with 64bits output.
2937 locations->AddTemp(Location::RegisterLocation(EAX));
2938 locations->AddTemp(Location::RegisterLocation(EDX));
2939 break;
2940 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002941 case Primitive::kPrimFloat:
2942 case Primitive::kPrimDouble: {
2943 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002944 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002945 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002946 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002947 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002948
2949 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002950 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002951 }
2952}
2953
2954void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2955 LocationSummary* locations = mul->GetLocations();
2956 Location first = locations->InAt(0);
2957 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002958 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002959
2960 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002961 case Primitive::kPrimInt:
2962 // The constant may have ended up in a register, so test explicitly to avoid
2963 // problems where the output may not be the same as the first operand.
2964 if (mul->InputAt(1)->IsIntConstant()) {
2965 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2966 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2967 } else if (second.IsRegister()) {
2968 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002970 } else {
2971 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002972 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002973 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002974 }
2975 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002976
2977 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002978 Register in1_hi = first.AsRegisterPairHigh<Register>();
2979 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002980 Register eax = locations->GetTemp(0).AsRegister<Register>();
2981 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002982
2983 DCHECK_EQ(EAX, eax);
2984 DCHECK_EQ(EDX, edx);
2985
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002986 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002987 // output: in1
2988 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2989 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2990 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002991 if (second.IsConstant()) {
2992 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002993
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002994 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2995 int32_t low_value = Low32Bits(value);
2996 int32_t high_value = High32Bits(value);
2997 Immediate low(low_value);
2998 Immediate high(high_value);
2999
3000 __ movl(eax, high);
3001 // eax <- in1.lo * in2.hi
3002 __ imull(eax, in1_lo);
3003 // in1.hi <- in1.hi * in2.lo
3004 __ imull(in1_hi, low);
3005 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3006 __ addl(in1_hi, eax);
3007 // move in2_lo to eax to prepare for double precision
3008 __ movl(eax, low);
3009 // edx:eax <- in1.lo * in2.lo
3010 __ mull(in1_lo);
3011 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3012 __ addl(in1_hi, edx);
3013 // in1.lo <- (in1.lo * in2.lo)[31:0];
3014 __ movl(in1_lo, eax);
3015 } else if (second.IsRegisterPair()) {
3016 Register in2_hi = second.AsRegisterPairHigh<Register>();
3017 Register in2_lo = second.AsRegisterPairLow<Register>();
3018
3019 __ movl(eax, in2_hi);
3020 // eax <- in1.lo * in2.hi
3021 __ imull(eax, in1_lo);
3022 // in1.hi <- in1.hi * in2.lo
3023 __ imull(in1_hi, in2_lo);
3024 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3025 __ addl(in1_hi, eax);
3026 // move in1_lo to eax to prepare for double precision
3027 __ movl(eax, in1_lo);
3028 // edx:eax <- in1.lo * in2.lo
3029 __ mull(in2_lo);
3030 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3031 __ addl(in1_hi, edx);
3032 // in1.lo <- (in1.lo * in2.lo)[31:0];
3033 __ movl(in1_lo, eax);
3034 } else {
3035 DCHECK(second.IsDoubleStackSlot()) << second;
3036 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3037 Address in2_lo(ESP, second.GetStackIndex());
3038
3039 __ movl(eax, in2_hi);
3040 // eax <- in1.lo * in2.hi
3041 __ imull(eax, in1_lo);
3042 // in1.hi <- in1.hi * in2.lo
3043 __ imull(in1_hi, in2_lo);
3044 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3045 __ addl(in1_hi, eax);
3046 // move in1_lo to eax to prepare for double precision
3047 __ movl(eax, in1_lo);
3048 // edx:eax <- in1.lo * in2.lo
3049 __ mull(in2_lo);
3050 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3051 __ addl(in1_hi, edx);
3052 // in1.lo <- (in1.lo * in2.lo)[31:0];
3053 __ movl(in1_lo, eax);
3054 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003055
3056 break;
3057 }
3058
Calin Juravleb5bfa962014-10-21 18:02:24 +01003059 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003060 DCHECK(first.Equals(locations->Out()));
3061 if (second.IsFpuRegister()) {
3062 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3063 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3064 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3065 DCHECK(!const_area->NeedsMaterialization());
3066 __ mulss(first.AsFpuRegister<XmmRegister>(),
3067 codegen_->LiteralFloatAddress(
3068 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3069 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3070 } else {
3071 DCHECK(second.IsStackSlot());
3072 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3073 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003074 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003075 }
3076
3077 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003078 DCHECK(first.Equals(locations->Out()));
3079 if (second.IsFpuRegister()) {
3080 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3081 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3082 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3083 DCHECK(!const_area->NeedsMaterialization());
3084 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3085 codegen_->LiteralDoubleAddress(
3086 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3087 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3088 } else {
3089 DCHECK(second.IsDoubleStackSlot());
3090 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3091 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003092 break;
3093 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003094
3095 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003096 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003097 }
3098}
3099
Roland Levillain232ade02015-04-20 15:14:36 +01003100void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3101 uint32_t temp_offset,
3102 uint32_t stack_adjustment,
3103 bool is_fp,
3104 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003105 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003106 DCHECK(!is_wide);
3107 if (is_fp) {
3108 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3109 } else {
3110 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3111 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003112 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003113 DCHECK(is_wide);
3114 if (is_fp) {
3115 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3116 } else {
3117 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3118 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003119 } else {
3120 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003121 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003122 Location stack_temp = Location::StackSlot(temp_offset);
3123 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003124 if (is_fp) {
3125 __ flds(Address(ESP, temp_offset));
3126 } else {
3127 __ filds(Address(ESP, temp_offset));
3128 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003129 } else {
3130 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3131 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003132 if (is_fp) {
3133 __ fldl(Address(ESP, temp_offset));
3134 } else {
3135 __ fildl(Address(ESP, temp_offset));
3136 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003137 }
3138 }
3139}
3140
3141void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3142 Primitive::Type type = rem->GetResultType();
3143 bool is_float = type == Primitive::kPrimFloat;
3144 size_t elem_size = Primitive::ComponentSize(type);
3145 LocationSummary* locations = rem->GetLocations();
3146 Location first = locations->InAt(0);
3147 Location second = locations->InAt(1);
3148 Location out = locations->Out();
3149
3150 // Create stack space for 2 elements.
3151 // TODO: enhance register allocator to ask for stack temporaries.
3152 __ subl(ESP, Immediate(2 * elem_size));
3153
3154 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003155 const bool is_wide = !is_float;
3156 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3157 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003158
3159 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003160 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003161 __ Bind(&retry);
3162 __ fprem();
3163
3164 // Move FP status to AX.
3165 __ fstsw();
3166
3167 // And see if the argument reduction is complete. This is signaled by the
3168 // C2 FPU flag bit set to 0.
3169 __ andl(EAX, Immediate(kC2ConditionMask));
3170 __ j(kNotEqual, &retry);
3171
3172 // We have settled on the final value. Retrieve it into an XMM register.
3173 // Store FP top of stack to real stack.
3174 if (is_float) {
3175 __ fsts(Address(ESP, 0));
3176 } else {
3177 __ fstl(Address(ESP, 0));
3178 }
3179
3180 // Pop the 2 items from the FP stack.
3181 __ fucompp();
3182
3183 // Load the value from the stack into an XMM register.
3184 DCHECK(out.IsFpuRegister()) << out;
3185 if (is_float) {
3186 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3187 } else {
3188 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3189 }
3190
3191 // And remove the temporary stack space we allocated.
3192 __ addl(ESP, Immediate(2 * elem_size));
3193}
3194
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003195
3196void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3197 DCHECK(instruction->IsDiv() || instruction->IsRem());
3198
3199 LocationSummary* locations = instruction->GetLocations();
3200 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003201 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003202
3203 Register out_register = locations->Out().AsRegister<Register>();
3204 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003205 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003206
3207 DCHECK(imm == 1 || imm == -1);
3208
3209 if (instruction->IsRem()) {
3210 __ xorl(out_register, out_register);
3211 } else {
3212 __ movl(out_register, input_register);
3213 if (imm == -1) {
3214 __ negl(out_register);
3215 }
3216 }
3217}
3218
3219
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003220void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003221 LocationSummary* locations = instruction->GetLocations();
3222
3223 Register out_register = locations->Out().AsRegister<Register>();
3224 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003225 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003226
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003227 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003228 Register num = locations->GetTemp(0).AsRegister<Register>();
3229
3230 __ leal(num, Address(input_register, std::abs(imm) - 1));
3231 __ testl(input_register, input_register);
3232 __ cmovl(kGreaterEqual, num, input_register);
3233 int shift = CTZ(imm);
3234 __ sarl(num, Immediate(shift));
3235
3236 if (imm < 0) {
3237 __ negl(num);
3238 }
3239
3240 __ movl(out_register, num);
3241}
3242
3243void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3244 DCHECK(instruction->IsDiv() || instruction->IsRem());
3245
3246 LocationSummary* locations = instruction->GetLocations();
3247 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3248
3249 Register eax = locations->InAt(0).AsRegister<Register>();
3250 Register out = locations->Out().AsRegister<Register>();
3251 Register num;
3252 Register edx;
3253
3254 if (instruction->IsDiv()) {
3255 edx = locations->GetTemp(0).AsRegister<Register>();
3256 num = locations->GetTemp(1).AsRegister<Register>();
3257 } else {
3258 edx = locations->Out().AsRegister<Register>();
3259 num = locations->GetTemp(0).AsRegister<Register>();
3260 }
3261
3262 DCHECK_EQ(EAX, eax);
3263 DCHECK_EQ(EDX, edx);
3264 if (instruction->IsDiv()) {
3265 DCHECK_EQ(EAX, out);
3266 } else {
3267 DCHECK_EQ(EDX, out);
3268 }
3269
3270 int64_t magic;
3271 int shift;
3272 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3273
Mark Mendell0c9497d2015-08-21 09:30:05 -04003274 NearLabel ndiv;
3275 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003276 // If numerator is 0, the result is 0, no computation needed.
3277 __ testl(eax, eax);
3278 __ j(kNotEqual, &ndiv);
3279
3280 __ xorl(out, out);
3281 __ jmp(&end);
3282
3283 __ Bind(&ndiv);
3284
3285 // Save the numerator.
3286 __ movl(num, eax);
3287
3288 // EAX = magic
3289 __ movl(eax, Immediate(magic));
3290
3291 // EDX:EAX = magic * numerator
3292 __ imull(num);
3293
3294 if (imm > 0 && magic < 0) {
3295 // EDX += num
3296 __ addl(edx, num);
3297 } else if (imm < 0 && magic > 0) {
3298 __ subl(edx, num);
3299 }
3300
3301 // Shift if needed.
3302 if (shift != 0) {
3303 __ sarl(edx, Immediate(shift));
3304 }
3305
3306 // EDX += 1 if EDX < 0
3307 __ movl(eax, edx);
3308 __ shrl(edx, Immediate(31));
3309 __ addl(edx, eax);
3310
3311 if (instruction->IsRem()) {
3312 __ movl(eax, num);
3313 __ imull(edx, Immediate(imm));
3314 __ subl(eax, edx);
3315 __ movl(edx, eax);
3316 } else {
3317 __ movl(eax, edx);
3318 }
3319 __ Bind(&end);
3320}
3321
Calin Juravlebacfec32014-11-14 15:54:36 +00003322void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3323 DCHECK(instruction->IsDiv() || instruction->IsRem());
3324
3325 LocationSummary* locations = instruction->GetLocations();
3326 Location out = locations->Out();
3327 Location first = locations->InAt(0);
3328 Location second = locations->InAt(1);
3329 bool is_div = instruction->IsDiv();
3330
3331 switch (instruction->GetResultType()) {
3332 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003333 DCHECK_EQ(EAX, first.AsRegister<Register>());
3334 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003335
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003336 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003337 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003338
3339 if (imm == 0) {
3340 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3341 } else if (imm == 1 || imm == -1) {
3342 DivRemOneOrMinusOne(instruction);
3343 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003344 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003345 } else {
3346 DCHECK(imm <= -2 || imm >= 2);
3347 GenerateDivRemWithAnyConstant(instruction);
3348 }
3349 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003350 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003351 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003352 is_div);
3353 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003354
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003355 Register second_reg = second.AsRegister<Register>();
3356 // 0x80000000/-1 triggers an arithmetic exception!
3357 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3358 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003359
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003360 __ cmpl(second_reg, Immediate(-1));
3361 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003362
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003363 // edx:eax <- sign-extended of eax
3364 __ cdq();
3365 // eax = quotient, edx = remainder
3366 __ idivl(second_reg);
3367 __ Bind(slow_path->GetExitLabel());
3368 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003369 break;
3370 }
3371
3372 case Primitive::kPrimLong: {
3373 InvokeRuntimeCallingConvention calling_convention;
3374 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3375 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3376 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3377 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3378 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3379 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3380
3381 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003382 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3383 instruction,
3384 instruction->GetDexPc(),
3385 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003386 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003387 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003388 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3389 instruction,
3390 instruction->GetDexPc(),
3391 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003392 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003393 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003394 break;
3395 }
3396
3397 default:
3398 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3399 }
3400}
3401
Calin Juravle7c4954d2014-10-28 16:57:40 +00003402void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003403 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003404 ? LocationSummary::kCall
3405 : LocationSummary::kNoCall;
3406 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3407
Calin Juravle7c4954d2014-10-28 16:57:40 +00003408 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003409 case Primitive::kPrimInt: {
3410 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003411 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003412 locations->SetOut(Location::SameAsFirstInput());
3413 // Intel uses edx:eax as the dividend.
3414 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3416 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3417 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003418 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003419 locations->AddTemp(Location::RequiresRegister());
3420 }
Calin Juravled0d48522014-11-04 16:40:20 +00003421 break;
3422 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003423 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003424 InvokeRuntimeCallingConvention calling_convention;
3425 locations->SetInAt(0, Location::RegisterPairLocation(
3426 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3427 locations->SetInAt(1, Location::RegisterPairLocation(
3428 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3429 // Runtime helper puts the result in EAX, EDX.
3430 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003431 break;
3432 }
3433 case Primitive::kPrimFloat:
3434 case Primitive::kPrimDouble: {
3435 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003436 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003437 locations->SetOut(Location::SameAsFirstInput());
3438 break;
3439 }
3440
3441 default:
3442 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3443 }
3444}
3445
3446void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3447 LocationSummary* locations = div->GetLocations();
3448 Location first = locations->InAt(0);
3449 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003450
3451 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003452 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003453 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003454 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003455 break;
3456 }
3457
3458 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003459 if (second.IsFpuRegister()) {
3460 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3461 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3462 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3463 DCHECK(!const_area->NeedsMaterialization());
3464 __ divss(first.AsFpuRegister<XmmRegister>(),
3465 codegen_->LiteralFloatAddress(
3466 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3467 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3468 } else {
3469 DCHECK(second.IsStackSlot());
3470 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3471 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003472 break;
3473 }
3474
3475 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003476 if (second.IsFpuRegister()) {
3477 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3478 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3479 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3480 DCHECK(!const_area->NeedsMaterialization());
3481 __ divsd(first.AsFpuRegister<XmmRegister>(),
3482 codegen_->LiteralDoubleAddress(
3483 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3484 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3485 } else {
3486 DCHECK(second.IsDoubleStackSlot());
3487 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3488 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003489 break;
3490 }
3491
3492 default:
3493 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3494 }
3495}
3496
Calin Juravlebacfec32014-11-14 15:54:36 +00003497void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003498 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003499
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003500 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3501 ? LocationSummary::kCall
3502 : LocationSummary::kNoCall;
3503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003504
Calin Juravled2ec87d2014-12-08 14:24:46 +00003505 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003506 case Primitive::kPrimInt: {
3507 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003508 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003509 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003510 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3511 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3512 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003513 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003514 locations->AddTemp(Location::RequiresRegister());
3515 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003516 break;
3517 }
3518 case Primitive::kPrimLong: {
3519 InvokeRuntimeCallingConvention calling_convention;
3520 locations->SetInAt(0, Location::RegisterPairLocation(
3521 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3522 locations->SetInAt(1, Location::RegisterPairLocation(
3523 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3524 // Runtime helper puts the result in EAX, EDX.
3525 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3526 break;
3527 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003528 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003529 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003530 locations->SetInAt(0, Location::Any());
3531 locations->SetInAt(1, Location::Any());
3532 locations->SetOut(Location::RequiresFpuRegister());
3533 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003534 break;
3535 }
3536
3537 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003538 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003539 }
3540}
3541
3542void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3543 Primitive::Type type = rem->GetResultType();
3544 switch (type) {
3545 case Primitive::kPrimInt:
3546 case Primitive::kPrimLong: {
3547 GenerateDivRemIntegral(rem);
3548 break;
3549 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003550 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003551 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003552 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003553 break;
3554 }
3555 default:
3556 LOG(FATAL) << "Unexpected rem type " << type;
3557 }
3558}
3559
Calin Juravled0d48522014-11-04 16:40:20 +00003560void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003561 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3562 ? LocationSummary::kCallOnSlowPath
3563 : LocationSummary::kNoCall;
3564 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003565 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003566 case Primitive::kPrimByte:
3567 case Primitive::kPrimChar:
3568 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003569 case Primitive::kPrimInt: {
3570 locations->SetInAt(0, Location::Any());
3571 break;
3572 }
3573 case Primitive::kPrimLong: {
3574 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3575 if (!instruction->IsConstant()) {
3576 locations->AddTemp(Location::RequiresRegister());
3577 }
3578 break;
3579 }
3580 default:
3581 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3582 }
Calin Juravled0d48522014-11-04 16:40:20 +00003583 if (instruction->HasUses()) {
3584 locations->SetOut(Location::SameAsFirstInput());
3585 }
3586}
3587
3588void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003589 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003590 codegen_->AddSlowPath(slow_path);
3591
3592 LocationSummary* locations = instruction->GetLocations();
3593 Location value = locations->InAt(0);
3594
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003595 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003596 case Primitive::kPrimByte:
3597 case Primitive::kPrimChar:
3598 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003599 case Primitive::kPrimInt: {
3600 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003601 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003602 __ j(kEqual, slow_path->GetEntryLabel());
3603 } else if (value.IsStackSlot()) {
3604 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3605 __ j(kEqual, slow_path->GetEntryLabel());
3606 } else {
3607 DCHECK(value.IsConstant()) << value;
3608 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3609 __ jmp(slow_path->GetEntryLabel());
3610 }
3611 }
3612 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003613 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003614 case Primitive::kPrimLong: {
3615 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003616 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003617 __ movl(temp, value.AsRegisterPairLow<Register>());
3618 __ orl(temp, value.AsRegisterPairHigh<Register>());
3619 __ j(kEqual, slow_path->GetEntryLabel());
3620 } else {
3621 DCHECK(value.IsConstant()) << value;
3622 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3623 __ jmp(slow_path->GetEntryLabel());
3624 }
3625 }
3626 break;
3627 }
3628 default:
3629 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003630 }
Calin Juravled0d48522014-11-04 16:40:20 +00003631}
3632
Calin Juravle9aec02f2014-11-18 23:06:35 +00003633void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3634 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3635
3636 LocationSummary* locations =
3637 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3638
3639 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003640 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003641 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003642 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003643 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003644 // The shift count needs to be in CL or a constant.
3645 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003646 locations->SetOut(Location::SameAsFirstInput());
3647 break;
3648 }
3649 default:
3650 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3651 }
3652}
3653
3654void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3655 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3656
3657 LocationSummary* locations = op->GetLocations();
3658 Location first = locations->InAt(0);
3659 Location second = locations->InAt(1);
3660 DCHECK(first.Equals(locations->Out()));
3661
3662 switch (op->GetResultType()) {
3663 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003664 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003665 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003666 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003667 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003668 DCHECK_EQ(ECX, second_reg);
3669 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003670 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003671 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003672 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003673 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003674 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003675 }
3676 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003677 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3678 if (shift == 0) {
3679 return;
3680 }
3681 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003682 if (op->IsShl()) {
3683 __ shll(first_reg, imm);
3684 } else if (op->IsShr()) {
3685 __ sarl(first_reg, imm);
3686 } else {
3687 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 }
3689 }
3690 break;
3691 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003692 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003693 if (second.IsRegister()) {
3694 Register second_reg = second.AsRegister<Register>();
3695 DCHECK_EQ(ECX, second_reg);
3696 if (op->IsShl()) {
3697 GenerateShlLong(first, second_reg);
3698 } else if (op->IsShr()) {
3699 GenerateShrLong(first, second_reg);
3700 } else {
3701 GenerateUShrLong(first, second_reg);
3702 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003703 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003704 // Shift by a constant.
3705 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3706 // Nothing to do if the shift is 0, as the input is already the output.
3707 if (shift != 0) {
3708 if (op->IsShl()) {
3709 GenerateShlLong(first, shift);
3710 } else if (op->IsShr()) {
3711 GenerateShrLong(first, shift);
3712 } else {
3713 GenerateUShrLong(first, shift);
3714 }
3715 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003716 }
3717 break;
3718 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003719 default:
3720 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3721 }
3722}
3723
Mark P Mendell73945692015-04-29 14:56:17 +00003724void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3725 Register low = loc.AsRegisterPairLow<Register>();
3726 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003727 if (shift == 1) {
3728 // This is just an addition.
3729 __ addl(low, low);
3730 __ adcl(high, high);
3731 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003732 // Shift by 32 is easy. High gets low, and low gets 0.
3733 codegen_->EmitParallelMoves(
3734 loc.ToLow(),
3735 loc.ToHigh(),
3736 Primitive::kPrimInt,
3737 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3738 loc.ToLow(),
3739 Primitive::kPrimInt);
3740 } else if (shift > 32) {
3741 // Low part becomes 0. High part is low part << (shift-32).
3742 __ movl(high, low);
3743 __ shll(high, Immediate(shift - 32));
3744 __ xorl(low, low);
3745 } else {
3746 // Between 1 and 31.
3747 __ shld(high, low, Immediate(shift));
3748 __ shll(low, Immediate(shift));
3749 }
3750}
3751
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003753 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003754 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3755 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3756 __ testl(shifter, Immediate(32));
3757 __ j(kEqual, &done);
3758 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3759 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3760 __ Bind(&done);
3761}
3762
Mark P Mendell73945692015-04-29 14:56:17 +00003763void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3764 Register low = loc.AsRegisterPairLow<Register>();
3765 Register high = loc.AsRegisterPairHigh<Register>();
3766 if (shift == 32) {
3767 // Need to copy the sign.
3768 DCHECK_NE(low, high);
3769 __ movl(low, high);
3770 __ sarl(high, Immediate(31));
3771 } else if (shift > 32) {
3772 DCHECK_NE(low, high);
3773 // High part becomes sign. Low part is shifted by shift - 32.
3774 __ movl(low, high);
3775 __ sarl(high, Immediate(31));
3776 __ sarl(low, Immediate(shift - 32));
3777 } else {
3778 // Between 1 and 31.
3779 __ shrd(low, high, Immediate(shift));
3780 __ sarl(high, Immediate(shift));
3781 }
3782}
3783
Calin Juravle9aec02f2014-11-18 23:06:35 +00003784void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003785 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003786 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3787 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3788 __ testl(shifter, Immediate(32));
3789 __ j(kEqual, &done);
3790 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3791 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3792 __ Bind(&done);
3793}
3794
Mark P Mendell73945692015-04-29 14:56:17 +00003795void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3796 Register low = loc.AsRegisterPairLow<Register>();
3797 Register high = loc.AsRegisterPairHigh<Register>();
3798 if (shift == 32) {
3799 // Shift by 32 is easy. Low gets high, and high gets 0.
3800 codegen_->EmitParallelMoves(
3801 loc.ToHigh(),
3802 loc.ToLow(),
3803 Primitive::kPrimInt,
3804 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3805 loc.ToHigh(),
3806 Primitive::kPrimInt);
3807 } else if (shift > 32) {
3808 // Low part is high >> (shift - 32). High part becomes 0.
3809 __ movl(low, high);
3810 __ shrl(low, Immediate(shift - 32));
3811 __ xorl(high, high);
3812 } else {
3813 // Between 1 and 31.
3814 __ shrd(low, high, Immediate(shift));
3815 __ shrl(high, Immediate(shift));
3816 }
3817}
3818
Calin Juravle9aec02f2014-11-18 23:06:35 +00003819void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003820 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003821 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3822 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3823 __ testl(shifter, Immediate(32));
3824 __ j(kEqual, &done);
3825 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3826 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3827 __ Bind(&done);
3828}
3829
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003830void LocationsBuilderX86::VisitRor(HRor* ror) {
3831 LocationSummary* locations =
3832 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3833
3834 switch (ror->GetResultType()) {
3835 case Primitive::kPrimLong:
3836 // Add the temporary needed.
3837 locations->AddTemp(Location::RequiresRegister());
3838 FALLTHROUGH_INTENDED;
3839 case Primitive::kPrimInt:
3840 locations->SetInAt(0, Location::RequiresRegister());
3841 // The shift count needs to be in CL (unless it is a constant).
3842 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3843 locations->SetOut(Location::SameAsFirstInput());
3844 break;
3845 default:
3846 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3847 UNREACHABLE();
3848 }
3849}
3850
3851void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3852 LocationSummary* locations = ror->GetLocations();
3853 Location first = locations->InAt(0);
3854 Location second = locations->InAt(1);
3855
3856 if (ror->GetResultType() == Primitive::kPrimInt) {
3857 Register first_reg = first.AsRegister<Register>();
3858 if (second.IsRegister()) {
3859 Register second_reg = second.AsRegister<Register>();
3860 __ rorl(first_reg, second_reg);
3861 } else {
3862 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3863 __ rorl(first_reg, imm);
3864 }
3865 return;
3866 }
3867
3868 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3869 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3870 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3871 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3872 if (second.IsRegister()) {
3873 Register second_reg = second.AsRegister<Register>();
3874 DCHECK_EQ(second_reg, ECX);
3875 __ movl(temp_reg, first_reg_hi);
3876 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3877 __ shrd(first_reg_lo, temp_reg, second_reg);
3878 __ movl(temp_reg, first_reg_hi);
3879 __ testl(second_reg, Immediate(32));
3880 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3881 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3882 } else {
3883 int32_t shift_amt =
3884 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3885 if (shift_amt == 0) {
3886 // Already fine.
3887 return;
3888 }
3889 if (shift_amt == 32) {
3890 // Just swap.
3891 __ movl(temp_reg, first_reg_lo);
3892 __ movl(first_reg_lo, first_reg_hi);
3893 __ movl(first_reg_hi, temp_reg);
3894 return;
3895 }
3896
3897 Immediate imm(shift_amt);
3898 // Save the constents of the low value.
3899 __ movl(temp_reg, first_reg_lo);
3900
3901 // Shift right into low, feeding bits from high.
3902 __ shrd(first_reg_lo, first_reg_hi, imm);
3903
3904 // Shift right into high, feeding bits from the original low.
3905 __ shrd(first_reg_hi, temp_reg, imm);
3906
3907 // Swap if needed.
3908 if (shift_amt > 32) {
3909 __ movl(temp_reg, first_reg_lo);
3910 __ movl(first_reg_lo, first_reg_hi);
3911 __ movl(first_reg_hi, temp_reg);
3912 }
3913 }
3914}
3915
Calin Juravle9aec02f2014-11-18 23:06:35 +00003916void LocationsBuilderX86::VisitShl(HShl* shl) {
3917 HandleShift(shl);
3918}
3919
3920void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3921 HandleShift(shl);
3922}
3923
3924void LocationsBuilderX86::VisitShr(HShr* shr) {
3925 HandleShift(shr);
3926}
3927
3928void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3929 HandleShift(shr);
3930}
3931
3932void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3933 HandleShift(ushr);
3934}
3935
3936void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3937 HandleShift(ushr);
3938}
3939
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003940void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003941 LocationSummary* locations =
3942 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003943 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003944 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003945 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3946 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003947}
3948
3949void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003950 // Note: if heap poisoning is enabled, the entry point takes cares
3951 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003952 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3953 instruction,
3954 instruction->GetDexPc(),
3955 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003956 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003957 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003958}
3959
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003960void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3961 LocationSummary* locations =
3962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3963 locations->SetOut(Location::RegisterLocation(EAX));
3964 InvokeRuntimeCallingConvention calling_convention;
3965 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003966 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003967 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003968}
3969
3970void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3971 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003972 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003973 // Note: if heap poisoning is enabled, the entry point takes cares
3974 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003975 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3976 instruction,
3977 instruction->GetDexPc(),
3978 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003979 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003980 DCHECK(!codegen_->IsLeafMethod());
3981}
3982
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003983void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003984 LocationSummary* locations =
3985 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003986 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3987 if (location.IsStackSlot()) {
3988 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3989 } else if (location.IsDoubleStackSlot()) {
3990 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003991 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003992 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003993}
3994
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003995void InstructionCodeGeneratorX86::VisitParameterValue(
3996 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3997}
3998
3999void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4000 LocationSummary* locations =
4001 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4002 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4003}
4004
4005void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004006}
4007
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004008void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004009 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004010 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004011 locations->SetInAt(0, Location::RequiresRegister());
4012 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004013}
4014
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004015void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4016 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004017 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004018 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004019 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004020 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004021 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004022 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004023 break;
4024
4025 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004026 __ notl(out.AsRegisterPairLow<Register>());
4027 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004028 break;
4029
4030 default:
4031 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4032 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004033}
4034
David Brazdil66d126e2015-04-03 16:02:44 +01004035void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4036 LocationSummary* locations =
4037 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4038 locations->SetInAt(0, Location::RequiresRegister());
4039 locations->SetOut(Location::SameAsFirstInput());
4040}
4041
4042void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004043 LocationSummary* locations = bool_not->GetLocations();
4044 Location in = locations->InAt(0);
4045 Location out = locations->Out();
4046 DCHECK(in.Equals(out));
4047 __ xorl(out.AsRegister<Register>(), Immediate(1));
4048}
4049
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004050void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004051 LocationSummary* locations =
4052 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004053 switch (compare->InputAt(0)->GetType()) {
4054 case Primitive::kPrimLong: {
4055 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004056 locations->SetInAt(1, Location::Any());
4057 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4058 break;
4059 }
4060 case Primitive::kPrimFloat:
4061 case Primitive::kPrimDouble: {
4062 locations->SetInAt(0, Location::RequiresFpuRegister());
4063 locations->SetInAt(1, Location::RequiresFpuRegister());
4064 locations->SetOut(Location::RequiresRegister());
4065 break;
4066 }
4067 default:
4068 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4069 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004070}
4071
4072void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004073 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004074 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004075 Location left = locations->InAt(0);
4076 Location right = locations->InAt(1);
4077
Mark Mendell0c9497d2015-08-21 09:30:05 -04004078 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004079 switch (compare->InputAt(0)->GetType()) {
4080 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004081 Register left_low = left.AsRegisterPairLow<Register>();
4082 Register left_high = left.AsRegisterPairHigh<Register>();
4083 int32_t val_low = 0;
4084 int32_t val_high = 0;
4085 bool right_is_const = false;
4086
4087 if (right.IsConstant()) {
4088 DCHECK(right.GetConstant()->IsLongConstant());
4089 right_is_const = true;
4090 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4091 val_low = Low32Bits(val);
4092 val_high = High32Bits(val);
4093 }
4094
Calin Juravleddb7df22014-11-25 20:56:51 +00004095 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004096 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004097 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004098 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004099 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004100 DCHECK(right_is_const) << right;
4101 if (val_high == 0) {
4102 __ testl(left_high, left_high);
4103 } else {
4104 __ cmpl(left_high, Immediate(val_high));
4105 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004106 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004107 __ j(kLess, &less); // Signed compare.
4108 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004109 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004110 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004111 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004112 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004113 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004114 DCHECK(right_is_const) << right;
4115 if (val_low == 0) {
4116 __ testl(left_low, left_low);
4117 } else {
4118 __ cmpl(left_low, Immediate(val_low));
4119 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004120 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004121 break;
4122 }
4123 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004124 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004125 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4126 break;
4127 }
4128 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004129 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004130 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004131 break;
4132 }
4133 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004134 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004135 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004136 __ movl(out, Immediate(0));
4137 __ j(kEqual, &done);
4138 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4139
4140 __ Bind(&greater);
4141 __ movl(out, Immediate(1));
4142 __ jmp(&done);
4143
4144 __ Bind(&less);
4145 __ movl(out, Immediate(-1));
4146
4147 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004148}
4149
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004150void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004151 LocationSummary* locations =
4152 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004153 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4154 locations->SetInAt(i, Location::Any());
4155 }
4156 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004157}
4158
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004159void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004160 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004161}
4162
Roland Levillain7c1559a2015-12-15 10:55:36 +00004163void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004164 /*
4165 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4166 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4167 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4168 */
4169 switch (kind) {
4170 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004171 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004172 break;
4173 }
4174 case MemBarrierKind::kAnyStore:
4175 case MemBarrierKind::kLoadAny:
4176 case MemBarrierKind::kStoreStore: {
4177 // nop
4178 break;
4179 }
4180 default:
4181 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004182 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004183}
4184
Vladimir Markodc151b22015-10-15 18:02:30 +01004185HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4186 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4187 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004188 switch (desired_dispatch_info.code_ptr_location) {
4189 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4190 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4191 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4192 // (Though the direct CALL ptr16:32 is available for consideration).
4193 return HInvokeStaticOrDirect::DispatchInfo {
4194 desired_dispatch_info.method_load_kind,
4195 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4196 desired_dispatch_info.method_load_data,
4197 0u
4198 };
4199 default:
4200 return desired_dispatch_info;
4201 }
4202}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004203
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004204Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4205 Register temp) {
4206 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004207 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004208 if (!invoke->GetLocations()->Intrinsified()) {
4209 return location.AsRegister<Register>();
4210 }
4211 // For intrinsics we allow any location, so it may be on the stack.
4212 if (!location.IsRegister()) {
4213 __ movl(temp, Address(ESP, location.GetStackIndex()));
4214 return temp;
4215 }
4216 // For register locations, check if the register was saved. If so, get it from the stack.
4217 // Note: There is a chance that the register was saved but not overwritten, so we could
4218 // save one load. However, since this is just an intrinsic slow path we prefer this
4219 // simple and more robust approach rather that trying to determine if that's the case.
4220 SlowPathCode* slow_path = GetCurrentSlowPath();
4221 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4222 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4223 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4224 __ movl(temp, Address(ESP, stack_offset));
4225 return temp;
4226 }
4227 return location.AsRegister<Register>();
4228}
4229
Vladimir Marko58155012015-08-19 12:49:41 +00004230void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4231 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4232 switch (invoke->GetMethodLoadKind()) {
4233 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4234 // temp = thread->string_init_entrypoint
4235 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4236 break;
4237 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004238 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004239 break;
4240 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4241 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4242 break;
4243 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4244 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4245 method_patches_.emplace_back(invoke->GetTargetMethod());
4246 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4247 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004248 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4249 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4250 temp.AsRegister<Register>());
4251 uint32_t offset = invoke->GetDexCacheArrayOffset();
4252 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4253 // Add the patch entry and bind its label at the end of the instruction.
4254 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4255 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4256 break;
4257 }
Vladimir Marko58155012015-08-19 12:49:41 +00004258 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004259 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004260 Register method_reg;
4261 Register reg = temp.AsRegister<Register>();
4262 if (current_method.IsRegister()) {
4263 method_reg = current_method.AsRegister<Register>();
4264 } else {
4265 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4266 DCHECK(!current_method.IsValid());
4267 method_reg = reg;
4268 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4269 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004270 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004271 __ movl(reg, Address(method_reg,
4272 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004273 // temp = temp[index_in_cache]
4274 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4275 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4276 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004277 }
Vladimir Marko58155012015-08-19 12:49:41 +00004278 }
4279
4280 switch (invoke->GetCodePtrLocation()) {
4281 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4282 __ call(GetFrameEntryLabel());
4283 break;
4284 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4285 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4286 Label* label = &relative_call_patches_.back().label;
4287 __ call(label); // Bind to the patch label, override at link time.
4288 __ Bind(label); // Bind the label at the end of the "call" insn.
4289 break;
4290 }
4291 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4292 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004293 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4294 LOG(FATAL) << "Unsupported";
4295 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004296 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4297 // (callee_method + offset_of_quick_compiled_code)()
4298 __ call(Address(callee_method.AsRegister<Register>(),
4299 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4300 kX86WordSize).Int32Value()));
4301 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004302 }
4303
4304 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004305}
4306
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004307void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4308 Register temp = temp_in.AsRegister<Register>();
4309 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4310 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004311
4312 // Use the calling convention instead of the location of the receiver, as
4313 // intrinsics may have put the receiver in a different register. In the intrinsics
4314 // slow path, the arguments have been moved to the right place, so here we are
4315 // guaranteed that the receiver is the first register of the calling convention.
4316 InvokeDexCallingConvention calling_convention;
4317 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004318 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004319 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004320 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004321 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004322 // Instead of simply (possibly) unpoisoning `temp` here, we should
4323 // emit a read barrier for the previous class reference load.
4324 // However this is not required in practice, as this is an
4325 // intermediate/temporary reference and because the current
4326 // concurrent copying collector keeps the from-space memory
4327 // intact/accessible until the end of the marking phase (the
4328 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004329 __ MaybeUnpoisonHeapReference(temp);
4330 // temp = temp->GetMethodAt(method_offset);
4331 __ movl(temp, Address(temp, method_offset));
4332 // call temp->GetEntryPoint();
4333 __ call(Address(
4334 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4335}
4336
Vladimir Marko58155012015-08-19 12:49:41 +00004337void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4338 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004339 size_t size =
4340 method_patches_.size() +
4341 relative_call_patches_.size() +
4342 pc_relative_dex_cache_patches_.size();
4343 linker_patches->reserve(size);
4344 // The label points to the end of the "movl" insn but the literal offset for method
4345 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4346 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004347 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004348 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004349 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4350 info.target_method.dex_file,
4351 info.target_method.dex_method_index));
4352 }
4353 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004354 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004355 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4356 info.target_method.dex_file,
4357 info.target_method.dex_method_index));
4358 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004359 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4360 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4361 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4362 &info.target_dex_file,
4363 GetMethodAddressOffset(),
4364 info.element_offset));
4365 }
Vladimir Marko58155012015-08-19 12:49:41 +00004366}
4367
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004368void CodeGeneratorX86::MarkGCCard(Register temp,
4369 Register card,
4370 Register object,
4371 Register value,
4372 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004373 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004374 if (value_can_be_null) {
4375 __ testl(value, value);
4376 __ j(kEqual, &is_null);
4377 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004378 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4379 __ movl(temp, object);
4380 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004381 __ movb(Address(temp, card, TIMES_1, 0),
4382 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004383 if (value_can_be_null) {
4384 __ Bind(&is_null);
4385 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004386}
4387
Calin Juravle52c48962014-12-16 17:02:57 +00004388void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4389 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004390
4391 bool object_field_get_with_read_barrier =
4392 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004393 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004394 new (GetGraph()->GetArena()) LocationSummary(instruction,
4395 kEmitCompilerReadBarrier ?
4396 LocationSummary::kCallOnSlowPath :
4397 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004398 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004399
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004400 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4401 locations->SetOut(Location::RequiresFpuRegister());
4402 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004403 // The output overlaps in case of long: we don't want the low move
4404 // to overwrite the object's location. Likewise, in the case of
4405 // an object field get with read barriers enabled, we do not want
4406 // the move to overwrite the object's location, as we need it to emit
4407 // the read barrier.
4408 locations->SetOut(
4409 Location::RequiresRegister(),
4410 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4411 Location::kOutputOverlap :
4412 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004413 }
Calin Juravle52c48962014-12-16 17:02:57 +00004414
4415 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4416 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004417 // So we use an XMM register as a temp to achieve atomicity (first
4418 // load the temp into the XMM and then copy the XMM into the
4419 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004420 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004421 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4422 // We need a temporary register for the read barrier marking slow
4423 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4424 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004425 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004426}
4427
Calin Juravle52c48962014-12-16 17:02:57 +00004428void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4429 const FieldInfo& field_info) {
4430 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004431
Calin Juravle52c48962014-12-16 17:02:57 +00004432 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004433 Location base_loc = locations->InAt(0);
4434 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004435 Location out = locations->Out();
4436 bool is_volatile = field_info.IsVolatile();
4437 Primitive::Type field_type = field_info.GetFieldType();
4438 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4439
4440 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004442 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443 break;
4444 }
4445
4446 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004447 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448 break;
4449 }
4450
4451 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004452 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004453 break;
4454 }
4455
4456 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004457 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004458 break;
4459 }
4460
4461 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004462 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004463 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004464
4465 case Primitive::kPrimNot: {
4466 // /* HeapReference<Object> */ out = *(base + offset)
4467 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4468 Location temp_loc = locations->GetTemp(0);
4469 // Note that a potential implicit null check is handled in this
4470 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4471 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4472 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4473 if (is_volatile) {
4474 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4475 }
4476 } else {
4477 __ movl(out.AsRegister<Register>(), Address(base, offset));
4478 codegen_->MaybeRecordImplicitNullCheck(instruction);
4479 if (is_volatile) {
4480 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4481 }
4482 // If read barriers are enabled, emit read barriers other than
4483 // Baker's using a slow path (and also unpoison the loaded
4484 // reference, if heap poisoning is enabled).
4485 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4486 }
4487 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004488 }
4489
4490 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004491 if (is_volatile) {
4492 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4493 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004494 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004495 __ movd(out.AsRegisterPairLow<Register>(), temp);
4496 __ psrlq(temp, Immediate(32));
4497 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4498 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004499 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004500 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004501 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004502 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4503 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004504 break;
4505 }
4506
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004507 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004508 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004509 break;
4510 }
4511
4512 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004513 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004514 break;
4515 }
4516
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004517 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004518 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004519 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004520 }
Calin Juravle52c48962014-12-16 17:02:57 +00004521
Roland Levillain7c1559a2015-12-15 10:55:36 +00004522 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4523 // Potential implicit null checks, in the case of reference or
4524 // long fields, are handled in the previous switch statement.
4525 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004526 codegen_->MaybeRecordImplicitNullCheck(instruction);
4527 }
4528
Calin Juravle52c48962014-12-16 17:02:57 +00004529 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004530 if (field_type == Primitive::kPrimNot) {
4531 // Memory barriers, in the case of references, are also handled
4532 // in the previous switch statement.
4533 } else {
4534 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4535 }
Roland Levillain4d027112015-07-01 15:41:14 +01004536 }
Calin Juravle52c48962014-12-16 17:02:57 +00004537}
4538
4539void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4540 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4541
4542 LocationSummary* locations =
4543 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4544 locations->SetInAt(0, Location::RequiresRegister());
4545 bool is_volatile = field_info.IsVolatile();
4546 Primitive::Type field_type = field_info.GetFieldType();
4547 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4548 || (field_type == Primitive::kPrimByte);
4549
4550 // The register allocator does not support multiple
4551 // inputs that die at entry with one in a specific register.
4552 if (is_byte_type) {
4553 // Ensure the value is in a byte register.
4554 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004555 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004556 if (is_volatile && field_type == Primitive::kPrimDouble) {
4557 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4558 locations->SetInAt(1, Location::RequiresFpuRegister());
4559 } else {
4560 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4561 }
4562 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4563 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004564 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004565
Calin Juravle52c48962014-12-16 17:02:57 +00004566 // 64bits value can be atomically written to an address with movsd and an XMM register.
4567 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4568 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4569 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4570 // isolated cases when we need this it isn't worth adding the extra complexity.
4571 locations->AddTemp(Location::RequiresFpuRegister());
4572 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004573 } else {
4574 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4575
4576 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4577 // Temporary registers for the write barrier.
4578 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4579 // Ensure the card is in a byte register.
4580 locations->AddTemp(Location::RegisterLocation(ECX));
4581 }
Calin Juravle52c48962014-12-16 17:02:57 +00004582 }
4583}
4584
4585void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004586 const FieldInfo& field_info,
4587 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004588 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4589
4590 LocationSummary* locations = instruction->GetLocations();
4591 Register base = locations->InAt(0).AsRegister<Register>();
4592 Location value = locations->InAt(1);
4593 bool is_volatile = field_info.IsVolatile();
4594 Primitive::Type field_type = field_info.GetFieldType();
4595 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004596 bool needs_write_barrier =
4597 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004598
4599 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004600 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004601 }
4602
Mark Mendell81489372015-11-04 11:30:41 -05004603 bool maybe_record_implicit_null_check_done = false;
4604
Calin Juravle52c48962014-12-16 17:02:57 +00004605 switch (field_type) {
4606 case Primitive::kPrimBoolean:
4607 case Primitive::kPrimByte: {
4608 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4609 break;
4610 }
4611
4612 case Primitive::kPrimShort:
4613 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004614 if (value.IsConstant()) {
4615 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4616 __ movw(Address(base, offset), Immediate(v));
4617 } else {
4618 __ movw(Address(base, offset), value.AsRegister<Register>());
4619 }
Calin Juravle52c48962014-12-16 17:02:57 +00004620 break;
4621 }
4622
4623 case Primitive::kPrimInt:
4624 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004625 if (kPoisonHeapReferences && needs_write_barrier) {
4626 // Note that in the case where `value` is a null reference,
4627 // we do not enter this block, as the reference does not
4628 // need poisoning.
4629 DCHECK_EQ(field_type, Primitive::kPrimNot);
4630 Register temp = locations->GetTemp(0).AsRegister<Register>();
4631 __ movl(temp, value.AsRegister<Register>());
4632 __ PoisonHeapReference(temp);
4633 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004634 } else if (value.IsConstant()) {
4635 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4636 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004637 } else {
4638 __ movl(Address(base, offset), value.AsRegister<Register>());
4639 }
Calin Juravle52c48962014-12-16 17:02:57 +00004640 break;
4641 }
4642
4643 case Primitive::kPrimLong: {
4644 if (is_volatile) {
4645 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4646 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4647 __ movd(temp1, value.AsRegisterPairLow<Register>());
4648 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4649 __ punpckldq(temp1, temp2);
4650 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004651 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004652 } else if (value.IsConstant()) {
4653 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4654 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4655 codegen_->MaybeRecordImplicitNullCheck(instruction);
4656 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004657 } else {
4658 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004659 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004660 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4661 }
Mark Mendell81489372015-11-04 11:30:41 -05004662 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004663 break;
4664 }
4665
4666 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004667 if (value.IsConstant()) {
4668 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4669 __ movl(Address(base, offset), Immediate(v));
4670 } else {
4671 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4672 }
Calin Juravle52c48962014-12-16 17:02:57 +00004673 break;
4674 }
4675
4676 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004677 if (value.IsConstant()) {
4678 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4679 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4680 codegen_->MaybeRecordImplicitNullCheck(instruction);
4681 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4682 maybe_record_implicit_null_check_done = true;
4683 } else {
4684 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4685 }
Calin Juravle52c48962014-12-16 17:02:57 +00004686 break;
4687 }
4688
4689 case Primitive::kPrimVoid:
4690 LOG(FATAL) << "Unreachable type " << field_type;
4691 UNREACHABLE();
4692 }
4693
Mark Mendell81489372015-11-04 11:30:41 -05004694 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004695 codegen_->MaybeRecordImplicitNullCheck(instruction);
4696 }
4697
Roland Levillain4d027112015-07-01 15:41:14 +01004698 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004699 Register temp = locations->GetTemp(0).AsRegister<Register>();
4700 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004701 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004702 }
4703
Calin Juravle52c48962014-12-16 17:02:57 +00004704 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004705 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004706 }
4707}
4708
4709void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4710 HandleFieldGet(instruction, instruction->GetFieldInfo());
4711}
4712
4713void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4714 HandleFieldGet(instruction, instruction->GetFieldInfo());
4715}
4716
4717void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4718 HandleFieldSet(instruction, instruction->GetFieldInfo());
4719}
4720
4721void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004722 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004723}
4724
4725void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4726 HandleFieldSet(instruction, instruction->GetFieldInfo());
4727}
4728
4729void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004730 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004731}
4732
4733void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4734 HandleFieldGet(instruction, instruction->GetFieldInfo());
4735}
4736
4737void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4738 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004739}
4740
Calin Juravlee460d1d2015-09-29 04:52:17 +01004741void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4742 HUnresolvedInstanceFieldGet* instruction) {
4743 FieldAccessCallingConventionX86 calling_convention;
4744 codegen_->CreateUnresolvedFieldLocationSummary(
4745 instruction, instruction->GetFieldType(), calling_convention);
4746}
4747
4748void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4749 HUnresolvedInstanceFieldGet* instruction) {
4750 FieldAccessCallingConventionX86 calling_convention;
4751 codegen_->GenerateUnresolvedFieldAccess(instruction,
4752 instruction->GetFieldType(),
4753 instruction->GetFieldIndex(),
4754 instruction->GetDexPc(),
4755 calling_convention);
4756}
4757
4758void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4759 HUnresolvedInstanceFieldSet* instruction) {
4760 FieldAccessCallingConventionX86 calling_convention;
4761 codegen_->CreateUnresolvedFieldLocationSummary(
4762 instruction, instruction->GetFieldType(), calling_convention);
4763}
4764
4765void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4766 HUnresolvedInstanceFieldSet* instruction) {
4767 FieldAccessCallingConventionX86 calling_convention;
4768 codegen_->GenerateUnresolvedFieldAccess(instruction,
4769 instruction->GetFieldType(),
4770 instruction->GetFieldIndex(),
4771 instruction->GetDexPc(),
4772 calling_convention);
4773}
4774
4775void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4776 HUnresolvedStaticFieldGet* instruction) {
4777 FieldAccessCallingConventionX86 calling_convention;
4778 codegen_->CreateUnresolvedFieldLocationSummary(
4779 instruction, instruction->GetFieldType(), calling_convention);
4780}
4781
4782void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4783 HUnresolvedStaticFieldGet* instruction) {
4784 FieldAccessCallingConventionX86 calling_convention;
4785 codegen_->GenerateUnresolvedFieldAccess(instruction,
4786 instruction->GetFieldType(),
4787 instruction->GetFieldIndex(),
4788 instruction->GetDexPc(),
4789 calling_convention);
4790}
4791
4792void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4793 HUnresolvedStaticFieldSet* instruction) {
4794 FieldAccessCallingConventionX86 calling_convention;
4795 codegen_->CreateUnresolvedFieldLocationSummary(
4796 instruction, instruction->GetFieldType(), calling_convention);
4797}
4798
4799void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4800 HUnresolvedStaticFieldSet* instruction) {
4801 FieldAccessCallingConventionX86 calling_convention;
4802 codegen_->GenerateUnresolvedFieldAccess(instruction,
4803 instruction->GetFieldType(),
4804 instruction->GetFieldIndex(),
4805 instruction->GetDexPc(),
4806 calling_convention);
4807}
4808
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004809void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004810 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4811 ? LocationSummary::kCallOnSlowPath
4812 : LocationSummary::kNoCall;
4813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4814 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004815 ? Location::RequiresRegister()
4816 : Location::Any();
4817 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004818 if (instruction->HasUses()) {
4819 locations->SetOut(Location::SameAsFirstInput());
4820 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004821}
4822
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004823void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004824 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4825 return;
4826 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004827 LocationSummary* locations = instruction->GetLocations();
4828 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004829
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004830 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4831 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4832}
4833
4834void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004835 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004836 codegen_->AddSlowPath(slow_path);
4837
4838 LocationSummary* locations = instruction->GetLocations();
4839 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004840
4841 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004842 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004843 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004844 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004845 } else {
4846 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004847 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004848 __ jmp(slow_path->GetEntryLabel());
4849 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004850 }
4851 __ j(kEqual, slow_path->GetEntryLabel());
4852}
4853
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004854void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004855 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004856 GenerateImplicitNullCheck(instruction);
4857 } else {
4858 GenerateExplicitNullCheck(instruction);
4859 }
4860}
4861
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004862void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004863 bool object_array_get_with_read_barrier =
4864 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004865 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004866 new (GetGraph()->GetArena()) LocationSummary(instruction,
4867 object_array_get_with_read_barrier ?
4868 LocationSummary::kCallOnSlowPath :
4869 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004870 locations->SetInAt(0, Location::RequiresRegister());
4871 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004872 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4873 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4874 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004875 // The output overlaps in case of long: we don't want the low move
4876 // to overwrite the array's location. Likewise, in the case of an
4877 // object array get with read barriers enabled, we do not want the
4878 // move to overwrite the array's location, as we need it to emit
4879 // the read barrier.
4880 locations->SetOut(
4881 Location::RequiresRegister(),
4882 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4883 Location::kOutputOverlap :
4884 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004885 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004886 // We need a temporary register for the read barrier marking slow
4887 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4888 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4889 locations->AddTemp(Location::RequiresRegister());
4890 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004891}
4892
4893void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4894 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004895 Location obj_loc = locations->InAt(0);
4896 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004897 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004898 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899
Calin Juravle77520bc2015-01-12 18:45:46 +00004900 Primitive::Type type = instruction->GetType();
4901 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004902 case Primitive::kPrimBoolean: {
4903 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004904 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004905 if (index.IsConstant()) {
4906 __ movzxb(out, Address(obj,
4907 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4908 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004909 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004910 }
4911 break;
4912 }
4913
4914 case Primitive::kPrimByte: {
4915 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004916 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004917 if (index.IsConstant()) {
4918 __ movsxb(out, Address(obj,
4919 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4920 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004921 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922 }
4923 break;
4924 }
4925
4926 case Primitive::kPrimShort: {
4927 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004928 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004929 if (index.IsConstant()) {
4930 __ movsxw(out, Address(obj,
4931 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4932 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004933 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934 }
4935 break;
4936 }
4937
4938 case Primitive::kPrimChar: {
4939 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004940 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004941 if (index.IsConstant()) {
4942 __ movzxw(out, Address(obj,
4943 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4944 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004945 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 }
4947 break;
4948 }
4949
Roland Levillain7c1559a2015-12-15 10:55:36 +00004950 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004951 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004952 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004953 if (index.IsConstant()) {
4954 __ movl(out, Address(obj,
4955 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4956 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004957 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004958 }
4959 break;
4960 }
4961
Roland Levillain7c1559a2015-12-15 10:55:36 +00004962 case Primitive::kPrimNot: {
4963 static_assert(
4964 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4965 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4966 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4967 // /* HeapReference<Object> */ out =
4968 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4969 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4970 Location temp = locations->GetTemp(0);
4971 // Note that a potential implicit null check is handled in this
4972 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4973 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4974 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4975 } else {
4976 Register out = out_loc.AsRegister<Register>();
4977 if (index.IsConstant()) {
4978 uint32_t offset =
4979 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4980 __ movl(out, Address(obj, offset));
4981 codegen_->MaybeRecordImplicitNullCheck(instruction);
4982 // If read barriers are enabled, emit read barriers other than
4983 // Baker's using a slow path (and also unpoison the loaded
4984 // reference, if heap poisoning is enabled).
4985 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4986 } else {
4987 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4988 codegen_->MaybeRecordImplicitNullCheck(instruction);
4989 // If read barriers are enabled, emit read barriers other than
4990 // Baker's using a slow path (and also unpoison the loaded
4991 // reference, if heap poisoning is enabled).
4992 codegen_->MaybeGenerateReadBarrierSlow(
4993 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4994 }
4995 }
4996 break;
4997 }
4998
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999 case Primitive::kPrimLong: {
5000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005001 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005002 if (index.IsConstant()) {
5003 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005004 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005005 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005006 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005007 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005008 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005009 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005010 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005011 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005012 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005013 }
5014 break;
5015 }
5016
Mark Mendell7c8d0092015-01-26 11:21:33 -05005017 case Primitive::kPrimFloat: {
5018 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005019 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005020 if (index.IsConstant()) {
5021 __ movss(out, Address(obj,
5022 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5023 } else {
5024 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5025 }
5026 break;
5027 }
5028
5029 case Primitive::kPrimDouble: {
5030 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005031 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005032 if (index.IsConstant()) {
5033 __ movsd(out, Address(obj,
5034 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5035 } else {
5036 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5037 }
5038 break;
5039 }
5040
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005042 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005043 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005044 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005045
Roland Levillain7c1559a2015-12-15 10:55:36 +00005046 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5047 // Potential implicit null checks, in the case of reference or
5048 // long arrays, are handled in the previous switch statement.
5049 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005050 codegen_->MaybeRecordImplicitNullCheck(instruction);
5051 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005052}
5053
5054void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05005055 // This location builder might end up asking to up to four registers, which is
5056 // not currently possible for baseline. The situation in which we need four
5057 // registers cannot be met by baseline though, because it has not run any
5058 // optimization.
5059
Nicolas Geoffray39468442014-09-02 15:17:15 +01005060 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005061
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005062 bool needs_write_barrier =
5063 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005064 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5065 bool object_array_set_with_read_barrier =
5066 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005067
Nicolas Geoffray39468442014-09-02 15:17:15 +01005068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5069 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005070 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5071 LocationSummary::kCallOnSlowPath :
5072 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005073
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005074 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5075 || (value_type == Primitive::kPrimByte);
5076 // We need the inputs to be different than the output in case of long operation.
5077 // In case of a byte operation, the register allocator does not support multiple
5078 // inputs that die at entry with one in a specific register.
5079 locations->SetInAt(0, Location::RequiresRegister());
5080 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5081 if (is_byte_type) {
5082 // Ensure the value is in a byte register.
5083 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5084 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005085 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005087 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5088 }
5089 if (needs_write_barrier) {
5090 // Temporary registers for the write barrier.
5091 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5092 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005093 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005094 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005095}
5096
5097void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5098 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005099 Location array_loc = locations->InAt(0);
5100 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005101 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005102 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005103 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005104 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5105 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5106 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005107 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005108 bool needs_write_barrier =
5109 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110
5111 switch (value_type) {
5112 case Primitive::kPrimBoolean:
5113 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005114 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5115 Address address = index.IsConstant()
5116 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5117 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5118 if (value.IsRegister()) {
5119 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005120 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005121 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005123 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 break;
5125 }
5126
5127 case Primitive::kPrimShort:
5128 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005129 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5130 Address address = index.IsConstant()
5131 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5132 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5133 if (value.IsRegister()) {
5134 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005135 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005136 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005137 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005138 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139 break;
5140 }
5141
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005142 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005143 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5144 Address address = index.IsConstant()
5145 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5146 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005147
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005148 if (!value.IsRegister()) {
5149 // Just setting null.
5150 DCHECK(instruction->InputAt(2)->IsNullConstant());
5151 DCHECK(value.IsConstant()) << value;
5152 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005153 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005154 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005155 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005156 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005158
5159 DCHECK(needs_write_barrier);
5160 Register register_value = value.AsRegister<Register>();
5161 NearLabel done, not_null, do_put;
5162 SlowPathCode* slow_path = nullptr;
5163 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005164 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005165 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5166 codegen_->AddSlowPath(slow_path);
5167 if (instruction->GetValueCanBeNull()) {
5168 __ testl(register_value, register_value);
5169 __ j(kNotEqual, &not_null);
5170 __ movl(address, Immediate(0));
5171 codegen_->MaybeRecordImplicitNullCheck(instruction);
5172 __ jmp(&done);
5173 __ Bind(&not_null);
5174 }
5175
Roland Levillain0d5a2812015-11-13 10:07:31 +00005176 if (kEmitCompilerReadBarrier) {
5177 // When read barriers are enabled, the type checking
5178 // instrumentation requires two read barriers:
5179 //
5180 // __ movl(temp2, temp);
5181 // // /* HeapReference<Class> */ temp = temp->component_type_
5182 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005183 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005184 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5185 //
5186 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5187 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005188 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005189 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5190 //
5191 // __ cmpl(temp, temp2);
5192 //
5193 // However, the second read barrier may trash `temp`, as it
5194 // is a temporary register, and as such would not be saved
5195 // along with live registers before calling the runtime (nor
5196 // restored afterwards). So in this case, we bail out and
5197 // delegate the work to the array set slow path.
5198 //
5199 // TODO: Extend the register allocator to support a new
5200 // "(locally) live temp" location so as to avoid always
5201 // going into the slow path when read barriers are enabled.
5202 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005203 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005204 // /* HeapReference<Class> */ temp = array->klass_
5205 __ movl(temp, Address(array, class_offset));
5206 codegen_->MaybeRecordImplicitNullCheck(instruction);
5207 __ MaybeUnpoisonHeapReference(temp);
5208
5209 // /* HeapReference<Class> */ temp = temp->component_type_
5210 __ movl(temp, Address(temp, component_offset));
5211 // If heap poisoning is enabled, no need to unpoison `temp`
5212 // nor the object reference in `register_value->klass`, as
5213 // we are comparing two poisoned references.
5214 __ cmpl(temp, Address(register_value, class_offset));
5215
5216 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5217 __ j(kEqual, &do_put);
5218 // If heap poisoning is enabled, the `temp` reference has
5219 // not been unpoisoned yet; unpoison it now.
5220 __ MaybeUnpoisonHeapReference(temp);
5221
5222 // /* HeapReference<Class> */ temp = temp->super_class_
5223 __ movl(temp, Address(temp, super_offset));
5224 // If heap poisoning is enabled, no need to unpoison
5225 // `temp`, as we are comparing against null below.
5226 __ testl(temp, temp);
5227 __ j(kNotEqual, slow_path->GetEntryLabel());
5228 __ Bind(&do_put);
5229 } else {
5230 __ j(kNotEqual, slow_path->GetEntryLabel());
5231 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005232 }
5233 }
5234
5235 if (kPoisonHeapReferences) {
5236 __ movl(temp, register_value);
5237 __ PoisonHeapReference(temp);
5238 __ movl(address, temp);
5239 } else {
5240 __ movl(address, register_value);
5241 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005242 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005243 codegen_->MaybeRecordImplicitNullCheck(instruction);
5244 }
5245
5246 Register card = locations->GetTemp(1).AsRegister<Register>();
5247 codegen_->MarkGCCard(
5248 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5249 __ Bind(&done);
5250
5251 if (slow_path != nullptr) {
5252 __ Bind(slow_path->GetExitLabel());
5253 }
5254
5255 break;
5256 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005257
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005258 case Primitive::kPrimInt: {
5259 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5260 Address address = index.IsConstant()
5261 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5262 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5263 if (value.IsRegister()) {
5264 __ movl(address, value.AsRegister<Register>());
5265 } else {
5266 DCHECK(value.IsConstant()) << value;
5267 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5268 __ movl(address, Immediate(v));
5269 }
5270 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271 break;
5272 }
5273
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 case Primitive::kPrimLong: {
5275 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 if (index.IsConstant()) {
5277 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005278 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005280 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005281 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005282 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005283 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005284 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005286 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005288 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005289 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005290 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005291 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005292 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005293 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005294 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005295 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005296 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005297 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005298 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005300 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005301 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005302 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005303 Immediate(High32Bits(val)));
5304 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005305 }
5306 break;
5307 }
5308
Mark Mendell7c8d0092015-01-26 11:21:33 -05005309 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005310 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5311 Address address = index.IsConstant()
5312 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5313 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005314 if (value.IsFpuRegister()) {
5315 __ movss(address, value.AsFpuRegister<XmmRegister>());
5316 } else {
5317 DCHECK(value.IsConstant());
5318 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5319 __ movl(address, Immediate(v));
5320 }
5321 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005322 break;
5323 }
5324
5325 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005326 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5327 Address address = index.IsConstant()
5328 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5329 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005330 if (value.IsFpuRegister()) {
5331 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5332 } else {
5333 DCHECK(value.IsConstant());
5334 Address address_hi = index.IsConstant() ?
5335 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5336 offset + kX86WordSize) :
5337 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5338 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5339 __ movl(address, Immediate(Low32Bits(v)));
5340 codegen_->MaybeRecordImplicitNullCheck(instruction);
5341 __ movl(address_hi, Immediate(High32Bits(v)));
5342 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005343 break;
5344 }
5345
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005346 case Primitive::kPrimVoid:
5347 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005348 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005349 }
5350}
5351
5352void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5353 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005354 locations->SetInAt(0, Location::RequiresRegister());
5355 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005356}
5357
5358void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5359 LocationSummary* locations = instruction->GetLocations();
5360 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005361 Register obj = locations->InAt(0).AsRegister<Register>();
5362 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005363 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005364 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005365}
5366
5367void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005368 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5369 ? LocationSummary::kCallOnSlowPath
5370 : LocationSummary::kNoCall;
5371 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005372 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005373 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005374 if (instruction->HasUses()) {
5375 locations->SetOut(Location::SameAsFirstInput());
5376 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377}
5378
5379void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5380 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005381 Location index_loc = locations->InAt(0);
5382 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005383 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005384 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385
Mark Mendell99dbd682015-04-22 16:18:52 -04005386 if (length_loc.IsConstant()) {
5387 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5388 if (index_loc.IsConstant()) {
5389 // BCE will remove the bounds check if we are guarenteed to pass.
5390 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5391 if (index < 0 || index >= length) {
5392 codegen_->AddSlowPath(slow_path);
5393 __ jmp(slow_path->GetEntryLabel());
5394 } else {
5395 // Some optimization after BCE may have generated this, and we should not
5396 // generate a bounds check if it is a valid range.
5397 }
5398 return;
5399 }
5400
5401 // We have to reverse the jump condition because the length is the constant.
5402 Register index_reg = index_loc.AsRegister<Register>();
5403 __ cmpl(index_reg, Immediate(length));
5404 codegen_->AddSlowPath(slow_path);
5405 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005406 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005407 Register length = length_loc.AsRegister<Register>();
5408 if (index_loc.IsConstant()) {
5409 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5410 __ cmpl(length, Immediate(value));
5411 } else {
5412 __ cmpl(length, index_loc.AsRegister<Register>());
5413 }
5414 codegen_->AddSlowPath(slow_path);
5415 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005416 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417}
5418
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005419void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5420 temp->SetLocations(nullptr);
5421}
5422
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005423void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005424 // Nothing to do, this is driven by the code generator.
5425}
5426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005427void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005428 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005429}
5430
5431void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005432 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5433}
5434
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005435void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5436 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5437}
5438
5439void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005440 HBasicBlock* block = instruction->GetBlock();
5441 if (block->GetLoopInformation() != nullptr) {
5442 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5443 // The back edge will generate the suspend check.
5444 return;
5445 }
5446 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5447 // The goto will generate the suspend check.
5448 return;
5449 }
5450 GenerateSuspendCheck(instruction, nullptr);
5451}
5452
5453void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5454 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005455 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005456 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5457 if (slow_path == nullptr) {
5458 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5459 instruction->SetSlowPath(slow_path);
5460 codegen_->AddSlowPath(slow_path);
5461 if (successor != nullptr) {
5462 DCHECK(successor->IsLoopHeader());
5463 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5464 }
5465 } else {
5466 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5467 }
5468
Roland Levillain7c1559a2015-12-15 10:55:36 +00005469 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5470 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005471 if (successor == nullptr) {
5472 __ j(kNotEqual, slow_path->GetEntryLabel());
5473 __ Bind(slow_path->GetReturnLabel());
5474 } else {
5475 __ j(kEqual, codegen_->GetLabelOf(successor));
5476 __ jmp(slow_path->GetEntryLabel());
5477 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005478}
5479
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005480X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5481 return codegen_->GetAssembler();
5482}
5483
Mark Mendell7c8d0092015-01-26 11:21:33 -05005484void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005485 ScratchRegisterScope ensure_scratch(
5486 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5487 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5488 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5489 __ movl(temp_reg, Address(ESP, src + stack_offset));
5490 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005491}
5492
5493void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005494 ScratchRegisterScope ensure_scratch(
5495 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5496 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5497 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5498 __ movl(temp_reg, Address(ESP, src + stack_offset));
5499 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5500 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5501 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005502}
5503
5504void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005505 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005506 Location source = move->GetSource();
5507 Location destination = move->GetDestination();
5508
5509 if (source.IsRegister()) {
5510 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005511 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005512 } else {
5513 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005514 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005515 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005516 } else if (source.IsFpuRegister()) {
5517 if (destination.IsFpuRegister()) {
5518 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5519 } else if (destination.IsStackSlot()) {
5520 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5521 } else {
5522 DCHECK(destination.IsDoubleStackSlot());
5523 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5524 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005525 } else if (source.IsStackSlot()) {
5526 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005527 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005528 } else if (destination.IsFpuRegister()) {
5529 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005530 } else {
5531 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005532 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5533 }
5534 } else if (source.IsDoubleStackSlot()) {
5535 if (destination.IsFpuRegister()) {
5536 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5537 } else {
5538 DCHECK(destination.IsDoubleStackSlot()) << destination;
5539 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005540 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005541 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005542 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005543 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005544 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005545 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005546 if (value == 0) {
5547 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5548 } else {
5549 __ movl(destination.AsRegister<Register>(), Immediate(value));
5550 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005551 } else {
5552 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005553 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005554 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005555 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005556 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005557 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005558 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005559 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005560 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5561 if (value == 0) {
5562 // Easy handling of 0.0.
5563 __ xorps(dest, dest);
5564 } else {
5565 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005566 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5567 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5568 __ movl(temp, Immediate(value));
5569 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005570 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005571 } else {
5572 DCHECK(destination.IsStackSlot()) << destination;
5573 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5574 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005575 } else if (constant->IsLongConstant()) {
5576 int64_t value = constant->AsLongConstant()->GetValue();
5577 int32_t low_value = Low32Bits(value);
5578 int32_t high_value = High32Bits(value);
5579 Immediate low(low_value);
5580 Immediate high(high_value);
5581 if (destination.IsDoubleStackSlot()) {
5582 __ movl(Address(ESP, destination.GetStackIndex()), low);
5583 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5584 } else {
5585 __ movl(destination.AsRegisterPairLow<Register>(), low);
5586 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5587 }
5588 } else {
5589 DCHECK(constant->IsDoubleConstant());
5590 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005591 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005592 int32_t low_value = Low32Bits(value);
5593 int32_t high_value = High32Bits(value);
5594 Immediate low(low_value);
5595 Immediate high(high_value);
5596 if (destination.IsFpuRegister()) {
5597 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5598 if (value == 0) {
5599 // Easy handling of 0.0.
5600 __ xorpd(dest, dest);
5601 } else {
5602 __ pushl(high);
5603 __ pushl(low);
5604 __ movsd(dest, Address(ESP, 0));
5605 __ addl(ESP, Immediate(8));
5606 }
5607 } else {
5608 DCHECK(destination.IsDoubleStackSlot()) << destination;
5609 __ movl(Address(ESP, destination.GetStackIndex()), low);
5610 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5611 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005612 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005613 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005614 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005615 }
5616}
5617
Mark Mendella5c19ce2015-04-01 12:51:05 -04005618void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005619 Register suggested_scratch = reg == EAX ? EBX : EAX;
5620 ScratchRegisterScope ensure_scratch(
5621 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5622
5623 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5624 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5625 __ movl(Address(ESP, mem + stack_offset), reg);
5626 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005627}
5628
Mark Mendell7c8d0092015-01-26 11:21:33 -05005629void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005630 ScratchRegisterScope ensure_scratch(
5631 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5632
5633 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5634 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5635 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5636 __ movss(Address(ESP, mem + stack_offset), reg);
5637 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005638}
5639
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005640void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005641 ScratchRegisterScope ensure_scratch1(
5642 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005643
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005644 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5645 ScratchRegisterScope ensure_scratch2(
5646 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005647
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005648 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5649 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5650 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5651 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5652 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5653 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005654}
5655
5656void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005657 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005658 Location source = move->GetSource();
5659 Location destination = move->GetDestination();
5660
5661 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005662 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5663 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5664 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5665 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5666 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005667 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005668 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005669 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005670 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005671 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5672 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005673 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5674 // Use XOR Swap algorithm to avoid a temporary.
5675 DCHECK_NE(source.reg(), destination.reg());
5676 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5677 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5678 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5679 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5680 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5681 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5682 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005683 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5684 // Take advantage of the 16 bytes in the XMM register.
5685 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5686 Address stack(ESP, destination.GetStackIndex());
5687 // Load the double into the high doubleword.
5688 __ movhpd(reg, stack);
5689
5690 // Store the low double into the destination.
5691 __ movsd(stack, reg);
5692
5693 // Move the high double to the low double.
5694 __ psrldq(reg, Immediate(8));
5695 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5696 // Take advantage of the 16 bytes in the XMM register.
5697 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5698 Address stack(ESP, source.GetStackIndex());
5699 // Load the double into the high doubleword.
5700 __ movhpd(reg, stack);
5701
5702 // Store the low double into the destination.
5703 __ movsd(stack, reg);
5704
5705 // Move the high double to the low double.
5706 __ psrldq(reg, Immediate(8));
5707 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5708 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5709 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005710 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005711 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005712 }
5713}
5714
5715void ParallelMoveResolverX86::SpillScratch(int reg) {
5716 __ pushl(static_cast<Register>(reg));
5717}
5718
5719void ParallelMoveResolverX86::RestoreScratch(int reg) {
5720 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005721}
5722
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005723void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005724 InvokeRuntimeCallingConvention calling_convention;
5725 CodeGenerator::CreateLoadClassLocationSummary(
5726 cls,
5727 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005728 Location::RegisterLocation(EAX),
5729 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005730}
5731
5732void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005733 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005734 if (cls->NeedsAccessCheck()) {
5735 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5736 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5737 cls,
5738 cls->GetDexPc(),
5739 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005740 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005741 return;
5742 }
5743
Roland Levillain0d5a2812015-11-13 10:07:31 +00005744 Location out_loc = locations->Out();
5745 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005746 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005747
Calin Juravle580b6092015-10-06 17:35:58 +01005748 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005749 DCHECK(!cls->CanCallRuntime());
5750 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005751 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5752 GenerateGcRootFieldLoad(
5753 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005754 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005755 // /* GcRoot<mirror::Class>[] */ out =
5756 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5757 __ movl(out, Address(current_method,
5758 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005759 // /* GcRoot<mirror::Class> */ out = out[type_index]
5760 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005761
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005762 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5763 DCHECK(cls->CanCallRuntime());
5764 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5765 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5766 codegen_->AddSlowPath(slow_path);
5767
5768 if (!cls->IsInDexCache()) {
5769 __ testl(out, out);
5770 __ j(kEqual, slow_path->GetEntryLabel());
5771 }
5772
5773 if (cls->MustGenerateClinitCheck()) {
5774 GenerateClassInitializationCheck(slow_path, out);
5775 } else {
5776 __ Bind(slow_path->GetExitLabel());
5777 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005778 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005779 }
5780}
5781
5782void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5783 LocationSummary* locations =
5784 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5785 locations->SetInAt(0, Location::RequiresRegister());
5786 if (check->HasUses()) {
5787 locations->SetOut(Location::SameAsFirstInput());
5788 }
5789}
5790
5791void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005792 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005793 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005794 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005795 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005796 GenerateClassInitializationCheck(slow_path,
5797 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005798}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005799
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005800void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005801 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005802 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5803 Immediate(mirror::Class::kStatusInitialized));
5804 __ j(kLess, slow_path->GetEntryLabel());
5805 __ Bind(slow_path->GetExitLabel());
5806 // No need for memory fence, thanks to the X86 memory model.
5807}
5808
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005809void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005810 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5811 ? LocationSummary::kCallOnSlowPath
5812 : LocationSummary::kNoCall;
5813 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005814 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005815 locations->SetOut(Location::RequiresRegister());
5816}
5817
5818void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005819 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005820 Location out_loc = locations->Out();
5821 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005822 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005823
Roland Levillain7c1559a2015-12-15 10:55:36 +00005824 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5825 GenerateGcRootFieldLoad(
5826 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005827 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005828 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005829 // /* GcRoot<mirror::String> */ out = out[string_index]
5830 GenerateGcRootFieldLoad(
5831 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005832
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005833 if (!load->IsInDexCache()) {
5834 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5835 codegen_->AddSlowPath(slow_path);
5836 __ testl(out, out);
5837 __ j(kEqual, slow_path->GetEntryLabel());
5838 __ Bind(slow_path->GetExitLabel());
5839 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005840}
5841
David Brazdilcb1c0552015-08-04 16:22:25 +01005842static Address GetExceptionTlsAddress() {
5843 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5844}
5845
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005846void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5847 LocationSummary* locations =
5848 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5849 locations->SetOut(Location::RequiresRegister());
5850}
5851
5852void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005853 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5854}
5855
5856void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5857 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5858}
5859
5860void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5861 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005862}
5863
5864void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5865 LocationSummary* locations =
5866 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5867 InvokeRuntimeCallingConvention calling_convention;
5868 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5869}
5870
5871void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005872 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5873 instruction,
5874 instruction->GetDexPc(),
5875 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005876 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005877}
5878
Roland Levillain7c1559a2015-12-15 10:55:36 +00005879static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5880 return kEmitCompilerReadBarrier &&
5881 (kUseBakerReadBarrier ||
5882 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5883 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5884 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5885}
5886
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005887void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005888 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005889 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5890 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 case TypeCheckKind::kExactCheck:
5892 case TypeCheckKind::kAbstractClassCheck:
5893 case TypeCheckKind::kClassHierarchyCheck:
5894 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005895 call_kind =
5896 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005897 break;
5898 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 case TypeCheckKind::kUnresolvedCheck:
5900 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005901 call_kind = LocationSummary::kCallOnSlowPath;
5902 break;
5903 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005904
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005905 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005906 locations->SetInAt(0, Location::RequiresRegister());
5907 locations->SetInAt(1, Location::Any());
5908 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5909 locations->SetOut(Location::RequiresRegister());
5910 // When read barriers are enabled, we need a temporary register for
5911 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005912 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005913 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005915}
5916
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005917void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005918 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005919 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005920 Location obj_loc = locations->InAt(0);
5921 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005922 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005923 Location out_loc = locations->Out();
5924 Register out = out_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005925 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5926 locations->GetTemp(0) :
5927 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005928 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005929 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5930 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5931 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005932 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005933 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005934
5935 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005936 // Avoid null check if we know obj is not null.
5937 if (instruction->MustDoNullCheck()) {
5938 __ testl(obj, obj);
5939 __ j(kEqual, &zero);
5940 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005941
Roland Levillain0d5a2812015-11-13 10:07:31 +00005942 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005943 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005944
Roland Levillain7c1559a2015-12-15 10:55:36 +00005945 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005946 case TypeCheckKind::kExactCheck: {
5947 if (cls.IsRegister()) {
5948 __ cmpl(out, cls.AsRegister<Register>());
5949 } else {
5950 DCHECK(cls.IsStackSlot()) << cls;
5951 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5952 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005953
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005954 // Classes must be equal for the instanceof to succeed.
5955 __ j(kNotEqual, &zero);
5956 __ movl(out, Immediate(1));
5957 __ jmp(&done);
5958 break;
5959 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005960
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005961 case TypeCheckKind::kAbstractClassCheck: {
5962 // If the class is abstract, we eagerly fetch the super class of the
5963 // object to avoid doing a comparison we know will fail.
5964 NearLabel loop;
5965 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005966 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005967 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005968 __ testl(out, out);
5969 // If `out` is null, we use it for the result, and jump to `done`.
5970 __ j(kEqual, &done);
5971 if (cls.IsRegister()) {
5972 __ cmpl(out, cls.AsRegister<Register>());
5973 } else {
5974 DCHECK(cls.IsStackSlot()) << cls;
5975 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5976 }
5977 __ j(kNotEqual, &loop);
5978 __ movl(out, Immediate(1));
5979 if (zero.IsLinked()) {
5980 __ jmp(&done);
5981 }
5982 break;
5983 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005984
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005985 case TypeCheckKind::kClassHierarchyCheck: {
5986 // Walk over the class hierarchy to find a match.
5987 NearLabel loop, success;
5988 __ Bind(&loop);
5989 if (cls.IsRegister()) {
5990 __ cmpl(out, cls.AsRegister<Register>());
5991 } else {
5992 DCHECK(cls.IsStackSlot()) << cls;
5993 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5994 }
5995 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005996 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005997 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005998 __ testl(out, out);
5999 __ j(kNotEqual, &loop);
6000 // If `out` is null, we use it for the result, and jump to `done`.
6001 __ jmp(&done);
6002 __ Bind(&success);
6003 __ movl(out, Immediate(1));
6004 if (zero.IsLinked()) {
6005 __ jmp(&done);
6006 }
6007 break;
6008 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006009
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006010 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006011 // Do an exact check.
6012 NearLabel exact_check;
6013 if (cls.IsRegister()) {
6014 __ cmpl(out, cls.AsRegister<Register>());
6015 } else {
6016 DCHECK(cls.IsStackSlot()) << cls;
6017 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6018 }
6019 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006020 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006021 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006022 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006023 __ testl(out, out);
6024 // If `out` is null, we use it for the result, and jump to `done`.
6025 __ j(kEqual, &done);
6026 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6027 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006028 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006029 __ movl(out, Immediate(1));
6030 __ jmp(&done);
6031 break;
6032 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006033
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006034 case TypeCheckKind::kArrayCheck: {
6035 if (cls.IsRegister()) {
6036 __ cmpl(out, cls.AsRegister<Register>());
6037 } else {
6038 DCHECK(cls.IsStackSlot()) << cls;
6039 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6040 }
6041 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006042 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6043 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006044 codegen_->AddSlowPath(slow_path);
6045 __ j(kNotEqual, slow_path->GetEntryLabel());
6046 __ movl(out, Immediate(1));
6047 if (zero.IsLinked()) {
6048 __ jmp(&done);
6049 }
6050 break;
6051 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006052
Calin Juravle98893e12015-10-02 21:05:03 +01006053 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006054 case TypeCheckKind::kInterfaceCheck: {
6055 // Note that we indeed only call on slow path, but we always go
6056 // into the slow path for the unresolved & interface check
6057 // cases.
6058 //
6059 // We cannot directly call the InstanceofNonTrivial runtime
6060 // entry point without resorting to a type checking slow path
6061 // here (i.e. by calling InvokeRuntime directly), as it would
6062 // require to assign fixed registers for the inputs of this
6063 // HInstanceOf instruction (following the runtime calling
6064 // convention), which might be cluttered by the potential first
6065 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006066 //
6067 // TODO: Introduce a new runtime entry point taking the object
6068 // to test (instead of its class) as argument, and let it deal
6069 // with the read barrier issues. This will let us refactor this
6070 // case of the `switch` code as it was previously (with a direct
6071 // call to the runtime not using a type checking slow path).
6072 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006073 DCHECK(locations->OnlyCallsOnSlowPath());
6074 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6075 /* is_fatal */ false);
6076 codegen_->AddSlowPath(slow_path);
6077 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006078 if (zero.IsLinked()) {
6079 __ jmp(&done);
6080 }
6081 break;
6082 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006083 }
6084
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006085 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006086 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006087 __ xorl(out, out);
6088 }
6089
6090 if (done.IsLinked()) {
6091 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006092 }
6093
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006094 if (slow_path != nullptr) {
6095 __ Bind(slow_path->GetExitLabel());
6096 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006097}
6098
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006099void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006100 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6101 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006102 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6103 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 case TypeCheckKind::kExactCheck:
6105 case TypeCheckKind::kAbstractClassCheck:
6106 case TypeCheckKind::kClassHierarchyCheck:
6107 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006108 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6109 LocationSummary::kCallOnSlowPath :
6110 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 break;
6112 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006113 case TypeCheckKind::kUnresolvedCheck:
6114 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006115 call_kind = LocationSummary::kCallOnSlowPath;
6116 break;
6117 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006118 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6119 locations->SetInAt(0, Location::RequiresRegister());
6120 locations->SetInAt(1, Location::Any());
6121 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6122 locations->AddTemp(Location::RequiresRegister());
6123 // When read barriers are enabled, we need an additional temporary
6124 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006125 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006127 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006128}
6129
6130void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006131 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006132 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006133 Location obj_loc = locations->InAt(0);
6134 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006135 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006136 Location temp_loc = locations->GetTemp(0);
6137 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006138 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
6139 locations->GetTemp(1) :
6140 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6142 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6143 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6144 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145
Roland Levillain0d5a2812015-11-13 10:07:31 +00006146 bool is_type_check_slow_path_fatal =
6147 (type_check_kind == TypeCheckKind::kExactCheck ||
6148 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6149 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6150 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6151 !instruction->CanThrowIntoCatchBlock();
6152 SlowPathCode* type_check_slow_path =
6153 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6154 is_type_check_slow_path_fatal);
6155 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156
Roland Levillain0d5a2812015-11-13 10:07:31 +00006157 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006158 // Avoid null check if we know obj is not null.
6159 if (instruction->MustDoNullCheck()) {
6160 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006161 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006162 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163
Roland Levillain0d5a2812015-11-13 10:07:31 +00006164 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006165 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006166
Roland Levillain0d5a2812015-11-13 10:07:31 +00006167 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006168 case TypeCheckKind::kExactCheck:
6169 case TypeCheckKind::kArrayCheck: {
6170 if (cls.IsRegister()) {
6171 __ cmpl(temp, cls.AsRegister<Register>());
6172 } else {
6173 DCHECK(cls.IsStackSlot()) << cls;
6174 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6175 }
6176 // Jump to slow path for throwing the exception or doing a
6177 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179 break;
6180 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006181
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182 case TypeCheckKind::kAbstractClassCheck: {
6183 // If the class is abstract, we eagerly fetch the super class of the
6184 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006186 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006187 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006188 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189
6190 // If the class reference currently in `temp` is not null, jump
6191 // to the `compare_classes` label to compare it with the checked
6192 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006194 __ j(kNotEqual, &compare_classes);
6195 // Otherwise, jump to the slow path to throw the exception.
6196 //
6197 // But before, move back the object's class into `temp` before
6198 // going into the slow path, as it has been overwritten in the
6199 // meantime.
6200 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006201 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006202 __ jmp(type_check_slow_path->GetEntryLabel());
6203
6204 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205 if (cls.IsRegister()) {
6206 __ cmpl(temp, cls.AsRegister<Register>());
6207 } else {
6208 DCHECK(cls.IsStackSlot()) << cls;
6209 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6210 }
6211 __ j(kNotEqual, &loop);
6212 break;
6213 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006214
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006215 case TypeCheckKind::kClassHierarchyCheck: {
6216 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006217 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006218 __ Bind(&loop);
6219 if (cls.IsRegister()) {
6220 __ cmpl(temp, cls.AsRegister<Register>());
6221 } else {
6222 DCHECK(cls.IsStackSlot()) << cls;
6223 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6224 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006225 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006226
Roland Levillain0d5a2812015-11-13 10:07:31 +00006227 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006228 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229
6230 // If the class reference currently in `temp` is not null, jump
6231 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 __ testl(temp, temp);
6233 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006234 // Otherwise, jump to the slow path to throw the exception.
6235 //
6236 // But before, move back the object's class into `temp` before
6237 // going into the slow path, as it has been overwritten in the
6238 // meantime.
6239 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006240 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006241 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 break;
6243 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006245 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006246 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006247 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006248 if (cls.IsRegister()) {
6249 __ cmpl(temp, cls.AsRegister<Register>());
6250 } else {
6251 DCHECK(cls.IsStackSlot()) << cls;
6252 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6253 }
6254 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006255
6256 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006257 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006258 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259
6260 // If the component type is not null (i.e. the object is indeed
6261 // an array), jump to label `check_non_primitive_component_type`
6262 // to further check that this component type is not a primitive
6263 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 __ j(kNotEqual, &check_non_primitive_component_type);
6266 // Otherwise, jump to the slow path to throw the exception.
6267 //
6268 // But before, move back the object's class into `temp` before
6269 // going into the slow path, as it has been overwritten in the
6270 // meantime.
6271 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006272 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006273 __ jmp(type_check_slow_path->GetEntryLabel());
6274
6275 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006276 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006277 __ j(kEqual, &done);
6278 // Same comment as above regarding `temp` and the slow path.
6279 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006280 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282 break;
6283 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006284
Calin Juravle98893e12015-10-02 21:05:03 +01006285 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006286 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287 // We always go into the type check slow path for the unresolved &
6288 // interface check cases.
6289 //
6290 // We cannot directly call the CheckCast runtime entry point
6291 // without resorting to a type checking slow path here (i.e. by
6292 // calling InvokeRuntime directly), as it would require to
6293 // assign fixed registers for the inputs of this HInstanceOf
6294 // instruction (following the runtime calling convention), which
6295 // might be cluttered by the potential first read barrier
6296 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006297 //
6298 // TODO: Introduce a new runtime entry point taking the object
6299 // to test (instead of its class) as argument, and let it deal
6300 // with the read barrier issues. This will let us refactor this
6301 // case of the `switch` code as it was previously (with a direct
6302 // call to the runtime not using a type checking slow path).
6303 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006304 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 break;
6306 }
6307 __ Bind(&done);
6308
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006310}
6311
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006312void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6313 LocationSummary* locations =
6314 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6315 InvokeRuntimeCallingConvention calling_convention;
6316 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6317}
6318
6319void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006320 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6321 : QUICK_ENTRY_POINT(pUnlockObject),
6322 instruction,
6323 instruction->GetDexPc(),
6324 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006325 if (instruction->IsEnter()) {
6326 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6327 } else {
6328 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6329 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006330}
6331
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006332void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6333void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6334void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6335
6336void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6337 LocationSummary* locations =
6338 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6339 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6340 || instruction->GetResultType() == Primitive::kPrimLong);
6341 locations->SetInAt(0, Location::RequiresRegister());
6342 locations->SetInAt(1, Location::Any());
6343 locations->SetOut(Location::SameAsFirstInput());
6344}
6345
6346void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6347 HandleBitwiseOperation(instruction);
6348}
6349
6350void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6351 HandleBitwiseOperation(instruction);
6352}
6353
6354void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6355 HandleBitwiseOperation(instruction);
6356}
6357
6358void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6359 LocationSummary* locations = instruction->GetLocations();
6360 Location first = locations->InAt(0);
6361 Location second = locations->InAt(1);
6362 DCHECK(first.Equals(locations->Out()));
6363
6364 if (instruction->GetResultType() == Primitive::kPrimInt) {
6365 if (second.IsRegister()) {
6366 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006367 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006368 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006369 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 } else {
6371 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006372 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006373 }
6374 } else if (second.IsConstant()) {
6375 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006376 __ andl(first.AsRegister<Register>(),
6377 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006378 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006379 __ orl(first.AsRegister<Register>(),
6380 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006381 } else {
6382 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006383 __ xorl(first.AsRegister<Register>(),
6384 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006385 }
6386 } else {
6387 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006388 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006389 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006390 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006391 } else {
6392 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006393 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006394 }
6395 }
6396 } else {
6397 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6398 if (second.IsRegisterPair()) {
6399 if (instruction->IsAnd()) {
6400 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6401 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6402 } else if (instruction->IsOr()) {
6403 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6404 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6405 } else {
6406 DCHECK(instruction->IsXor());
6407 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6408 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6409 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006410 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006411 if (instruction->IsAnd()) {
6412 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6413 __ andl(first.AsRegisterPairHigh<Register>(),
6414 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6415 } else if (instruction->IsOr()) {
6416 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6417 __ orl(first.AsRegisterPairHigh<Register>(),
6418 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6419 } else {
6420 DCHECK(instruction->IsXor());
6421 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6422 __ xorl(first.AsRegisterPairHigh<Register>(),
6423 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6424 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006425 } else {
6426 DCHECK(second.IsConstant()) << second;
6427 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006428 int32_t low_value = Low32Bits(value);
6429 int32_t high_value = High32Bits(value);
6430 Immediate low(low_value);
6431 Immediate high(high_value);
6432 Register first_low = first.AsRegisterPairLow<Register>();
6433 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006434 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006435 if (low_value == 0) {
6436 __ xorl(first_low, first_low);
6437 } else if (low_value != -1) {
6438 __ andl(first_low, low);
6439 }
6440 if (high_value == 0) {
6441 __ xorl(first_high, first_high);
6442 } else if (high_value != -1) {
6443 __ andl(first_high, high);
6444 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006445 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006446 if (low_value != 0) {
6447 __ orl(first_low, low);
6448 }
6449 if (high_value != 0) {
6450 __ orl(first_high, high);
6451 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006452 } else {
6453 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006454 if (low_value != 0) {
6455 __ xorl(first_low, low);
6456 }
6457 if (high_value != 0) {
6458 __ xorl(first_high, high);
6459 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006460 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006461 }
6462 }
6463}
6464
Roland Levillain7c1559a2015-12-15 10:55:36 +00006465void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6466 Location out,
6467 uint32_t offset,
6468 Location temp) {
6469 Register out_reg = out.AsRegister<Register>();
6470 if (kEmitCompilerReadBarrier) {
6471 if (kUseBakerReadBarrier) {
6472 // Load with fast path based Baker's read barrier.
6473 // /* HeapReference<Object> */ out = *(out + offset)
6474 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6475 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6476 } else {
6477 // Load with slow path based read barrier.
6478 // Save the value of `out` into `temp` before overwriting it
6479 // in the following move operation, as we will need it for the
6480 // read barrier below.
6481 __ movl(temp.AsRegister<Register>(), out_reg);
6482 // /* HeapReference<Object> */ out = *(out + offset)
6483 __ movl(out_reg, Address(out_reg, offset));
6484 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6485 }
6486 } else {
6487 // Plain load with no read barrier.
6488 // /* HeapReference<Object> */ out = *(out + offset)
6489 __ movl(out_reg, Address(out_reg, offset));
6490 __ MaybeUnpoisonHeapReference(out_reg);
6491 }
6492}
6493
6494void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6495 Location out,
6496 Location obj,
6497 uint32_t offset,
6498 Location temp) {
6499 Register out_reg = out.AsRegister<Register>();
6500 Register obj_reg = obj.AsRegister<Register>();
6501 if (kEmitCompilerReadBarrier) {
6502 if (kUseBakerReadBarrier) {
6503 // Load with fast path based Baker's read barrier.
6504 // /* HeapReference<Object> */ out = *(obj + offset)
6505 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6506 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6507 } else {
6508 // Load with slow path based read barrier.
6509 // /* HeapReference<Object> */ out = *(obj + offset)
6510 __ movl(out_reg, Address(obj_reg, offset));
6511 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6512 }
6513 } else {
6514 // Plain load with no read barrier.
6515 // /* HeapReference<Object> */ out = *(obj + offset)
6516 __ movl(out_reg, Address(obj_reg, offset));
6517 __ MaybeUnpoisonHeapReference(out_reg);
6518 }
6519}
6520
6521void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6522 Location root,
6523 Register obj,
6524 uint32_t offset) {
6525 Register root_reg = root.AsRegister<Register>();
6526 if (kEmitCompilerReadBarrier) {
6527 if (kUseBakerReadBarrier) {
6528 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6529 // Baker's read barrier are used:
6530 //
6531 // root = obj.field;
6532 // if (Thread::Current()->GetIsGcMarking()) {
6533 // root = ReadBarrier::Mark(root)
6534 // }
6535
6536 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6537 __ movl(root_reg, Address(obj, offset));
6538 static_assert(
6539 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6540 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6541 "have different sizes.");
6542 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6543 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6544 "have different sizes.");
6545
6546 // Slow path used to mark the GC root `root`.
6547 SlowPathCode* slow_path =
6548 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6549 codegen_->AddSlowPath(slow_path);
6550
6551 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6552 Immediate(0));
6553 __ j(kNotEqual, slow_path->GetEntryLabel());
6554 __ Bind(slow_path->GetExitLabel());
6555 } else {
6556 // GC root loaded through a slow path for read barriers other
6557 // than Baker's.
6558 // /* GcRoot<mirror::Object>* */ root = obj + offset
6559 __ leal(root_reg, Address(obj, offset));
6560 // /* mirror::Object* */ root = root->Read()
6561 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6562 }
6563 } else {
6564 // Plain GC root load with no read barrier.
6565 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6566 __ movl(root_reg, Address(obj, offset));
6567 }
6568}
6569
6570void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6571 Location ref,
6572 Register obj,
6573 uint32_t offset,
6574 Location temp,
6575 bool needs_null_check) {
6576 DCHECK(kEmitCompilerReadBarrier);
6577 DCHECK(kUseBakerReadBarrier);
6578
6579 // /* HeapReference<Object> */ ref = *(obj + offset)
6580 Address src(obj, offset);
6581 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6582}
6583
6584void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6585 Location ref,
6586 Register obj,
6587 uint32_t data_offset,
6588 Location index,
6589 Location temp,
6590 bool needs_null_check) {
6591 DCHECK(kEmitCompilerReadBarrier);
6592 DCHECK(kUseBakerReadBarrier);
6593
6594 // /* HeapReference<Object> */ ref =
6595 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6596 Address src = index.IsConstant() ?
6597 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6598 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6599 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6600}
6601
6602void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6603 Location ref,
6604 Register obj,
6605 const Address& src,
6606 Location temp,
6607 bool needs_null_check) {
6608 DCHECK(kEmitCompilerReadBarrier);
6609 DCHECK(kUseBakerReadBarrier);
6610
6611 // In slow path based read barriers, the read barrier call is
6612 // inserted after the original load. However, in fast path based
6613 // Baker's read barriers, we need to perform the load of
6614 // mirror::Object::monitor_ *before* the original reference load.
6615 // This load-load ordering is required by the read barrier.
6616 // The fast path/slow path (for Baker's algorithm) should look like:
6617 //
6618 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6619 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6620 // HeapReference<Object> ref = *src; // Original reference load.
6621 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6622 // if (is_gray) {
6623 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6624 // }
6625 //
6626 // Note: the original implementation in ReadBarrier::Barrier is
6627 // slightly more complex as:
6628 // - it implements the load-load fence using a data dependency on
6629 // the high-bits of rb_state, which are expected to be all zeroes;
6630 // - it performs additional checks that we do not do here for
6631 // performance reasons.
6632
6633 Register ref_reg = ref.AsRegister<Register>();
6634 Register temp_reg = temp.AsRegister<Register>();
6635 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6636
6637 // /* int32_t */ monitor = obj->monitor_
6638 __ movl(temp_reg, Address(obj, monitor_offset));
6639 if (needs_null_check) {
6640 MaybeRecordImplicitNullCheck(instruction);
6641 }
6642 // /* LockWord */ lock_word = LockWord(monitor)
6643 static_assert(sizeof(LockWord) == sizeof(int32_t),
6644 "art::LockWord and int32_t have different sizes.");
6645 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6646 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6647 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6648 static_assert(
6649 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6650 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6651
6652 // Load fence to prevent load-load reordering.
6653 // Note that this is a no-op, thanks to the x86 memory model.
6654 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6655
6656 // The actual reference load.
6657 // /* HeapReference<Object> */ ref = *src
6658 __ movl(ref_reg, src);
6659
6660 // Object* ref = ref_addr->AsMirrorPtr()
6661 __ MaybeUnpoisonHeapReference(ref_reg);
6662
6663 // Slow path used to mark the object `ref` when it is gray.
6664 SlowPathCode* slow_path =
6665 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6666 AddSlowPath(slow_path);
6667
6668 // if (rb_state == ReadBarrier::gray_ptr_)
6669 // ref = ReadBarrier::Mark(ref);
6670 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6671 __ j(kEqual, slow_path->GetEntryLabel());
6672 __ Bind(slow_path->GetExitLabel());
6673}
6674
6675void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6676 Location out,
6677 Location ref,
6678 Location obj,
6679 uint32_t offset,
6680 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 DCHECK(kEmitCompilerReadBarrier);
6682
Roland Levillain7c1559a2015-12-15 10:55:36 +00006683 // Insert a slow path based read barrier *after* the reference load.
6684 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 // If heap poisoning is enabled, the unpoisoning of the loaded
6686 // reference will be carried out by the runtime within the slow
6687 // path.
6688 //
6689 // Note that `ref` currently does not get unpoisoned (when heap
6690 // poisoning is enabled), which is alright as the `ref` argument is
6691 // not used by the artReadBarrierSlow entry point.
6692 //
6693 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6694 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6695 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6696 AddSlowPath(slow_path);
6697
Roland Levillain0d5a2812015-11-13 10:07:31 +00006698 __ jmp(slow_path->GetEntryLabel());
6699 __ Bind(slow_path->GetExitLabel());
6700}
6701
Roland Levillain7c1559a2015-12-15 10:55:36 +00006702void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6703 Location out,
6704 Location ref,
6705 Location obj,
6706 uint32_t offset,
6707 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006708 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006709 // Baker's read barriers shall be handled by the fast path
6710 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6711 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 // If heap poisoning is enabled, unpoisoning will be taken care of
6713 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006714 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006715 } else if (kPoisonHeapReferences) {
6716 __ UnpoisonHeapReference(out.AsRegister<Register>());
6717 }
6718}
6719
Roland Levillain7c1559a2015-12-15 10:55:36 +00006720void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6721 Location out,
6722 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006723 DCHECK(kEmitCompilerReadBarrier);
6724
Roland Levillain7c1559a2015-12-15 10:55:36 +00006725 // Insert a slow path based read barrier *after* the GC root load.
6726 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 // Note that GC roots are not affected by heap poisoning, so we do
6728 // not need to do anything special for this here.
6729 SlowPathCode* slow_path =
6730 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6731 AddSlowPath(slow_path);
6732
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 __ jmp(slow_path->GetEntryLabel());
6734 __ Bind(slow_path->GetExitLabel());
6735}
6736
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006737void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006738 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006739 LOG(FATAL) << "Unreachable";
6740}
6741
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006742void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006743 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006744 LOG(FATAL) << "Unreachable";
6745}
6746
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006747void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6748 DCHECK(codegen_->IsBaseline());
6749 LocationSummary* locations =
6750 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6751 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6752}
6753
6754void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6755 DCHECK(codegen_->IsBaseline());
6756 // Will be generated at use site.
6757}
6758
Mark Mendellfe57faa2015-09-18 09:26:15 -04006759// Simple implementation of packed switch - generate cascaded compare/jumps.
6760void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6761 LocationSummary* locations =
6762 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6763 locations->SetInAt(0, Location::RequiresRegister());
6764}
6765
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006766void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6767 int32_t lower_bound,
6768 uint32_t num_entries,
6769 HBasicBlock* switch_block,
6770 HBasicBlock* default_block) {
6771 // Figure out the correct compare values and jump conditions.
6772 // Handle the first compare/branch as a special case because it might
6773 // jump to the default case.
6774 DCHECK_GT(num_entries, 2u);
6775 Condition first_condition;
6776 uint32_t index;
6777 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6778 if (lower_bound != 0) {
6779 first_condition = kLess;
6780 __ cmpl(value_reg, Immediate(lower_bound));
6781 __ j(first_condition, codegen_->GetLabelOf(default_block));
6782 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006783
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006784 index = 1;
6785 } else {
6786 // Handle all the compare/jumps below.
6787 first_condition = kBelow;
6788 index = 0;
6789 }
6790
6791 // Handle the rest of the compare/jumps.
6792 for (; index + 1 < num_entries; index += 2) {
6793 int32_t compare_to_value = lower_bound + index + 1;
6794 __ cmpl(value_reg, Immediate(compare_to_value));
6795 // Jump to successors[index] if value < case_value[index].
6796 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6797 // Jump to successors[index + 1] if value == case_value[index + 1].
6798 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6799 }
6800
6801 if (index != num_entries) {
6802 // There are an odd number of entries. Handle the last one.
6803 DCHECK_EQ(index + 1, num_entries);
6804 __ cmpl(value_reg, Immediate(lower_bound + index));
6805 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006806 }
6807
6808 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006809 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6810 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006811 }
6812}
6813
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006814void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6815 int32_t lower_bound = switch_instr->GetStartValue();
6816 uint32_t num_entries = switch_instr->GetNumEntries();
6817 LocationSummary* locations = switch_instr->GetLocations();
6818 Register value_reg = locations->InAt(0).AsRegister<Register>();
6819
6820 GenPackedSwitchWithCompares(value_reg,
6821 lower_bound,
6822 num_entries,
6823 switch_instr->GetBlock(),
6824 switch_instr->GetDefaultBlock());
6825}
6826
Mark Mendell805b3b52015-09-18 14:10:29 -04006827void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6828 LocationSummary* locations =
6829 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6830 locations->SetInAt(0, Location::RequiresRegister());
6831
6832 // Constant area pointer.
6833 locations->SetInAt(1, Location::RequiresRegister());
6834
6835 // And the temporary we need.
6836 locations->AddTemp(Location::RequiresRegister());
6837}
6838
6839void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6840 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006841 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006842 LocationSummary* locations = switch_instr->GetLocations();
6843 Register value_reg = locations->InAt(0).AsRegister<Register>();
6844 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6845
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006846 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6847 GenPackedSwitchWithCompares(value_reg,
6848 lower_bound,
6849 num_entries,
6850 switch_instr->GetBlock(),
6851 default_block);
6852 return;
6853 }
6854
Mark Mendell805b3b52015-09-18 14:10:29 -04006855 // Optimizing has a jump area.
6856 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6857 Register constant_area = locations->InAt(1).AsRegister<Register>();
6858
6859 // Remove the bias, if needed.
6860 if (lower_bound != 0) {
6861 __ leal(temp_reg, Address(value_reg, -lower_bound));
6862 value_reg = temp_reg;
6863 }
6864
6865 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006866 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006867 __ cmpl(value_reg, Immediate(num_entries - 1));
6868 __ j(kAbove, codegen_->GetLabelOf(default_block));
6869
6870 // We are in the range of the table.
6871 // Load (target-constant_area) from the jump table, indexing by the value.
6872 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6873
6874 // Compute the actual target address by adding in constant_area.
6875 __ addl(temp_reg, constant_area);
6876
6877 // And jump.
6878 __ jmp(temp_reg);
6879}
6880
Mark Mendell0616ae02015-04-17 12:49:27 -04006881void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6882 HX86ComputeBaseMethodAddress* insn) {
6883 LocationSummary* locations =
6884 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6885 locations->SetOut(Location::RequiresRegister());
6886}
6887
6888void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6889 HX86ComputeBaseMethodAddress* insn) {
6890 LocationSummary* locations = insn->GetLocations();
6891 Register reg = locations->Out().AsRegister<Register>();
6892
6893 // Generate call to next instruction.
6894 Label next_instruction;
6895 __ call(&next_instruction);
6896 __ Bind(&next_instruction);
6897
6898 // Remember this offset for later use with constant area.
6899 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6900
6901 // Grab the return address off the stack.
6902 __ popl(reg);
6903}
6904
6905void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6906 HX86LoadFromConstantTable* insn) {
6907 LocationSummary* locations =
6908 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6909
6910 locations->SetInAt(0, Location::RequiresRegister());
6911 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6912
6913 // If we don't need to be materialized, we only need the inputs to be set.
6914 if (!insn->NeedsMaterialization()) {
6915 return;
6916 }
6917
6918 switch (insn->GetType()) {
6919 case Primitive::kPrimFloat:
6920 case Primitive::kPrimDouble:
6921 locations->SetOut(Location::RequiresFpuRegister());
6922 break;
6923
6924 case Primitive::kPrimInt:
6925 locations->SetOut(Location::RequiresRegister());
6926 break;
6927
6928 default:
6929 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6930 }
6931}
6932
6933void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6934 if (!insn->NeedsMaterialization()) {
6935 return;
6936 }
6937
6938 LocationSummary* locations = insn->GetLocations();
6939 Location out = locations->Out();
6940 Register const_area = locations->InAt(0).AsRegister<Register>();
6941 HConstant *value = insn->GetConstant();
6942
6943 switch (insn->GetType()) {
6944 case Primitive::kPrimFloat:
6945 __ movss(out.AsFpuRegister<XmmRegister>(),
6946 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6947 break;
6948
6949 case Primitive::kPrimDouble:
6950 __ movsd(out.AsFpuRegister<XmmRegister>(),
6951 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6952 break;
6953
6954 case Primitive::kPrimInt:
6955 __ movl(out.AsRegister<Register>(),
6956 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6957 break;
6958
6959 default:
6960 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6961 }
6962}
6963
Mark Mendell0616ae02015-04-17 12:49:27 -04006964/**
6965 * Class to handle late fixup of offsets into constant area.
6966 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006967class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006968 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006969 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6970 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6971
6972 protected:
6973 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6974
6975 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006976
6977 private:
6978 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6979 // Patch the correct offset for the instruction. The place to patch is the
6980 // last 4 bytes of the instruction.
6981 // The value to patch is the distance from the offset in the constant area
6982 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006983 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6984 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006985
6986 // Patch in the right value.
6987 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6988 }
6989
Mark Mendell0616ae02015-04-17 12:49:27 -04006990 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006991 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006992};
6993
Mark Mendell805b3b52015-09-18 14:10:29 -04006994/**
6995 * Class to handle late fixup of offsets to a jump table that will be created in the
6996 * constant area.
6997 */
6998class JumpTableRIPFixup : public RIPFixup {
6999 public:
7000 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7001 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7002
7003 void CreateJumpTable() {
7004 X86Assembler* assembler = codegen_->GetAssembler();
7005
7006 // Ensure that the reference to the jump table has the correct offset.
7007 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7008 SetOffset(offset_in_constant_table);
7009
7010 // The label values in the jump table are computed relative to the
7011 // instruction addressing the constant area.
7012 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7013
7014 // Populate the jump table with the correct values for the jump table.
7015 int32_t num_entries = switch_instr_->GetNumEntries();
7016 HBasicBlock* block = switch_instr_->GetBlock();
7017 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7018 // The value that we want is the target offset - the position of the table.
7019 for (int32_t i = 0; i < num_entries; i++) {
7020 HBasicBlock* b = successors[i];
7021 Label* l = codegen_->GetLabelOf(b);
7022 DCHECK(l->IsBound());
7023 int32_t offset_to_block = l->Position() - relative_offset;
7024 assembler->AppendInt32(offset_to_block);
7025 }
7026 }
7027
7028 private:
7029 const HX86PackedSwitch* switch_instr_;
7030};
7031
7032void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7033 // Generate the constant area if needed.
7034 X86Assembler* assembler = GetAssembler();
7035 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7036 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7037 // byte values.
7038 assembler->Align(4, 0);
7039 constant_area_start_ = assembler->CodeSize();
7040
7041 // Populate any jump tables.
7042 for (auto jump_table : fixups_to_jump_tables_) {
7043 jump_table->CreateJumpTable();
7044 }
7045
7046 // And now add the constant area to the generated code.
7047 assembler->AddConstantArea();
7048 }
7049
7050 // And finish up.
7051 CodeGenerator::Finalize(allocator);
7052}
7053
Mark Mendell0616ae02015-04-17 12:49:27 -04007054Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7055 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7056 return Address(reg, kDummy32BitOffset, fixup);
7057}
7058
7059Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7060 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7061 return Address(reg, kDummy32BitOffset, fixup);
7062}
7063
7064Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7065 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7066 return Address(reg, kDummy32BitOffset, fixup);
7067}
7068
7069Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7070 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7071 return Address(reg, kDummy32BitOffset, fixup);
7072}
7073
Mark Mendell805b3b52015-09-18 14:10:29 -04007074Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7075 Register reg,
7076 Register value) {
7077 // Create a fixup to be used to create and address the jump table.
7078 JumpTableRIPFixup* table_fixup =
7079 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7080
7081 // We have to populate the jump tables.
7082 fixups_to_jump_tables_.push_back(table_fixup);
7083
7084 // We want a scaled address, as we are extracting the correct offset from the table.
7085 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7086}
7087
Andreas Gampe85b62f22015-09-09 13:15:38 -07007088// TODO: target as memory.
7089void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7090 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007091 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007092 return;
7093 }
7094
7095 DCHECK_NE(type, Primitive::kPrimVoid);
7096
7097 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7098 if (target.Equals(return_loc)) {
7099 return;
7100 }
7101
7102 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7103 // with the else branch.
7104 if (type == Primitive::kPrimLong) {
7105 HParallelMove parallel_move(GetGraph()->GetArena());
7106 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7107 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7108 GetMoveResolver()->EmitNativeCode(&parallel_move);
7109 } else {
7110 // Let the parallel move resolver take care of all of this.
7111 HParallelMove parallel_move(GetGraph()->GetArena());
7112 parallel_move.AddMove(return_loc, target, type, nullptr);
7113 GetMoveResolver()->EmitNativeCode(&parallel_move);
7114 }
7115}
7116
Roland Levillain4d027112015-07-01 15:41:14 +01007117#undef __
7118
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007119} // namespace x86
7120} // namespace art