blob: 8910249f20f004629f6fffd2a764c68408dbbe02 [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) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001628 if (codegen_->HasStackMapAtCurrentPc()) {
1629 // Ensure that we do not collide with the stack map of the previous instruction.
1630 __ nop();
1631 }
David Srbecky0cf44932015-12-09 14:09:59 +00001632 codegen_->RecordPcInfo(info, info->GetDexPc());
1633}
1634
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001635void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001636 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001637}
1638
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001639void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1640 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001641}
1642
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001643void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001644 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001645}
1646
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001647void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001648 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001649}
1650
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001651void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001652 LocationSummary* locations =
1653 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001654 switch (store->InputAt(1)->GetType()) {
1655 case Primitive::kPrimBoolean:
1656 case Primitive::kPrimByte:
1657 case Primitive::kPrimChar:
1658 case Primitive::kPrimShort:
1659 case Primitive::kPrimInt:
1660 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001661 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001662 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1663 break;
1664
1665 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001666 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001667 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1668 break;
1669
1670 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001671 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001672 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001673}
1674
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001675void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001676}
1677
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001678void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001679 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001680 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001681 // Handle the long/FP comparisons made in instruction simplification.
1682 switch (cond->InputAt(0)->GetType()) {
1683 case Primitive::kPrimLong: {
1684 locations->SetInAt(0, Location::RequiresRegister());
1685 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1686 if (cond->NeedsMaterialization()) {
1687 locations->SetOut(Location::RequiresRegister());
1688 }
1689 break;
1690 }
1691 case Primitive::kPrimFloat:
1692 case Primitive::kPrimDouble: {
1693 locations->SetInAt(0, Location::RequiresFpuRegister());
1694 locations->SetInAt(1, Location::RequiresFpuRegister());
1695 if (cond->NeedsMaterialization()) {
1696 locations->SetOut(Location::RequiresRegister());
1697 }
1698 break;
1699 }
1700 default:
1701 locations->SetInAt(0, Location::RequiresRegister());
1702 locations->SetInAt(1, Location::Any());
1703 if (cond->NeedsMaterialization()) {
1704 // We need a byte register.
1705 locations->SetOut(Location::RegisterLocation(ECX));
1706 }
1707 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001708 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001709}
1710
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001711void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001712 if (!cond->NeedsMaterialization()) {
1713 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001714 }
Mark Mendellc4701932015-04-10 13:18:51 -04001715
1716 LocationSummary* locations = cond->GetLocations();
1717 Location lhs = locations->InAt(0);
1718 Location rhs = locations->InAt(1);
1719 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001720 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001721
1722 switch (cond->InputAt(0)->GetType()) {
1723 default: {
1724 // Integer case.
1725
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001726 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001727 __ xorl(reg, reg);
1728
1729 if (rhs.IsRegister()) {
1730 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1731 } else if (rhs.IsConstant()) {
1732 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1733 if (constant == 0) {
1734 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1735 } else {
1736 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1737 }
1738 } else {
1739 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1740 }
Aart Bike9f37602015-10-09 11:15:55 -07001741 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001742 return;
1743 }
1744 case Primitive::kPrimLong:
1745 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1746 break;
1747 case Primitive::kPrimFloat:
1748 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1749 GenerateFPJumps(cond, &true_label, &false_label);
1750 break;
1751 case Primitive::kPrimDouble:
1752 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1753 GenerateFPJumps(cond, &true_label, &false_label);
1754 break;
1755 }
1756
1757 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001758 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001759
Roland Levillain4fa13f62015-07-06 18:11:54 +01001760 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001761 __ Bind(&false_label);
1762 __ xorl(reg, reg);
1763 __ jmp(&done_label);
1764
Roland Levillain4fa13f62015-07-06 18:11:54 +01001765 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001766 __ Bind(&true_label);
1767 __ movl(reg, Immediate(1));
1768 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001773}
1774
1775void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001777}
1778
1779void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001781}
1782
1783void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001785}
1786
1787void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001789}
1790
1791void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001793}
1794
1795void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001797}
1798
1799void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001801}
1802
1803void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001805}
1806
1807void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001808 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001809}
1810
1811void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001812 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001813}
1814
1815void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001816 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001817}
1818
Aart Bike9f37602015-10-09 11:15:55 -07001819void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001820 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001821}
1822
1823void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001824 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001825}
1826
1827void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001828 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001829}
1830
1831void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001832 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001833}
1834
1835void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001836 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001837}
1838
1839void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001840 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001841}
1842
1843void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001844 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001845}
1846
1847void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001848 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001849}
1850
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001851void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001852 LocationSummary* locations =
1853 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001854 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001855}
1856
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001857void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001858 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859}
1860
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001861void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1862 LocationSummary* locations =
1863 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1864 locations->SetOut(Location::ConstantLocation(constant));
1865}
1866
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001867void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001868 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001869}
1870
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001871void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001872 LocationSummary* locations =
1873 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001874 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875}
1876
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001877void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 // Will be generated at use site.
1879}
1880
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001881void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1882 LocationSummary* locations =
1883 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1884 locations->SetOut(Location::ConstantLocation(constant));
1885}
1886
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001887void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001888 // Will be generated at use site.
1889}
1890
1891void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1892 LocationSummary* locations =
1893 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1894 locations->SetOut(Location::ConstantLocation(constant));
1895}
1896
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001897void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001898 // Will be generated at use site.
1899}
1900
Calin Juravle27df7582015-04-17 19:12:31 +01001901void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1902 memory_barrier->SetLocations(nullptr);
1903}
1904
1905void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001906 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001907}
1908
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001909void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001910 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001911}
1912
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001913void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001914 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001915}
1916
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001917void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001918 LocationSummary* locations =
1919 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 switch (ret->InputAt(0)->GetType()) {
1921 case Primitive::kPrimBoolean:
1922 case Primitive::kPrimByte:
1923 case Primitive::kPrimChar:
1924 case Primitive::kPrimShort:
1925 case Primitive::kPrimInt:
1926 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001927 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001928 break;
1929
1930 case Primitive::kPrimLong:
1931 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001932 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001933 break;
1934
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001935 case Primitive::kPrimFloat:
1936 case Primitive::kPrimDouble:
1937 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001938 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001939 break;
1940
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001941 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001942 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001943 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001944}
1945
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001946void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001947 if (kIsDebugBuild) {
1948 switch (ret->InputAt(0)->GetType()) {
1949 case Primitive::kPrimBoolean:
1950 case Primitive::kPrimByte:
1951 case Primitive::kPrimChar:
1952 case Primitive::kPrimShort:
1953 case Primitive::kPrimInt:
1954 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001955 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001956 break;
1957
1958 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001959 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1960 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001961 break;
1962
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001963 case Primitive::kPrimFloat:
1964 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001965 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001966 break;
1967
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001968 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001969 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001970 }
1971 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001972 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001973}
1974
Calin Juravle175dc732015-08-25 15:42:32 +01001975void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1976 // The trampoline uses the same calling convention as dex calling conventions,
1977 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1978 // the method_idx.
1979 HandleInvoke(invoke);
1980}
1981
1982void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1983 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1984}
1985
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001986void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01001987 // When we do not run baseline, explicit clinit checks triggered by static
1988 // invokes must have been pruned by art::PrepareForRegisterAllocation.
1989 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001990
Mark Mendellfb8d2792015-03-31 22:16:59 -04001991 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001992 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001993 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001994 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001995 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001996 return;
1997 }
1998
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001999 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002000
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002001 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2002 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002003 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002004 }
2005
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002006 if (codegen_->IsBaseline()) {
2007 // Baseline does not have enough registers if the current method also
2008 // needs a register. We therefore do not require a register for it, and let
2009 // the code generation of the invoke handle it.
2010 LocationSummary* locations = invoke->GetLocations();
Vladimir Markoc53c0792015-11-19 15:48:33 +00002011 Location location = locations->InAt(invoke->GetSpecialInputIndex());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002012 if (location.IsUnallocated() && location.GetPolicy() == Location::kRequiresRegister) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002013 locations->SetInAt(invoke->GetSpecialInputIndex(), Location::NoLocation());
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002014 }
2015 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002016}
2017
Mark Mendell09ed1a32015-03-25 08:30:06 -04002018static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2019 if (invoke->GetLocations()->Intrinsified()) {
2020 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2021 intrinsic.Dispatch(invoke);
2022 return true;
2023 }
2024 return false;
2025}
2026
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002027void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
Roland Levillain3e3d7332015-04-28 11:00:54 +01002028 // When we do not run baseline, explicit clinit checks triggered by static
2029 // invokes must have been pruned by art::PrepareForRegisterAllocation.
2030 DCHECK(codegen_->IsBaseline() || !invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002031
Mark Mendell09ed1a32015-03-25 08:30:06 -04002032 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2033 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002034 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002035
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002036 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002037 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002038 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002039 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002040}
2041
2042void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002043 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2044 if (intrinsic.TryDispatch(invoke)) {
2045 return;
2046 }
2047
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002048 HandleInvoke(invoke);
2049}
2050
2051void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002052 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002053 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002054}
2055
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002056void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002057 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2058 return;
2059 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002060
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002061 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002062 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002063 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002064}
2065
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002066void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002067 // This call to HandleInvoke allocates a temporary (core) register
2068 // which is also used to transfer the hidden argument from FP to
2069 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002070 HandleInvoke(invoke);
2071 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002072 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002073}
2074
2075void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2076 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002077 LocationSummary* locations = invoke->GetLocations();
2078 Register temp = locations->GetTemp(0).AsRegister<Register>();
2079 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002080 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2081 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002082 Location receiver = locations->InAt(0);
2083 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2084
Roland Levillain0d5a2812015-11-13 10:07:31 +00002085 // Set the hidden argument. This is safe to do this here, as XMM7
2086 // won't be modified thereafter, before the `call` instruction.
2087 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002088 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002089 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002090
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002091 if (receiver.IsStackSlot()) {
2092 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002093 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002094 __ movl(temp, Address(temp, class_offset));
2095 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002096 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002097 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002098 }
Roland Levillain4d027112015-07-01 15:41:14 +01002099 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002100 // Instead of simply (possibly) unpoisoning `temp` here, we should
2101 // emit a read barrier for the previous class reference load.
2102 // However this is not required in practice, as this is an
2103 // intermediate/temporary reference and because the current
2104 // concurrent copying collector keeps the from-space memory
2105 // intact/accessible until the end of the marking phase (the
2106 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002107 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002108 // temp = temp->GetImtEntryAt(method_offset);
2109 __ movl(temp, Address(temp, method_offset));
2110 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002111 __ call(Address(temp,
2112 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002113
2114 DCHECK(!codegen_->IsLeafMethod());
2115 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2116}
2117
Roland Levillain88cb1752014-10-20 16:36:47 +01002118void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2119 LocationSummary* locations =
2120 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2121 switch (neg->GetResultType()) {
2122 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002123 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002124 locations->SetInAt(0, Location::RequiresRegister());
2125 locations->SetOut(Location::SameAsFirstInput());
2126 break;
2127
Roland Levillain88cb1752014-10-20 16:36:47 +01002128 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002129 locations->SetInAt(0, Location::RequiresFpuRegister());
2130 locations->SetOut(Location::SameAsFirstInput());
2131 locations->AddTemp(Location::RequiresRegister());
2132 locations->AddTemp(Location::RequiresFpuRegister());
2133 break;
2134
Roland Levillain88cb1752014-10-20 16:36:47 +01002135 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002136 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002137 locations->SetOut(Location::SameAsFirstInput());
2138 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002139 break;
2140
2141 default:
2142 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2143 }
2144}
2145
2146void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2147 LocationSummary* locations = neg->GetLocations();
2148 Location out = locations->Out();
2149 Location in = locations->InAt(0);
2150 switch (neg->GetResultType()) {
2151 case Primitive::kPrimInt:
2152 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002153 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002154 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002155 break;
2156
2157 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002158 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002159 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002160 __ negl(out.AsRegisterPairLow<Register>());
2161 // Negation is similar to subtraction from zero. The least
2162 // significant byte triggers a borrow when it is different from
2163 // zero; to take it into account, add 1 to the most significant
2164 // byte if the carry flag (CF) is set to 1 after the first NEGL
2165 // operation.
2166 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2167 __ negl(out.AsRegisterPairHigh<Register>());
2168 break;
2169
Roland Levillain5368c212014-11-27 15:03:41 +00002170 case Primitive::kPrimFloat: {
2171 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002172 Register constant = locations->GetTemp(0).AsRegister<Register>();
2173 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002174 // Implement float negation with an exclusive or with value
2175 // 0x80000000 (mask for bit 31, representing the sign of a
2176 // single-precision floating-point number).
2177 __ movl(constant, Immediate(INT32_C(0x80000000)));
2178 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002179 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002180 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002181 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002182
Roland Levillain5368c212014-11-27 15:03:41 +00002183 case Primitive::kPrimDouble: {
2184 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002185 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002186 // Implement double negation with an exclusive or with value
2187 // 0x8000000000000000 (mask for bit 63, representing the sign of
2188 // a double-precision floating-point number).
2189 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002190 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002191 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002192 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002193
2194 default:
2195 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2196 }
2197}
2198
Roland Levillaindff1f282014-11-05 14:15:05 +00002199void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002200 Primitive::Type result_type = conversion->GetResultType();
2201 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002202 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002203
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002204 // The float-to-long and double-to-long type conversions rely on a
2205 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002206 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002207 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2208 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002209 ? LocationSummary::kCall
2210 : LocationSummary::kNoCall;
2211 LocationSummary* locations =
2212 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2213
David Brazdilb2bd1c52015-03-25 11:17:37 +00002214 // The Java language does not allow treating boolean as an integral type but
2215 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002216
Roland Levillaindff1f282014-11-05 14:15:05 +00002217 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002218 case Primitive::kPrimByte:
2219 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002220 case Primitive::kPrimBoolean:
2221 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002222 case Primitive::kPrimShort:
2223 case Primitive::kPrimInt:
2224 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002225 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002226 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2227 // Make the output overlap to please the register allocator. This greatly simplifies
2228 // the validation of the linear scan implementation
2229 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002230 break;
2231
2232 default:
2233 LOG(FATAL) << "Unexpected type conversion from " << input_type
2234 << " to " << result_type;
2235 }
2236 break;
2237
Roland Levillain01a8d712014-11-14 16:27:39 +00002238 case Primitive::kPrimShort:
2239 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002240 case Primitive::kPrimBoolean:
2241 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002242 case Primitive::kPrimByte:
2243 case Primitive::kPrimInt:
2244 case Primitive::kPrimChar:
2245 // Processing a Dex `int-to-short' instruction.
2246 locations->SetInAt(0, Location::Any());
2247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2248 break;
2249
2250 default:
2251 LOG(FATAL) << "Unexpected type conversion from " << input_type
2252 << " to " << result_type;
2253 }
2254 break;
2255
Roland Levillain946e1432014-11-11 17:35:19 +00002256 case Primitive::kPrimInt:
2257 switch (input_type) {
2258 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002259 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002260 locations->SetInAt(0, Location::Any());
2261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2262 break;
2263
2264 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002265 // Processing a Dex `float-to-int' instruction.
2266 locations->SetInAt(0, Location::RequiresFpuRegister());
2267 locations->SetOut(Location::RequiresRegister());
2268 locations->AddTemp(Location::RequiresFpuRegister());
2269 break;
2270
Roland Levillain946e1432014-11-11 17:35:19 +00002271 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002272 // Processing a Dex `double-to-int' instruction.
2273 locations->SetInAt(0, Location::RequiresFpuRegister());
2274 locations->SetOut(Location::RequiresRegister());
2275 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002276 break;
2277
2278 default:
2279 LOG(FATAL) << "Unexpected type conversion from " << input_type
2280 << " to " << result_type;
2281 }
2282 break;
2283
Roland Levillaindff1f282014-11-05 14:15:05 +00002284 case Primitive::kPrimLong:
2285 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002286 case Primitive::kPrimBoolean:
2287 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002288 case Primitive::kPrimByte:
2289 case Primitive::kPrimShort:
2290 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002291 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002292 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002293 locations->SetInAt(0, Location::RegisterLocation(EAX));
2294 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2295 break;
2296
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002297 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002298 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002299 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002300 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002301 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2302 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2303
Vladimir Marko949c91f2015-01-27 10:48:44 +00002304 // The runtime helper puts the result in EAX, EDX.
2305 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002306 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002307 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002308
2309 default:
2310 LOG(FATAL) << "Unexpected type conversion from " << input_type
2311 << " to " << result_type;
2312 }
2313 break;
2314
Roland Levillain981e4542014-11-14 11:47:14 +00002315 case Primitive::kPrimChar:
2316 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002317 case Primitive::kPrimBoolean:
2318 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002319 case Primitive::kPrimByte:
2320 case Primitive::kPrimShort:
2321 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002322 // Processing a Dex `int-to-char' instruction.
2323 locations->SetInAt(0, Location::Any());
2324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2325 break;
2326
2327 default:
2328 LOG(FATAL) << "Unexpected type conversion from " << input_type
2329 << " to " << result_type;
2330 }
2331 break;
2332
Roland Levillaindff1f282014-11-05 14:15:05 +00002333 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002334 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002335 case Primitive::kPrimBoolean:
2336 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002337 case Primitive::kPrimByte:
2338 case Primitive::kPrimShort:
2339 case Primitive::kPrimInt:
2340 case Primitive::kPrimChar:
2341 // Processing a Dex `int-to-float' instruction.
2342 locations->SetInAt(0, Location::RequiresRegister());
2343 locations->SetOut(Location::RequiresFpuRegister());
2344 break;
2345
2346 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002347 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002348 locations->SetInAt(0, Location::Any());
2349 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002350 break;
2351
Roland Levillaincff13742014-11-17 14:32:17 +00002352 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002353 // Processing a Dex `double-to-float' instruction.
2354 locations->SetInAt(0, Location::RequiresFpuRegister());
2355 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002356 break;
2357
2358 default:
2359 LOG(FATAL) << "Unexpected type conversion from " << input_type
2360 << " to " << result_type;
2361 };
2362 break;
2363
Roland Levillaindff1f282014-11-05 14:15:05 +00002364 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002365 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002366 case Primitive::kPrimBoolean:
2367 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002368 case Primitive::kPrimByte:
2369 case Primitive::kPrimShort:
2370 case Primitive::kPrimInt:
2371 case Primitive::kPrimChar:
2372 // Processing a Dex `int-to-double' instruction.
2373 locations->SetInAt(0, Location::RequiresRegister());
2374 locations->SetOut(Location::RequiresFpuRegister());
2375 break;
2376
2377 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002378 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002379 locations->SetInAt(0, Location::Any());
2380 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002381 break;
2382
Roland Levillaincff13742014-11-17 14:32:17 +00002383 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002384 // Processing a Dex `float-to-double' instruction.
2385 locations->SetInAt(0, Location::RequiresFpuRegister());
2386 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002387 break;
2388
2389 default:
2390 LOG(FATAL) << "Unexpected type conversion from " << input_type
2391 << " to " << result_type;
2392 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002393 break;
2394
2395 default:
2396 LOG(FATAL) << "Unexpected type conversion from " << input_type
2397 << " to " << result_type;
2398 }
2399}
2400
2401void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2402 LocationSummary* locations = conversion->GetLocations();
2403 Location out = locations->Out();
2404 Location in = locations->InAt(0);
2405 Primitive::Type result_type = conversion->GetResultType();
2406 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002407 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002408 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002409 case Primitive::kPrimByte:
2410 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002411 case Primitive::kPrimBoolean:
2412 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002413 case Primitive::kPrimShort:
2414 case Primitive::kPrimInt:
2415 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002416 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002417 if (in.IsRegister()) {
2418 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002419 } else {
2420 DCHECK(in.GetConstant()->IsIntConstant());
2421 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2422 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2423 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected type conversion from " << input_type
2428 << " to " << result_type;
2429 }
2430 break;
2431
Roland Levillain01a8d712014-11-14 16:27:39 +00002432 case Primitive::kPrimShort:
2433 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002434 case Primitive::kPrimBoolean:
2435 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002436 case Primitive::kPrimByte:
2437 case Primitive::kPrimInt:
2438 case Primitive::kPrimChar:
2439 // Processing a Dex `int-to-short' instruction.
2440 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002441 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002442 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002443 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002444 } else {
2445 DCHECK(in.GetConstant()->IsIntConstant());
2446 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002447 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002448 }
2449 break;
2450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
2455 break;
2456
Roland Levillain946e1432014-11-11 17:35:19 +00002457 case Primitive::kPrimInt:
2458 switch (input_type) {
2459 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002460 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002461 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002462 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002463 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002464 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002465 } else {
2466 DCHECK(in.IsConstant());
2467 DCHECK(in.GetConstant()->IsLongConstant());
2468 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002469 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002470 }
2471 break;
2472
Roland Levillain3f8f9362014-12-02 17:45:01 +00002473 case Primitive::kPrimFloat: {
2474 // Processing a Dex `float-to-int' instruction.
2475 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2476 Register output = out.AsRegister<Register>();
2477 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002478 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002479
2480 __ movl(output, Immediate(kPrimIntMax));
2481 // temp = int-to-float(output)
2482 __ cvtsi2ss(temp, output);
2483 // if input >= temp goto done
2484 __ comiss(input, temp);
2485 __ j(kAboveEqual, &done);
2486 // if input == NaN goto nan
2487 __ j(kUnordered, &nan);
2488 // output = float-to-int-truncate(input)
2489 __ cvttss2si(output, input);
2490 __ jmp(&done);
2491 __ Bind(&nan);
2492 // output = 0
2493 __ xorl(output, output);
2494 __ Bind(&done);
2495 break;
2496 }
2497
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002498 case Primitive::kPrimDouble: {
2499 // Processing a Dex `double-to-int' instruction.
2500 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2501 Register output = out.AsRegister<Register>();
2502 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002503 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002504
2505 __ movl(output, Immediate(kPrimIntMax));
2506 // temp = int-to-double(output)
2507 __ cvtsi2sd(temp, output);
2508 // if input >= temp goto done
2509 __ comisd(input, temp);
2510 __ j(kAboveEqual, &done);
2511 // if input == NaN goto nan
2512 __ j(kUnordered, &nan);
2513 // output = double-to-int-truncate(input)
2514 __ cvttsd2si(output, input);
2515 __ jmp(&done);
2516 __ Bind(&nan);
2517 // output = 0
2518 __ xorl(output, output);
2519 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002520 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002521 }
Roland Levillain946e1432014-11-11 17:35:19 +00002522
2523 default:
2524 LOG(FATAL) << "Unexpected type conversion from " << input_type
2525 << " to " << result_type;
2526 }
2527 break;
2528
Roland Levillaindff1f282014-11-05 14:15:05 +00002529 case Primitive::kPrimLong:
2530 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002531 case Primitive::kPrimBoolean:
2532 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002533 case Primitive::kPrimByte:
2534 case Primitive::kPrimShort:
2535 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002536 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002537 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002538 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2539 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002540 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002541 __ cdq();
2542 break;
2543
2544 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002545 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002546 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2547 conversion,
2548 conversion->GetDexPc(),
2549 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002550 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002551 break;
2552
Roland Levillaindff1f282014-11-05 14:15:05 +00002553 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002554 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002555 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2556 conversion,
2557 conversion->GetDexPc(),
2558 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002559 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
2566 break;
2567
Roland Levillain981e4542014-11-14 11:47:14 +00002568 case Primitive::kPrimChar:
2569 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002570 case Primitive::kPrimBoolean:
2571 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002572 case Primitive::kPrimByte:
2573 case Primitive::kPrimShort:
2574 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002575 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2576 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002577 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002578 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002579 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002580 } else {
2581 DCHECK(in.GetConstant()->IsIntConstant());
2582 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002584 }
2585 break;
2586
2587 default:
2588 LOG(FATAL) << "Unexpected type conversion from " << input_type
2589 << " to " << result_type;
2590 }
2591 break;
2592
Roland Levillaindff1f282014-11-05 14:15:05 +00002593 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002594 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002595 case Primitive::kPrimBoolean:
2596 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002597 case Primitive::kPrimByte:
2598 case Primitive::kPrimShort:
2599 case Primitive::kPrimInt:
2600 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002601 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002602 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002603 break;
2604
Roland Levillain6d0e4832014-11-27 18:31:21 +00002605 case Primitive::kPrimLong: {
2606 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002607 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002608
Roland Levillain232ade02015-04-20 15:14:36 +01002609 // Create stack space for the call to
2610 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2611 // TODO: enhance register allocator to ask for stack temporaries.
2612 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2613 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2614 __ subl(ESP, Immediate(adjustment));
2615 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002616
Roland Levillain232ade02015-04-20 15:14:36 +01002617 // Load the value to the FP stack, using temporaries if needed.
2618 PushOntoFPStack(in, 0, adjustment, false, true);
2619
2620 if (out.IsStackSlot()) {
2621 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2622 } else {
2623 __ fstps(Address(ESP, 0));
2624 Location stack_temp = Location::StackSlot(0);
2625 codegen_->Move32(out, stack_temp);
2626 }
2627
2628 // Remove the temporary stack space we allocated.
2629 if (adjustment != 0) {
2630 __ addl(ESP, Immediate(adjustment));
2631 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002632 break;
2633 }
2634
Roland Levillaincff13742014-11-17 14:32:17 +00002635 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002636 // Processing a Dex `double-to-float' instruction.
2637 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002638 break;
2639
2640 default:
2641 LOG(FATAL) << "Unexpected type conversion from " << input_type
2642 << " to " << result_type;
2643 };
2644 break;
2645
Roland Levillaindff1f282014-11-05 14:15:05 +00002646 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002647 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002648 case Primitive::kPrimBoolean:
2649 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002650 case Primitive::kPrimByte:
2651 case Primitive::kPrimShort:
2652 case Primitive::kPrimInt:
2653 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002654 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002655 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002656 break;
2657
Roland Levillain647b9ed2014-11-27 12:06:00 +00002658 case Primitive::kPrimLong: {
2659 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002660 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002661
Roland Levillain232ade02015-04-20 15:14:36 +01002662 // Create stack space for the call to
2663 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2664 // TODO: enhance register allocator to ask for stack temporaries.
2665 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2666 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2667 __ subl(ESP, Immediate(adjustment));
2668 }
2669
2670 // Load the value to the FP stack, using temporaries if needed.
2671 PushOntoFPStack(in, 0, adjustment, false, true);
2672
2673 if (out.IsDoubleStackSlot()) {
2674 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2675 } else {
2676 __ fstpl(Address(ESP, 0));
2677 Location stack_temp = Location::DoubleStackSlot(0);
2678 codegen_->Move64(out, stack_temp);
2679 }
2680
2681 // Remove the temporary stack space we allocated.
2682 if (adjustment != 0) {
2683 __ addl(ESP, Immediate(adjustment));
2684 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002685 break;
2686 }
2687
Roland Levillaincff13742014-11-17 14:32:17 +00002688 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002689 // Processing a Dex `float-to-double' instruction.
2690 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002691 break;
2692
2693 default:
2694 LOG(FATAL) << "Unexpected type conversion from " << input_type
2695 << " to " << result_type;
2696 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002697 break;
2698
2699 default:
2700 LOG(FATAL) << "Unexpected type conversion from " << input_type
2701 << " to " << result_type;
2702 }
2703}
2704
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002705void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002706 LocationSummary* locations =
2707 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002708 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002709 case Primitive::kPrimInt: {
2710 locations->SetInAt(0, Location::RequiresRegister());
2711 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2712 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2713 break;
2714 }
2715
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002716 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002717 locations->SetInAt(0, Location::RequiresRegister());
2718 locations->SetInAt(1, Location::Any());
2719 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002720 break;
2721 }
2722
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002723 case Primitive::kPrimFloat:
2724 case Primitive::kPrimDouble: {
2725 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002726 locations->SetInAt(1, Location::Any());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002727 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002728 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002729 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002730
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002731 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002732 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2733 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002734 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002735}
2736
2737void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2738 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002739 Location first = locations->InAt(0);
2740 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002741 Location out = locations->Out();
2742
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002743 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002744 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002745 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002746 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2747 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002748 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2749 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002750 } else {
2751 __ leal(out.AsRegister<Register>(), Address(
2752 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2753 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002754 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002755 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2756 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2757 __ addl(out.AsRegister<Register>(), Immediate(value));
2758 } else {
2759 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2760 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002761 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002762 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002763 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002764 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002765 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002766 }
2767
2768 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002769 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002770 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2771 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002772 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002773 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2774 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002775 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002776 } else {
2777 DCHECK(second.IsConstant()) << second;
2778 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2779 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2780 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002781 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002782 break;
2783 }
2784
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002785 case Primitive::kPrimFloat: {
2786 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002787 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002788 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2789 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2790 DCHECK(!const_area->NeedsMaterialization());
2791 __ addss(first.AsFpuRegister<XmmRegister>(),
2792 codegen_->LiteralFloatAddress(
2793 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2794 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2795 } else {
2796 DCHECK(second.IsStackSlot());
2797 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002798 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002799 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002800 }
2801
2802 case Primitive::kPrimDouble: {
2803 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002804 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002805 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2806 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
2807 DCHECK(!const_area->NeedsMaterialization());
2808 __ addsd(first.AsFpuRegister<XmmRegister>(),
2809 codegen_->LiteralDoubleAddress(
2810 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2811 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2812 } else {
2813 DCHECK(second.IsDoubleStackSlot());
2814 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002815 }
2816 break;
2817 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002818
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002819 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002820 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002821 }
2822}
2823
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002824void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002825 LocationSummary* locations =
2826 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002827 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002828 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002829 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002830 locations->SetInAt(0, Location::RequiresRegister());
2831 locations->SetInAt(1, Location::Any());
2832 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002833 break;
2834 }
Calin Juravle11351682014-10-23 15:38:15 +01002835 case Primitive::kPrimFloat:
2836 case Primitive::kPrimDouble: {
2837 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002838 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002839 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002840 break;
Calin Juravle11351682014-10-23 15:38:15 +01002841 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002842
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002843 default:
Calin Juravle11351682014-10-23 15:38:15 +01002844 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002846}
2847
2848void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2849 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002850 Location first = locations->InAt(0);
2851 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002852 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002853 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002854 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002855 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002857 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002858 __ subl(first.AsRegister<Register>(),
2859 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002860 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002861 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002862 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002863 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002864 }
2865
2866 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002867 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002868 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2869 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002870 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002871 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002872 __ sbbl(first.AsRegisterPairHigh<Register>(),
2873 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002874 } else {
2875 DCHECK(second.IsConstant()) << second;
2876 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2877 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2878 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002879 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002880 break;
2881 }
2882
Calin Juravle11351682014-10-23 15:38:15 +01002883 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002884 if (second.IsFpuRegister()) {
2885 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2886 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2887 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2888 DCHECK(!const_area->NeedsMaterialization());
2889 __ subss(first.AsFpuRegister<XmmRegister>(),
2890 codegen_->LiteralFloatAddress(
2891 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2892 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2893 } else {
2894 DCHECK(second.IsStackSlot());
2895 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2896 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002897 break;
Calin Juravle11351682014-10-23 15:38:15 +01002898 }
2899
2900 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002901 if (second.IsFpuRegister()) {
2902 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2903 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2904 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2905 DCHECK(!const_area->NeedsMaterialization());
2906 __ subsd(first.AsFpuRegister<XmmRegister>(),
2907 codegen_->LiteralDoubleAddress(
2908 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2909 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2910 } else {
2911 DCHECK(second.IsDoubleStackSlot());
2912 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2913 }
Calin Juravle11351682014-10-23 15:38:15 +01002914 break;
2915 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002916
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002917 default:
Calin Juravle11351682014-10-23 15:38:15 +01002918 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002919 }
2920}
2921
Calin Juravle34bacdf2014-10-07 20:23:36 +01002922void LocationsBuilderX86::VisitMul(HMul* mul) {
2923 LocationSummary* locations =
2924 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2925 switch (mul->GetResultType()) {
2926 case Primitive::kPrimInt:
2927 locations->SetInAt(0, Location::RequiresRegister());
2928 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002929 if (mul->InputAt(1)->IsIntConstant()) {
2930 // Can use 3 operand multiply.
2931 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2932 } else {
2933 locations->SetOut(Location::SameAsFirstInput());
2934 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002935 break;
2936 case Primitive::kPrimLong: {
2937 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002938 locations->SetInAt(1, Location::Any());
2939 locations->SetOut(Location::SameAsFirstInput());
2940 // Needed for imul on 32bits with 64bits output.
2941 locations->AddTemp(Location::RegisterLocation(EAX));
2942 locations->AddTemp(Location::RegisterLocation(EDX));
2943 break;
2944 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002945 case Primitive::kPrimFloat:
2946 case Primitive::kPrimDouble: {
2947 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002948 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002949 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002950 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002951 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002952
2953 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002954 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002955 }
2956}
2957
2958void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2959 LocationSummary* locations = mul->GetLocations();
2960 Location first = locations->InAt(0);
2961 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002962 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002963
2964 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002965 case Primitive::kPrimInt:
2966 // The constant may have ended up in a register, so test explicitly to avoid
2967 // problems where the output may not be the same as the first operand.
2968 if (mul->InputAt(1)->IsIntConstant()) {
2969 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2970 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2971 } else if (second.IsRegister()) {
2972 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002973 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002974 } else {
2975 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002976 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002977 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002978 }
2979 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002980
2981 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002982 Register in1_hi = first.AsRegisterPairHigh<Register>();
2983 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002984 Register eax = locations->GetTemp(0).AsRegister<Register>();
2985 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002986
2987 DCHECK_EQ(EAX, eax);
2988 DCHECK_EQ(EDX, edx);
2989
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002990 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002991 // output: in1
2992 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2993 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2994 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002995 if (second.IsConstant()) {
2996 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002997
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002998 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2999 int32_t low_value = Low32Bits(value);
3000 int32_t high_value = High32Bits(value);
3001 Immediate low(low_value);
3002 Immediate high(high_value);
3003
3004 __ movl(eax, high);
3005 // eax <- in1.lo * in2.hi
3006 __ imull(eax, in1_lo);
3007 // in1.hi <- in1.hi * in2.lo
3008 __ imull(in1_hi, low);
3009 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3010 __ addl(in1_hi, eax);
3011 // move in2_lo to eax to prepare for double precision
3012 __ movl(eax, low);
3013 // edx:eax <- in1.lo * in2.lo
3014 __ mull(in1_lo);
3015 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3016 __ addl(in1_hi, edx);
3017 // in1.lo <- (in1.lo * in2.lo)[31:0];
3018 __ movl(in1_lo, eax);
3019 } else if (second.IsRegisterPair()) {
3020 Register in2_hi = second.AsRegisterPairHigh<Register>();
3021 Register in2_lo = second.AsRegisterPairLow<Register>();
3022
3023 __ movl(eax, in2_hi);
3024 // eax <- in1.lo * in2.hi
3025 __ imull(eax, in1_lo);
3026 // in1.hi <- in1.hi * in2.lo
3027 __ imull(in1_hi, in2_lo);
3028 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3029 __ addl(in1_hi, eax);
3030 // move in1_lo to eax to prepare for double precision
3031 __ movl(eax, in1_lo);
3032 // edx:eax <- in1.lo * in2.lo
3033 __ mull(in2_lo);
3034 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3035 __ addl(in1_hi, edx);
3036 // in1.lo <- (in1.lo * in2.lo)[31:0];
3037 __ movl(in1_lo, eax);
3038 } else {
3039 DCHECK(second.IsDoubleStackSlot()) << second;
3040 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3041 Address in2_lo(ESP, second.GetStackIndex());
3042
3043 __ movl(eax, in2_hi);
3044 // eax <- in1.lo * in2.hi
3045 __ imull(eax, in1_lo);
3046 // in1.hi <- in1.hi * in2.lo
3047 __ imull(in1_hi, in2_lo);
3048 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3049 __ addl(in1_hi, eax);
3050 // move in1_lo to eax to prepare for double precision
3051 __ movl(eax, in1_lo);
3052 // edx:eax <- in1.lo * in2.lo
3053 __ mull(in2_lo);
3054 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3055 __ addl(in1_hi, edx);
3056 // in1.lo <- (in1.lo * in2.lo)[31:0];
3057 __ movl(in1_lo, eax);
3058 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003059
3060 break;
3061 }
3062
Calin Juravleb5bfa962014-10-21 18:02:24 +01003063 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003064 DCHECK(first.Equals(locations->Out()));
3065 if (second.IsFpuRegister()) {
3066 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3067 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3068 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3069 DCHECK(!const_area->NeedsMaterialization());
3070 __ mulss(first.AsFpuRegister<XmmRegister>(),
3071 codegen_->LiteralFloatAddress(
3072 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3073 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3074 } else {
3075 DCHECK(second.IsStackSlot());
3076 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3077 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003078 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003079 }
3080
3081 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003082 DCHECK(first.Equals(locations->Out()));
3083 if (second.IsFpuRegister()) {
3084 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3085 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3086 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3087 DCHECK(!const_area->NeedsMaterialization());
3088 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3089 codegen_->LiteralDoubleAddress(
3090 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3091 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3092 } else {
3093 DCHECK(second.IsDoubleStackSlot());
3094 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3095 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003096 break;
3097 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003098
3099 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003100 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003101 }
3102}
3103
Roland Levillain232ade02015-04-20 15:14:36 +01003104void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3105 uint32_t temp_offset,
3106 uint32_t stack_adjustment,
3107 bool is_fp,
3108 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003109 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003110 DCHECK(!is_wide);
3111 if (is_fp) {
3112 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3113 } else {
3114 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3115 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003116 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003117 DCHECK(is_wide);
3118 if (is_fp) {
3119 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3120 } else {
3121 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3122 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003123 } else {
3124 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003125 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003126 Location stack_temp = Location::StackSlot(temp_offset);
3127 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003128 if (is_fp) {
3129 __ flds(Address(ESP, temp_offset));
3130 } else {
3131 __ filds(Address(ESP, temp_offset));
3132 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003133 } else {
3134 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3135 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003136 if (is_fp) {
3137 __ fldl(Address(ESP, temp_offset));
3138 } else {
3139 __ fildl(Address(ESP, temp_offset));
3140 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003141 }
3142 }
3143}
3144
3145void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3146 Primitive::Type type = rem->GetResultType();
3147 bool is_float = type == Primitive::kPrimFloat;
3148 size_t elem_size = Primitive::ComponentSize(type);
3149 LocationSummary* locations = rem->GetLocations();
3150 Location first = locations->InAt(0);
3151 Location second = locations->InAt(1);
3152 Location out = locations->Out();
3153
3154 // Create stack space for 2 elements.
3155 // TODO: enhance register allocator to ask for stack temporaries.
3156 __ subl(ESP, Immediate(2 * elem_size));
3157
3158 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003159 const bool is_wide = !is_float;
3160 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3161 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003162
3163 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003164 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003165 __ Bind(&retry);
3166 __ fprem();
3167
3168 // Move FP status to AX.
3169 __ fstsw();
3170
3171 // And see if the argument reduction is complete. This is signaled by the
3172 // C2 FPU flag bit set to 0.
3173 __ andl(EAX, Immediate(kC2ConditionMask));
3174 __ j(kNotEqual, &retry);
3175
3176 // We have settled on the final value. Retrieve it into an XMM register.
3177 // Store FP top of stack to real stack.
3178 if (is_float) {
3179 __ fsts(Address(ESP, 0));
3180 } else {
3181 __ fstl(Address(ESP, 0));
3182 }
3183
3184 // Pop the 2 items from the FP stack.
3185 __ fucompp();
3186
3187 // Load the value from the stack into an XMM register.
3188 DCHECK(out.IsFpuRegister()) << out;
3189 if (is_float) {
3190 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3191 } else {
3192 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3193 }
3194
3195 // And remove the temporary stack space we allocated.
3196 __ addl(ESP, Immediate(2 * elem_size));
3197}
3198
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003199
3200void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3201 DCHECK(instruction->IsDiv() || instruction->IsRem());
3202
3203 LocationSummary* locations = instruction->GetLocations();
3204 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003205 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003206
3207 Register out_register = locations->Out().AsRegister<Register>();
3208 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003209 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003210
3211 DCHECK(imm == 1 || imm == -1);
3212
3213 if (instruction->IsRem()) {
3214 __ xorl(out_register, out_register);
3215 } else {
3216 __ movl(out_register, input_register);
3217 if (imm == -1) {
3218 __ negl(out_register);
3219 }
3220 }
3221}
3222
3223
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003224void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003225 LocationSummary* locations = instruction->GetLocations();
3226
3227 Register out_register = locations->Out().AsRegister<Register>();
3228 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003229 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003230
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003231 DCHECK(IsPowerOfTwo(std::abs(imm)));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003232 Register num = locations->GetTemp(0).AsRegister<Register>();
3233
3234 __ leal(num, Address(input_register, std::abs(imm) - 1));
3235 __ testl(input_register, input_register);
3236 __ cmovl(kGreaterEqual, num, input_register);
3237 int shift = CTZ(imm);
3238 __ sarl(num, Immediate(shift));
3239
3240 if (imm < 0) {
3241 __ negl(num);
3242 }
3243
3244 __ movl(out_register, num);
3245}
3246
3247void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3248 DCHECK(instruction->IsDiv() || instruction->IsRem());
3249
3250 LocationSummary* locations = instruction->GetLocations();
3251 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3252
3253 Register eax = locations->InAt(0).AsRegister<Register>();
3254 Register out = locations->Out().AsRegister<Register>();
3255 Register num;
3256 Register edx;
3257
3258 if (instruction->IsDiv()) {
3259 edx = locations->GetTemp(0).AsRegister<Register>();
3260 num = locations->GetTemp(1).AsRegister<Register>();
3261 } else {
3262 edx = locations->Out().AsRegister<Register>();
3263 num = locations->GetTemp(0).AsRegister<Register>();
3264 }
3265
3266 DCHECK_EQ(EAX, eax);
3267 DCHECK_EQ(EDX, edx);
3268 if (instruction->IsDiv()) {
3269 DCHECK_EQ(EAX, out);
3270 } else {
3271 DCHECK_EQ(EDX, out);
3272 }
3273
3274 int64_t magic;
3275 int shift;
3276 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3277
Mark Mendell0c9497d2015-08-21 09:30:05 -04003278 NearLabel ndiv;
3279 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003280 // If numerator is 0, the result is 0, no computation needed.
3281 __ testl(eax, eax);
3282 __ j(kNotEqual, &ndiv);
3283
3284 __ xorl(out, out);
3285 __ jmp(&end);
3286
3287 __ Bind(&ndiv);
3288
3289 // Save the numerator.
3290 __ movl(num, eax);
3291
3292 // EAX = magic
3293 __ movl(eax, Immediate(magic));
3294
3295 // EDX:EAX = magic * numerator
3296 __ imull(num);
3297
3298 if (imm > 0 && magic < 0) {
3299 // EDX += num
3300 __ addl(edx, num);
3301 } else if (imm < 0 && magic > 0) {
3302 __ subl(edx, num);
3303 }
3304
3305 // Shift if needed.
3306 if (shift != 0) {
3307 __ sarl(edx, Immediate(shift));
3308 }
3309
3310 // EDX += 1 if EDX < 0
3311 __ movl(eax, edx);
3312 __ shrl(edx, Immediate(31));
3313 __ addl(edx, eax);
3314
3315 if (instruction->IsRem()) {
3316 __ movl(eax, num);
3317 __ imull(edx, Immediate(imm));
3318 __ subl(eax, edx);
3319 __ movl(edx, eax);
3320 } else {
3321 __ movl(eax, edx);
3322 }
3323 __ Bind(&end);
3324}
3325
Calin Juravlebacfec32014-11-14 15:54:36 +00003326void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3327 DCHECK(instruction->IsDiv() || instruction->IsRem());
3328
3329 LocationSummary* locations = instruction->GetLocations();
3330 Location out = locations->Out();
3331 Location first = locations->InAt(0);
3332 Location second = locations->InAt(1);
3333 bool is_div = instruction->IsDiv();
3334
3335 switch (instruction->GetResultType()) {
3336 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003337 DCHECK_EQ(EAX, first.AsRegister<Register>());
3338 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003339
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003340 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003341 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003342
3343 if (imm == 0) {
3344 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3345 } else if (imm == 1 || imm == -1) {
3346 DivRemOneOrMinusOne(instruction);
3347 } else if (is_div && IsPowerOfTwo(std::abs(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003348 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349 } else {
3350 DCHECK(imm <= -2 || imm >= 2);
3351 GenerateDivRemWithAnyConstant(instruction);
3352 }
3353 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003354 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003355 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003356 is_div);
3357 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003358
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003359 Register second_reg = second.AsRegister<Register>();
3360 // 0x80000000/-1 triggers an arithmetic exception!
3361 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3362 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003363
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003364 __ cmpl(second_reg, Immediate(-1));
3365 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003366
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003367 // edx:eax <- sign-extended of eax
3368 __ cdq();
3369 // eax = quotient, edx = remainder
3370 __ idivl(second_reg);
3371 __ Bind(slow_path->GetExitLabel());
3372 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003373 break;
3374 }
3375
3376 case Primitive::kPrimLong: {
3377 InvokeRuntimeCallingConvention calling_convention;
3378 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3379 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3380 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3381 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3382 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3383 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3384
3385 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003386 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3387 instruction,
3388 instruction->GetDexPc(),
3389 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003390 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003391 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003392 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3393 instruction,
3394 instruction->GetDexPc(),
3395 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003396 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003397 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003398 break;
3399 }
3400
3401 default:
3402 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3403 }
3404}
3405
Calin Juravle7c4954d2014-10-28 16:57:40 +00003406void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003407 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003408 ? LocationSummary::kCall
3409 : LocationSummary::kNoCall;
3410 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3411
Calin Juravle7c4954d2014-10-28 16:57:40 +00003412 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003413 case Primitive::kPrimInt: {
3414 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003415 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003416 locations->SetOut(Location::SameAsFirstInput());
3417 // Intel uses edx:eax as the dividend.
3418 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003419 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3420 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3421 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003422 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003423 locations->AddTemp(Location::RequiresRegister());
3424 }
Calin Juravled0d48522014-11-04 16:40:20 +00003425 break;
3426 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003427 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003428 InvokeRuntimeCallingConvention calling_convention;
3429 locations->SetInAt(0, Location::RegisterPairLocation(
3430 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3431 locations->SetInAt(1, Location::RegisterPairLocation(
3432 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3433 // Runtime helper puts the result in EAX, EDX.
3434 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003435 break;
3436 }
3437 case Primitive::kPrimFloat:
3438 case Primitive::kPrimDouble: {
3439 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003440 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003441 locations->SetOut(Location::SameAsFirstInput());
3442 break;
3443 }
3444
3445 default:
3446 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3447 }
3448}
3449
3450void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3451 LocationSummary* locations = div->GetLocations();
3452 Location first = locations->InAt(0);
3453 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003454
3455 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003456 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003457 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003458 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003459 break;
3460 }
3461
3462 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003463 if (second.IsFpuRegister()) {
3464 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3465 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3466 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3467 DCHECK(!const_area->NeedsMaterialization());
3468 __ divss(first.AsFpuRegister<XmmRegister>(),
3469 codegen_->LiteralFloatAddress(
3470 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3471 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3472 } else {
3473 DCHECK(second.IsStackSlot());
3474 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3475 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003476 break;
3477 }
3478
3479 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003480 if (second.IsFpuRegister()) {
3481 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3482 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3483 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3484 DCHECK(!const_area->NeedsMaterialization());
3485 __ divsd(first.AsFpuRegister<XmmRegister>(),
3486 codegen_->LiteralDoubleAddress(
3487 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3488 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3489 } else {
3490 DCHECK(second.IsDoubleStackSlot());
3491 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3492 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003493 break;
3494 }
3495
3496 default:
3497 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3498 }
3499}
3500
Calin Juravlebacfec32014-11-14 15:54:36 +00003501void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003502 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003503
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003504 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3505 ? LocationSummary::kCall
3506 : LocationSummary::kNoCall;
3507 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003508
Calin Juravled2ec87d2014-12-08 14:24:46 +00003509 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003510 case Primitive::kPrimInt: {
3511 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003512 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003513 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003514 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3515 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3516 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003517 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003518 locations->AddTemp(Location::RequiresRegister());
3519 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003520 break;
3521 }
3522 case Primitive::kPrimLong: {
3523 InvokeRuntimeCallingConvention calling_convention;
3524 locations->SetInAt(0, Location::RegisterPairLocation(
3525 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3526 locations->SetInAt(1, Location::RegisterPairLocation(
3527 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3528 // Runtime helper puts the result in EAX, EDX.
3529 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3530 break;
3531 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003532 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003533 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003534 locations->SetInAt(0, Location::Any());
3535 locations->SetInAt(1, Location::Any());
3536 locations->SetOut(Location::RequiresFpuRegister());
3537 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003538 break;
3539 }
3540
3541 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003542 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 }
3544}
3545
3546void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3547 Primitive::Type type = rem->GetResultType();
3548 switch (type) {
3549 case Primitive::kPrimInt:
3550 case Primitive::kPrimLong: {
3551 GenerateDivRemIntegral(rem);
3552 break;
3553 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003554 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003555 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003556 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003557 break;
3558 }
3559 default:
3560 LOG(FATAL) << "Unexpected rem type " << type;
3561 }
3562}
3563
Calin Juravled0d48522014-11-04 16:40:20 +00003564void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003565 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3566 ? LocationSummary::kCallOnSlowPath
3567 : LocationSummary::kNoCall;
3568 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003569 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003570 case Primitive::kPrimByte:
3571 case Primitive::kPrimChar:
3572 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003573 case Primitive::kPrimInt: {
3574 locations->SetInAt(0, Location::Any());
3575 break;
3576 }
3577 case Primitive::kPrimLong: {
3578 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3579 if (!instruction->IsConstant()) {
3580 locations->AddTemp(Location::RequiresRegister());
3581 }
3582 break;
3583 }
3584 default:
3585 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3586 }
Calin Juravled0d48522014-11-04 16:40:20 +00003587 if (instruction->HasUses()) {
3588 locations->SetOut(Location::SameAsFirstInput());
3589 }
3590}
3591
3592void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003593 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003594 codegen_->AddSlowPath(slow_path);
3595
3596 LocationSummary* locations = instruction->GetLocations();
3597 Location value = locations->InAt(0);
3598
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003599 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003600 case Primitive::kPrimByte:
3601 case Primitive::kPrimChar:
3602 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003603 case Primitive::kPrimInt: {
3604 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003605 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003606 __ j(kEqual, slow_path->GetEntryLabel());
3607 } else if (value.IsStackSlot()) {
3608 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3609 __ j(kEqual, slow_path->GetEntryLabel());
3610 } else {
3611 DCHECK(value.IsConstant()) << value;
3612 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3613 __ jmp(slow_path->GetEntryLabel());
3614 }
3615 }
3616 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003617 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003618 case Primitive::kPrimLong: {
3619 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003620 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003621 __ movl(temp, value.AsRegisterPairLow<Register>());
3622 __ orl(temp, value.AsRegisterPairHigh<Register>());
3623 __ j(kEqual, slow_path->GetEntryLabel());
3624 } else {
3625 DCHECK(value.IsConstant()) << value;
3626 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3627 __ jmp(slow_path->GetEntryLabel());
3628 }
3629 }
3630 break;
3631 }
3632 default:
3633 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003634 }
Calin Juravled0d48522014-11-04 16:40:20 +00003635}
3636
Calin Juravle9aec02f2014-11-18 23:06:35 +00003637void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3638 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3639
3640 LocationSummary* locations =
3641 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3642
3643 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003644 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003645 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003646 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003647 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003648 // The shift count needs to be in CL or a constant.
3649 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003650 locations->SetOut(Location::SameAsFirstInput());
3651 break;
3652 }
3653 default:
3654 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3655 }
3656}
3657
3658void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3659 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3660
3661 LocationSummary* locations = op->GetLocations();
3662 Location first = locations->InAt(0);
3663 Location second = locations->InAt(1);
3664 DCHECK(first.Equals(locations->Out()));
3665
3666 switch (op->GetResultType()) {
3667 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003668 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003669 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003670 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003671 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003672 DCHECK_EQ(ECX, second_reg);
3673 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003674 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003675 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003676 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003678 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003679 }
3680 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003681 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3682 if (shift == 0) {
3683 return;
3684 }
3685 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003686 if (op->IsShl()) {
3687 __ shll(first_reg, imm);
3688 } else if (op->IsShr()) {
3689 __ sarl(first_reg, imm);
3690 } else {
3691 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003692 }
3693 }
3694 break;
3695 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003696 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003697 if (second.IsRegister()) {
3698 Register second_reg = second.AsRegister<Register>();
3699 DCHECK_EQ(ECX, second_reg);
3700 if (op->IsShl()) {
3701 GenerateShlLong(first, second_reg);
3702 } else if (op->IsShr()) {
3703 GenerateShrLong(first, second_reg);
3704 } else {
3705 GenerateUShrLong(first, second_reg);
3706 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003707 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003708 // Shift by a constant.
3709 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3710 // Nothing to do if the shift is 0, as the input is already the output.
3711 if (shift != 0) {
3712 if (op->IsShl()) {
3713 GenerateShlLong(first, shift);
3714 } else if (op->IsShr()) {
3715 GenerateShrLong(first, shift);
3716 } else {
3717 GenerateUShrLong(first, shift);
3718 }
3719 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003720 }
3721 break;
3722 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003723 default:
3724 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3725 }
3726}
3727
Mark P Mendell73945692015-04-29 14:56:17 +00003728void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3729 Register low = loc.AsRegisterPairLow<Register>();
3730 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003731 if (shift == 1) {
3732 // This is just an addition.
3733 __ addl(low, low);
3734 __ adcl(high, high);
3735 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003736 // Shift by 32 is easy. High gets low, and low gets 0.
3737 codegen_->EmitParallelMoves(
3738 loc.ToLow(),
3739 loc.ToHigh(),
3740 Primitive::kPrimInt,
3741 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3742 loc.ToLow(),
3743 Primitive::kPrimInt);
3744 } else if (shift > 32) {
3745 // Low part becomes 0. High part is low part << (shift-32).
3746 __ movl(high, low);
3747 __ shll(high, Immediate(shift - 32));
3748 __ xorl(low, low);
3749 } else {
3750 // Between 1 and 31.
3751 __ shld(high, low, Immediate(shift));
3752 __ shll(low, Immediate(shift));
3753 }
3754}
3755
Calin Juravle9aec02f2014-11-18 23:06:35 +00003756void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003757 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003758 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3759 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3760 __ testl(shifter, Immediate(32));
3761 __ j(kEqual, &done);
3762 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3763 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3764 __ Bind(&done);
3765}
3766
Mark P Mendell73945692015-04-29 14:56:17 +00003767void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3768 Register low = loc.AsRegisterPairLow<Register>();
3769 Register high = loc.AsRegisterPairHigh<Register>();
3770 if (shift == 32) {
3771 // Need to copy the sign.
3772 DCHECK_NE(low, high);
3773 __ movl(low, high);
3774 __ sarl(high, Immediate(31));
3775 } else if (shift > 32) {
3776 DCHECK_NE(low, high);
3777 // High part becomes sign. Low part is shifted by shift - 32.
3778 __ movl(low, high);
3779 __ sarl(high, Immediate(31));
3780 __ sarl(low, Immediate(shift - 32));
3781 } else {
3782 // Between 1 and 31.
3783 __ shrd(low, high, Immediate(shift));
3784 __ sarl(high, Immediate(shift));
3785 }
3786}
3787
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003789 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003790 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3791 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3792 __ testl(shifter, Immediate(32));
3793 __ j(kEqual, &done);
3794 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3795 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3796 __ Bind(&done);
3797}
3798
Mark P Mendell73945692015-04-29 14:56:17 +00003799void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3800 Register low = loc.AsRegisterPairLow<Register>();
3801 Register high = loc.AsRegisterPairHigh<Register>();
3802 if (shift == 32) {
3803 // Shift by 32 is easy. Low gets high, and high gets 0.
3804 codegen_->EmitParallelMoves(
3805 loc.ToHigh(),
3806 loc.ToLow(),
3807 Primitive::kPrimInt,
3808 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3809 loc.ToHigh(),
3810 Primitive::kPrimInt);
3811 } else if (shift > 32) {
3812 // Low part is high >> (shift - 32). High part becomes 0.
3813 __ movl(low, high);
3814 __ shrl(low, Immediate(shift - 32));
3815 __ xorl(high, high);
3816 } else {
3817 // Between 1 and 31.
3818 __ shrd(low, high, Immediate(shift));
3819 __ shrl(high, Immediate(shift));
3820 }
3821}
3822
Calin Juravle9aec02f2014-11-18 23:06:35 +00003823void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003824 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003825 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3826 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3827 __ testl(shifter, Immediate(32));
3828 __ j(kEqual, &done);
3829 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3830 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3831 __ Bind(&done);
3832}
3833
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003834void LocationsBuilderX86::VisitRor(HRor* ror) {
3835 LocationSummary* locations =
3836 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3837
3838 switch (ror->GetResultType()) {
3839 case Primitive::kPrimLong:
3840 // Add the temporary needed.
3841 locations->AddTemp(Location::RequiresRegister());
3842 FALLTHROUGH_INTENDED;
3843 case Primitive::kPrimInt:
3844 locations->SetInAt(0, Location::RequiresRegister());
3845 // The shift count needs to be in CL (unless it is a constant).
3846 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3847 locations->SetOut(Location::SameAsFirstInput());
3848 break;
3849 default:
3850 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3851 UNREACHABLE();
3852 }
3853}
3854
3855void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3856 LocationSummary* locations = ror->GetLocations();
3857 Location first = locations->InAt(0);
3858 Location second = locations->InAt(1);
3859
3860 if (ror->GetResultType() == Primitive::kPrimInt) {
3861 Register first_reg = first.AsRegister<Register>();
3862 if (second.IsRegister()) {
3863 Register second_reg = second.AsRegister<Register>();
3864 __ rorl(first_reg, second_reg);
3865 } else {
3866 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3867 __ rorl(first_reg, imm);
3868 }
3869 return;
3870 }
3871
3872 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3873 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3874 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3875 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3876 if (second.IsRegister()) {
3877 Register second_reg = second.AsRegister<Register>();
3878 DCHECK_EQ(second_reg, ECX);
3879 __ movl(temp_reg, first_reg_hi);
3880 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3881 __ shrd(first_reg_lo, temp_reg, second_reg);
3882 __ movl(temp_reg, first_reg_hi);
3883 __ testl(second_reg, Immediate(32));
3884 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3885 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3886 } else {
3887 int32_t shift_amt =
3888 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3889 if (shift_amt == 0) {
3890 // Already fine.
3891 return;
3892 }
3893 if (shift_amt == 32) {
3894 // Just swap.
3895 __ movl(temp_reg, first_reg_lo);
3896 __ movl(first_reg_lo, first_reg_hi);
3897 __ movl(first_reg_hi, temp_reg);
3898 return;
3899 }
3900
3901 Immediate imm(shift_amt);
3902 // Save the constents of the low value.
3903 __ movl(temp_reg, first_reg_lo);
3904
3905 // Shift right into low, feeding bits from high.
3906 __ shrd(first_reg_lo, first_reg_hi, imm);
3907
3908 // Shift right into high, feeding bits from the original low.
3909 __ shrd(first_reg_hi, temp_reg, imm);
3910
3911 // Swap if needed.
3912 if (shift_amt > 32) {
3913 __ movl(temp_reg, first_reg_lo);
3914 __ movl(first_reg_lo, first_reg_hi);
3915 __ movl(first_reg_hi, temp_reg);
3916 }
3917 }
3918}
3919
Calin Juravle9aec02f2014-11-18 23:06:35 +00003920void LocationsBuilderX86::VisitShl(HShl* shl) {
3921 HandleShift(shl);
3922}
3923
3924void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3925 HandleShift(shl);
3926}
3927
3928void LocationsBuilderX86::VisitShr(HShr* shr) {
3929 HandleShift(shr);
3930}
3931
3932void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3933 HandleShift(shr);
3934}
3935
3936void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3937 HandleShift(ushr);
3938}
3939
3940void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3941 HandleShift(ushr);
3942}
3943
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003944void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003945 LocationSummary* locations =
3946 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003947 locations->SetOut(Location::RegisterLocation(EAX));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003948 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffray729645a2015-11-19 13:29:02 +00003949 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3950 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003951}
3952
3953void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003954 // Note: if heap poisoning is enabled, the entry point takes cares
3955 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003956 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3957 instruction,
3958 instruction->GetDexPc(),
3959 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003960 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01003961 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003962}
3963
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003964void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3965 LocationSummary* locations =
3966 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3967 locations->SetOut(Location::RegisterLocation(EAX));
3968 InvokeRuntimeCallingConvention calling_convention;
3969 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003970 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003971 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003972}
3973
3974void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3975 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003976 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003977 // Note: if heap poisoning is enabled, the entry point takes cares
3978 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003979 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3980 instruction,
3981 instruction->GetDexPc(),
3982 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003983 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003984 DCHECK(!codegen_->IsLeafMethod());
3985}
3986
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003987void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003988 LocationSummary* locations =
3989 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003990 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3991 if (location.IsStackSlot()) {
3992 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3993 } else if (location.IsDoubleStackSlot()) {
3994 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003995 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003996 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003997}
3998
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003999void InstructionCodeGeneratorX86::VisitParameterValue(
4000 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4001}
4002
4003void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4004 LocationSummary* locations =
4005 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4006 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4007}
4008
4009void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004010}
4011
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004012void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004013 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004014 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004015 locations->SetInAt(0, Location::RequiresRegister());
4016 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004017}
4018
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004019void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4020 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004021 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004022 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004023 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004024 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004025 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004026 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004027 break;
4028
4029 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004030 __ notl(out.AsRegisterPairLow<Register>());
4031 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004032 break;
4033
4034 default:
4035 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4036 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004037}
4038
David Brazdil66d126e2015-04-03 16:02:44 +01004039void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4040 LocationSummary* locations =
4041 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4042 locations->SetInAt(0, Location::RequiresRegister());
4043 locations->SetOut(Location::SameAsFirstInput());
4044}
4045
4046void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004047 LocationSummary* locations = bool_not->GetLocations();
4048 Location in = locations->InAt(0);
4049 Location out = locations->Out();
4050 DCHECK(in.Equals(out));
4051 __ xorl(out.AsRegister<Register>(), Immediate(1));
4052}
4053
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004054void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004055 LocationSummary* locations =
4056 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004057 switch (compare->InputAt(0)->GetType()) {
4058 case Primitive::kPrimLong: {
4059 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004060 locations->SetInAt(1, Location::Any());
4061 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4062 break;
4063 }
4064 case Primitive::kPrimFloat:
4065 case Primitive::kPrimDouble: {
4066 locations->SetInAt(0, Location::RequiresFpuRegister());
4067 locations->SetInAt(1, Location::RequiresFpuRegister());
4068 locations->SetOut(Location::RequiresRegister());
4069 break;
4070 }
4071 default:
4072 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4073 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004074}
4075
4076void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004077 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004078 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004079 Location left = locations->InAt(0);
4080 Location right = locations->InAt(1);
4081
Mark Mendell0c9497d2015-08-21 09:30:05 -04004082 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004083 switch (compare->InputAt(0)->GetType()) {
4084 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004085 Register left_low = left.AsRegisterPairLow<Register>();
4086 Register left_high = left.AsRegisterPairHigh<Register>();
4087 int32_t val_low = 0;
4088 int32_t val_high = 0;
4089 bool right_is_const = false;
4090
4091 if (right.IsConstant()) {
4092 DCHECK(right.GetConstant()->IsLongConstant());
4093 right_is_const = true;
4094 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4095 val_low = Low32Bits(val);
4096 val_high = High32Bits(val);
4097 }
4098
Calin Juravleddb7df22014-11-25 20:56:51 +00004099 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004100 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004101 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004102 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004103 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004104 DCHECK(right_is_const) << right;
4105 if (val_high == 0) {
4106 __ testl(left_high, left_high);
4107 } else {
4108 __ cmpl(left_high, Immediate(val_high));
4109 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004110 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004111 __ j(kLess, &less); // Signed compare.
4112 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004113 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004114 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004115 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004116 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004117 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004118 DCHECK(right_is_const) << right;
4119 if (val_low == 0) {
4120 __ testl(left_low, left_low);
4121 } else {
4122 __ cmpl(left_low, Immediate(val_low));
4123 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004124 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004125 break;
4126 }
4127 case Primitive::kPrimFloat: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004128 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004129 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4130 break;
4131 }
4132 case Primitive::kPrimDouble: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004133 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004134 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004135 break;
4136 }
4137 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004138 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004139 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004140 __ movl(out, Immediate(0));
4141 __ j(kEqual, &done);
4142 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4143
4144 __ Bind(&greater);
4145 __ movl(out, Immediate(1));
4146 __ jmp(&done);
4147
4148 __ Bind(&less);
4149 __ movl(out, Immediate(-1));
4150
4151 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004152}
4153
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004154void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004155 LocationSummary* locations =
4156 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004157 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4158 locations->SetInAt(i, Location::Any());
4159 }
4160 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004161}
4162
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004163void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004164 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004165}
4166
Roland Levillain7c1559a2015-12-15 10:55:36 +00004167void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004168 /*
4169 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4170 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4171 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4172 */
4173 switch (kind) {
4174 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004175 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004176 break;
4177 }
4178 case MemBarrierKind::kAnyStore:
4179 case MemBarrierKind::kLoadAny:
4180 case MemBarrierKind::kStoreStore: {
4181 // nop
4182 break;
4183 }
4184 default:
4185 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004186 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004187}
4188
Vladimir Markodc151b22015-10-15 18:02:30 +01004189HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4190 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4191 MethodReference target_method ATTRIBUTE_UNUSED) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004192 switch (desired_dispatch_info.code_ptr_location) {
4193 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4194 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4195 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4196 // (Though the direct CALL ptr16:32 is available for consideration).
4197 return HInvokeStaticOrDirect::DispatchInfo {
4198 desired_dispatch_info.method_load_kind,
4199 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
4200 desired_dispatch_info.method_load_data,
4201 0u
4202 };
4203 default:
4204 return desired_dispatch_info;
4205 }
4206}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004207
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004208Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4209 Register temp) {
4210 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004211 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004212 if (!invoke->GetLocations()->Intrinsified()) {
4213 return location.AsRegister<Register>();
4214 }
4215 // For intrinsics we allow any location, so it may be on the stack.
4216 if (!location.IsRegister()) {
4217 __ movl(temp, Address(ESP, location.GetStackIndex()));
4218 return temp;
4219 }
4220 // For register locations, check if the register was saved. If so, get it from the stack.
4221 // Note: There is a chance that the register was saved but not overwritten, so we could
4222 // save one load. However, since this is just an intrinsic slow path we prefer this
4223 // simple and more robust approach rather that trying to determine if that's the case.
4224 SlowPathCode* slow_path = GetCurrentSlowPath();
4225 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4226 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4227 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4228 __ movl(temp, Address(ESP, stack_offset));
4229 return temp;
4230 }
4231 return location.AsRegister<Register>();
4232}
4233
Vladimir Marko58155012015-08-19 12:49:41 +00004234void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4235 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4236 switch (invoke->GetMethodLoadKind()) {
4237 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4238 // temp = thread->string_init_entrypoint
4239 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4240 break;
4241 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004242 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004243 break;
4244 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4245 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4246 break;
4247 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4248 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4249 method_patches_.emplace_back(invoke->GetTargetMethod());
4250 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4251 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004252 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4253 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4254 temp.AsRegister<Register>());
4255 uint32_t offset = invoke->GetDexCacheArrayOffset();
4256 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4257 // Add the patch entry and bind its label at the end of the instruction.
4258 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4259 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4260 break;
4261 }
Vladimir Marko58155012015-08-19 12:49:41 +00004262 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004263 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004264 Register method_reg;
4265 Register reg = temp.AsRegister<Register>();
4266 if (current_method.IsRegister()) {
4267 method_reg = current_method.AsRegister<Register>();
4268 } else {
4269 DCHECK(IsBaseline() || invoke->GetLocations()->Intrinsified());
4270 DCHECK(!current_method.IsValid());
4271 method_reg = reg;
4272 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4273 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004274 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004275 __ movl(reg, Address(method_reg,
4276 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004277 // temp = temp[index_in_cache]
4278 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4279 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4280 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004281 }
Vladimir Marko58155012015-08-19 12:49:41 +00004282 }
4283
4284 switch (invoke->GetCodePtrLocation()) {
4285 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4286 __ call(GetFrameEntryLabel());
4287 break;
4288 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4289 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4290 Label* label = &relative_call_patches_.back().label;
4291 __ call(label); // Bind to the patch label, override at link time.
4292 __ Bind(label); // Bind the label at the end of the "call" insn.
4293 break;
4294 }
4295 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4296 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004297 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4298 LOG(FATAL) << "Unsupported";
4299 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004300 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4301 // (callee_method + offset_of_quick_compiled_code)()
4302 __ call(Address(callee_method.AsRegister<Register>(),
4303 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4304 kX86WordSize).Int32Value()));
4305 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004306 }
4307
4308 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004309}
4310
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004311void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4312 Register temp = temp_in.AsRegister<Register>();
4313 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4314 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004315
4316 // Use the calling convention instead of the location of the receiver, as
4317 // intrinsics may have put the receiver in a different register. In the intrinsics
4318 // slow path, the arguments have been moved to the right place, so here we are
4319 // guaranteed that the receiver is the first register of the calling convention.
4320 InvokeDexCallingConvention calling_convention;
4321 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004322 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004323 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004324 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004325 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004326 // Instead of simply (possibly) unpoisoning `temp` here, we should
4327 // emit a read barrier for the previous class reference load.
4328 // However this is not required in practice, as this is an
4329 // intermediate/temporary reference and because the current
4330 // concurrent copying collector keeps the from-space memory
4331 // intact/accessible until the end of the marking phase (the
4332 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004333 __ MaybeUnpoisonHeapReference(temp);
4334 // temp = temp->GetMethodAt(method_offset);
4335 __ movl(temp, Address(temp, method_offset));
4336 // call temp->GetEntryPoint();
4337 __ call(Address(
4338 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4339}
4340
Vladimir Marko58155012015-08-19 12:49:41 +00004341void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4342 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004343 size_t size =
4344 method_patches_.size() +
4345 relative_call_patches_.size() +
4346 pc_relative_dex_cache_patches_.size();
4347 linker_patches->reserve(size);
4348 // The label points to the end of the "movl" insn but the literal offset for method
4349 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4350 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004351 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004352 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004353 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4354 info.target_method.dex_file,
4355 info.target_method.dex_method_index));
4356 }
4357 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004358 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004359 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4360 info.target_method.dex_file,
4361 info.target_method.dex_method_index));
4362 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004363 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4364 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4365 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4366 &info.target_dex_file,
4367 GetMethodAddressOffset(),
4368 info.element_offset));
4369 }
Vladimir Marko58155012015-08-19 12:49:41 +00004370}
4371
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004372void CodeGeneratorX86::MarkGCCard(Register temp,
4373 Register card,
4374 Register object,
4375 Register value,
4376 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004377 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004378 if (value_can_be_null) {
4379 __ testl(value, value);
4380 __ j(kEqual, &is_null);
4381 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004382 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4383 __ movl(temp, object);
4384 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004385 __ movb(Address(temp, card, TIMES_1, 0),
4386 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004387 if (value_can_be_null) {
4388 __ Bind(&is_null);
4389 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004390}
4391
Calin Juravle52c48962014-12-16 17:02:57 +00004392void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4393 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004394
4395 bool object_field_get_with_read_barrier =
4396 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004397 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004398 new (GetGraph()->GetArena()) LocationSummary(instruction,
4399 kEmitCompilerReadBarrier ?
4400 LocationSummary::kCallOnSlowPath :
4401 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004402 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004403
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004404 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4405 locations->SetOut(Location::RequiresFpuRegister());
4406 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004407 // The output overlaps in case of long: we don't want the low move
4408 // to overwrite the object's location. Likewise, in the case of
4409 // an object field get with read barriers enabled, we do not want
4410 // the move to overwrite the object's location, as we need it to emit
4411 // the read barrier.
4412 locations->SetOut(
4413 Location::RequiresRegister(),
4414 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4415 Location::kOutputOverlap :
4416 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004417 }
Calin Juravle52c48962014-12-16 17:02:57 +00004418
4419 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4420 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004421 // So we use an XMM register as a temp to achieve atomicity (first
4422 // load the temp into the XMM and then copy the XMM into the
4423 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004424 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004425 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4426 // We need a temporary register for the read barrier marking slow
4427 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4428 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004429 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430}
4431
Calin Juravle52c48962014-12-16 17:02:57 +00004432void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4433 const FieldInfo& field_info) {
4434 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435
Calin Juravle52c48962014-12-16 17:02:57 +00004436 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004437 Location base_loc = locations->InAt(0);
4438 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004439 Location out = locations->Out();
4440 bool is_volatile = field_info.IsVolatile();
4441 Primitive::Type field_type = field_info.GetFieldType();
4442 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4443
4444 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004446 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004447 break;
4448 }
4449
4450 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004451 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004452 break;
4453 }
4454
4455 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004456 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004457 break;
4458 }
4459
4460 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004461 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004462 break;
4463 }
4464
4465 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004466 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004467 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004468
4469 case Primitive::kPrimNot: {
4470 // /* HeapReference<Object> */ out = *(base + offset)
4471 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4472 Location temp_loc = locations->GetTemp(0);
4473 // Note that a potential implicit null check is handled in this
4474 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4475 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4476 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4477 if (is_volatile) {
4478 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4479 }
4480 } else {
4481 __ movl(out.AsRegister<Register>(), Address(base, offset));
4482 codegen_->MaybeRecordImplicitNullCheck(instruction);
4483 if (is_volatile) {
4484 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4485 }
4486 // If read barriers are enabled, emit read barriers other than
4487 // Baker's using a slow path (and also unpoison the loaded
4488 // reference, if heap poisoning is enabled).
4489 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4490 }
4491 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004492 }
4493
4494 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004495 if (is_volatile) {
4496 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4497 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004498 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004499 __ movd(out.AsRegisterPairLow<Register>(), temp);
4500 __ psrlq(temp, Immediate(32));
4501 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4502 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004503 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004504 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004505 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004506 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4507 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004508 break;
4509 }
4510
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004511 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004512 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004513 break;
4514 }
4515
4516 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004517 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004518 break;
4519 }
4520
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004522 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004523 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004524 }
Calin Juravle52c48962014-12-16 17:02:57 +00004525
Roland Levillain7c1559a2015-12-15 10:55:36 +00004526 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4527 // Potential implicit null checks, in the case of reference or
4528 // long fields, are handled in the previous switch statement.
4529 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004530 codegen_->MaybeRecordImplicitNullCheck(instruction);
4531 }
4532
Calin Juravle52c48962014-12-16 17:02:57 +00004533 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004534 if (field_type == Primitive::kPrimNot) {
4535 // Memory barriers, in the case of references, are also handled
4536 // in the previous switch statement.
4537 } else {
4538 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4539 }
Roland Levillain4d027112015-07-01 15:41:14 +01004540 }
Calin Juravle52c48962014-12-16 17:02:57 +00004541}
4542
4543void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4544 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4545
4546 LocationSummary* locations =
4547 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4548 locations->SetInAt(0, Location::RequiresRegister());
4549 bool is_volatile = field_info.IsVolatile();
4550 Primitive::Type field_type = field_info.GetFieldType();
4551 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4552 || (field_type == Primitive::kPrimByte);
4553
4554 // The register allocator does not support multiple
4555 // inputs that die at entry with one in a specific register.
4556 if (is_byte_type) {
4557 // Ensure the value is in a byte register.
4558 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004559 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004560 if (is_volatile && field_type == Primitive::kPrimDouble) {
4561 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4562 locations->SetInAt(1, Location::RequiresFpuRegister());
4563 } else {
4564 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4565 }
4566 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4567 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004568 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004569
Calin Juravle52c48962014-12-16 17:02:57 +00004570 // 64bits value can be atomically written to an address with movsd and an XMM register.
4571 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4572 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4573 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4574 // isolated cases when we need this it isn't worth adding the extra complexity.
4575 locations->AddTemp(Location::RequiresFpuRegister());
4576 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004577 } else {
4578 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4579
4580 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4581 // Temporary registers for the write barrier.
4582 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4583 // Ensure the card is in a byte register.
4584 locations->AddTemp(Location::RegisterLocation(ECX));
4585 }
Calin Juravle52c48962014-12-16 17:02:57 +00004586 }
4587}
4588
4589void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004590 const FieldInfo& field_info,
4591 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004592 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4593
4594 LocationSummary* locations = instruction->GetLocations();
4595 Register base = locations->InAt(0).AsRegister<Register>();
4596 Location value = locations->InAt(1);
4597 bool is_volatile = field_info.IsVolatile();
4598 Primitive::Type field_type = field_info.GetFieldType();
4599 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004600 bool needs_write_barrier =
4601 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004602
4603 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004604 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004605 }
4606
Mark Mendell81489372015-11-04 11:30:41 -05004607 bool maybe_record_implicit_null_check_done = false;
4608
Calin Juravle52c48962014-12-16 17:02:57 +00004609 switch (field_type) {
4610 case Primitive::kPrimBoolean:
4611 case Primitive::kPrimByte: {
4612 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4613 break;
4614 }
4615
4616 case Primitive::kPrimShort:
4617 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004618 if (value.IsConstant()) {
4619 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4620 __ movw(Address(base, offset), Immediate(v));
4621 } else {
4622 __ movw(Address(base, offset), value.AsRegister<Register>());
4623 }
Calin Juravle52c48962014-12-16 17:02:57 +00004624 break;
4625 }
4626
4627 case Primitive::kPrimInt:
4628 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004629 if (kPoisonHeapReferences && needs_write_barrier) {
4630 // Note that in the case where `value` is a null reference,
4631 // we do not enter this block, as the reference does not
4632 // need poisoning.
4633 DCHECK_EQ(field_type, Primitive::kPrimNot);
4634 Register temp = locations->GetTemp(0).AsRegister<Register>();
4635 __ movl(temp, value.AsRegister<Register>());
4636 __ PoisonHeapReference(temp);
4637 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004638 } else if (value.IsConstant()) {
4639 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4640 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004641 } else {
4642 __ movl(Address(base, offset), value.AsRegister<Register>());
4643 }
Calin Juravle52c48962014-12-16 17:02:57 +00004644 break;
4645 }
4646
4647 case Primitive::kPrimLong: {
4648 if (is_volatile) {
4649 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4650 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4651 __ movd(temp1, value.AsRegisterPairLow<Register>());
4652 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4653 __ punpckldq(temp1, temp2);
4654 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004655 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004656 } else if (value.IsConstant()) {
4657 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4658 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4659 codegen_->MaybeRecordImplicitNullCheck(instruction);
4660 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004661 } else {
4662 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004663 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4665 }
Mark Mendell81489372015-11-04 11:30:41 -05004666 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004667 break;
4668 }
4669
4670 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004671 if (value.IsConstant()) {
4672 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4673 __ movl(Address(base, offset), Immediate(v));
4674 } else {
4675 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4676 }
Calin Juravle52c48962014-12-16 17:02:57 +00004677 break;
4678 }
4679
4680 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004681 if (value.IsConstant()) {
4682 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4683 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4684 codegen_->MaybeRecordImplicitNullCheck(instruction);
4685 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4686 maybe_record_implicit_null_check_done = true;
4687 } else {
4688 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4689 }
Calin Juravle52c48962014-12-16 17:02:57 +00004690 break;
4691 }
4692
4693 case Primitive::kPrimVoid:
4694 LOG(FATAL) << "Unreachable type " << field_type;
4695 UNREACHABLE();
4696 }
4697
Mark Mendell81489372015-11-04 11:30:41 -05004698 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004699 codegen_->MaybeRecordImplicitNullCheck(instruction);
4700 }
4701
Roland Levillain4d027112015-07-01 15:41:14 +01004702 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004703 Register temp = locations->GetTemp(0).AsRegister<Register>();
4704 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004705 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004706 }
4707
Calin Juravle52c48962014-12-16 17:02:57 +00004708 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004709 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004710 }
4711}
4712
4713void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4714 HandleFieldGet(instruction, instruction->GetFieldInfo());
4715}
4716
4717void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4718 HandleFieldGet(instruction, instruction->GetFieldInfo());
4719}
4720
4721void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4722 HandleFieldSet(instruction, instruction->GetFieldInfo());
4723}
4724
4725void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004726 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004727}
4728
4729void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4730 HandleFieldSet(instruction, instruction->GetFieldInfo());
4731}
4732
4733void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004734 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004735}
4736
4737void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4738 HandleFieldGet(instruction, instruction->GetFieldInfo());
4739}
4740
4741void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4742 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004743}
4744
Calin Juravlee460d1d2015-09-29 04:52:17 +01004745void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4746 HUnresolvedInstanceFieldGet* instruction) {
4747 FieldAccessCallingConventionX86 calling_convention;
4748 codegen_->CreateUnresolvedFieldLocationSummary(
4749 instruction, instruction->GetFieldType(), calling_convention);
4750}
4751
4752void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4753 HUnresolvedInstanceFieldGet* instruction) {
4754 FieldAccessCallingConventionX86 calling_convention;
4755 codegen_->GenerateUnresolvedFieldAccess(instruction,
4756 instruction->GetFieldType(),
4757 instruction->GetFieldIndex(),
4758 instruction->GetDexPc(),
4759 calling_convention);
4760}
4761
4762void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4763 HUnresolvedInstanceFieldSet* instruction) {
4764 FieldAccessCallingConventionX86 calling_convention;
4765 codegen_->CreateUnresolvedFieldLocationSummary(
4766 instruction, instruction->GetFieldType(), calling_convention);
4767}
4768
4769void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4770 HUnresolvedInstanceFieldSet* instruction) {
4771 FieldAccessCallingConventionX86 calling_convention;
4772 codegen_->GenerateUnresolvedFieldAccess(instruction,
4773 instruction->GetFieldType(),
4774 instruction->GetFieldIndex(),
4775 instruction->GetDexPc(),
4776 calling_convention);
4777}
4778
4779void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4780 HUnresolvedStaticFieldGet* instruction) {
4781 FieldAccessCallingConventionX86 calling_convention;
4782 codegen_->CreateUnresolvedFieldLocationSummary(
4783 instruction, instruction->GetFieldType(), calling_convention);
4784}
4785
4786void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4787 HUnresolvedStaticFieldGet* instruction) {
4788 FieldAccessCallingConventionX86 calling_convention;
4789 codegen_->GenerateUnresolvedFieldAccess(instruction,
4790 instruction->GetFieldType(),
4791 instruction->GetFieldIndex(),
4792 instruction->GetDexPc(),
4793 calling_convention);
4794}
4795
4796void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4797 HUnresolvedStaticFieldSet* instruction) {
4798 FieldAccessCallingConventionX86 calling_convention;
4799 codegen_->CreateUnresolvedFieldLocationSummary(
4800 instruction, instruction->GetFieldType(), calling_convention);
4801}
4802
4803void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4804 HUnresolvedStaticFieldSet* instruction) {
4805 FieldAccessCallingConventionX86 calling_convention;
4806 codegen_->GenerateUnresolvedFieldAccess(instruction,
4807 instruction->GetFieldType(),
4808 instruction->GetFieldIndex(),
4809 instruction->GetDexPc(),
4810 calling_convention);
4811}
4812
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004813void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004814 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4815 ? LocationSummary::kCallOnSlowPath
4816 : LocationSummary::kNoCall;
4817 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4818 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004819 ? Location::RequiresRegister()
4820 : Location::Any();
4821 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004822 if (instruction->HasUses()) {
4823 locations->SetOut(Location::SameAsFirstInput());
4824 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004825}
4826
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004827void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004828 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4829 return;
4830 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004831 LocationSummary* locations = instruction->GetLocations();
4832 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004833
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004834 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4835 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4836}
4837
4838void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004839 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004840 codegen_->AddSlowPath(slow_path);
4841
4842 LocationSummary* locations = instruction->GetLocations();
4843 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004844
4845 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004846 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004847 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004848 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004849 } else {
4850 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004851 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004852 __ jmp(slow_path->GetEntryLabel());
4853 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004854 }
4855 __ j(kEqual, slow_path->GetEntryLabel());
4856}
4857
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004858void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004859 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004860 GenerateImplicitNullCheck(instruction);
4861 } else {
4862 GenerateExplicitNullCheck(instruction);
4863 }
4864}
4865
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004866void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004867 bool object_array_get_with_read_barrier =
4868 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004869 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004870 new (GetGraph()->GetArena()) LocationSummary(instruction,
4871 object_array_get_with_read_barrier ?
4872 LocationSummary::kCallOnSlowPath :
4873 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004874 locations->SetInAt(0, Location::RequiresRegister());
4875 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004876 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4877 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4878 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004879 // The output overlaps in case of long: we don't want the low move
4880 // to overwrite the array's location. Likewise, in the case of an
4881 // object array get with read barriers enabled, we do not want the
4882 // move to overwrite the array's location, as we need it to emit
4883 // the read barrier.
4884 locations->SetOut(
4885 Location::RequiresRegister(),
4886 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4887 Location::kOutputOverlap :
4888 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004889 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004890 // We need a temporary register for the read barrier marking slow
4891 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4892 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4893 locations->AddTemp(Location::RequiresRegister());
4894 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004895}
4896
4897void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4898 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004899 Location obj_loc = locations->InAt(0);
4900 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004901 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004902 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004903
Calin Juravle77520bc2015-01-12 18:45:46 +00004904 Primitive::Type type = instruction->GetType();
4905 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004906 case Primitive::kPrimBoolean: {
4907 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004908 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004909 if (index.IsConstant()) {
4910 __ movzxb(out, Address(obj,
4911 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4912 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004913 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004914 }
4915 break;
4916 }
4917
4918 case Primitive::kPrimByte: {
4919 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004920 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004921 if (index.IsConstant()) {
4922 __ movsxb(out, Address(obj,
4923 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4924 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004925 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004926 }
4927 break;
4928 }
4929
4930 case Primitive::kPrimShort: {
4931 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004932 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004933 if (index.IsConstant()) {
4934 __ movsxw(out, Address(obj,
4935 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4936 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004937 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004938 }
4939 break;
4940 }
4941
4942 case Primitive::kPrimChar: {
4943 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004944 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004945 if (index.IsConstant()) {
4946 __ movzxw(out, Address(obj,
4947 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4948 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004949 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004950 }
4951 break;
4952 }
4953
Roland Levillain7c1559a2015-12-15 10:55:36 +00004954 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004956 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004957 if (index.IsConstant()) {
4958 __ movl(out, Address(obj,
4959 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4960 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004961 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004962 }
4963 break;
4964 }
4965
Roland Levillain7c1559a2015-12-15 10:55:36 +00004966 case Primitive::kPrimNot: {
4967 static_assert(
4968 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4969 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4970 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4971 // /* HeapReference<Object> */ out =
4972 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4973 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4974 Location temp = locations->GetTemp(0);
4975 // Note that a potential implicit null check is handled in this
4976 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4977 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4978 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4979 } else {
4980 Register out = out_loc.AsRegister<Register>();
4981 if (index.IsConstant()) {
4982 uint32_t offset =
4983 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4984 __ movl(out, Address(obj, offset));
4985 codegen_->MaybeRecordImplicitNullCheck(instruction);
4986 // If read barriers are enabled, emit read barriers other than
4987 // Baker's using a slow path (and also unpoison the loaded
4988 // reference, if heap poisoning is enabled).
4989 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4990 } else {
4991 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4992 codegen_->MaybeRecordImplicitNullCheck(instruction);
4993 // If read barriers are enabled, emit read barriers other than
4994 // Baker's using a slow path (and also unpoison the loaded
4995 // reference, if heap poisoning is enabled).
4996 codegen_->MaybeGenerateReadBarrierSlow(
4997 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4998 }
4999 }
5000 break;
5001 }
5002
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005003 case Primitive::kPrimLong: {
5004 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005005 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005006 if (index.IsConstant()) {
5007 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005008 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005009 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005010 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005011 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005012 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005013 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005014 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005015 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005016 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005017 }
5018 break;
5019 }
5020
Mark Mendell7c8d0092015-01-26 11:21:33 -05005021 case Primitive::kPrimFloat: {
5022 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005023 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005024 if (index.IsConstant()) {
5025 __ movss(out, Address(obj,
5026 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5027 } else {
5028 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5029 }
5030 break;
5031 }
5032
5033 case Primitive::kPrimDouble: {
5034 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005035 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005036 if (index.IsConstant()) {
5037 __ movsd(out, Address(obj,
5038 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5039 } else {
5040 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5041 }
5042 break;
5043 }
5044
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005045 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005046 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005047 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005049
Roland Levillain7c1559a2015-12-15 10:55:36 +00005050 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5051 // Potential implicit null checks, in the case of reference or
5052 // long arrays, are handled in the previous switch statement.
5053 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005054 codegen_->MaybeRecordImplicitNullCheck(instruction);
5055 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005056}
5057
5058void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Mark Mendell5f874182015-03-04 15:42:45 -05005059 // This location builder might end up asking to up to four registers, which is
5060 // not currently possible for baseline. The situation in which we need four
5061 // registers cannot be met by baseline though, because it has not run any
5062 // optimization.
5063
Nicolas Geoffray39468442014-09-02 15:17:15 +01005064 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005065
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005066 bool needs_write_barrier =
5067 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005068 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5069 bool object_array_set_with_read_barrier =
5070 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005071
Nicolas Geoffray39468442014-09-02 15:17:15 +01005072 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5073 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005074 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5075 LocationSummary::kCallOnSlowPath :
5076 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005077
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005078 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5079 || (value_type == Primitive::kPrimByte);
5080 // We need the inputs to be different than the output in case of long operation.
5081 // In case of a byte operation, the register allocator does not support multiple
5082 // inputs that die at entry with one in a specific register.
5083 locations->SetInAt(0, Location::RequiresRegister());
5084 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5085 if (is_byte_type) {
5086 // Ensure the value is in a byte register.
5087 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5088 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005089 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005091 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5092 }
5093 if (needs_write_barrier) {
5094 // Temporary registers for the write barrier.
5095 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5096 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005097 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099}
5100
5101void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5102 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005103 Location array_loc = locations->InAt(0);
5104 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005106 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005107 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005108 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5109 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5110 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005111 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005112 bool needs_write_barrier =
5113 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114
5115 switch (value_type) {
5116 case Primitive::kPrimBoolean:
5117 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005118 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5119 Address address = index.IsConstant()
5120 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5121 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5122 if (value.IsRegister()) {
5123 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005124 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005126 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005127 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005128 break;
5129 }
5130
5131 case Primitive::kPrimShort:
5132 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005133 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5134 Address address = index.IsConstant()
5135 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5136 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5137 if (value.IsRegister()) {
5138 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005140 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005141 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 break;
5144 }
5145
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005146 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005147 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5148 Address address = index.IsConstant()
5149 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5150 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005151
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005152 if (!value.IsRegister()) {
5153 // Just setting null.
5154 DCHECK(instruction->InputAt(2)->IsNullConstant());
5155 DCHECK(value.IsConstant()) << value;
5156 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005157 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005158 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005159 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005160 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005162
5163 DCHECK(needs_write_barrier);
5164 Register register_value = value.AsRegister<Register>();
5165 NearLabel done, not_null, do_put;
5166 SlowPathCode* slow_path = nullptr;
5167 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005168 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005169 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5170 codegen_->AddSlowPath(slow_path);
5171 if (instruction->GetValueCanBeNull()) {
5172 __ testl(register_value, register_value);
5173 __ j(kNotEqual, &not_null);
5174 __ movl(address, Immediate(0));
5175 codegen_->MaybeRecordImplicitNullCheck(instruction);
5176 __ jmp(&done);
5177 __ Bind(&not_null);
5178 }
5179
Roland Levillain0d5a2812015-11-13 10:07:31 +00005180 if (kEmitCompilerReadBarrier) {
5181 // When read barriers are enabled, the type checking
5182 // instrumentation requires two read barriers:
5183 //
5184 // __ movl(temp2, temp);
5185 // // /* HeapReference<Class> */ temp = temp->component_type_
5186 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005187 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005188 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5189 //
5190 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5191 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005192 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005193 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5194 //
5195 // __ cmpl(temp, temp2);
5196 //
5197 // However, the second read barrier may trash `temp`, as it
5198 // is a temporary register, and as such would not be saved
5199 // along with live registers before calling the runtime (nor
5200 // restored afterwards). So in this case, we bail out and
5201 // delegate the work to the array set slow path.
5202 //
5203 // TODO: Extend the register allocator to support a new
5204 // "(locally) live temp" location so as to avoid always
5205 // going into the slow path when read barriers are enabled.
5206 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005207 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005208 // /* HeapReference<Class> */ temp = array->klass_
5209 __ movl(temp, Address(array, class_offset));
5210 codegen_->MaybeRecordImplicitNullCheck(instruction);
5211 __ MaybeUnpoisonHeapReference(temp);
5212
5213 // /* HeapReference<Class> */ temp = temp->component_type_
5214 __ movl(temp, Address(temp, component_offset));
5215 // If heap poisoning is enabled, no need to unpoison `temp`
5216 // nor the object reference in `register_value->klass`, as
5217 // we are comparing two poisoned references.
5218 __ cmpl(temp, Address(register_value, class_offset));
5219
5220 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5221 __ j(kEqual, &do_put);
5222 // If heap poisoning is enabled, the `temp` reference has
5223 // not been unpoisoned yet; unpoison it now.
5224 __ MaybeUnpoisonHeapReference(temp);
5225
5226 // /* HeapReference<Class> */ temp = temp->super_class_
5227 __ movl(temp, Address(temp, super_offset));
5228 // If heap poisoning is enabled, no need to unpoison
5229 // `temp`, as we are comparing against null below.
5230 __ testl(temp, temp);
5231 __ j(kNotEqual, slow_path->GetEntryLabel());
5232 __ Bind(&do_put);
5233 } else {
5234 __ j(kNotEqual, slow_path->GetEntryLabel());
5235 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 }
5237 }
5238
5239 if (kPoisonHeapReferences) {
5240 __ movl(temp, register_value);
5241 __ PoisonHeapReference(temp);
5242 __ movl(address, temp);
5243 } else {
5244 __ movl(address, register_value);
5245 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005246 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005247 codegen_->MaybeRecordImplicitNullCheck(instruction);
5248 }
5249
5250 Register card = locations->GetTemp(1).AsRegister<Register>();
5251 codegen_->MarkGCCard(
5252 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5253 __ Bind(&done);
5254
5255 if (slow_path != nullptr) {
5256 __ Bind(slow_path->GetExitLabel());
5257 }
5258
5259 break;
5260 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005261
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005262 case Primitive::kPrimInt: {
5263 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5264 Address address = index.IsConstant()
5265 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5266 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5267 if (value.IsRegister()) {
5268 __ movl(address, value.AsRegister<Register>());
5269 } else {
5270 DCHECK(value.IsConstant()) << value;
5271 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5272 __ movl(address, Immediate(v));
5273 }
5274 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005275 break;
5276 }
5277
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 case Primitive::kPrimLong: {
5279 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005280 if (index.IsConstant()) {
5281 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005282 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005284 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005286 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005287 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005288 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005289 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005290 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005291 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005292 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005293 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005294 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005296 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005297 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005298 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005299 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005300 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005301 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005302 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005304 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005305 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005306 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005307 Immediate(High32Bits(val)));
5308 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 }
5310 break;
5311 }
5312
Mark Mendell7c8d0092015-01-26 11:21:33 -05005313 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5315 Address address = index.IsConstant()
5316 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5317 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005318 if (value.IsFpuRegister()) {
5319 __ movss(address, value.AsFpuRegister<XmmRegister>());
5320 } else {
5321 DCHECK(value.IsConstant());
5322 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5323 __ movl(address, Immediate(v));
5324 }
5325 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005326 break;
5327 }
5328
5329 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005330 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5331 Address address = index.IsConstant()
5332 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5333 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005334 if (value.IsFpuRegister()) {
5335 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5336 } else {
5337 DCHECK(value.IsConstant());
5338 Address address_hi = index.IsConstant() ?
5339 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5340 offset + kX86WordSize) :
5341 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5342 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5343 __ movl(address, Immediate(Low32Bits(v)));
5344 codegen_->MaybeRecordImplicitNullCheck(instruction);
5345 __ movl(address_hi, Immediate(High32Bits(v)));
5346 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005347 break;
5348 }
5349
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005350 case Primitive::kPrimVoid:
5351 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005352 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005353 }
5354}
5355
5356void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005358 locations->SetInAt(0, Location::RequiresRegister());
5359 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005360}
5361
5362void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5363 LocationSummary* locations = instruction->GetLocations();
5364 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005365 Register obj = locations->InAt(0).AsRegister<Register>();
5366 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005367 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005368 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369}
5370
5371void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005372 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5373 ? LocationSummary::kCallOnSlowPath
5374 : LocationSummary::kNoCall;
5375 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005376 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005377 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005378 if (instruction->HasUses()) {
5379 locations->SetOut(Location::SameAsFirstInput());
5380 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005381}
5382
5383void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5384 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005385 Location index_loc = locations->InAt(0);
5386 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005387 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005388 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389
Mark Mendell99dbd682015-04-22 16:18:52 -04005390 if (length_loc.IsConstant()) {
5391 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5392 if (index_loc.IsConstant()) {
5393 // BCE will remove the bounds check if we are guarenteed to pass.
5394 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5395 if (index < 0 || index >= length) {
5396 codegen_->AddSlowPath(slow_path);
5397 __ jmp(slow_path->GetEntryLabel());
5398 } else {
5399 // Some optimization after BCE may have generated this, and we should not
5400 // generate a bounds check if it is a valid range.
5401 }
5402 return;
5403 }
5404
5405 // We have to reverse the jump condition because the length is the constant.
5406 Register index_reg = index_loc.AsRegister<Register>();
5407 __ cmpl(index_reg, Immediate(length));
5408 codegen_->AddSlowPath(slow_path);
5409 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005410 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005411 Register length = length_loc.AsRegister<Register>();
5412 if (index_loc.IsConstant()) {
5413 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5414 __ cmpl(length, Immediate(value));
5415 } else {
5416 __ cmpl(length, index_loc.AsRegister<Register>());
5417 }
5418 codegen_->AddSlowPath(slow_path);
5419 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005420 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421}
5422
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005423void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5424 temp->SetLocations(nullptr);
5425}
5426
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005427void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005428 // Nothing to do, this is driven by the code generator.
5429}
5430
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005431void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005432 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005433}
5434
5435void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005436 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5437}
5438
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005439void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5440 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5441}
5442
5443void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005444 HBasicBlock* block = instruction->GetBlock();
5445 if (block->GetLoopInformation() != nullptr) {
5446 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5447 // The back edge will generate the suspend check.
5448 return;
5449 }
5450 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5451 // The goto will generate the suspend check.
5452 return;
5453 }
5454 GenerateSuspendCheck(instruction, nullptr);
5455}
5456
5457void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5458 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005459 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005460 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5461 if (slow_path == nullptr) {
5462 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5463 instruction->SetSlowPath(slow_path);
5464 codegen_->AddSlowPath(slow_path);
5465 if (successor != nullptr) {
5466 DCHECK(successor->IsLoopHeader());
5467 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5468 }
5469 } else {
5470 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5471 }
5472
Roland Levillain7c1559a2015-12-15 10:55:36 +00005473 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5474 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005475 if (successor == nullptr) {
5476 __ j(kNotEqual, slow_path->GetEntryLabel());
5477 __ Bind(slow_path->GetReturnLabel());
5478 } else {
5479 __ j(kEqual, codegen_->GetLabelOf(successor));
5480 __ jmp(slow_path->GetEntryLabel());
5481 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005482}
5483
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005484X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5485 return codegen_->GetAssembler();
5486}
5487
Mark Mendell7c8d0092015-01-26 11:21:33 -05005488void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005489 ScratchRegisterScope ensure_scratch(
5490 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5491 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5492 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5493 __ movl(temp_reg, Address(ESP, src + stack_offset));
5494 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005495}
5496
5497void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005498 ScratchRegisterScope ensure_scratch(
5499 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5500 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5501 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5502 __ movl(temp_reg, Address(ESP, src + stack_offset));
5503 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5504 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5505 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005506}
5507
5508void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005509 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005510 Location source = move->GetSource();
5511 Location destination = move->GetDestination();
5512
5513 if (source.IsRegister()) {
5514 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005515 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005516 } else {
5517 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005518 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005519 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005520 } else if (source.IsFpuRegister()) {
5521 if (destination.IsFpuRegister()) {
5522 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5523 } else if (destination.IsStackSlot()) {
5524 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5525 } else {
5526 DCHECK(destination.IsDoubleStackSlot());
5527 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5528 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005529 } else if (source.IsStackSlot()) {
5530 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005531 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005532 } else if (destination.IsFpuRegister()) {
5533 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005534 } else {
5535 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005536 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5537 }
5538 } else if (source.IsDoubleStackSlot()) {
5539 if (destination.IsFpuRegister()) {
5540 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5541 } else {
5542 DCHECK(destination.IsDoubleStackSlot()) << destination;
5543 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005544 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005545 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005546 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005547 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005548 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005549 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005550 if (value == 0) {
5551 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5552 } else {
5553 __ movl(destination.AsRegister<Register>(), Immediate(value));
5554 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005555 } else {
5556 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005557 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005558 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005559 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005560 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005561 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005562 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005563 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005564 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5565 if (value == 0) {
5566 // Easy handling of 0.0.
5567 __ xorps(dest, dest);
5568 } else {
5569 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005570 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5571 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5572 __ movl(temp, Immediate(value));
5573 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005574 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005575 } else {
5576 DCHECK(destination.IsStackSlot()) << destination;
5577 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5578 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005579 } else if (constant->IsLongConstant()) {
5580 int64_t value = constant->AsLongConstant()->GetValue();
5581 int32_t low_value = Low32Bits(value);
5582 int32_t high_value = High32Bits(value);
5583 Immediate low(low_value);
5584 Immediate high(high_value);
5585 if (destination.IsDoubleStackSlot()) {
5586 __ movl(Address(ESP, destination.GetStackIndex()), low);
5587 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5588 } else {
5589 __ movl(destination.AsRegisterPairLow<Register>(), low);
5590 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5591 }
5592 } else {
5593 DCHECK(constant->IsDoubleConstant());
5594 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005595 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005596 int32_t low_value = Low32Bits(value);
5597 int32_t high_value = High32Bits(value);
5598 Immediate low(low_value);
5599 Immediate high(high_value);
5600 if (destination.IsFpuRegister()) {
5601 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5602 if (value == 0) {
5603 // Easy handling of 0.0.
5604 __ xorpd(dest, dest);
5605 } else {
5606 __ pushl(high);
5607 __ pushl(low);
5608 __ movsd(dest, Address(ESP, 0));
5609 __ addl(ESP, Immediate(8));
5610 }
5611 } else {
5612 DCHECK(destination.IsDoubleStackSlot()) << destination;
5613 __ movl(Address(ESP, destination.GetStackIndex()), low);
5614 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5615 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005616 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005617 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005618 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005619 }
5620}
5621
Mark Mendella5c19ce2015-04-01 12:51:05 -04005622void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005623 Register suggested_scratch = reg == EAX ? EBX : EAX;
5624 ScratchRegisterScope ensure_scratch(
5625 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5626
5627 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5628 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5629 __ movl(Address(ESP, mem + stack_offset), reg);
5630 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005631}
5632
Mark Mendell7c8d0092015-01-26 11:21:33 -05005633void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005634 ScratchRegisterScope ensure_scratch(
5635 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5636
5637 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5638 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5639 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5640 __ movss(Address(ESP, mem + stack_offset), reg);
5641 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005642}
5643
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005644void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005645 ScratchRegisterScope ensure_scratch1(
5646 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005647
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005648 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5649 ScratchRegisterScope ensure_scratch2(
5650 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005651
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005652 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5653 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5654 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5655 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5656 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5657 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005658}
5659
5660void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005661 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005662 Location source = move->GetSource();
5663 Location destination = move->GetDestination();
5664
5665 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005666 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5667 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5668 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5669 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5670 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005671 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005672 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005673 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005674 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005675 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5676 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005677 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5678 // Use XOR Swap algorithm to avoid a temporary.
5679 DCHECK_NE(source.reg(), destination.reg());
5680 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5681 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5682 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5683 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5684 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5685 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5686 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005687 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5688 // Take advantage of the 16 bytes in the XMM register.
5689 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5690 Address stack(ESP, destination.GetStackIndex());
5691 // Load the double into the high doubleword.
5692 __ movhpd(reg, stack);
5693
5694 // Store the low double into the destination.
5695 __ movsd(stack, reg);
5696
5697 // Move the high double to the low double.
5698 __ psrldq(reg, Immediate(8));
5699 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5700 // Take advantage of the 16 bytes in the XMM register.
5701 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5702 Address stack(ESP, source.GetStackIndex());
5703 // Load the double into the high doubleword.
5704 __ movhpd(reg, stack);
5705
5706 // Store the low double into the destination.
5707 __ movsd(stack, reg);
5708
5709 // Move the high double to the low double.
5710 __ psrldq(reg, Immediate(8));
5711 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5712 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5713 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005714 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005716 }
5717}
5718
5719void ParallelMoveResolverX86::SpillScratch(int reg) {
5720 __ pushl(static_cast<Register>(reg));
5721}
5722
5723void ParallelMoveResolverX86::RestoreScratch(int reg) {
5724 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005725}
5726
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005727void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005728 InvokeRuntimeCallingConvention calling_convention;
5729 CodeGenerator::CreateLoadClassLocationSummary(
5730 cls,
5731 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732 Location::RegisterLocation(EAX),
5733 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005734}
5735
5736void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005737 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005738 if (cls->NeedsAccessCheck()) {
5739 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5740 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5741 cls,
5742 cls->GetDexPc(),
5743 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005744 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005745 return;
5746 }
5747
Roland Levillain0d5a2812015-11-13 10:07:31 +00005748 Location out_loc = locations->Out();
5749 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005750 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005751
Calin Juravle580b6092015-10-06 17:35:58 +01005752 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005753 DCHECK(!cls->CanCallRuntime());
5754 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005755 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5756 GenerateGcRootFieldLoad(
5757 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005758 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005759 // /* GcRoot<mirror::Class>[] */ out =
5760 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5761 __ movl(out, Address(current_method,
5762 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005763 // /* GcRoot<mirror::Class> */ out = out[type_index]
5764 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005765
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005766 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5767 DCHECK(cls->CanCallRuntime());
5768 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5769 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5770 codegen_->AddSlowPath(slow_path);
5771
5772 if (!cls->IsInDexCache()) {
5773 __ testl(out, out);
5774 __ j(kEqual, slow_path->GetEntryLabel());
5775 }
5776
5777 if (cls->MustGenerateClinitCheck()) {
5778 GenerateClassInitializationCheck(slow_path, out);
5779 } else {
5780 __ Bind(slow_path->GetExitLabel());
5781 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005782 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005783 }
5784}
5785
5786void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5787 LocationSummary* locations =
5788 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5789 locations->SetInAt(0, Location::RequiresRegister());
5790 if (check->HasUses()) {
5791 locations->SetOut(Location::SameAsFirstInput());
5792 }
5793}
5794
5795void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005796 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005797 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005798 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005799 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005800 GenerateClassInitializationCheck(slow_path,
5801 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005802}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005803
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005804void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005805 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005806 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5807 Immediate(mirror::Class::kStatusInitialized));
5808 __ j(kLess, slow_path->GetEntryLabel());
5809 __ Bind(slow_path->GetExitLabel());
5810 // No need for memory fence, thanks to the X86 memory model.
5811}
5812
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005813void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005814 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5815 ? LocationSummary::kCallOnSlowPath
5816 : LocationSummary::kNoCall;
5817 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005818 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005819 locations->SetOut(Location::RequiresRegister());
5820}
5821
5822void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005823 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005824 Location out_loc = locations->Out();
5825 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005826 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005827
Roland Levillain7c1559a2015-12-15 10:55:36 +00005828 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5829 GenerateGcRootFieldLoad(
5830 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005831 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005832 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005833 // /* GcRoot<mirror::String> */ out = out[string_index]
5834 GenerateGcRootFieldLoad(
5835 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005836
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005837 if (!load->IsInDexCache()) {
5838 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5839 codegen_->AddSlowPath(slow_path);
5840 __ testl(out, out);
5841 __ j(kEqual, slow_path->GetEntryLabel());
5842 __ Bind(slow_path->GetExitLabel());
5843 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005844}
5845
David Brazdilcb1c0552015-08-04 16:22:25 +01005846static Address GetExceptionTlsAddress() {
5847 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5848}
5849
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005850void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5851 LocationSummary* locations =
5852 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5853 locations->SetOut(Location::RequiresRegister());
5854}
5855
5856void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005857 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5858}
5859
5860void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5861 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5862}
5863
5864void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5865 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005866}
5867
5868void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5869 LocationSummary* locations =
5870 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5871 InvokeRuntimeCallingConvention calling_convention;
5872 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5873}
5874
5875void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005876 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5877 instruction,
5878 instruction->GetDexPc(),
5879 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005880 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005881}
5882
Roland Levillain7c1559a2015-12-15 10:55:36 +00005883static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5884 return kEmitCompilerReadBarrier &&
5885 (kUseBakerReadBarrier ||
5886 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5887 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5888 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5889}
5890
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005891void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005892 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5894 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005895 case TypeCheckKind::kExactCheck:
5896 case TypeCheckKind::kAbstractClassCheck:
5897 case TypeCheckKind::kClassHierarchyCheck:
5898 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005899 call_kind =
5900 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005901 break;
5902 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005903 case TypeCheckKind::kUnresolvedCheck:
5904 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005905 call_kind = LocationSummary::kCallOnSlowPath;
5906 break;
5907 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005909 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005910 locations->SetInAt(0, Location::RequiresRegister());
5911 locations->SetInAt(1, Location::Any());
5912 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5913 locations->SetOut(Location::RequiresRegister());
5914 // When read barriers are enabled, we need a temporary register for
5915 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005916 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005917 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005919}
5920
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005921void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005922 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005923 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005924 Location obj_loc = locations->InAt(0);
5925 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005926 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005927 Location out_loc = locations->Out();
5928 Register out = out_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005929 Location temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
5930 locations->GetTemp(0) :
5931 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005932 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005933 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5934 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5935 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005936 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005937 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005938
5939 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005940 // Avoid null check if we know obj is not null.
5941 if (instruction->MustDoNullCheck()) {
5942 __ testl(obj, obj);
5943 __ j(kEqual, &zero);
5944 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005947 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005948
Roland Levillain7c1559a2015-12-15 10:55:36 +00005949 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005950 case TypeCheckKind::kExactCheck: {
5951 if (cls.IsRegister()) {
5952 __ cmpl(out, cls.AsRegister<Register>());
5953 } else {
5954 DCHECK(cls.IsStackSlot()) << cls;
5955 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5956 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005957
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005958 // Classes must be equal for the instanceof to succeed.
5959 __ j(kNotEqual, &zero);
5960 __ movl(out, Immediate(1));
5961 __ jmp(&done);
5962 break;
5963 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005964
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005965 case TypeCheckKind::kAbstractClassCheck: {
5966 // If the class is abstract, we eagerly fetch the super class of the
5967 // object to avoid doing a comparison we know will fail.
5968 NearLabel loop;
5969 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005970 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00005971 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 __ testl(out, out);
5973 // If `out` is null, we use it for the result, and jump to `done`.
5974 __ j(kEqual, &done);
5975 if (cls.IsRegister()) {
5976 __ cmpl(out, cls.AsRegister<Register>());
5977 } else {
5978 DCHECK(cls.IsStackSlot()) << cls;
5979 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5980 }
5981 __ j(kNotEqual, &loop);
5982 __ movl(out, Immediate(1));
5983 if (zero.IsLinked()) {
5984 __ jmp(&done);
5985 }
5986 break;
5987 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005988
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005989 case TypeCheckKind::kClassHierarchyCheck: {
5990 // Walk over the class hierarchy to find a match.
5991 NearLabel loop, success;
5992 __ Bind(&loop);
5993 if (cls.IsRegister()) {
5994 __ cmpl(out, cls.AsRegister<Register>());
5995 } else {
5996 DCHECK(cls.IsStackSlot()) << cls;
5997 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5998 }
5999 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006000 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006001 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006002 __ testl(out, out);
6003 __ j(kNotEqual, &loop);
6004 // If `out` is null, we use it for the result, and jump to `done`.
6005 __ jmp(&done);
6006 __ Bind(&success);
6007 __ movl(out, Immediate(1));
6008 if (zero.IsLinked()) {
6009 __ jmp(&done);
6010 }
6011 break;
6012 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006014 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006015 // Do an exact check.
6016 NearLabel exact_check;
6017 if (cls.IsRegister()) {
6018 __ cmpl(out, cls.AsRegister<Register>());
6019 } else {
6020 DCHECK(cls.IsStackSlot()) << cls;
6021 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6022 }
6023 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006024 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006025 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006026 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006027 __ testl(out, out);
6028 // If `out` is null, we use it for the result, and jump to `done`.
6029 __ j(kEqual, &done);
6030 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6031 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006032 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006033 __ movl(out, Immediate(1));
6034 __ jmp(&done);
6035 break;
6036 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006037
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006038 case TypeCheckKind::kArrayCheck: {
6039 if (cls.IsRegister()) {
6040 __ cmpl(out, cls.AsRegister<Register>());
6041 } else {
6042 DCHECK(cls.IsStackSlot()) << cls;
6043 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6044 }
6045 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006046 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6047 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006048 codegen_->AddSlowPath(slow_path);
6049 __ j(kNotEqual, slow_path->GetEntryLabel());
6050 __ movl(out, Immediate(1));
6051 if (zero.IsLinked()) {
6052 __ jmp(&done);
6053 }
6054 break;
6055 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006056
Calin Juravle98893e12015-10-02 21:05:03 +01006057 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006058 case TypeCheckKind::kInterfaceCheck: {
6059 // Note that we indeed only call on slow path, but we always go
6060 // into the slow path for the unresolved & interface check
6061 // cases.
6062 //
6063 // We cannot directly call the InstanceofNonTrivial runtime
6064 // entry point without resorting to a type checking slow path
6065 // here (i.e. by calling InvokeRuntime directly), as it would
6066 // require to assign fixed registers for the inputs of this
6067 // HInstanceOf instruction (following the runtime calling
6068 // convention), which might be cluttered by the potential first
6069 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006070 //
6071 // TODO: Introduce a new runtime entry point taking the object
6072 // to test (instead of its class) as argument, and let it deal
6073 // with the read barrier issues. This will let us refactor this
6074 // case of the `switch` code as it was previously (with a direct
6075 // call to the runtime not using a type checking slow path).
6076 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077 DCHECK(locations->OnlyCallsOnSlowPath());
6078 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6079 /* is_fatal */ false);
6080 codegen_->AddSlowPath(slow_path);
6081 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006082 if (zero.IsLinked()) {
6083 __ jmp(&done);
6084 }
6085 break;
6086 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006087 }
6088
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006089 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006090 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006091 __ xorl(out, out);
6092 }
6093
6094 if (done.IsLinked()) {
6095 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006096 }
6097
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006098 if (slow_path != nullptr) {
6099 __ Bind(slow_path->GetExitLabel());
6100 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006101}
6102
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006103void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6105 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006106 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6107 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006108 case TypeCheckKind::kExactCheck:
6109 case TypeCheckKind::kAbstractClassCheck:
6110 case TypeCheckKind::kClassHierarchyCheck:
6111 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006112 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6113 LocationSummary::kCallOnSlowPath :
6114 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006115 break;
6116 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006117 case TypeCheckKind::kUnresolvedCheck:
6118 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006119 call_kind = LocationSummary::kCallOnSlowPath;
6120 break;
6121 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006122 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6123 locations->SetInAt(0, Location::RequiresRegister());
6124 locations->SetInAt(1, Location::Any());
6125 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6126 locations->AddTemp(Location::RequiresRegister());
6127 // When read barriers are enabled, we need an additional temporary
6128 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006129 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006131 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006132}
6133
6134void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006135 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006136 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 Location obj_loc = locations->InAt(0);
6138 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006139 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140 Location temp_loc = locations->GetTemp(0);
6141 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00006142 Location temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
6143 locations->GetTemp(1) :
6144 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6146 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6147 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6148 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006149
Roland Levillain0d5a2812015-11-13 10:07:31 +00006150 bool is_type_check_slow_path_fatal =
6151 (type_check_kind == TypeCheckKind::kExactCheck ||
6152 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6153 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6154 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6155 !instruction->CanThrowIntoCatchBlock();
6156 SlowPathCode* type_check_slow_path =
6157 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6158 is_type_check_slow_path_fatal);
6159 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006160
Roland Levillain0d5a2812015-11-13 10:07:31 +00006161 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006162 // Avoid null check if we know obj is not null.
6163 if (instruction->MustDoNullCheck()) {
6164 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006165 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006166 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006169 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006170
Roland Levillain0d5a2812015-11-13 10:07:31 +00006171 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006172 case TypeCheckKind::kExactCheck:
6173 case TypeCheckKind::kArrayCheck: {
6174 if (cls.IsRegister()) {
6175 __ cmpl(temp, cls.AsRegister<Register>());
6176 } else {
6177 DCHECK(cls.IsStackSlot()) << cls;
6178 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6179 }
6180 // Jump to slow path for throwing the exception or doing a
6181 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006182 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 break;
6184 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006185
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006186 case TypeCheckKind::kAbstractClassCheck: {
6187 // If the class is abstract, we eagerly fetch the super class of the
6188 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006189 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006190 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006191 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006192 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006193
6194 // If the class reference currently in `temp` is not null, jump
6195 // to the `compare_classes` label to compare it with the checked
6196 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006197 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006198 __ j(kNotEqual, &compare_classes);
6199 // Otherwise, jump to the slow path to throw the exception.
6200 //
6201 // But before, move back the object's class into `temp` before
6202 // going into the slow path, as it has been overwritten in the
6203 // meantime.
6204 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006205 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006206 __ jmp(type_check_slow_path->GetEntryLabel());
6207
6208 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209 if (cls.IsRegister()) {
6210 __ cmpl(temp, cls.AsRegister<Register>());
6211 } else {
6212 DCHECK(cls.IsStackSlot()) << cls;
6213 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6214 }
6215 __ j(kNotEqual, &loop);
6216 break;
6217 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006218
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006219 case TypeCheckKind::kClassHierarchyCheck: {
6220 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006221 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006222 __ Bind(&loop);
6223 if (cls.IsRegister()) {
6224 __ cmpl(temp, cls.AsRegister<Register>());
6225 } else {
6226 DCHECK(cls.IsStackSlot()) << cls;
6227 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6228 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006229 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006230
Roland Levillain0d5a2812015-11-13 10:07:31 +00006231 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006232 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006233
6234 // If the class reference currently in `temp` is not null, jump
6235 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 __ testl(temp, temp);
6237 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006238 // Otherwise, jump to the slow path to throw the exception.
6239 //
6240 // But before, move back the object's class into `temp` before
6241 // going into the slow path, as it has been overwritten in the
6242 // meantime.
6243 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006244 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006245 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006246 break;
6247 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006248
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006249 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006250 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006251 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006252 if (cls.IsRegister()) {
6253 __ cmpl(temp, cls.AsRegister<Register>());
6254 } else {
6255 DCHECK(cls.IsStackSlot()) << cls;
6256 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6257 }
6258 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259
6260 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006262 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006263
6264 // If the component type is not null (i.e. the object is indeed
6265 // an array), jump to label `check_non_primitive_component_type`
6266 // to further check that this component type is not a primitive
6267 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006268 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269 __ j(kNotEqual, &check_non_primitive_component_type);
6270 // Otherwise, jump to the slow path to throw the exception.
6271 //
6272 // But before, move back the object's class into `temp` before
6273 // going into the slow path, as it has been overwritten in the
6274 // meantime.
6275 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006276 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006277 __ jmp(type_check_slow_path->GetEntryLabel());
6278
6279 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006280 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006281 __ j(kEqual, &done);
6282 // Same comment as above regarding `temp` and the slow path.
6283 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain7c1559a2015-12-15 10:55:36 +00006284 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006286 break;
6287 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006288
Calin Juravle98893e12015-10-02 21:05:03 +01006289 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006290 case TypeCheckKind::kInterfaceCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006291 // We always go into the type check slow path for the unresolved &
6292 // interface check cases.
6293 //
6294 // We cannot directly call the CheckCast runtime entry point
6295 // without resorting to a type checking slow path here (i.e. by
6296 // calling InvokeRuntime directly), as it would require to
6297 // assign fixed registers for the inputs of this HInstanceOf
6298 // instruction (following the runtime calling convention), which
6299 // might be cluttered by the potential first read barrier
6300 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006301 //
6302 // TODO: Introduce a new runtime entry point taking the object
6303 // to test (instead of its class) as argument, and let it deal
6304 // with the read barrier issues. This will let us refactor this
6305 // case of the `switch` code as it was previously (with a direct
6306 // call to the runtime not using a type checking slow path).
6307 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006308 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006309 break;
6310 }
6311 __ Bind(&done);
6312
Roland Levillain0d5a2812015-11-13 10:07:31 +00006313 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006314}
6315
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006316void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6317 LocationSummary* locations =
6318 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6319 InvokeRuntimeCallingConvention calling_convention;
6320 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6321}
6322
6323void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006324 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6325 : QUICK_ENTRY_POINT(pUnlockObject),
6326 instruction,
6327 instruction->GetDexPc(),
6328 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006329 if (instruction->IsEnter()) {
6330 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6331 } else {
6332 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6333 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006334}
6335
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006336void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6337void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6338void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6339
6340void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6341 LocationSummary* locations =
6342 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6343 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6344 || instruction->GetResultType() == Primitive::kPrimLong);
6345 locations->SetInAt(0, Location::RequiresRegister());
6346 locations->SetInAt(1, Location::Any());
6347 locations->SetOut(Location::SameAsFirstInput());
6348}
6349
6350void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6351 HandleBitwiseOperation(instruction);
6352}
6353
6354void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6355 HandleBitwiseOperation(instruction);
6356}
6357
6358void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6359 HandleBitwiseOperation(instruction);
6360}
6361
6362void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6363 LocationSummary* locations = instruction->GetLocations();
6364 Location first = locations->InAt(0);
6365 Location second = locations->InAt(1);
6366 DCHECK(first.Equals(locations->Out()));
6367
6368 if (instruction->GetResultType() == Primitive::kPrimInt) {
6369 if (second.IsRegister()) {
6370 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006371 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006372 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006373 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006374 } else {
6375 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006376 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006377 }
6378 } else if (second.IsConstant()) {
6379 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006380 __ andl(first.AsRegister<Register>(),
6381 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006382 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006383 __ orl(first.AsRegister<Register>(),
6384 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006385 } else {
6386 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006387 __ xorl(first.AsRegister<Register>(),
6388 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006389 }
6390 } else {
6391 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006392 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006393 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006394 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006395 } else {
6396 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006397 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006398 }
6399 }
6400 } else {
6401 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6402 if (second.IsRegisterPair()) {
6403 if (instruction->IsAnd()) {
6404 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6405 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6406 } else if (instruction->IsOr()) {
6407 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6408 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6409 } else {
6410 DCHECK(instruction->IsXor());
6411 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6412 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6413 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006414 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006415 if (instruction->IsAnd()) {
6416 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6417 __ andl(first.AsRegisterPairHigh<Register>(),
6418 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6419 } else if (instruction->IsOr()) {
6420 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6421 __ orl(first.AsRegisterPairHigh<Register>(),
6422 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6423 } else {
6424 DCHECK(instruction->IsXor());
6425 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6426 __ xorl(first.AsRegisterPairHigh<Register>(),
6427 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6428 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006429 } else {
6430 DCHECK(second.IsConstant()) << second;
6431 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006432 int32_t low_value = Low32Bits(value);
6433 int32_t high_value = High32Bits(value);
6434 Immediate low(low_value);
6435 Immediate high(high_value);
6436 Register first_low = first.AsRegisterPairLow<Register>();
6437 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006438 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006439 if (low_value == 0) {
6440 __ xorl(first_low, first_low);
6441 } else if (low_value != -1) {
6442 __ andl(first_low, low);
6443 }
6444 if (high_value == 0) {
6445 __ xorl(first_high, first_high);
6446 } else if (high_value != -1) {
6447 __ andl(first_high, high);
6448 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006449 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006450 if (low_value != 0) {
6451 __ orl(first_low, low);
6452 }
6453 if (high_value != 0) {
6454 __ orl(first_high, high);
6455 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006456 } else {
6457 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006458 if (low_value != 0) {
6459 __ xorl(first_low, low);
6460 }
6461 if (high_value != 0) {
6462 __ xorl(first_high, high);
6463 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006464 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006465 }
6466 }
6467}
6468
Roland Levillain7c1559a2015-12-15 10:55:36 +00006469void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6470 Location out,
6471 uint32_t offset,
6472 Location temp) {
6473 Register out_reg = out.AsRegister<Register>();
6474 if (kEmitCompilerReadBarrier) {
6475 if (kUseBakerReadBarrier) {
6476 // Load with fast path based Baker's read barrier.
6477 // /* HeapReference<Object> */ out = *(out + offset)
6478 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6479 instruction, out, out_reg, offset, temp, /* needs_null_check */ false);
6480 } else {
6481 // Load with slow path based read barrier.
6482 // Save the value of `out` into `temp` before overwriting it
6483 // in the following move operation, as we will need it for the
6484 // read barrier below.
6485 __ movl(temp.AsRegister<Register>(), out_reg);
6486 // /* HeapReference<Object> */ out = *(out + offset)
6487 __ movl(out_reg, Address(out_reg, offset));
6488 codegen_->GenerateReadBarrierSlow(instruction, out, out, temp, offset);
6489 }
6490 } else {
6491 // Plain load with no read barrier.
6492 // /* HeapReference<Object> */ out = *(out + offset)
6493 __ movl(out_reg, Address(out_reg, offset));
6494 __ MaybeUnpoisonHeapReference(out_reg);
6495 }
6496}
6497
6498void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6499 Location out,
6500 Location obj,
6501 uint32_t offset,
6502 Location temp) {
6503 Register out_reg = out.AsRegister<Register>();
6504 Register obj_reg = obj.AsRegister<Register>();
6505 if (kEmitCompilerReadBarrier) {
6506 if (kUseBakerReadBarrier) {
6507 // Load with fast path based Baker's read barrier.
6508 // /* HeapReference<Object> */ out = *(obj + offset)
6509 codegen_->GenerateFieldLoadWithBakerReadBarrier(
6510 instruction, out, obj_reg, offset, temp, /* needs_null_check */ false);
6511 } else {
6512 // Load with slow path based read barrier.
6513 // /* HeapReference<Object> */ out = *(obj + offset)
6514 __ movl(out_reg, Address(obj_reg, offset));
6515 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6516 }
6517 } else {
6518 // Plain load with no read barrier.
6519 // /* HeapReference<Object> */ out = *(obj + offset)
6520 __ movl(out_reg, Address(obj_reg, offset));
6521 __ MaybeUnpoisonHeapReference(out_reg);
6522 }
6523}
6524
6525void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6526 Location root,
6527 Register obj,
6528 uint32_t offset) {
6529 Register root_reg = root.AsRegister<Register>();
6530 if (kEmitCompilerReadBarrier) {
6531 if (kUseBakerReadBarrier) {
6532 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6533 // Baker's read barrier are used:
6534 //
6535 // root = obj.field;
6536 // if (Thread::Current()->GetIsGcMarking()) {
6537 // root = ReadBarrier::Mark(root)
6538 // }
6539
6540 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6541 __ movl(root_reg, Address(obj, offset));
6542 static_assert(
6543 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6544 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6545 "have different sizes.");
6546 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6547 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6548 "have different sizes.");
6549
6550 // Slow path used to mark the GC root `root`.
6551 SlowPathCode* slow_path =
6552 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6553 codegen_->AddSlowPath(slow_path);
6554
6555 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6556 Immediate(0));
6557 __ j(kNotEqual, slow_path->GetEntryLabel());
6558 __ Bind(slow_path->GetExitLabel());
6559 } else {
6560 // GC root loaded through a slow path for read barriers other
6561 // than Baker's.
6562 // /* GcRoot<mirror::Object>* */ root = obj + offset
6563 __ leal(root_reg, Address(obj, offset));
6564 // /* mirror::Object* */ root = root->Read()
6565 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6566 }
6567 } else {
6568 // Plain GC root load with no read barrier.
6569 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6570 __ movl(root_reg, Address(obj, offset));
6571 }
6572}
6573
6574void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6575 Location ref,
6576 Register obj,
6577 uint32_t offset,
6578 Location temp,
6579 bool needs_null_check) {
6580 DCHECK(kEmitCompilerReadBarrier);
6581 DCHECK(kUseBakerReadBarrier);
6582
6583 // /* HeapReference<Object> */ ref = *(obj + offset)
6584 Address src(obj, offset);
6585 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6586}
6587
6588void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6589 Location ref,
6590 Register obj,
6591 uint32_t data_offset,
6592 Location index,
6593 Location temp,
6594 bool needs_null_check) {
6595 DCHECK(kEmitCompilerReadBarrier);
6596 DCHECK(kUseBakerReadBarrier);
6597
6598 // /* HeapReference<Object> */ ref =
6599 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6600 Address src = index.IsConstant() ?
6601 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6602 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6603 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6604}
6605
6606void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6607 Location ref,
6608 Register obj,
6609 const Address& src,
6610 Location temp,
6611 bool needs_null_check) {
6612 DCHECK(kEmitCompilerReadBarrier);
6613 DCHECK(kUseBakerReadBarrier);
6614
6615 // In slow path based read barriers, the read barrier call is
6616 // inserted after the original load. However, in fast path based
6617 // Baker's read barriers, we need to perform the load of
6618 // mirror::Object::monitor_ *before* the original reference load.
6619 // This load-load ordering is required by the read barrier.
6620 // The fast path/slow path (for Baker's algorithm) should look like:
6621 //
6622 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6623 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6624 // HeapReference<Object> ref = *src; // Original reference load.
6625 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6626 // if (is_gray) {
6627 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6628 // }
6629 //
6630 // Note: the original implementation in ReadBarrier::Barrier is
6631 // slightly more complex as:
6632 // - it implements the load-load fence using a data dependency on
6633 // the high-bits of rb_state, which are expected to be all zeroes;
6634 // - it performs additional checks that we do not do here for
6635 // performance reasons.
6636
6637 Register ref_reg = ref.AsRegister<Register>();
6638 Register temp_reg = temp.AsRegister<Register>();
6639 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6640
6641 // /* int32_t */ monitor = obj->monitor_
6642 __ movl(temp_reg, Address(obj, monitor_offset));
6643 if (needs_null_check) {
6644 MaybeRecordImplicitNullCheck(instruction);
6645 }
6646 // /* LockWord */ lock_word = LockWord(monitor)
6647 static_assert(sizeof(LockWord) == sizeof(int32_t),
6648 "art::LockWord and int32_t have different sizes.");
6649 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6650 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6651 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6652 static_assert(
6653 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6654 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6655
6656 // Load fence to prevent load-load reordering.
6657 // Note that this is a no-op, thanks to the x86 memory model.
6658 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6659
6660 // The actual reference load.
6661 // /* HeapReference<Object> */ ref = *src
6662 __ movl(ref_reg, src);
6663
6664 // Object* ref = ref_addr->AsMirrorPtr()
6665 __ MaybeUnpoisonHeapReference(ref_reg);
6666
6667 // Slow path used to mark the object `ref` when it is gray.
6668 SlowPathCode* slow_path =
6669 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6670 AddSlowPath(slow_path);
6671
6672 // if (rb_state == ReadBarrier::gray_ptr_)
6673 // ref = ReadBarrier::Mark(ref);
6674 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6675 __ j(kEqual, slow_path->GetEntryLabel());
6676 __ Bind(slow_path->GetExitLabel());
6677}
6678
6679void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6680 Location out,
6681 Location ref,
6682 Location obj,
6683 uint32_t offset,
6684 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 DCHECK(kEmitCompilerReadBarrier);
6686
Roland Levillain7c1559a2015-12-15 10:55:36 +00006687 // Insert a slow path based read barrier *after* the reference load.
6688 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006689 // If heap poisoning is enabled, the unpoisoning of the loaded
6690 // reference will be carried out by the runtime within the slow
6691 // path.
6692 //
6693 // Note that `ref` currently does not get unpoisoned (when heap
6694 // poisoning is enabled), which is alright as the `ref` argument is
6695 // not used by the artReadBarrierSlow entry point.
6696 //
6697 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6698 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6699 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6700 AddSlowPath(slow_path);
6701
Roland Levillain0d5a2812015-11-13 10:07:31 +00006702 __ jmp(slow_path->GetEntryLabel());
6703 __ Bind(slow_path->GetExitLabel());
6704}
6705
Roland Levillain7c1559a2015-12-15 10:55:36 +00006706void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6707 Location out,
6708 Location ref,
6709 Location obj,
6710 uint32_t offset,
6711 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006712 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006713 // Baker's read barriers shall be handled by the fast path
6714 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6715 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006716 // If heap poisoning is enabled, unpoisoning will be taken care of
6717 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006718 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006719 } else if (kPoisonHeapReferences) {
6720 __ UnpoisonHeapReference(out.AsRegister<Register>());
6721 }
6722}
6723
Roland Levillain7c1559a2015-12-15 10:55:36 +00006724void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6725 Location out,
6726 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006727 DCHECK(kEmitCompilerReadBarrier);
6728
Roland Levillain7c1559a2015-12-15 10:55:36 +00006729 // Insert a slow path based read barrier *after* the GC root load.
6730 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006731 // Note that GC roots are not affected by heap poisoning, so we do
6732 // not need to do anything special for this here.
6733 SlowPathCode* slow_path =
6734 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6735 AddSlowPath(slow_path);
6736
Roland Levillain0d5a2812015-11-13 10:07:31 +00006737 __ jmp(slow_path->GetEntryLabel());
6738 __ Bind(slow_path->GetExitLabel());
6739}
6740
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006741void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006742 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006743 LOG(FATAL) << "Unreachable";
6744}
6745
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006746void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006747 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006748 LOG(FATAL) << "Unreachable";
6749}
6750
Nicolas Geoffray2e7cd752015-07-10 11:38:52 +01006751void LocationsBuilderX86::VisitFakeString(HFakeString* instruction) {
6752 DCHECK(codegen_->IsBaseline());
6753 LocationSummary* locations =
6754 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6755 locations->SetOut(Location::ConstantLocation(GetGraph()->GetNullConstant()));
6756}
6757
6758void InstructionCodeGeneratorX86::VisitFakeString(HFakeString* instruction ATTRIBUTE_UNUSED) {
6759 DCHECK(codegen_->IsBaseline());
6760 // Will be generated at use site.
6761}
6762
Mark Mendellfe57faa2015-09-18 09:26:15 -04006763// Simple implementation of packed switch - generate cascaded compare/jumps.
6764void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6765 LocationSummary* locations =
6766 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6767 locations->SetInAt(0, Location::RequiresRegister());
6768}
6769
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006770void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6771 int32_t lower_bound,
6772 uint32_t num_entries,
6773 HBasicBlock* switch_block,
6774 HBasicBlock* default_block) {
6775 // Figure out the correct compare values and jump conditions.
6776 // Handle the first compare/branch as a special case because it might
6777 // jump to the default case.
6778 DCHECK_GT(num_entries, 2u);
6779 Condition first_condition;
6780 uint32_t index;
6781 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6782 if (lower_bound != 0) {
6783 first_condition = kLess;
6784 __ cmpl(value_reg, Immediate(lower_bound));
6785 __ j(first_condition, codegen_->GetLabelOf(default_block));
6786 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006787
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006788 index = 1;
6789 } else {
6790 // Handle all the compare/jumps below.
6791 first_condition = kBelow;
6792 index = 0;
6793 }
6794
6795 // Handle the rest of the compare/jumps.
6796 for (; index + 1 < num_entries; index += 2) {
6797 int32_t compare_to_value = lower_bound + index + 1;
6798 __ cmpl(value_reg, Immediate(compare_to_value));
6799 // Jump to successors[index] if value < case_value[index].
6800 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6801 // Jump to successors[index + 1] if value == case_value[index + 1].
6802 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6803 }
6804
6805 if (index != num_entries) {
6806 // There are an odd number of entries. Handle the last one.
6807 DCHECK_EQ(index + 1, num_entries);
6808 __ cmpl(value_reg, Immediate(lower_bound + index));
6809 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006810 }
6811
6812 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006813 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6814 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006815 }
6816}
6817
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006818void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6819 int32_t lower_bound = switch_instr->GetStartValue();
6820 uint32_t num_entries = switch_instr->GetNumEntries();
6821 LocationSummary* locations = switch_instr->GetLocations();
6822 Register value_reg = locations->InAt(0).AsRegister<Register>();
6823
6824 GenPackedSwitchWithCompares(value_reg,
6825 lower_bound,
6826 num_entries,
6827 switch_instr->GetBlock(),
6828 switch_instr->GetDefaultBlock());
6829}
6830
Mark Mendell805b3b52015-09-18 14:10:29 -04006831void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6832 LocationSummary* locations =
6833 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6834 locations->SetInAt(0, Location::RequiresRegister());
6835
6836 // Constant area pointer.
6837 locations->SetInAt(1, Location::RequiresRegister());
6838
6839 // And the temporary we need.
6840 locations->AddTemp(Location::RequiresRegister());
6841}
6842
6843void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6844 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006845 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006846 LocationSummary* locations = switch_instr->GetLocations();
6847 Register value_reg = locations->InAt(0).AsRegister<Register>();
6848 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6849
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006850 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6851 GenPackedSwitchWithCompares(value_reg,
6852 lower_bound,
6853 num_entries,
6854 switch_instr->GetBlock(),
6855 default_block);
6856 return;
6857 }
6858
Mark Mendell805b3b52015-09-18 14:10:29 -04006859 // Optimizing has a jump area.
6860 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6861 Register constant_area = locations->InAt(1).AsRegister<Register>();
6862
6863 // Remove the bias, if needed.
6864 if (lower_bound != 0) {
6865 __ leal(temp_reg, Address(value_reg, -lower_bound));
6866 value_reg = temp_reg;
6867 }
6868
6869 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006870 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006871 __ cmpl(value_reg, Immediate(num_entries - 1));
6872 __ j(kAbove, codegen_->GetLabelOf(default_block));
6873
6874 // We are in the range of the table.
6875 // Load (target-constant_area) from the jump table, indexing by the value.
6876 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6877
6878 // Compute the actual target address by adding in constant_area.
6879 __ addl(temp_reg, constant_area);
6880
6881 // And jump.
6882 __ jmp(temp_reg);
6883}
6884
Mark Mendell0616ae02015-04-17 12:49:27 -04006885void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6886 HX86ComputeBaseMethodAddress* insn) {
6887 LocationSummary* locations =
6888 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6889 locations->SetOut(Location::RequiresRegister());
6890}
6891
6892void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6893 HX86ComputeBaseMethodAddress* insn) {
6894 LocationSummary* locations = insn->GetLocations();
6895 Register reg = locations->Out().AsRegister<Register>();
6896
6897 // Generate call to next instruction.
6898 Label next_instruction;
6899 __ call(&next_instruction);
6900 __ Bind(&next_instruction);
6901
6902 // Remember this offset for later use with constant area.
6903 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6904
6905 // Grab the return address off the stack.
6906 __ popl(reg);
6907}
6908
6909void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6910 HX86LoadFromConstantTable* insn) {
6911 LocationSummary* locations =
6912 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6913
6914 locations->SetInAt(0, Location::RequiresRegister());
6915 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6916
6917 // If we don't need to be materialized, we only need the inputs to be set.
6918 if (!insn->NeedsMaterialization()) {
6919 return;
6920 }
6921
6922 switch (insn->GetType()) {
6923 case Primitive::kPrimFloat:
6924 case Primitive::kPrimDouble:
6925 locations->SetOut(Location::RequiresFpuRegister());
6926 break;
6927
6928 case Primitive::kPrimInt:
6929 locations->SetOut(Location::RequiresRegister());
6930 break;
6931
6932 default:
6933 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6934 }
6935}
6936
6937void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6938 if (!insn->NeedsMaterialization()) {
6939 return;
6940 }
6941
6942 LocationSummary* locations = insn->GetLocations();
6943 Location out = locations->Out();
6944 Register const_area = locations->InAt(0).AsRegister<Register>();
6945 HConstant *value = insn->GetConstant();
6946
6947 switch (insn->GetType()) {
6948 case Primitive::kPrimFloat:
6949 __ movss(out.AsFpuRegister<XmmRegister>(),
6950 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6951 break;
6952
6953 case Primitive::kPrimDouble:
6954 __ movsd(out.AsFpuRegister<XmmRegister>(),
6955 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6956 break;
6957
6958 case Primitive::kPrimInt:
6959 __ movl(out.AsRegister<Register>(),
6960 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6961 break;
6962
6963 default:
6964 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6965 }
6966}
6967
Mark Mendell0616ae02015-04-17 12:49:27 -04006968/**
6969 * Class to handle late fixup of offsets into constant area.
6970 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006971class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006972 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006973 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6974 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6975
6976 protected:
6977 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6978
6979 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006980
6981 private:
6982 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6983 // Patch the correct offset for the instruction. The place to patch is the
6984 // last 4 bytes of the instruction.
6985 // The value to patch is the distance from the offset in the constant area
6986 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006987 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6988 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006989
6990 // Patch in the right value.
6991 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6992 }
6993
Mark Mendell0616ae02015-04-17 12:49:27 -04006994 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006995 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006996};
6997
Mark Mendell805b3b52015-09-18 14:10:29 -04006998/**
6999 * Class to handle late fixup of offsets to a jump table that will be created in the
7000 * constant area.
7001 */
7002class JumpTableRIPFixup : public RIPFixup {
7003 public:
7004 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7005 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7006
7007 void CreateJumpTable() {
7008 X86Assembler* assembler = codegen_->GetAssembler();
7009
7010 // Ensure that the reference to the jump table has the correct offset.
7011 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7012 SetOffset(offset_in_constant_table);
7013
7014 // The label values in the jump table are computed relative to the
7015 // instruction addressing the constant area.
7016 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7017
7018 // Populate the jump table with the correct values for the jump table.
7019 int32_t num_entries = switch_instr_->GetNumEntries();
7020 HBasicBlock* block = switch_instr_->GetBlock();
7021 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7022 // The value that we want is the target offset - the position of the table.
7023 for (int32_t i = 0; i < num_entries; i++) {
7024 HBasicBlock* b = successors[i];
7025 Label* l = codegen_->GetLabelOf(b);
7026 DCHECK(l->IsBound());
7027 int32_t offset_to_block = l->Position() - relative_offset;
7028 assembler->AppendInt32(offset_to_block);
7029 }
7030 }
7031
7032 private:
7033 const HX86PackedSwitch* switch_instr_;
7034};
7035
7036void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7037 // Generate the constant area if needed.
7038 X86Assembler* assembler = GetAssembler();
7039 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7040 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7041 // byte values.
7042 assembler->Align(4, 0);
7043 constant_area_start_ = assembler->CodeSize();
7044
7045 // Populate any jump tables.
7046 for (auto jump_table : fixups_to_jump_tables_) {
7047 jump_table->CreateJumpTable();
7048 }
7049
7050 // And now add the constant area to the generated code.
7051 assembler->AddConstantArea();
7052 }
7053
7054 // And finish up.
7055 CodeGenerator::Finalize(allocator);
7056}
7057
Mark Mendell0616ae02015-04-17 12:49:27 -04007058Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7059 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7060 return Address(reg, kDummy32BitOffset, fixup);
7061}
7062
7063Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7064 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7065 return Address(reg, kDummy32BitOffset, fixup);
7066}
7067
7068Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7069 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7070 return Address(reg, kDummy32BitOffset, fixup);
7071}
7072
7073Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7074 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7075 return Address(reg, kDummy32BitOffset, fixup);
7076}
7077
Mark Mendell805b3b52015-09-18 14:10:29 -04007078Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7079 Register reg,
7080 Register value) {
7081 // Create a fixup to be used to create and address the jump table.
7082 JumpTableRIPFixup* table_fixup =
7083 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7084
7085 // We have to populate the jump tables.
7086 fixups_to_jump_tables_.push_back(table_fixup);
7087
7088 // We want a scaled address, as we are extracting the correct offset from the table.
7089 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7090}
7091
Andreas Gampe85b62f22015-09-09 13:15:38 -07007092// TODO: target as memory.
7093void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7094 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007096 return;
7097 }
7098
7099 DCHECK_NE(type, Primitive::kPrimVoid);
7100
7101 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7102 if (target.Equals(return_loc)) {
7103 return;
7104 }
7105
7106 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7107 // with the else branch.
7108 if (type == Primitive::kPrimLong) {
7109 HParallelMove parallel_move(GetGraph()->GetArena());
7110 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7111 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7112 GetMoveResolver()->EmitNativeCode(&parallel_move);
7113 } else {
7114 // Let the parallel move resolver take care of all of this.
7115 HParallelMove parallel_move(GetGraph()->GetArena());
7116 parallel_move.AddMove(return_loc, target, type, nullptr);
7117 GetMoveResolver()->EmitNativeCode(&parallel_move);
7118 }
7119}
7120
Roland Levillain4d027112015-07-01 15:41:14 +01007121#undef __
7122
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007123} // namespace x86
7124} // namespace art