blob: 371369033596e9448afe2e5737cc0c4592e952ea [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:
Aart Bik42249c32016-01-07 15:33:50 -0800368 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 : instruction_(instruction) {}
370
371 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100372 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700373 __ Bind(GetEntryLabel());
374 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100375 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
376 instruction_,
377 instruction_->GetDexPc(),
378 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000379 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700380 }
381
Alexandre Rames9931f312015-06-19 14:47:01 +0100382 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
383
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700384 private:
Aart Bik42249c32016-01-07 15:33:50 -0800385 HDeoptimize* const instruction_;
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700386 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
387};
388
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389class ArraySetSlowPathX86 : public SlowPathCode {
390 public:
391 explicit ArraySetSlowPathX86(HInstruction* instruction) : instruction_(instruction) {}
392
393 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
394 LocationSummary* locations = instruction_->GetLocations();
395 __ Bind(GetEntryLabel());
396 SaveLiveRegisters(codegen, locations);
397
398 InvokeRuntimeCallingConvention calling_convention;
399 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
400 parallel_move.AddMove(
401 locations->InAt(0),
402 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
403 Primitive::kPrimNot,
404 nullptr);
405 parallel_move.AddMove(
406 locations->InAt(1),
407 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
408 Primitive::kPrimInt,
409 nullptr);
410 parallel_move.AddMove(
411 locations->InAt(2),
412 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
413 Primitive::kPrimNot,
414 nullptr);
415 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
416
417 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
418 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
419 instruction_,
420 instruction_->GetDexPc(),
421 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000422 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 RestoreLiveRegisters(codegen, locations);
424 __ jmp(GetExitLabel());
425 }
426
427 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
428
429 private:
430 HInstruction* const instruction_;
431
432 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
433};
434
Roland Levillain7c1559a2015-12-15 10:55:36 +0000435// Slow path marking an object during a read barrier.
436class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
437 public:
438 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
439 : instruction_(instruction), out_(out), obj_(obj) {
440 DCHECK(kEmitCompilerReadBarrier);
441 }
442
443 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
446 LocationSummary* locations = instruction_->GetLocations();
447 Register reg_out = out_.AsRegister<Register>();
448 DCHECK(locations->CanCall());
449 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
450 DCHECK(instruction_->IsInstanceFieldGet() ||
451 instruction_->IsStaticFieldGet() ||
452 instruction_->IsArrayGet() ||
453 instruction_->IsLoadClass() ||
454 instruction_->IsLoadString() ||
455 instruction_->IsInstanceOf() ||
456 instruction_->IsCheckCast())
457 << "Unexpected instruction in read barrier marking slow path: "
458 << instruction_->DebugName();
459
460 __ Bind(GetEntryLabel());
461 SaveLiveRegisters(codegen, locations);
462
463 InvokeRuntimeCallingConvention calling_convention;
464 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
465 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
466 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
467 instruction_,
468 instruction_->GetDexPc(),
469 this);
470 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
471 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
472
473 RestoreLiveRegisters(codegen, locations);
474 __ jmp(GetExitLabel());
475 }
476
477 private:
478 HInstruction* const instruction_;
479 const Location out_;
480 const Location obj_;
481
482 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
483};
484
Roland Levillain0d5a2812015-11-13 10:07:31 +0000485// Slow path generating a read barrier for a heap reference.
486class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
487 public:
488 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
489 Location out,
490 Location ref,
491 Location obj,
492 uint32_t offset,
493 Location index)
494 : instruction_(instruction),
495 out_(out),
496 ref_(ref),
497 obj_(obj),
498 offset_(offset),
499 index_(index) {
500 DCHECK(kEmitCompilerReadBarrier);
501 // If `obj` is equal to `out` or `ref`, it means the initial object
502 // has been overwritten by (or after) the heap object reference load
503 // to be instrumented, e.g.:
504 //
505 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000507 //
508 // In that case, we have lost the information about the original
509 // object, and the emitted read barrier cannot work properly.
510 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
511 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
512 }
513
514 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
515 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
516 LocationSummary* locations = instruction_->GetLocations();
517 Register reg_out = out_.AsRegister<Register>();
518 DCHECK(locations->CanCall());
519 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
520 DCHECK(!instruction_->IsInvoke() ||
521 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000522 instruction_->GetLocations()->Intrinsified()))
523 << "Unexpected instruction in read barrier for heap reference slow path: "
524 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000525
526 __ Bind(GetEntryLabel());
527 SaveLiveRegisters(codegen, locations);
528
529 // We may have to change the index's value, but as `index_` is a
530 // constant member (like other "inputs" of this slow path),
531 // introduce a copy of it, `index`.
532 Location index = index_;
533 if (index_.IsValid()) {
534 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
535 if (instruction_->IsArrayGet()) {
536 // Compute the actual memory offset and store it in `index`.
537 Register index_reg = index_.AsRegister<Register>();
538 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
539 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
540 // We are about to change the value of `index_reg` (see the
541 // calls to art::x86::X86Assembler::shll and
542 // art::x86::X86Assembler::AddImmediate below), but it has
543 // not been saved by the previous call to
544 // art::SlowPathCode::SaveLiveRegisters, as it is a
545 // callee-save register --
546 // art::SlowPathCode::SaveLiveRegisters does not consider
547 // callee-save registers, as it has been designed with the
548 // assumption that callee-save registers are supposed to be
549 // handled by the called function. So, as a callee-save
550 // register, `index_reg` _would_ eventually be saved onto
551 // the stack, but it would be too late: we would have
552 // changed its value earlier. Therefore, we manually save
553 // it here into another freely available register,
554 // `free_reg`, chosen of course among the caller-save
555 // registers (as a callee-save `free_reg` register would
556 // exhibit the same problem).
557 //
558 // Note we could have requested a temporary register from
559 // the register allocator instead; but we prefer not to, as
560 // this is a slow path, and we know we can find a
561 // caller-save register that is available.
562 Register free_reg = FindAvailableCallerSaveRegister(codegen);
563 __ movl(free_reg, index_reg);
564 index_reg = free_reg;
565 index = Location::RegisterLocation(index_reg);
566 } else {
567 // The initial register stored in `index_` has already been
568 // saved in the call to art::SlowPathCode::SaveLiveRegisters
569 // (as it is not a callee-save register), so we can freely
570 // use it.
571 }
572 // Shifting the index value contained in `index_reg` by the scale
573 // factor (2) cannot overflow in practice, as the runtime is
574 // unable to allocate object arrays with a size larger than
575 // 2^26 - 1 (that is, 2^28 - 4 bytes).
576 __ shll(index_reg, Immediate(TIMES_4));
577 static_assert(
578 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
579 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
580 __ AddImmediate(index_reg, Immediate(offset_));
581 } else {
582 DCHECK(instruction_->IsInvoke());
583 DCHECK(instruction_->GetLocations()->Intrinsified());
584 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
585 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
586 << instruction_->AsInvoke()->GetIntrinsic();
587 DCHECK_EQ(offset_, 0U);
588 DCHECK(index_.IsRegisterPair());
589 // UnsafeGet's offset location is a register pair, the low
590 // part contains the correct offset.
591 index = index_.ToLow();
592 }
593 }
594
595 // We're moving two or three locations to locations that could
596 // overlap, so we need a parallel move resolver.
597 InvokeRuntimeCallingConvention calling_convention;
598 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
599 parallel_move.AddMove(ref_,
600 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
601 Primitive::kPrimNot,
602 nullptr);
603 parallel_move.AddMove(obj_,
604 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
605 Primitive::kPrimNot,
606 nullptr);
607 if (index.IsValid()) {
608 parallel_move.AddMove(index,
609 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
610 Primitive::kPrimInt,
611 nullptr);
612 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
613 } else {
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
616 }
617 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
618 instruction_,
619 instruction_->GetDexPc(),
620 this);
621 CheckEntrypointTypes<
622 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
623 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
624
625 RestoreLiveRegisters(codegen, locations);
626 __ jmp(GetExitLabel());
627 }
628
629 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
630
631 private:
632 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
633 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
634 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
635 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
636 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
637 return static_cast<Register>(i);
638 }
639 }
640 // We shall never fail to find a free caller-save register, as
641 // there are more than two core caller-save registers on x86
642 // (meaning it is possible to find one which is different from
643 // `ref` and `obj`).
644 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
645 LOG(FATAL) << "Could not find a free caller-save register";
646 UNREACHABLE();
647 }
648
649 HInstruction* const instruction_;
650 const Location out_;
651 const Location ref_;
652 const Location obj_;
653 const uint32_t offset_;
654 // An additional location containing an index to an array.
655 // Only used for HArrayGet and the UnsafeGetObject &
656 // UnsafeGetObjectVolatile intrinsics.
657 const Location index_;
658
659 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
660};
661
662// Slow path generating a read barrier for a GC root.
663class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
664 public:
665 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
Roland Levillain7c1559a2015-12-15 10:55:36 +0000666 : instruction_(instruction), out_(out), root_(root) {
667 DCHECK(kEmitCompilerReadBarrier);
668 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000669
670 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
671 LocationSummary* locations = instruction_->GetLocations();
672 Register reg_out = out_.AsRegister<Register>();
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000675 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
676 << "Unexpected instruction in read barrier for GC root slow path: "
677 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000678
679 __ Bind(GetEntryLabel());
680 SaveLiveRegisters(codegen, locations);
681
682 InvokeRuntimeCallingConvention calling_convention;
683 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
684 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
685 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
686 instruction_,
687 instruction_->GetDexPc(),
688 this);
689 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
690 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
691
692 RestoreLiveRegisters(codegen, locations);
693 __ jmp(GetExitLabel());
694 }
695
696 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
697
698 private:
699 HInstruction* const instruction_;
700 const Location out_;
701 const Location root_;
702
703 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
704};
705
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100706#undef __
Roland Levillain62a46b22015-06-01 18:24:13 +0100707#define __ down_cast<X86Assembler*>(GetAssembler())->
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100708
Aart Bike9f37602015-10-09 11:15:55 -0700709inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700710 switch (cond) {
711 case kCondEQ: return kEqual;
712 case kCondNE: return kNotEqual;
713 case kCondLT: return kLess;
714 case kCondLE: return kLessEqual;
715 case kCondGT: return kGreater;
716 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700717 case kCondB: return kBelow;
718 case kCondBE: return kBelowEqual;
719 case kCondA: return kAbove;
720 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700721 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100722 LOG(FATAL) << "Unreachable";
723 UNREACHABLE();
724}
725
Aart Bike9f37602015-10-09 11:15:55 -0700726// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100727inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
728 switch (cond) {
729 case kCondEQ: return kEqual;
730 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700731 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100732 case kCondLT: return kBelow;
733 case kCondLE: return kBelowEqual;
734 case kCondGT: return kAbove;
735 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700736 // Unsigned remain unchanged.
737 case kCondB: return kBelow;
738 case kCondBE: return kBelowEqual;
739 case kCondA: return kAbove;
740 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100741 }
742 LOG(FATAL) << "Unreachable";
743 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700744}
745
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100746void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100747 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100748}
749
750void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100751 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100752}
753
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100754size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
755 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
756 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100757}
758
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100759size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
760 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
761 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100762}
763
Mark Mendell7c8d0092015-01-26 11:21:33 -0500764size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
765 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
766 return GetFloatingPointSpillSlotSize();
767}
768
769size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
770 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
771 return GetFloatingPointSpillSlotSize();
772}
773
Calin Juravle175dc732015-08-25 15:42:32 +0100774void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
775 HInstruction* instruction,
776 uint32_t dex_pc,
777 SlowPathCode* slow_path) {
778 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
779 instruction,
780 dex_pc,
781 slow_path);
782}
783
784void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100785 HInstruction* instruction,
786 uint32_t dex_pc,
787 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100788 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100789 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100790 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100791}
792
Mark Mendellfb8d2792015-03-31 22:16:59 -0400793CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000794 const X86InstructionSetFeatures& isa_features,
795 const CompilerOptions& compiler_options,
796 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500797 : CodeGenerator(graph,
798 kNumberOfCpuRegisters,
799 kNumberOfXmmRegisters,
800 kNumberOfRegisterPairs,
801 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
802 arraysize(kCoreCalleeSaves))
803 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100804 0,
805 compiler_options,
806 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100807 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100808 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100809 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400810 move_resolver_(graph->GetArena(), this),
Vladimir Marko58155012015-08-19 12:49:41 +0000811 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100812 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400813 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000814 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400815 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000816 // Use a fake return address register to mimic Quick.
817 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100818}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819
David Brazdil58282f42016-01-14 12:45:10 +0000820void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100822 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100823
824 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100825 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100826
Calin Juravle34bacdf2014-10-07 20:23:36 +0100827 UpdateBlockedPairRegisters();
828}
829
830void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
831 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
832 X86ManagedRegister current =
833 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
834 if (blocked_core_registers_[current.AsRegisterPairLow()]
835 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
836 blocked_register_pairs_[i] = true;
837 }
838 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100839}
840
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100841InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800842 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100843 assembler_(codegen->GetAssembler()),
844 codegen_(codegen) {}
845
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100846static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100847 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100848}
849
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000850void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100851 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000852 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000853 bool skip_overflow_check =
854 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000855 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000856
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000857 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100858 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100859 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100860 }
861
Mark Mendell5f874182015-03-04 15:42:45 -0500862 if (HasEmptyFrame()) {
863 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000864 }
Mark Mendell5f874182015-03-04 15:42:45 -0500865
866 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
867 Register reg = kCoreCalleeSaves[i];
868 if (allocated_registers_.ContainsCoreRegister(reg)) {
869 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100870 __ cfi().AdjustCFAOffset(kX86WordSize);
871 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500872 }
873 }
874
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100875 int adjust = GetFrameSize() - FrameEntrySpillSize();
876 __ subl(ESP, Immediate(adjust));
877 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100878 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000879}
880
881void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100882 __ cfi().RememberState();
883 if (!HasEmptyFrame()) {
884 int adjust = GetFrameSize() - FrameEntrySpillSize();
885 __ addl(ESP, Immediate(adjust));
886 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500887
David Srbeckyc34dc932015-04-12 09:27:43 +0100888 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
889 Register reg = kCoreCalleeSaves[i];
890 if (allocated_registers_.ContainsCoreRegister(reg)) {
891 __ popl(reg);
892 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
893 __ cfi().Restore(DWARFReg(reg));
894 }
Mark Mendell5f874182015-03-04 15:42:45 -0500895 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000896 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100897 __ ret();
898 __ cfi().RestoreState();
899 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000900}
901
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100902void CodeGeneratorX86::Bind(HBasicBlock* block) {
903 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000904}
905
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100906Location CodeGeneratorX86::GetStackLocation(HLoadLocal* load) const {
907 switch (load->GetType()) {
908 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100909 case Primitive::kPrimDouble:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100910 return Location::DoubleStackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100911
912 case Primitive::kPrimInt:
913 case Primitive::kPrimNot:
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100914 case Primitive::kPrimFloat:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100915 return Location::StackSlot(GetStackSlot(load->GetLocal()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100916
917 case Primitive::kPrimBoolean:
918 case Primitive::kPrimByte:
919 case Primitive::kPrimChar:
920 case Primitive::kPrimShort:
921 case Primitive::kPrimVoid:
922 LOG(FATAL) << "Unexpected type " << load->GetType();
Andreas Gampe65b798e2015-04-06 09:35:22 -0700923 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100924 }
925
926 LOG(FATAL) << "Unreachable";
Andreas Gampe65b798e2015-04-06 09:35:22 -0700927 UNREACHABLE();
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100928}
929
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100930Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
931 switch (type) {
932 case Primitive::kPrimBoolean:
933 case Primitive::kPrimByte:
934 case Primitive::kPrimChar:
935 case Primitive::kPrimShort:
936 case Primitive::kPrimInt:
937 case Primitive::kPrimNot:
938 return Location::RegisterLocation(EAX);
939
940 case Primitive::kPrimLong:
941 return Location::RegisterPairLocation(EAX, EDX);
942
943 case Primitive::kPrimVoid:
944 return Location::NoLocation();
945
946 case Primitive::kPrimDouble:
947 case Primitive::kPrimFloat:
948 return Location::FpuRegisterLocation(XMM0);
949 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100950
951 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100952}
953
954Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
955 return Location::RegisterLocation(kMethodRegisterArgument);
956}
957
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100958Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959 switch (type) {
960 case Primitive::kPrimBoolean:
961 case Primitive::kPrimByte:
962 case Primitive::kPrimChar:
963 case Primitive::kPrimShort:
964 case Primitive::kPrimInt:
965 case Primitive::kPrimNot: {
966 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000967 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100968 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100969 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100970 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100972 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100973 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100974
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000975 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 uint32_t index = gp_index_;
977 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000978 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100980 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
981 calling_convention.GetRegisterPairAt(index));
982 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000984 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
985 }
986 }
987
988 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100989 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000990 stack_index_++;
991 if (index < calling_convention.GetNumberOfFpuRegisters()) {
992 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
993 } else {
994 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
995 }
996 }
997
998 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100999 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001000 stack_index_ += 2;
1001 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1002 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1003 } else {
1004 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001005 }
1006 }
1007
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001008 case Primitive::kPrimVoid:
1009 LOG(FATAL) << "Unexpected parameter type " << type;
1010 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001011 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001012 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001013}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001014
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001015void CodeGeneratorX86::Move32(Location destination, Location source) {
1016 if (source.Equals(destination)) {
1017 return;
1018 }
1019 if (destination.IsRegister()) {
1020 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001021 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001022 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001023 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001024 } else {
1025 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001026 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001028 } else if (destination.IsFpuRegister()) {
1029 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001030 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001031 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001032 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001033 } else {
1034 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001035 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001036 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001037 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001038 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001039 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001040 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001041 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001042 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001043 } else if (source.IsConstant()) {
1044 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001045 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001046 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 } else {
1048 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001049 __ pushl(Address(ESP, source.GetStackIndex()));
1050 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001051 }
1052 }
1053}
1054
1055void CodeGeneratorX86::Move64(Location destination, Location source) {
1056 if (source.Equals(destination)) {
1057 return;
1058 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001059 if (destination.IsRegisterPair()) {
1060 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001061 EmitParallelMoves(
1062 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1063 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001064 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001065 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001066 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1067 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001069 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1070 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1071 __ psrlq(src_reg, Immediate(32));
1072 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001074 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001075 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001076 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1077 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001078 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1079 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001080 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001081 if (source.IsFpuRegister()) {
1082 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1083 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001084 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001085 } else if (source.IsRegisterPair()) {
1086 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1087 // Create stack space for 2 elements.
1088 __ subl(ESP, Immediate(2 * elem_size));
1089 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1090 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1091 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1092 // And remove the temporary stack space we allocated.
1093 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001094 } else {
1095 LOG(FATAL) << "Unimplemented";
1096 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001098 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001099 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001100 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001101 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001102 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001103 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001104 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001105 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001106 } else if (source.IsConstant()) {
1107 HConstant* constant = source.GetConstant();
1108 int64_t value;
1109 if (constant->IsLongConstant()) {
1110 value = constant->AsLongConstant()->GetValue();
1111 } else {
1112 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001113 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001114 }
1115 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1116 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001117 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001118 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001119 EmitParallelMoves(
1120 Location::StackSlot(source.GetStackIndex()),
1121 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001122 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001123 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001124 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1125 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001126 }
1127 }
1128}
1129
Calin Juravle175dc732015-08-25 15:42:32 +01001130void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1131 DCHECK(location.IsRegister());
1132 __ movl(location.AsRegister<Register>(), Immediate(value));
1133}
1134
Calin Juravlee460d1d2015-09-29 04:52:17 +01001135void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001136 HParallelMove move(GetGraph()->GetArena());
1137 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1138 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1139 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001140 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001141 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001142 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001143 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001144}
1145
1146void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1147 if (location.IsRegister()) {
1148 locations->AddTemp(location);
1149 } else if (location.IsRegisterPair()) {
1150 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1151 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1152 } else {
1153 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1154 }
1155}
1156
David Brazdilfc6a86a2015-06-26 10:33:45 +00001157void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001158 DCHECK(!successor->IsExitBlock());
1159
1160 HBasicBlock* block = got->GetBlock();
1161 HInstruction* previous = got->GetPrevious();
1162
1163 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001164 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001165 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1166 return;
1167 }
1168
1169 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1170 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1171 }
1172 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001173 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001174 }
1175}
1176
David Brazdilfc6a86a2015-06-26 10:33:45 +00001177void LocationsBuilderX86::VisitGoto(HGoto* got) {
1178 got->SetLocations(nullptr);
1179}
1180
1181void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1182 HandleGoto(got, got->GetSuccessor());
1183}
1184
1185void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1186 try_boundary->SetLocations(nullptr);
1187}
1188
1189void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1190 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1191 if (!successor->IsExitBlock()) {
1192 HandleGoto(try_boundary, successor);
1193 }
1194}
1195
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001196void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001197 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001198}
1199
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001200void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001201}
1202
Mark Mendell152408f2015-12-31 12:28:50 -05001203template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001204void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001205 LabelType* true_label,
1206 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001207 if (cond->IsFPConditionTrueIfNaN()) {
1208 __ j(kUnordered, true_label);
1209 } else if (cond->IsFPConditionFalseIfNaN()) {
1210 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001211 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001212 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001213}
1214
Mark Mendell152408f2015-12-31 12:28:50 -05001215template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001216void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001217 LabelType* true_label,
1218 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001219 LocationSummary* locations = cond->GetLocations();
1220 Location left = locations->InAt(0);
1221 Location right = locations->InAt(1);
1222 IfCondition if_cond = cond->GetCondition();
1223
Mark Mendellc4701932015-04-10 13:18:51 -04001224 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001225 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001226 IfCondition true_high_cond = if_cond;
1227 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001228 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001229
1230 // Set the conditions for the test, remembering that == needs to be
1231 // decided using the low words.
1232 switch (if_cond) {
1233 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001234 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001235 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001236 break;
1237 case kCondLT:
1238 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001239 break;
1240 case kCondLE:
1241 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001242 break;
1243 case kCondGT:
1244 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001245 break;
1246 case kCondGE:
1247 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001248 break;
Aart Bike9f37602015-10-09 11:15:55 -07001249 case kCondB:
1250 false_high_cond = kCondA;
1251 break;
1252 case kCondBE:
1253 true_high_cond = kCondB;
1254 break;
1255 case kCondA:
1256 false_high_cond = kCondB;
1257 break;
1258 case kCondAE:
1259 true_high_cond = kCondA;
1260 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001261 }
1262
1263 if (right.IsConstant()) {
1264 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001265 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001266 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001267
Aart Bika19616e2016-02-01 18:57:58 -08001268 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001269 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001270 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001272 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001273 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001274 __ j(X86Condition(true_high_cond), true_label);
1275 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001276 }
1277 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001278 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendellc4701932015-04-10 13:18:51 -04001279 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001280 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001281 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001282
1283 __ cmpl(left_high, right_high);
1284 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001285 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001286 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001287 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001288 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001289 __ j(X86Condition(true_high_cond), true_label);
1290 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001291 }
1292 // Must be equal high, so compare the lows.
1293 __ cmpl(left_low, right_low);
1294 }
1295 // The last comparison might be unsigned.
1296 __ j(final_condition, true_label);
1297}
1298
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001299void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1300 Location rhs,
1301 HInstruction* insn,
1302 bool is_double) {
1303 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1304 if (is_double) {
1305 if (rhs.IsFpuRegister()) {
1306 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1307 } else if (const_area != nullptr) {
1308 DCHECK(const_area->IsEmittedAtUseSite());
1309 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1310 codegen_->LiteralDoubleAddress(
1311 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1312 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1313 } else {
1314 DCHECK(rhs.IsDoubleStackSlot());
1315 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1316 }
1317 } else {
1318 if (rhs.IsFpuRegister()) {
1319 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1320 } else if (const_area != nullptr) {
1321 DCHECK(const_area->IsEmittedAtUseSite());
1322 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1323 codegen_->LiteralFloatAddress(
1324 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1325 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1326 } else {
1327 DCHECK(rhs.IsStackSlot());
1328 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1329 }
1330 }
1331}
1332
Mark Mendell152408f2015-12-31 12:28:50 -05001333template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001334void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001335 LabelType* true_target_in,
1336 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001337 // Generated branching requires both targets to be explicit. If either of the
1338 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001339 LabelType fallthrough_target;
1340 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1341 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001342
Mark Mendellc4701932015-04-10 13:18:51 -04001343 LocationSummary* locations = condition->GetLocations();
1344 Location left = locations->InAt(0);
1345 Location right = locations->InAt(1);
1346
Mark Mendellc4701932015-04-10 13:18:51 -04001347 Primitive::Type type = condition->InputAt(0)->GetType();
1348 switch (type) {
1349 case Primitive::kPrimLong:
1350 GenerateLongComparesAndJumps(condition, true_target, false_target);
1351 break;
1352 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001353 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001354 GenerateFPJumps(condition, true_target, false_target);
1355 break;
1356 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001357 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001358 GenerateFPJumps(condition, true_target, false_target);
1359 break;
1360 default:
1361 LOG(FATAL) << "Unexpected compare type " << type;
1362 }
1363
David Brazdil0debae72015-11-12 18:37:00 +00001364 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001365 __ jmp(false_target);
1366 }
David Brazdil0debae72015-11-12 18:37:00 +00001367
1368 if (fallthrough_target.IsLinked()) {
1369 __ Bind(&fallthrough_target);
1370 }
Mark Mendellc4701932015-04-10 13:18:51 -04001371}
1372
David Brazdil0debae72015-11-12 18:37:00 +00001373static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1374 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1375 // are set only strictly before `branch`. We can't use the eflags on long/FP
1376 // conditions if they are materialized due to the complex branching.
1377 return cond->IsCondition() &&
1378 cond->GetNext() == branch &&
1379 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1380 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1381}
1382
Mark Mendell152408f2015-12-31 12:28:50 -05001383template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001384void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001385 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001386 LabelType* true_target,
1387 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001388 HInstruction* cond = instruction->InputAt(condition_input_index);
1389
1390 if (true_target == nullptr && false_target == nullptr) {
1391 // Nothing to do. The code always falls through.
1392 return;
1393 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001394 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001395 if (cond->AsIntConstant()->IsOne()) {
1396 if (true_target != nullptr) {
1397 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001398 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001399 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001400 DCHECK(cond->AsIntConstant()->IsZero());
1401 if (false_target != nullptr) {
1402 __ jmp(false_target);
1403 }
1404 }
1405 return;
1406 }
1407
1408 // The following code generates these patterns:
1409 // (1) true_target == nullptr && false_target != nullptr
1410 // - opposite condition true => branch to false_target
1411 // (2) true_target != nullptr && false_target == nullptr
1412 // - condition true => branch to true_target
1413 // (3) true_target != nullptr && false_target != nullptr
1414 // - condition true => branch to true_target
1415 // - branch to false_target
1416 if (IsBooleanValueOrMaterializedCondition(cond)) {
1417 if (AreEflagsSetFrom(cond, instruction)) {
1418 if (true_target == nullptr) {
1419 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1420 } else {
1421 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1422 }
1423 } else {
1424 // Materialized condition, compare against 0.
1425 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1426 if (lhs.IsRegister()) {
1427 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1428 } else {
1429 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1430 }
1431 if (true_target == nullptr) {
1432 __ j(kEqual, false_target);
1433 } else {
1434 __ j(kNotEqual, true_target);
1435 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001436 }
1437 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001438 // Condition has not been materialized, use its inputs as the comparison and
1439 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001440 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001441
1442 // If this is a long or FP comparison that has been folded into
1443 // the HCondition, generate the comparison directly.
1444 Primitive::Type type = condition->InputAt(0)->GetType();
1445 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1446 GenerateCompareTestAndBranch(condition, true_target, false_target);
1447 return;
1448 }
1449
1450 Location lhs = condition->GetLocations()->InAt(0);
1451 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001452 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001453 if (rhs.IsRegister()) {
1454 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1455 } else if (rhs.IsConstant()) {
1456 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001457 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001458 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001459 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1460 }
1461 if (true_target == nullptr) {
1462 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1463 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001464 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001465 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001466 }
David Brazdil0debae72015-11-12 18:37:00 +00001467
1468 // If neither branch falls through (case 3), the conditional branch to `true_target`
1469 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1470 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001471 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001472 }
1473}
1474
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001475void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001476 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1477 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001478 locations->SetInAt(0, Location::Any());
1479 }
1480}
1481
1482void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001483 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1484 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1485 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1486 nullptr : codegen_->GetLabelOf(true_successor);
1487 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1488 nullptr : codegen_->GetLabelOf(false_successor);
1489 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001490}
1491
1492void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1493 LocationSummary* locations = new (GetGraph()->GetArena())
1494 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001495 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001496 locations->SetInAt(0, Location::Any());
1497 }
1498}
1499
1500void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001501 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001502 GenerateTestAndBranch<Label>(deoptimize,
1503 /* condition_input_index */ 0,
1504 slow_path->GetEntryLabel(),
1505 /* false_target */ nullptr);
1506}
1507
1508void LocationsBuilderX86::VisitSelect(HSelect* select) {
1509 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
1510 Primitive::Type select_type = select->GetType();
1511 HInstruction* cond = select->GetCondition();
1512
1513 if (Primitive::IsFloatingPointType(select_type)) {
1514 locations->SetInAt(0, Location::RequiresFpuRegister());
1515 } else {
1516 locations->SetInAt(0, Location::RequiresRegister());
1517 }
1518 locations->SetInAt(1, Location::Any());
1519 if (IsBooleanValueOrMaterializedCondition(cond)) {
1520 locations->SetInAt(2, Location::Any());
1521 }
1522 locations->SetOut(Location::SameAsFirstInput());
1523}
1524
1525void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1526 LocationSummary* locations = select->GetLocations();
1527 NearLabel false_target;
1528 GenerateTestAndBranch<NearLabel>(
1529 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1530 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1531 __ Bind(&false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001532}
1533
David Srbecky0cf44932015-12-09 14:09:59 +00001534void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1535 new (GetGraph()->GetArena()) LocationSummary(info);
1536}
1537
1538void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001539 if (codegen_->HasStackMapAtCurrentPc()) {
1540 // Ensure that we do not collide with the stack map of the previous instruction.
1541 __ nop();
1542 }
David Srbecky0cf44932015-12-09 14:09:59 +00001543 codegen_->RecordPcInfo(info, info->GetDexPc());
1544}
1545
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001546void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001547 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001548}
1549
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001550void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1551 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001552}
1553
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001554void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001555 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001556}
1557
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001558void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001559 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001560}
1561
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001562void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001563 LocationSummary* locations =
1564 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001565 switch (store->InputAt(1)->GetType()) {
1566 case Primitive::kPrimBoolean:
1567 case Primitive::kPrimByte:
1568 case Primitive::kPrimChar:
1569 case Primitive::kPrimShort:
1570 case Primitive::kPrimInt:
1571 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001572 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001573 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1574 break;
1575
1576 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001577 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001578 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1579 break;
1580
1581 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001582 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001583 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001584}
1585
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001586void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001587}
1588
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001589void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001590 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001591 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001592 // Handle the long/FP comparisons made in instruction simplification.
1593 switch (cond->InputAt(0)->GetType()) {
1594 case Primitive::kPrimLong: {
1595 locations->SetInAt(0, Location::RequiresRegister());
1596 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
David Brazdilb3e773e2016-01-26 11:28:37 +00001597 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001598 locations->SetOut(Location::RequiresRegister());
1599 }
1600 break;
1601 }
1602 case Primitive::kPrimFloat:
1603 case Primitive::kPrimDouble: {
1604 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001605 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1606 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1607 } else if (cond->InputAt(1)->IsConstant()) {
1608 locations->SetInAt(1, Location::RequiresFpuRegister());
1609 } else {
1610 locations->SetInAt(1, Location::Any());
1611 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001612 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001613 locations->SetOut(Location::RequiresRegister());
1614 }
1615 break;
1616 }
1617 default:
1618 locations->SetInAt(0, Location::RequiresRegister());
1619 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001620 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001621 // We need a byte register.
1622 locations->SetOut(Location::RegisterLocation(ECX));
1623 }
1624 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001625 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001626}
1627
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001628void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001629 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001630 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001631 }
Mark Mendellc4701932015-04-10 13:18:51 -04001632
1633 LocationSummary* locations = cond->GetLocations();
1634 Location lhs = locations->InAt(0);
1635 Location rhs = locations->InAt(1);
1636 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001637 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001638
1639 switch (cond->InputAt(0)->GetType()) {
1640 default: {
1641 // Integer case.
1642
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001643 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001644 __ xorl(reg, reg);
1645
1646 if (rhs.IsRegister()) {
1647 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1648 } else if (rhs.IsConstant()) {
1649 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001650 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Mark Mendellc4701932015-04-10 13:18:51 -04001651 } else {
1652 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1653 }
Aart Bike9f37602015-10-09 11:15:55 -07001654 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001655 return;
1656 }
1657 case Primitive::kPrimLong:
1658 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1659 break;
1660 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001661 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001662 GenerateFPJumps(cond, &true_label, &false_label);
1663 break;
1664 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001665 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001666 GenerateFPJumps(cond, &true_label, &false_label);
1667 break;
1668 }
1669
1670 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001671 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001672
Roland Levillain4fa13f62015-07-06 18:11:54 +01001673 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001674 __ Bind(&false_label);
1675 __ xorl(reg, reg);
1676 __ jmp(&done_label);
1677
Roland Levillain4fa13f62015-07-06 18:11:54 +01001678 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001679 __ Bind(&true_label);
1680 __ movl(reg, Immediate(1));
1681 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001682}
1683
1684void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001685 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001686}
1687
1688void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001689 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001690}
1691
1692void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001693 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001694}
1695
1696void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001697 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001698}
1699
1700void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001701 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001702}
1703
1704void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001705 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001706}
1707
1708void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001709 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001710}
1711
1712void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001713 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001714}
1715
1716void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001718}
1719
1720void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001730}
1731
Aart Bike9f37602015-10-09 11:15:55 -07001732void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001734}
1735
1736void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001738}
1739
1740void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001742}
1743
1744void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001746}
1747
1748void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001750}
1751
1752void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001754}
1755
1756void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001758}
1759
1760void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001762}
1763
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001764void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001765 LocationSummary* locations =
1766 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001767 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001768}
1769
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001770void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001771 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001772}
1773
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001774void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1775 LocationSummary* locations =
1776 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1777 locations->SetOut(Location::ConstantLocation(constant));
1778}
1779
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001780void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001781 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001782}
1783
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001784void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001785 LocationSummary* locations =
1786 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001787 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001788}
1789
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001790void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001791 // Will be generated at use site.
1792}
1793
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001794void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1795 LocationSummary* locations =
1796 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1797 locations->SetOut(Location::ConstantLocation(constant));
1798}
1799
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001800void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001801 // Will be generated at use site.
1802}
1803
1804void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1805 LocationSummary* locations =
1806 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1807 locations->SetOut(Location::ConstantLocation(constant));
1808}
1809
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001810void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001811 // Will be generated at use site.
1812}
1813
Calin Juravle27df7582015-04-17 19:12:31 +01001814void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1815 memory_barrier->SetLocations(nullptr);
1816}
1817
1818void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001819 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001820}
1821
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001822void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001823 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001824}
1825
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001826void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001827 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001828}
1829
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001830void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001831 LocationSummary* locations =
1832 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001833 switch (ret->InputAt(0)->GetType()) {
1834 case Primitive::kPrimBoolean:
1835 case Primitive::kPrimByte:
1836 case Primitive::kPrimChar:
1837 case Primitive::kPrimShort:
1838 case Primitive::kPrimInt:
1839 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001840 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001841 break;
1842
1843 case Primitive::kPrimLong:
1844 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001845 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001846 break;
1847
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001848 case Primitive::kPrimFloat:
1849 case Primitive::kPrimDouble:
1850 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001851 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001852 break;
1853
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001854 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001855 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001856 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001857}
1858
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001860 if (kIsDebugBuild) {
1861 switch (ret->InputAt(0)->GetType()) {
1862 case Primitive::kPrimBoolean:
1863 case Primitive::kPrimByte:
1864 case Primitive::kPrimChar:
1865 case Primitive::kPrimShort:
1866 case Primitive::kPrimInt:
1867 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001868 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001869 break;
1870
1871 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001872 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1873 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 break;
1875
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001876 case Primitive::kPrimFloat:
1877 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001878 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001879 break;
1880
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001881 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 }
1884 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001885 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001886}
1887
Calin Juravle175dc732015-08-25 15:42:32 +01001888void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1889 // The trampoline uses the same calling convention as dex calling conventions,
1890 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1891 // the method_idx.
1892 HandleInvoke(invoke);
1893}
1894
1895void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1896 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1897}
1898
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001899void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001900 // Explicit clinit checks triggered by static invokes must have been pruned by
1901 // art::PrepareForRegisterAllocation.
1902 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001903
Mark Mendellfb8d2792015-03-31 22:16:59 -04001904 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001905 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001906 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001907 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001908 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001909 return;
1910 }
1911
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001912 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001913
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001914 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1915 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001916 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001917 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001918}
1919
Mark Mendell09ed1a32015-03-25 08:30:06 -04001920static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1921 if (invoke->GetLocations()->Intrinsified()) {
1922 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1923 intrinsic.Dispatch(invoke);
1924 return true;
1925 }
1926 return false;
1927}
1928
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001929void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001930 // Explicit clinit checks triggered by static invokes must have been pruned by
1931 // art::PrepareForRegisterAllocation.
1932 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001933
Mark Mendell09ed1a32015-03-25 08:30:06 -04001934 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1935 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001936 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001937
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001938 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001939 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001940 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001941 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001942}
1943
1944void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001945 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1946 if (intrinsic.TryDispatch(invoke)) {
1947 return;
1948 }
1949
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001950 HandleInvoke(invoke);
1951}
1952
1953void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001954 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001955 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001956}
1957
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001958void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001959 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1960 return;
1961 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001962
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001963 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001964 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001965 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001966}
1967
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001968void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001969 // This call to HandleInvoke allocates a temporary (core) register
1970 // which is also used to transfer the hidden argument from FP to
1971 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001972 HandleInvoke(invoke);
1973 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001974 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001975}
1976
1977void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
1978 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00001979 LocationSummary* locations = invoke->GetLocations();
1980 Register temp = locations->GetTemp(0).AsRegister<Register>();
1981 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001982 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
1983 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001984 Location receiver = locations->InAt(0);
1985 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
1986
Roland Levillain0d5a2812015-11-13 10:07:31 +00001987 // Set the hidden argument. This is safe to do this here, as XMM7
1988 // won't be modified thereafter, before the `call` instruction.
1989 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001990 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00001991 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001992
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001993 if (receiver.IsStackSlot()) {
1994 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00001995 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00001996 __ movl(temp, Address(temp, class_offset));
1997 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00001998 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00001999 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002000 }
Roland Levillain4d027112015-07-01 15:41:14 +01002001 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002002 // Instead of simply (possibly) unpoisoning `temp` here, we should
2003 // emit a read barrier for the previous class reference load.
2004 // However this is not required in practice, as this is an
2005 // intermediate/temporary reference and because the current
2006 // concurrent copying collector keeps the from-space memory
2007 // intact/accessible until the end of the marking phase (the
2008 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002009 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002010 // temp = temp->GetImtEntryAt(method_offset);
2011 __ movl(temp, Address(temp, method_offset));
2012 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002013 __ call(Address(temp,
2014 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002015
2016 DCHECK(!codegen_->IsLeafMethod());
2017 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2018}
2019
Roland Levillain88cb1752014-10-20 16:36:47 +01002020void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2023 switch (neg->GetResultType()) {
2024 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002025 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002026 locations->SetInAt(0, Location::RequiresRegister());
2027 locations->SetOut(Location::SameAsFirstInput());
2028 break;
2029
Roland Levillain88cb1752014-10-20 16:36:47 +01002030 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002031 locations->SetInAt(0, Location::RequiresFpuRegister());
2032 locations->SetOut(Location::SameAsFirstInput());
2033 locations->AddTemp(Location::RequiresRegister());
2034 locations->AddTemp(Location::RequiresFpuRegister());
2035 break;
2036
Roland Levillain88cb1752014-10-20 16:36:47 +01002037 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002038 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002039 locations->SetOut(Location::SameAsFirstInput());
2040 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002041 break;
2042
2043 default:
2044 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2045 }
2046}
2047
2048void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2049 LocationSummary* locations = neg->GetLocations();
2050 Location out = locations->Out();
2051 Location in = locations->InAt(0);
2052 switch (neg->GetResultType()) {
2053 case Primitive::kPrimInt:
2054 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002055 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002056 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002057 break;
2058
2059 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002060 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002061 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002062 __ negl(out.AsRegisterPairLow<Register>());
2063 // Negation is similar to subtraction from zero. The least
2064 // significant byte triggers a borrow when it is different from
2065 // zero; to take it into account, add 1 to the most significant
2066 // byte if the carry flag (CF) is set to 1 after the first NEGL
2067 // operation.
2068 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2069 __ negl(out.AsRegisterPairHigh<Register>());
2070 break;
2071
Roland Levillain5368c212014-11-27 15:03:41 +00002072 case Primitive::kPrimFloat: {
2073 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002074 Register constant = locations->GetTemp(0).AsRegister<Register>();
2075 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002076 // Implement float negation with an exclusive or with value
2077 // 0x80000000 (mask for bit 31, representing the sign of a
2078 // single-precision floating-point number).
2079 __ movl(constant, Immediate(INT32_C(0x80000000)));
2080 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002081 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002082 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002083 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002084
Roland Levillain5368c212014-11-27 15:03:41 +00002085 case Primitive::kPrimDouble: {
2086 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002087 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002088 // Implement double negation with an exclusive or with value
2089 // 0x8000000000000000 (mask for bit 63, representing the sign of
2090 // a double-precision floating-point number).
2091 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002093 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002094 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002095
2096 default:
2097 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2098 }
2099}
2100
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002101void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2102 LocationSummary* locations =
2103 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2104 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2105 locations->SetInAt(0, Location::RequiresFpuRegister());
2106 locations->SetInAt(1, Location::RequiresRegister());
2107 locations->SetOut(Location::SameAsFirstInput());
2108 locations->AddTemp(Location::RequiresFpuRegister());
2109}
2110
2111void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2112 LocationSummary* locations = neg->GetLocations();
2113 Location out = locations->Out();
2114 DCHECK(locations->InAt(0).Equals(out));
2115
2116 Register constant_area = locations->InAt(1).AsRegister<Register>();
2117 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2118 if (neg->GetType() == Primitive::kPrimFloat) {
2119 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2120 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2121 } else {
2122 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2123 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2124 }
2125}
2126
Roland Levillaindff1f282014-11-05 14:15:05 +00002127void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002128 Primitive::Type result_type = conversion->GetResultType();
2129 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002130 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002131
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002132 // The float-to-long and double-to-long type conversions rely on a
2133 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002134 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002135 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2136 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002137 ? LocationSummary::kCall
2138 : LocationSummary::kNoCall;
2139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2141
David Brazdilb2bd1c52015-03-25 11:17:37 +00002142 // The Java language does not allow treating boolean as an integral type but
2143 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002144
Roland Levillaindff1f282014-11-05 14:15:05 +00002145 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002146 case Primitive::kPrimByte:
2147 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002148 case Primitive::kPrimBoolean:
2149 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002150 case Primitive::kPrimShort:
2151 case Primitive::kPrimInt:
2152 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002153 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002154 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2155 // Make the output overlap to please the register allocator. This greatly simplifies
2156 // the validation of the linear scan implementation
2157 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002158 break;
2159
2160 default:
2161 LOG(FATAL) << "Unexpected type conversion from " << input_type
2162 << " to " << result_type;
2163 }
2164 break;
2165
Roland Levillain01a8d712014-11-14 16:27:39 +00002166 case Primitive::kPrimShort:
2167 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002168 case Primitive::kPrimBoolean:
2169 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002170 case Primitive::kPrimByte:
2171 case Primitive::kPrimInt:
2172 case Primitive::kPrimChar:
2173 // Processing a Dex `int-to-short' instruction.
2174 locations->SetInAt(0, Location::Any());
2175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2176 break;
2177
2178 default:
2179 LOG(FATAL) << "Unexpected type conversion from " << input_type
2180 << " to " << result_type;
2181 }
2182 break;
2183
Roland Levillain946e1432014-11-11 17:35:19 +00002184 case Primitive::kPrimInt:
2185 switch (input_type) {
2186 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002187 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002188 locations->SetInAt(0, Location::Any());
2189 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2190 break;
2191
2192 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002193 // Processing a Dex `float-to-int' instruction.
2194 locations->SetInAt(0, Location::RequiresFpuRegister());
2195 locations->SetOut(Location::RequiresRegister());
2196 locations->AddTemp(Location::RequiresFpuRegister());
2197 break;
2198
Roland Levillain946e1432014-11-11 17:35:19 +00002199 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002200 // Processing a Dex `double-to-int' instruction.
2201 locations->SetInAt(0, Location::RequiresFpuRegister());
2202 locations->SetOut(Location::RequiresRegister());
2203 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002204 break;
2205
2206 default:
2207 LOG(FATAL) << "Unexpected type conversion from " << input_type
2208 << " to " << result_type;
2209 }
2210 break;
2211
Roland Levillaindff1f282014-11-05 14:15:05 +00002212 case Primitive::kPrimLong:
2213 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002214 case Primitive::kPrimBoolean:
2215 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002216 case Primitive::kPrimByte:
2217 case Primitive::kPrimShort:
2218 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002219 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002220 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002221 locations->SetInAt(0, Location::RegisterLocation(EAX));
2222 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2223 break;
2224
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002225 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002226 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002227 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002228 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002229 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2230 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2231
Vladimir Marko949c91f2015-01-27 10:48:44 +00002232 // The runtime helper puts the result in EAX, EDX.
2233 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002234 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002235 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002236
2237 default:
2238 LOG(FATAL) << "Unexpected type conversion from " << input_type
2239 << " to " << result_type;
2240 }
2241 break;
2242
Roland Levillain981e4542014-11-14 11:47:14 +00002243 case Primitive::kPrimChar:
2244 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002245 case Primitive::kPrimBoolean:
2246 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002247 case Primitive::kPrimByte:
2248 case Primitive::kPrimShort:
2249 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002250 // Processing a Dex `int-to-char' instruction.
2251 locations->SetInAt(0, Location::Any());
2252 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2253 break;
2254
2255 default:
2256 LOG(FATAL) << "Unexpected type conversion from " << input_type
2257 << " to " << result_type;
2258 }
2259 break;
2260
Roland Levillaindff1f282014-11-05 14:15:05 +00002261 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002262 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002263 case Primitive::kPrimBoolean:
2264 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002265 case Primitive::kPrimByte:
2266 case Primitive::kPrimShort:
2267 case Primitive::kPrimInt:
2268 case Primitive::kPrimChar:
2269 // Processing a Dex `int-to-float' instruction.
2270 locations->SetInAt(0, Location::RequiresRegister());
2271 locations->SetOut(Location::RequiresFpuRegister());
2272 break;
2273
2274 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002275 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002276 locations->SetInAt(0, Location::Any());
2277 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002278 break;
2279
Roland Levillaincff13742014-11-17 14:32:17 +00002280 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002281 // Processing a Dex `double-to-float' instruction.
2282 locations->SetInAt(0, Location::RequiresFpuRegister());
2283 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002284 break;
2285
2286 default:
2287 LOG(FATAL) << "Unexpected type conversion from " << input_type
2288 << " to " << result_type;
2289 };
2290 break;
2291
Roland Levillaindff1f282014-11-05 14:15:05 +00002292 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002293 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002294 case Primitive::kPrimBoolean:
2295 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002296 case Primitive::kPrimByte:
2297 case Primitive::kPrimShort:
2298 case Primitive::kPrimInt:
2299 case Primitive::kPrimChar:
2300 // Processing a Dex `int-to-double' instruction.
2301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetOut(Location::RequiresFpuRegister());
2303 break;
2304
2305 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002306 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002307 locations->SetInAt(0, Location::Any());
2308 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002309 break;
2310
Roland Levillaincff13742014-11-17 14:32:17 +00002311 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002312 // Processing a Dex `float-to-double' instruction.
2313 locations->SetInAt(0, Location::RequiresFpuRegister());
2314 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 }
2327}
2328
2329void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2330 LocationSummary* locations = conversion->GetLocations();
2331 Location out = locations->Out();
2332 Location in = locations->InAt(0);
2333 Primitive::Type result_type = conversion->GetResultType();
2334 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002335 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002336 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002337 case Primitive::kPrimByte:
2338 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002339 case Primitive::kPrimBoolean:
2340 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002341 case Primitive::kPrimShort:
2342 case Primitive::kPrimInt:
2343 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002344 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002345 if (in.IsRegister()) {
2346 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002347 } else {
2348 DCHECK(in.GetConstant()->IsIntConstant());
2349 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2350 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2351 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002352 break;
2353
2354 default:
2355 LOG(FATAL) << "Unexpected type conversion from " << input_type
2356 << " to " << result_type;
2357 }
2358 break;
2359
Roland Levillain01a8d712014-11-14 16:27:39 +00002360 case Primitive::kPrimShort:
2361 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002362 case Primitive::kPrimBoolean:
2363 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002364 case Primitive::kPrimByte:
2365 case Primitive::kPrimInt:
2366 case Primitive::kPrimChar:
2367 // Processing a Dex `int-to-short' instruction.
2368 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002369 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002370 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002371 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002372 } else {
2373 DCHECK(in.GetConstant()->IsIntConstant());
2374 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002376 }
2377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383 break;
2384
Roland Levillain946e1432014-11-11 17:35:19 +00002385 case Primitive::kPrimInt:
2386 switch (input_type) {
2387 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002388 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002389 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002390 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002391 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002392 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002393 } else {
2394 DCHECK(in.IsConstant());
2395 DCHECK(in.GetConstant()->IsLongConstant());
2396 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002397 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002398 }
2399 break;
2400
Roland Levillain3f8f9362014-12-02 17:45:01 +00002401 case Primitive::kPrimFloat: {
2402 // Processing a Dex `float-to-int' instruction.
2403 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2404 Register output = out.AsRegister<Register>();
2405 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002406 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002407
2408 __ movl(output, Immediate(kPrimIntMax));
2409 // temp = int-to-float(output)
2410 __ cvtsi2ss(temp, output);
2411 // if input >= temp goto done
2412 __ comiss(input, temp);
2413 __ j(kAboveEqual, &done);
2414 // if input == NaN goto nan
2415 __ j(kUnordered, &nan);
2416 // output = float-to-int-truncate(input)
2417 __ cvttss2si(output, input);
2418 __ jmp(&done);
2419 __ Bind(&nan);
2420 // output = 0
2421 __ xorl(output, output);
2422 __ Bind(&done);
2423 break;
2424 }
2425
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002426 case Primitive::kPrimDouble: {
2427 // Processing a Dex `double-to-int' instruction.
2428 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2429 Register output = out.AsRegister<Register>();
2430 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002431 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002432
2433 __ movl(output, Immediate(kPrimIntMax));
2434 // temp = int-to-double(output)
2435 __ cvtsi2sd(temp, output);
2436 // if input >= temp goto done
2437 __ comisd(input, temp);
2438 __ j(kAboveEqual, &done);
2439 // if input == NaN goto nan
2440 __ j(kUnordered, &nan);
2441 // output = double-to-int-truncate(input)
2442 __ cvttsd2si(output, input);
2443 __ jmp(&done);
2444 __ Bind(&nan);
2445 // output = 0
2446 __ xorl(output, output);
2447 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002448 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002449 }
Roland Levillain946e1432014-11-11 17:35:19 +00002450
2451 default:
2452 LOG(FATAL) << "Unexpected type conversion from " << input_type
2453 << " to " << result_type;
2454 }
2455 break;
2456
Roland Levillaindff1f282014-11-05 14:15:05 +00002457 case Primitive::kPrimLong:
2458 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002459 case Primitive::kPrimBoolean:
2460 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002461 case Primitive::kPrimByte:
2462 case Primitive::kPrimShort:
2463 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002464 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002465 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002466 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2467 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002469 __ cdq();
2470 break;
2471
2472 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002473 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002474 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2475 conversion,
2476 conversion->GetDexPc(),
2477 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002478 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002479 break;
2480
Roland Levillaindff1f282014-11-05 14:15:05 +00002481 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002482 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002483 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2484 conversion,
2485 conversion->GetDexPc(),
2486 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002487 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002488 break;
2489
2490 default:
2491 LOG(FATAL) << "Unexpected type conversion from " << input_type
2492 << " to " << result_type;
2493 }
2494 break;
2495
Roland Levillain981e4542014-11-14 11:47:14 +00002496 case Primitive::kPrimChar:
2497 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002498 case Primitive::kPrimBoolean:
2499 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002500 case Primitive::kPrimByte:
2501 case Primitive::kPrimShort:
2502 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002503 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2504 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002505 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002506 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002507 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002508 } else {
2509 DCHECK(in.GetConstant()->IsIntConstant());
2510 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002512 }
2513 break;
2514
2515 default:
2516 LOG(FATAL) << "Unexpected type conversion from " << input_type
2517 << " to " << result_type;
2518 }
2519 break;
2520
Roland Levillaindff1f282014-11-05 14:15:05 +00002521 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002522 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002523 case Primitive::kPrimBoolean:
2524 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002525 case Primitive::kPrimByte:
2526 case Primitive::kPrimShort:
2527 case Primitive::kPrimInt:
2528 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002529 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002530 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002531 break;
2532
Roland Levillain6d0e4832014-11-27 18:31:21 +00002533 case Primitive::kPrimLong: {
2534 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002535 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002536
Roland Levillain232ade02015-04-20 15:14:36 +01002537 // Create stack space for the call to
2538 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2539 // TODO: enhance register allocator to ask for stack temporaries.
2540 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2541 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2542 __ subl(ESP, Immediate(adjustment));
2543 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002544
Roland Levillain232ade02015-04-20 15:14:36 +01002545 // Load the value to the FP stack, using temporaries if needed.
2546 PushOntoFPStack(in, 0, adjustment, false, true);
2547
2548 if (out.IsStackSlot()) {
2549 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2550 } else {
2551 __ fstps(Address(ESP, 0));
2552 Location stack_temp = Location::StackSlot(0);
2553 codegen_->Move32(out, stack_temp);
2554 }
2555
2556 // Remove the temporary stack space we allocated.
2557 if (adjustment != 0) {
2558 __ addl(ESP, Immediate(adjustment));
2559 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002560 break;
2561 }
2562
Roland Levillaincff13742014-11-17 14:32:17 +00002563 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002564 // Processing a Dex `double-to-float' instruction.
2565 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002566 break;
2567
2568 default:
2569 LOG(FATAL) << "Unexpected type conversion from " << input_type
2570 << " to " << result_type;
2571 };
2572 break;
2573
Roland Levillaindff1f282014-11-05 14:15:05 +00002574 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002575 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002576 case Primitive::kPrimBoolean:
2577 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002578 case Primitive::kPrimByte:
2579 case Primitive::kPrimShort:
2580 case Primitive::kPrimInt:
2581 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002582 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002583 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002584 break;
2585
Roland Levillain647b9ed2014-11-27 12:06:00 +00002586 case Primitive::kPrimLong: {
2587 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002588 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002589
Roland Levillain232ade02015-04-20 15:14:36 +01002590 // Create stack space for the call to
2591 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2592 // TODO: enhance register allocator to ask for stack temporaries.
2593 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2594 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2595 __ subl(ESP, Immediate(adjustment));
2596 }
2597
2598 // Load the value to the FP stack, using temporaries if needed.
2599 PushOntoFPStack(in, 0, adjustment, false, true);
2600
2601 if (out.IsDoubleStackSlot()) {
2602 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2603 } else {
2604 __ fstpl(Address(ESP, 0));
2605 Location stack_temp = Location::DoubleStackSlot(0);
2606 codegen_->Move64(out, stack_temp);
2607 }
2608
2609 // Remove the temporary stack space we allocated.
2610 if (adjustment != 0) {
2611 __ addl(ESP, Immediate(adjustment));
2612 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002613 break;
2614 }
2615
Roland Levillaincff13742014-11-17 14:32:17 +00002616 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002617 // Processing a Dex `float-to-double' instruction.
2618 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002619 break;
2620
2621 default:
2622 LOG(FATAL) << "Unexpected type conversion from " << input_type
2623 << " to " << result_type;
2624 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002625 break;
2626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
2630 }
2631}
2632
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002633void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002634 LocationSummary* locations =
2635 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002636 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002637 case Primitive::kPrimInt: {
2638 locations->SetInAt(0, Location::RequiresRegister());
2639 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2640 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2641 break;
2642 }
2643
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002644 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002645 locations->SetInAt(0, Location::RequiresRegister());
2646 locations->SetInAt(1, Location::Any());
2647 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002648 break;
2649 }
2650
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002651 case Primitive::kPrimFloat:
2652 case Primitive::kPrimDouble: {
2653 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002654 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2655 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002656 } else if (add->InputAt(1)->IsConstant()) {
2657 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002658 } else {
2659 locations->SetInAt(1, Location::Any());
2660 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002661 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002662 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002663 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002664
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002665 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002666 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2667 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002668 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002669}
2670
2671void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2672 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002673 Location first = locations->InAt(0);
2674 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002675 Location out = locations->Out();
2676
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002677 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002678 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002679 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002680 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2681 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002682 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2683 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002684 } else {
2685 __ leal(out.AsRegister<Register>(), Address(
2686 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2687 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002688 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002689 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2690 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2691 __ addl(out.AsRegister<Register>(), Immediate(value));
2692 } else {
2693 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2694 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002695 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002696 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002697 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002698 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002699 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002700 }
2701
2702 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002703 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002704 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2705 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002706 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002707 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2708 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002709 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002710 } else {
2711 DCHECK(second.IsConstant()) << second;
2712 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2713 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2714 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002715 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002716 break;
2717 }
2718
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002719 case Primitive::kPrimFloat: {
2720 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002721 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002722 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2723 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002724 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002725 __ addss(first.AsFpuRegister<XmmRegister>(),
2726 codegen_->LiteralFloatAddress(
2727 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2728 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2729 } else {
2730 DCHECK(second.IsStackSlot());
2731 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002732 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002733 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002734 }
2735
2736 case Primitive::kPrimDouble: {
2737 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002738 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002739 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2740 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002741 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002742 __ addsd(first.AsFpuRegister<XmmRegister>(),
2743 codegen_->LiteralDoubleAddress(
2744 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2745 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2746 } else {
2747 DCHECK(second.IsDoubleStackSlot());
2748 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002749 }
2750 break;
2751 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002752
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002753 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002754 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002755 }
2756}
2757
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002758void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002759 LocationSummary* locations =
2760 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002761 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002762 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002763 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002764 locations->SetInAt(0, Location::RequiresRegister());
2765 locations->SetInAt(1, Location::Any());
2766 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002767 break;
2768 }
Calin Juravle11351682014-10-23 15:38:15 +01002769 case Primitive::kPrimFloat:
2770 case Primitive::kPrimDouble: {
2771 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002772 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2773 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002774 } else if (sub->InputAt(1)->IsConstant()) {
2775 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002776 } else {
2777 locations->SetInAt(1, Location::Any());
2778 }
Calin Juravle11351682014-10-23 15:38:15 +01002779 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002780 break;
Calin Juravle11351682014-10-23 15:38:15 +01002781 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002782
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002783 default:
Calin Juravle11351682014-10-23 15:38:15 +01002784 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002785 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002786}
2787
2788void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2789 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002790 Location first = locations->InAt(0);
2791 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002792 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002793 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002794 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002795 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002796 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002797 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002798 __ subl(first.AsRegister<Register>(),
2799 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002800 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002801 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002802 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002803 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002804 }
2805
2806 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002807 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002808 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2809 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002810 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002811 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002812 __ sbbl(first.AsRegisterPairHigh<Register>(),
2813 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002814 } else {
2815 DCHECK(second.IsConstant()) << second;
2816 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2817 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2818 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002819 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002820 break;
2821 }
2822
Calin Juravle11351682014-10-23 15:38:15 +01002823 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002824 if (second.IsFpuRegister()) {
2825 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2826 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2827 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002828 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002829 __ subss(first.AsFpuRegister<XmmRegister>(),
2830 codegen_->LiteralFloatAddress(
2831 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2832 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2833 } else {
2834 DCHECK(second.IsStackSlot());
2835 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2836 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002837 break;
Calin Juravle11351682014-10-23 15:38:15 +01002838 }
2839
2840 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002841 if (second.IsFpuRegister()) {
2842 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2843 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2844 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002845 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002846 __ subsd(first.AsFpuRegister<XmmRegister>(),
2847 codegen_->LiteralDoubleAddress(
2848 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2849 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2850 } else {
2851 DCHECK(second.IsDoubleStackSlot());
2852 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2853 }
Calin Juravle11351682014-10-23 15:38:15 +01002854 break;
2855 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002856
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002857 default:
Calin Juravle11351682014-10-23 15:38:15 +01002858 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002859 }
2860}
2861
Calin Juravle34bacdf2014-10-07 20:23:36 +01002862void LocationsBuilderX86::VisitMul(HMul* mul) {
2863 LocationSummary* locations =
2864 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2865 switch (mul->GetResultType()) {
2866 case Primitive::kPrimInt:
2867 locations->SetInAt(0, Location::RequiresRegister());
2868 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002869 if (mul->InputAt(1)->IsIntConstant()) {
2870 // Can use 3 operand multiply.
2871 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2872 } else {
2873 locations->SetOut(Location::SameAsFirstInput());
2874 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002875 break;
2876 case Primitive::kPrimLong: {
2877 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002878 locations->SetInAt(1, Location::Any());
2879 locations->SetOut(Location::SameAsFirstInput());
2880 // Needed for imul on 32bits with 64bits output.
2881 locations->AddTemp(Location::RegisterLocation(EAX));
2882 locations->AddTemp(Location::RegisterLocation(EDX));
2883 break;
2884 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002885 case Primitive::kPrimFloat:
2886 case Primitive::kPrimDouble: {
2887 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002888 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2889 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002890 } else if (mul->InputAt(1)->IsConstant()) {
2891 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002892 } else {
2893 locations->SetInAt(1, Location::Any());
2894 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002895 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002896 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002897 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002898
2899 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002900 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002901 }
2902}
2903
2904void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2905 LocationSummary* locations = mul->GetLocations();
2906 Location first = locations->InAt(0);
2907 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002908 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002909
2910 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002911 case Primitive::kPrimInt:
2912 // The constant may have ended up in a register, so test explicitly to avoid
2913 // problems where the output may not be the same as the first operand.
2914 if (mul->InputAt(1)->IsIntConstant()) {
2915 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2916 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2917 } else if (second.IsRegister()) {
2918 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002919 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002920 } else {
2921 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002922 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002923 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002924 }
2925 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002926
2927 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002928 Register in1_hi = first.AsRegisterPairHigh<Register>();
2929 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002930 Register eax = locations->GetTemp(0).AsRegister<Register>();
2931 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002932
2933 DCHECK_EQ(EAX, eax);
2934 DCHECK_EQ(EDX, edx);
2935
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002936 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002937 // output: in1
2938 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2939 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2940 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002941 if (second.IsConstant()) {
2942 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002943
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002944 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2945 int32_t low_value = Low32Bits(value);
2946 int32_t high_value = High32Bits(value);
2947 Immediate low(low_value);
2948 Immediate high(high_value);
2949
2950 __ movl(eax, high);
2951 // eax <- in1.lo * in2.hi
2952 __ imull(eax, in1_lo);
2953 // in1.hi <- in1.hi * in2.lo
2954 __ imull(in1_hi, low);
2955 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2956 __ addl(in1_hi, eax);
2957 // move in2_lo to eax to prepare for double precision
2958 __ movl(eax, low);
2959 // edx:eax <- in1.lo * in2.lo
2960 __ mull(in1_lo);
2961 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2962 __ addl(in1_hi, edx);
2963 // in1.lo <- (in1.lo * in2.lo)[31:0];
2964 __ movl(in1_lo, eax);
2965 } else if (second.IsRegisterPair()) {
2966 Register in2_hi = second.AsRegisterPairHigh<Register>();
2967 Register in2_lo = second.AsRegisterPairLow<Register>();
2968
2969 __ movl(eax, in2_hi);
2970 // eax <- in1.lo * in2.hi
2971 __ imull(eax, in1_lo);
2972 // in1.hi <- in1.hi * in2.lo
2973 __ imull(in1_hi, in2_lo);
2974 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2975 __ addl(in1_hi, eax);
2976 // move in1_lo to eax to prepare for double precision
2977 __ movl(eax, in1_lo);
2978 // edx:eax <- in1.lo * in2.lo
2979 __ mull(in2_lo);
2980 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2981 __ addl(in1_hi, edx);
2982 // in1.lo <- (in1.lo * in2.lo)[31:0];
2983 __ movl(in1_lo, eax);
2984 } else {
2985 DCHECK(second.IsDoubleStackSlot()) << second;
2986 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2987 Address in2_lo(ESP, second.GetStackIndex());
2988
2989 __ movl(eax, in2_hi);
2990 // eax <- in1.lo * in2.hi
2991 __ imull(eax, in1_lo);
2992 // in1.hi <- in1.hi * in2.lo
2993 __ imull(in1_hi, in2_lo);
2994 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2995 __ addl(in1_hi, eax);
2996 // move in1_lo to eax to prepare for double precision
2997 __ movl(eax, in1_lo);
2998 // edx:eax <- in1.lo * in2.lo
2999 __ mull(in2_lo);
3000 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3001 __ addl(in1_hi, edx);
3002 // in1.lo <- (in1.lo * in2.lo)[31:0];
3003 __ movl(in1_lo, eax);
3004 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003005
3006 break;
3007 }
3008
Calin Juravleb5bfa962014-10-21 18:02:24 +01003009 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003010 DCHECK(first.Equals(locations->Out()));
3011 if (second.IsFpuRegister()) {
3012 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3013 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3014 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003015 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003016 __ mulss(first.AsFpuRegister<XmmRegister>(),
3017 codegen_->LiteralFloatAddress(
3018 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3019 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3020 } else {
3021 DCHECK(second.IsStackSlot());
3022 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3023 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003024 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003025 }
3026
3027 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003028 DCHECK(first.Equals(locations->Out()));
3029 if (second.IsFpuRegister()) {
3030 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3031 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3032 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003033 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003034 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3035 codegen_->LiteralDoubleAddress(
3036 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3037 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3038 } else {
3039 DCHECK(second.IsDoubleStackSlot());
3040 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3041 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003042 break;
3043 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003044
3045 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003046 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003047 }
3048}
3049
Roland Levillain232ade02015-04-20 15:14:36 +01003050void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3051 uint32_t temp_offset,
3052 uint32_t stack_adjustment,
3053 bool is_fp,
3054 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003055 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003056 DCHECK(!is_wide);
3057 if (is_fp) {
3058 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3059 } else {
3060 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3061 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003062 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003063 DCHECK(is_wide);
3064 if (is_fp) {
3065 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3066 } else {
3067 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3068 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003069 } else {
3070 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003071 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003072 Location stack_temp = Location::StackSlot(temp_offset);
3073 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003074 if (is_fp) {
3075 __ flds(Address(ESP, temp_offset));
3076 } else {
3077 __ filds(Address(ESP, temp_offset));
3078 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003079 } else {
3080 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3081 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003082 if (is_fp) {
3083 __ fldl(Address(ESP, temp_offset));
3084 } else {
3085 __ fildl(Address(ESP, temp_offset));
3086 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003087 }
3088 }
3089}
3090
3091void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3092 Primitive::Type type = rem->GetResultType();
3093 bool is_float = type == Primitive::kPrimFloat;
3094 size_t elem_size = Primitive::ComponentSize(type);
3095 LocationSummary* locations = rem->GetLocations();
3096 Location first = locations->InAt(0);
3097 Location second = locations->InAt(1);
3098 Location out = locations->Out();
3099
3100 // Create stack space for 2 elements.
3101 // TODO: enhance register allocator to ask for stack temporaries.
3102 __ subl(ESP, Immediate(2 * elem_size));
3103
3104 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003105 const bool is_wide = !is_float;
3106 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3107 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003108
3109 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003110 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003111 __ Bind(&retry);
3112 __ fprem();
3113
3114 // Move FP status to AX.
3115 __ fstsw();
3116
3117 // And see if the argument reduction is complete. This is signaled by the
3118 // C2 FPU flag bit set to 0.
3119 __ andl(EAX, Immediate(kC2ConditionMask));
3120 __ j(kNotEqual, &retry);
3121
3122 // We have settled on the final value. Retrieve it into an XMM register.
3123 // Store FP top of stack to real stack.
3124 if (is_float) {
3125 __ fsts(Address(ESP, 0));
3126 } else {
3127 __ fstl(Address(ESP, 0));
3128 }
3129
3130 // Pop the 2 items from the FP stack.
3131 __ fucompp();
3132
3133 // Load the value from the stack into an XMM register.
3134 DCHECK(out.IsFpuRegister()) << out;
3135 if (is_float) {
3136 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3137 } else {
3138 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3139 }
3140
3141 // And remove the temporary stack space we allocated.
3142 __ addl(ESP, Immediate(2 * elem_size));
3143}
3144
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003145
3146void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3147 DCHECK(instruction->IsDiv() || instruction->IsRem());
3148
3149 LocationSummary* locations = instruction->GetLocations();
3150 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003151 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003152
3153 Register out_register = locations->Out().AsRegister<Register>();
3154 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003155 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003156
3157 DCHECK(imm == 1 || imm == -1);
3158
3159 if (instruction->IsRem()) {
3160 __ xorl(out_register, out_register);
3161 } else {
3162 __ movl(out_register, input_register);
3163 if (imm == -1) {
3164 __ negl(out_register);
3165 }
3166 }
3167}
3168
3169
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003170void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003171 LocationSummary* locations = instruction->GetLocations();
3172
3173 Register out_register = locations->Out().AsRegister<Register>();
3174 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003175 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003176 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3177 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003178
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003179 Register num = locations->GetTemp(0).AsRegister<Register>();
3180
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003181 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003182 __ testl(input_register, input_register);
3183 __ cmovl(kGreaterEqual, num, input_register);
3184 int shift = CTZ(imm);
3185 __ sarl(num, Immediate(shift));
3186
3187 if (imm < 0) {
3188 __ negl(num);
3189 }
3190
3191 __ movl(out_register, num);
3192}
3193
3194void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3195 DCHECK(instruction->IsDiv() || instruction->IsRem());
3196
3197 LocationSummary* locations = instruction->GetLocations();
3198 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3199
3200 Register eax = locations->InAt(0).AsRegister<Register>();
3201 Register out = locations->Out().AsRegister<Register>();
3202 Register num;
3203 Register edx;
3204
3205 if (instruction->IsDiv()) {
3206 edx = locations->GetTemp(0).AsRegister<Register>();
3207 num = locations->GetTemp(1).AsRegister<Register>();
3208 } else {
3209 edx = locations->Out().AsRegister<Register>();
3210 num = locations->GetTemp(0).AsRegister<Register>();
3211 }
3212
3213 DCHECK_EQ(EAX, eax);
3214 DCHECK_EQ(EDX, edx);
3215 if (instruction->IsDiv()) {
3216 DCHECK_EQ(EAX, out);
3217 } else {
3218 DCHECK_EQ(EDX, out);
3219 }
3220
3221 int64_t magic;
3222 int shift;
3223 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3224
Mark Mendell0c9497d2015-08-21 09:30:05 -04003225 NearLabel ndiv;
3226 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003227 // If numerator is 0, the result is 0, no computation needed.
3228 __ testl(eax, eax);
3229 __ j(kNotEqual, &ndiv);
3230
3231 __ xorl(out, out);
3232 __ jmp(&end);
3233
3234 __ Bind(&ndiv);
3235
3236 // Save the numerator.
3237 __ movl(num, eax);
3238
3239 // EAX = magic
3240 __ movl(eax, Immediate(magic));
3241
3242 // EDX:EAX = magic * numerator
3243 __ imull(num);
3244
3245 if (imm > 0 && magic < 0) {
3246 // EDX += num
3247 __ addl(edx, num);
3248 } else if (imm < 0 && magic > 0) {
3249 __ subl(edx, num);
3250 }
3251
3252 // Shift if needed.
3253 if (shift != 0) {
3254 __ sarl(edx, Immediate(shift));
3255 }
3256
3257 // EDX += 1 if EDX < 0
3258 __ movl(eax, edx);
3259 __ shrl(edx, Immediate(31));
3260 __ addl(edx, eax);
3261
3262 if (instruction->IsRem()) {
3263 __ movl(eax, num);
3264 __ imull(edx, Immediate(imm));
3265 __ subl(eax, edx);
3266 __ movl(edx, eax);
3267 } else {
3268 __ movl(eax, edx);
3269 }
3270 __ Bind(&end);
3271}
3272
Calin Juravlebacfec32014-11-14 15:54:36 +00003273void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3274 DCHECK(instruction->IsDiv() || instruction->IsRem());
3275
3276 LocationSummary* locations = instruction->GetLocations();
3277 Location out = locations->Out();
3278 Location first = locations->InAt(0);
3279 Location second = locations->InAt(1);
3280 bool is_div = instruction->IsDiv();
3281
3282 switch (instruction->GetResultType()) {
3283 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003284 DCHECK_EQ(EAX, first.AsRegister<Register>());
3285 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003286
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003287 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003288 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003289
3290 if (imm == 0) {
3291 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3292 } else if (imm == 1 || imm == -1) {
3293 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003294 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003295 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003296 } else {
3297 DCHECK(imm <= -2 || imm >= 2);
3298 GenerateDivRemWithAnyConstant(instruction);
3299 }
3300 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003301 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003302 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003303 is_div);
3304 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003305
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003306 Register second_reg = second.AsRegister<Register>();
3307 // 0x80000000/-1 triggers an arithmetic exception!
3308 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3309 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003310
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003311 __ cmpl(second_reg, Immediate(-1));
3312 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003313
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003314 // edx:eax <- sign-extended of eax
3315 __ cdq();
3316 // eax = quotient, edx = remainder
3317 __ idivl(second_reg);
3318 __ Bind(slow_path->GetExitLabel());
3319 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003320 break;
3321 }
3322
3323 case Primitive::kPrimLong: {
3324 InvokeRuntimeCallingConvention calling_convention;
3325 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3326 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3327 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3328 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3329 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3330 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3331
3332 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003333 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3334 instruction,
3335 instruction->GetDexPc(),
3336 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003337 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003338 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003339 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3340 instruction,
3341 instruction->GetDexPc(),
3342 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003343 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003344 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003345 break;
3346 }
3347
3348 default:
3349 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3350 }
3351}
3352
Calin Juravle7c4954d2014-10-28 16:57:40 +00003353void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003354 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003355 ? LocationSummary::kCall
3356 : LocationSummary::kNoCall;
3357 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3358
Calin Juravle7c4954d2014-10-28 16:57:40 +00003359 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003360 case Primitive::kPrimInt: {
3361 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003362 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003363 locations->SetOut(Location::SameAsFirstInput());
3364 // Intel uses edx:eax as the dividend.
3365 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003366 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3367 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3368 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003369 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003370 locations->AddTemp(Location::RequiresRegister());
3371 }
Calin Juravled0d48522014-11-04 16:40:20 +00003372 break;
3373 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003374 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003375 InvokeRuntimeCallingConvention calling_convention;
3376 locations->SetInAt(0, Location::RegisterPairLocation(
3377 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3378 locations->SetInAt(1, Location::RegisterPairLocation(
3379 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3380 // Runtime helper puts the result in EAX, EDX.
3381 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003382 break;
3383 }
3384 case Primitive::kPrimFloat:
3385 case Primitive::kPrimDouble: {
3386 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003387 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3388 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003389 } else if (div->InputAt(1)->IsConstant()) {
3390 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003391 } else {
3392 locations->SetInAt(1, Location::Any());
3393 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003394 locations->SetOut(Location::SameAsFirstInput());
3395 break;
3396 }
3397
3398 default:
3399 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3400 }
3401}
3402
3403void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3404 LocationSummary* locations = div->GetLocations();
3405 Location first = locations->InAt(0);
3406 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003407
3408 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003409 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003410 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003411 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003412 break;
3413 }
3414
3415 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003416 if (second.IsFpuRegister()) {
3417 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3418 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3419 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003420 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003421 __ divss(first.AsFpuRegister<XmmRegister>(),
3422 codegen_->LiteralFloatAddress(
3423 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3424 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3425 } else {
3426 DCHECK(second.IsStackSlot());
3427 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3428 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003429 break;
3430 }
3431
3432 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003433 if (second.IsFpuRegister()) {
3434 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3435 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3436 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003437 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003438 __ divsd(first.AsFpuRegister<XmmRegister>(),
3439 codegen_->LiteralDoubleAddress(
3440 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3441 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3442 } else {
3443 DCHECK(second.IsDoubleStackSlot());
3444 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3445 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003446 break;
3447 }
3448
3449 default:
3450 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3451 }
3452}
3453
Calin Juravlebacfec32014-11-14 15:54:36 +00003454void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003455 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003456
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003457 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3458 ? LocationSummary::kCall
3459 : LocationSummary::kNoCall;
3460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003461
Calin Juravled2ec87d2014-12-08 14:24:46 +00003462 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003463 case Primitive::kPrimInt: {
3464 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003465 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003466 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003467 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3468 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3469 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003470 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003471 locations->AddTemp(Location::RequiresRegister());
3472 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003473 break;
3474 }
3475 case Primitive::kPrimLong: {
3476 InvokeRuntimeCallingConvention calling_convention;
3477 locations->SetInAt(0, Location::RegisterPairLocation(
3478 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3479 locations->SetInAt(1, Location::RegisterPairLocation(
3480 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3481 // Runtime helper puts the result in EAX, EDX.
3482 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3483 break;
3484 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003485 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003486 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003487 locations->SetInAt(0, Location::Any());
3488 locations->SetInAt(1, Location::Any());
3489 locations->SetOut(Location::RequiresFpuRegister());
3490 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003491 break;
3492 }
3493
3494 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003495 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003496 }
3497}
3498
3499void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3500 Primitive::Type type = rem->GetResultType();
3501 switch (type) {
3502 case Primitive::kPrimInt:
3503 case Primitive::kPrimLong: {
3504 GenerateDivRemIntegral(rem);
3505 break;
3506 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003507 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003508 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003509 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003510 break;
3511 }
3512 default:
3513 LOG(FATAL) << "Unexpected rem type " << type;
3514 }
3515}
3516
Calin Juravled0d48522014-11-04 16:40:20 +00003517void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003518 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3519 ? LocationSummary::kCallOnSlowPath
3520 : LocationSummary::kNoCall;
3521 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003522 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003523 case Primitive::kPrimByte:
3524 case Primitive::kPrimChar:
3525 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003526 case Primitive::kPrimInt: {
3527 locations->SetInAt(0, Location::Any());
3528 break;
3529 }
3530 case Primitive::kPrimLong: {
3531 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3532 if (!instruction->IsConstant()) {
3533 locations->AddTemp(Location::RequiresRegister());
3534 }
3535 break;
3536 }
3537 default:
3538 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3539 }
Calin Juravled0d48522014-11-04 16:40:20 +00003540 if (instruction->HasUses()) {
3541 locations->SetOut(Location::SameAsFirstInput());
3542 }
3543}
3544
3545void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003546 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003547 codegen_->AddSlowPath(slow_path);
3548
3549 LocationSummary* locations = instruction->GetLocations();
3550 Location value = locations->InAt(0);
3551
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003552 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003553 case Primitive::kPrimByte:
3554 case Primitive::kPrimChar:
3555 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003556 case Primitive::kPrimInt: {
3557 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003558 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003559 __ j(kEqual, slow_path->GetEntryLabel());
3560 } else if (value.IsStackSlot()) {
3561 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3562 __ j(kEqual, slow_path->GetEntryLabel());
3563 } else {
3564 DCHECK(value.IsConstant()) << value;
3565 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3566 __ jmp(slow_path->GetEntryLabel());
3567 }
3568 }
3569 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003570 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003571 case Primitive::kPrimLong: {
3572 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003573 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003574 __ movl(temp, value.AsRegisterPairLow<Register>());
3575 __ orl(temp, value.AsRegisterPairHigh<Register>());
3576 __ j(kEqual, slow_path->GetEntryLabel());
3577 } else {
3578 DCHECK(value.IsConstant()) << value;
3579 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3580 __ jmp(slow_path->GetEntryLabel());
3581 }
3582 }
3583 break;
3584 }
3585 default:
3586 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003587 }
Calin Juravled0d48522014-11-04 16:40:20 +00003588}
3589
Calin Juravle9aec02f2014-11-18 23:06:35 +00003590void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3591 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3592
3593 LocationSummary* locations =
3594 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3595
3596 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003597 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003598 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003599 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003600 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003601 // The shift count needs to be in CL or a constant.
3602 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003603 locations->SetOut(Location::SameAsFirstInput());
3604 break;
3605 }
3606 default:
3607 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3608 }
3609}
3610
3611void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3612 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3613
3614 LocationSummary* locations = op->GetLocations();
3615 Location first = locations->InAt(0);
3616 Location second = locations->InAt(1);
3617 DCHECK(first.Equals(locations->Out()));
3618
3619 switch (op->GetResultType()) {
3620 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003621 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003622 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003623 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003624 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003625 DCHECK_EQ(ECX, second_reg);
3626 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003627 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003628 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003629 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003630 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003631 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003632 }
3633 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003634 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3635 if (shift == 0) {
3636 return;
3637 }
3638 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003639 if (op->IsShl()) {
3640 __ shll(first_reg, imm);
3641 } else if (op->IsShr()) {
3642 __ sarl(first_reg, imm);
3643 } else {
3644 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003645 }
3646 }
3647 break;
3648 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003649 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003650 if (second.IsRegister()) {
3651 Register second_reg = second.AsRegister<Register>();
3652 DCHECK_EQ(ECX, second_reg);
3653 if (op->IsShl()) {
3654 GenerateShlLong(first, second_reg);
3655 } else if (op->IsShr()) {
3656 GenerateShrLong(first, second_reg);
3657 } else {
3658 GenerateUShrLong(first, second_reg);
3659 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003660 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003661 // Shift by a constant.
3662 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3663 // Nothing to do if the shift is 0, as the input is already the output.
3664 if (shift != 0) {
3665 if (op->IsShl()) {
3666 GenerateShlLong(first, shift);
3667 } else if (op->IsShr()) {
3668 GenerateShrLong(first, shift);
3669 } else {
3670 GenerateUShrLong(first, shift);
3671 }
3672 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003673 }
3674 break;
3675 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003676 default:
3677 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3678 }
3679}
3680
Mark P Mendell73945692015-04-29 14:56:17 +00003681void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3682 Register low = loc.AsRegisterPairLow<Register>();
3683 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003684 if (shift == 1) {
3685 // This is just an addition.
3686 __ addl(low, low);
3687 __ adcl(high, high);
3688 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003689 // Shift by 32 is easy. High gets low, and low gets 0.
3690 codegen_->EmitParallelMoves(
3691 loc.ToLow(),
3692 loc.ToHigh(),
3693 Primitive::kPrimInt,
3694 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3695 loc.ToLow(),
3696 Primitive::kPrimInt);
3697 } else if (shift > 32) {
3698 // Low part becomes 0. High part is low part << (shift-32).
3699 __ movl(high, low);
3700 __ shll(high, Immediate(shift - 32));
3701 __ xorl(low, low);
3702 } else {
3703 // Between 1 and 31.
3704 __ shld(high, low, Immediate(shift));
3705 __ shll(low, Immediate(shift));
3706 }
3707}
3708
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003710 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003711 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3712 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3713 __ testl(shifter, Immediate(32));
3714 __ j(kEqual, &done);
3715 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3716 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3717 __ Bind(&done);
3718}
3719
Mark P Mendell73945692015-04-29 14:56:17 +00003720void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3721 Register low = loc.AsRegisterPairLow<Register>();
3722 Register high = loc.AsRegisterPairHigh<Register>();
3723 if (shift == 32) {
3724 // Need to copy the sign.
3725 DCHECK_NE(low, high);
3726 __ movl(low, high);
3727 __ sarl(high, Immediate(31));
3728 } else if (shift > 32) {
3729 DCHECK_NE(low, high);
3730 // High part becomes sign. Low part is shifted by shift - 32.
3731 __ movl(low, high);
3732 __ sarl(high, Immediate(31));
3733 __ sarl(low, Immediate(shift - 32));
3734 } else {
3735 // Between 1 and 31.
3736 __ shrd(low, high, Immediate(shift));
3737 __ sarl(high, Immediate(shift));
3738 }
3739}
3740
Calin Juravle9aec02f2014-11-18 23:06:35 +00003741void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003742 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003743 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3744 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3745 __ testl(shifter, Immediate(32));
3746 __ j(kEqual, &done);
3747 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3748 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3749 __ Bind(&done);
3750}
3751
Mark P Mendell73945692015-04-29 14:56:17 +00003752void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3753 Register low = loc.AsRegisterPairLow<Register>();
3754 Register high = loc.AsRegisterPairHigh<Register>();
3755 if (shift == 32) {
3756 // Shift by 32 is easy. Low gets high, and high gets 0.
3757 codegen_->EmitParallelMoves(
3758 loc.ToHigh(),
3759 loc.ToLow(),
3760 Primitive::kPrimInt,
3761 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3762 loc.ToHigh(),
3763 Primitive::kPrimInt);
3764 } else if (shift > 32) {
3765 // Low part is high >> (shift - 32). High part becomes 0.
3766 __ movl(low, high);
3767 __ shrl(low, Immediate(shift - 32));
3768 __ xorl(high, high);
3769 } else {
3770 // Between 1 and 31.
3771 __ shrd(low, high, Immediate(shift));
3772 __ shrl(high, Immediate(shift));
3773 }
3774}
3775
Calin Juravle9aec02f2014-11-18 23:06:35 +00003776void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003777 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003778 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3779 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3780 __ testl(shifter, Immediate(32));
3781 __ j(kEqual, &done);
3782 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3783 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3784 __ Bind(&done);
3785}
3786
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003787void LocationsBuilderX86::VisitRor(HRor* ror) {
3788 LocationSummary* locations =
3789 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3790
3791 switch (ror->GetResultType()) {
3792 case Primitive::kPrimLong:
3793 // Add the temporary needed.
3794 locations->AddTemp(Location::RequiresRegister());
3795 FALLTHROUGH_INTENDED;
3796 case Primitive::kPrimInt:
3797 locations->SetInAt(0, Location::RequiresRegister());
3798 // The shift count needs to be in CL (unless it is a constant).
3799 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3800 locations->SetOut(Location::SameAsFirstInput());
3801 break;
3802 default:
3803 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3804 UNREACHABLE();
3805 }
3806}
3807
3808void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3809 LocationSummary* locations = ror->GetLocations();
3810 Location first = locations->InAt(0);
3811 Location second = locations->InAt(1);
3812
3813 if (ror->GetResultType() == Primitive::kPrimInt) {
3814 Register first_reg = first.AsRegister<Register>();
3815 if (second.IsRegister()) {
3816 Register second_reg = second.AsRegister<Register>();
3817 __ rorl(first_reg, second_reg);
3818 } else {
3819 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3820 __ rorl(first_reg, imm);
3821 }
3822 return;
3823 }
3824
3825 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3826 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3827 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3828 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3829 if (second.IsRegister()) {
3830 Register second_reg = second.AsRegister<Register>();
3831 DCHECK_EQ(second_reg, ECX);
3832 __ movl(temp_reg, first_reg_hi);
3833 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3834 __ shrd(first_reg_lo, temp_reg, second_reg);
3835 __ movl(temp_reg, first_reg_hi);
3836 __ testl(second_reg, Immediate(32));
3837 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3838 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3839 } else {
3840 int32_t shift_amt =
3841 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3842 if (shift_amt == 0) {
3843 // Already fine.
3844 return;
3845 }
3846 if (shift_amt == 32) {
3847 // Just swap.
3848 __ movl(temp_reg, first_reg_lo);
3849 __ movl(first_reg_lo, first_reg_hi);
3850 __ movl(first_reg_hi, temp_reg);
3851 return;
3852 }
3853
3854 Immediate imm(shift_amt);
3855 // Save the constents of the low value.
3856 __ movl(temp_reg, first_reg_lo);
3857
3858 // Shift right into low, feeding bits from high.
3859 __ shrd(first_reg_lo, first_reg_hi, imm);
3860
3861 // Shift right into high, feeding bits from the original low.
3862 __ shrd(first_reg_hi, temp_reg, imm);
3863
3864 // Swap if needed.
3865 if (shift_amt > 32) {
3866 __ movl(temp_reg, first_reg_lo);
3867 __ movl(first_reg_lo, first_reg_hi);
3868 __ movl(first_reg_hi, temp_reg);
3869 }
3870 }
3871}
3872
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873void LocationsBuilderX86::VisitShl(HShl* shl) {
3874 HandleShift(shl);
3875}
3876
3877void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3878 HandleShift(shl);
3879}
3880
3881void LocationsBuilderX86::VisitShr(HShr* shr) {
3882 HandleShift(shr);
3883}
3884
3885void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3886 HandleShift(shr);
3887}
3888
3889void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3890 HandleShift(ushr);
3891}
3892
3893void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3894 HandleShift(ushr);
3895}
3896
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003897void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003898 LocationSummary* locations =
3899 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003900 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003901 if (instruction->IsStringAlloc()) {
3902 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3903 } else {
3904 InvokeRuntimeCallingConvention calling_convention;
3905 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3906 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3907 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003908}
3909
3910void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003911 // Note: if heap poisoning is enabled, the entry point takes cares
3912 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003913 if (instruction->IsStringAlloc()) {
3914 // String is allocated through StringFactory. Call NewEmptyString entry point.
3915 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3916 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3917 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3918 __ call(Address(temp, code_offset.Int32Value()));
3919 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3920 } else {
3921 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3922 instruction,
3923 instruction->GetDexPc(),
3924 nullptr);
3925 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3926 DCHECK(!codegen_->IsLeafMethod());
3927 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003928}
3929
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003930void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3931 LocationSummary* locations =
3932 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3933 locations->SetOut(Location::RegisterLocation(EAX));
3934 InvokeRuntimeCallingConvention calling_convention;
3935 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003936 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003937 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003938}
3939
3940void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3941 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003942 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003943 // Note: if heap poisoning is enabled, the entry point takes cares
3944 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003945 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3946 instruction,
3947 instruction->GetDexPc(),
3948 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003949 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003950 DCHECK(!codegen_->IsLeafMethod());
3951}
3952
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003953void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003954 LocationSummary* locations =
3955 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003956 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3957 if (location.IsStackSlot()) {
3958 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3959 } else if (location.IsDoubleStackSlot()) {
3960 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003961 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003962 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003963}
3964
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003965void InstructionCodeGeneratorX86::VisitParameterValue(
3966 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3967}
3968
3969void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3970 LocationSummary* locations =
3971 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3972 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3973}
3974
3975void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003976}
3977
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003978void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
3979 LocationSummary* locations =
3980 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3981 locations->SetInAt(0, Location::RequiresRegister());
3982 locations->SetOut(Location::RequiresRegister());
3983}
3984
3985void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
3986 LocationSummary* locations = instruction->GetLocations();
3987 uint32_t method_offset = 0;
3988 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
3989 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3990 instruction->GetIndex(), kX86PointerSize).SizeValue();
3991 } else {
3992 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3993 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
3994 }
3995 __ movl(locations->Out().AsRegister<Register>(),
3996 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
3997}
3998
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003999void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004000 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004001 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004002 locations->SetInAt(0, Location::RequiresRegister());
4003 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004004}
4005
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004006void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4007 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004008 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004009 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004010 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004011 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004012 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004013 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004014 break;
4015
4016 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004017 __ notl(out.AsRegisterPairLow<Register>());
4018 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004019 break;
4020
4021 default:
4022 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4023 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004024}
4025
David Brazdil66d126e2015-04-03 16:02:44 +01004026void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4027 LocationSummary* locations =
4028 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4029 locations->SetInAt(0, Location::RequiresRegister());
4030 locations->SetOut(Location::SameAsFirstInput());
4031}
4032
4033void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004034 LocationSummary* locations = bool_not->GetLocations();
4035 Location in = locations->InAt(0);
4036 Location out = locations->Out();
4037 DCHECK(in.Equals(out));
4038 __ xorl(out.AsRegister<Register>(), Immediate(1));
4039}
4040
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004041void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004042 LocationSummary* locations =
4043 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004044 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004045 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004046 case Primitive::kPrimLong: {
4047 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004048 locations->SetInAt(1, Location::Any());
4049 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4050 break;
4051 }
4052 case Primitive::kPrimFloat:
4053 case Primitive::kPrimDouble: {
4054 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004055 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4056 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4057 } else if (compare->InputAt(1)->IsConstant()) {
4058 locations->SetInAt(1, Location::RequiresFpuRegister());
4059 } else {
4060 locations->SetInAt(1, Location::Any());
4061 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004062 locations->SetOut(Location::RequiresRegister());
4063 break;
4064 }
4065 default:
4066 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4067 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004068}
4069
4070void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004071 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004072 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004073 Location left = locations->InAt(0);
4074 Location right = locations->InAt(1);
4075
Mark Mendell0c9497d2015-08-21 09:30:05 -04004076 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004077 Condition less_cond = kLess;
4078
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004079 switch (compare->InputAt(0)->GetType()) {
Aart Bika19616e2016-02-01 18:57:58 -08004080 case Primitive::kPrimInt: {
4081 Register left_reg = left.AsRegister<Register>();
4082 if (right.IsConstant()) {
4083 int32_t value = right.GetConstant()->AsIntConstant()->GetValue();
4084 codegen_->Compare32BitValue(left_reg, value);
4085 } else if (right.IsStackSlot()) {
4086 __ cmpl(left_reg, Address(ESP, right.GetStackIndex()));
4087 } else {
4088 __ cmpl(left_reg, right.AsRegister<Register>());
4089 }
4090 break;
4091 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004092 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004093 Register left_low = left.AsRegisterPairLow<Register>();
4094 Register left_high = left.AsRegisterPairHigh<Register>();
4095 int32_t val_low = 0;
4096 int32_t val_high = 0;
4097 bool right_is_const = false;
4098
4099 if (right.IsConstant()) {
4100 DCHECK(right.GetConstant()->IsLongConstant());
4101 right_is_const = true;
4102 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4103 val_low = Low32Bits(val);
4104 val_high = High32Bits(val);
4105 }
4106
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004108 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004109 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004110 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004111 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004112 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004113 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004114 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004115 __ j(kLess, &less); // Signed compare.
4116 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004117 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004118 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004119 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004120 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004121 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004122 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004123 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004124 }
Aart Bika19616e2016-02-01 18:57:58 -08004125 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004126 break;
4127 }
4128 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004129 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004130 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004131 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004132 break;
4133 }
4134 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004135 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004136 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004137 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004138 break;
4139 }
4140 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004141 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004142 }
Aart Bika19616e2016-02-01 18:57:58 -08004143
Calin Juravleddb7df22014-11-25 20:56:51 +00004144 __ movl(out, Immediate(0));
4145 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004146 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004147
4148 __ Bind(&greater);
4149 __ movl(out, Immediate(1));
4150 __ jmp(&done);
4151
4152 __ Bind(&less);
4153 __ movl(out, Immediate(-1));
4154
4155 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004156}
4157
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004158void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004159 LocationSummary* locations =
4160 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004161 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4162 locations->SetInAt(i, Location::Any());
4163 }
4164 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004165}
4166
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004167void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004168 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004169}
4170
Roland Levillain7c1559a2015-12-15 10:55:36 +00004171void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004172 /*
4173 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4174 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4175 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4176 */
4177 switch (kind) {
4178 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004179 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004180 break;
4181 }
4182 case MemBarrierKind::kAnyStore:
4183 case MemBarrierKind::kLoadAny:
4184 case MemBarrierKind::kStoreStore: {
4185 // nop
4186 break;
4187 }
4188 default:
4189 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004190 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004191}
4192
Vladimir Markodc151b22015-10-15 18:02:30 +01004193HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4194 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4195 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004196 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4197
4198 // We disable pc-relative load when there is an irreducible loop, as the optimization
4199 // is incompatible with it.
4200 if (GetGraph()->HasIrreducibleLoops() &&
4201 (dispatch_info.method_load_kind ==
4202 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4203 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4204 }
4205 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004206 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4207 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4208 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4209 // (Though the direct CALL ptr16:32 is available for consideration).
4210 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004211 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004212 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004213 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004214 0u
4215 };
4216 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004217 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004218 }
4219}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004220
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004221Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4222 Register temp) {
4223 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004224 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004225 if (!invoke->GetLocations()->Intrinsified()) {
4226 return location.AsRegister<Register>();
4227 }
4228 // For intrinsics we allow any location, so it may be on the stack.
4229 if (!location.IsRegister()) {
4230 __ movl(temp, Address(ESP, location.GetStackIndex()));
4231 return temp;
4232 }
4233 // For register locations, check if the register was saved. If so, get it from the stack.
4234 // Note: There is a chance that the register was saved but not overwritten, so we could
4235 // save one load. However, since this is just an intrinsic slow path we prefer this
4236 // simple and more robust approach rather that trying to determine if that's the case.
4237 SlowPathCode* slow_path = GetCurrentSlowPath();
4238 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4239 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4240 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4241 __ movl(temp, Address(ESP, stack_offset));
4242 return temp;
4243 }
4244 return location.AsRegister<Register>();
4245}
4246
Vladimir Marko58155012015-08-19 12:49:41 +00004247void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4248 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4249 switch (invoke->GetMethodLoadKind()) {
4250 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4251 // temp = thread->string_init_entrypoint
4252 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4253 break;
4254 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004255 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004256 break;
4257 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4258 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4259 break;
4260 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4261 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4262 method_patches_.emplace_back(invoke->GetTargetMethod());
4263 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4264 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004265 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4266 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4267 temp.AsRegister<Register>());
4268 uint32_t offset = invoke->GetDexCacheArrayOffset();
4269 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4270 // Add the patch entry and bind its label at the end of the instruction.
4271 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4272 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4273 break;
4274 }
Vladimir Marko58155012015-08-19 12:49:41 +00004275 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004276 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004277 Register method_reg;
4278 Register reg = temp.AsRegister<Register>();
4279 if (current_method.IsRegister()) {
4280 method_reg = current_method.AsRegister<Register>();
4281 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004282 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004283 DCHECK(!current_method.IsValid());
4284 method_reg = reg;
4285 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4286 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004287 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004288 __ movl(reg, Address(method_reg,
4289 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004290 // temp = temp[index_in_cache]
4291 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4292 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4293 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004294 }
Vladimir Marko58155012015-08-19 12:49:41 +00004295 }
4296
4297 switch (invoke->GetCodePtrLocation()) {
4298 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4299 __ call(GetFrameEntryLabel());
4300 break;
4301 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4302 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4303 Label* label = &relative_call_patches_.back().label;
4304 __ call(label); // Bind to the patch label, override at link time.
4305 __ Bind(label); // Bind the label at the end of the "call" insn.
4306 break;
4307 }
4308 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4309 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004310 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4311 LOG(FATAL) << "Unsupported";
4312 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004313 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4314 // (callee_method + offset_of_quick_compiled_code)()
4315 __ call(Address(callee_method.AsRegister<Register>(),
4316 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4317 kX86WordSize).Int32Value()));
4318 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004319 }
4320
4321 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004322}
4323
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004324void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4325 Register temp = temp_in.AsRegister<Register>();
4326 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4327 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004328
4329 // Use the calling convention instead of the location of the receiver, as
4330 // intrinsics may have put the receiver in a different register. In the intrinsics
4331 // slow path, the arguments have been moved to the right place, so here we are
4332 // guaranteed that the receiver is the first register of the calling convention.
4333 InvokeDexCallingConvention calling_convention;
4334 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004335 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004336 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004337 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004338 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004339 // Instead of simply (possibly) unpoisoning `temp` here, we should
4340 // emit a read barrier for the previous class reference load.
4341 // However this is not required in practice, as this is an
4342 // intermediate/temporary reference and because the current
4343 // concurrent copying collector keeps the from-space memory
4344 // intact/accessible until the end of the marking phase (the
4345 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004346 __ MaybeUnpoisonHeapReference(temp);
4347 // temp = temp->GetMethodAt(method_offset);
4348 __ movl(temp, Address(temp, method_offset));
4349 // call temp->GetEntryPoint();
4350 __ call(Address(
4351 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4352}
4353
Vladimir Marko58155012015-08-19 12:49:41 +00004354void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4355 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004356 size_t size =
4357 method_patches_.size() +
4358 relative_call_patches_.size() +
4359 pc_relative_dex_cache_patches_.size();
4360 linker_patches->reserve(size);
4361 // The label points to the end of the "movl" insn but the literal offset for method
4362 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4363 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004364 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004365 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004366 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4367 info.target_method.dex_file,
4368 info.target_method.dex_method_index));
4369 }
4370 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004371 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004372 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4373 info.target_method.dex_file,
4374 info.target_method.dex_method_index));
4375 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004376 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4377 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4378 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4379 &info.target_dex_file,
4380 GetMethodAddressOffset(),
4381 info.element_offset));
4382 }
Vladimir Marko58155012015-08-19 12:49:41 +00004383}
4384
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004385void CodeGeneratorX86::MarkGCCard(Register temp,
4386 Register card,
4387 Register object,
4388 Register value,
4389 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004390 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004391 if (value_can_be_null) {
4392 __ testl(value, value);
4393 __ j(kEqual, &is_null);
4394 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004395 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4396 __ movl(temp, object);
4397 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004398 __ movb(Address(temp, card, TIMES_1, 0),
4399 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004400 if (value_can_be_null) {
4401 __ Bind(&is_null);
4402 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004403}
4404
Calin Juravle52c48962014-12-16 17:02:57 +00004405void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4406 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004407
4408 bool object_field_get_with_read_barrier =
4409 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004410 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004411 new (GetGraph()->GetArena()) LocationSummary(instruction,
4412 kEmitCompilerReadBarrier ?
4413 LocationSummary::kCallOnSlowPath :
4414 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004415 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004416
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004417 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4418 locations->SetOut(Location::RequiresFpuRegister());
4419 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004420 // The output overlaps in case of long: we don't want the low move
4421 // to overwrite the object's location. Likewise, in the case of
4422 // an object field get with read barriers enabled, we do not want
4423 // the move to overwrite the object's location, as we need it to emit
4424 // the read barrier.
4425 locations->SetOut(
4426 Location::RequiresRegister(),
4427 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4428 Location::kOutputOverlap :
4429 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004430 }
Calin Juravle52c48962014-12-16 17:02:57 +00004431
4432 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4433 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004434 // So we use an XMM register as a temp to achieve atomicity (first
4435 // load the temp into the XMM and then copy the XMM into the
4436 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004437 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004438 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4439 // We need a temporary register for the read barrier marking slow
4440 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4441 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004442 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004443}
4444
Calin Juravle52c48962014-12-16 17:02:57 +00004445void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4446 const FieldInfo& field_info) {
4447 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004448
Calin Juravle52c48962014-12-16 17:02:57 +00004449 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004450 Location base_loc = locations->InAt(0);
4451 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004452 Location out = locations->Out();
4453 bool is_volatile = field_info.IsVolatile();
4454 Primitive::Type field_type = field_info.GetFieldType();
4455 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4456
4457 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004458 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004459 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004460 break;
4461 }
4462
4463 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004464 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004465 break;
4466 }
4467
4468 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004469 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470 break;
4471 }
4472
4473 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004474 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004475 break;
4476 }
4477
4478 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004479 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004480 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004481
4482 case Primitive::kPrimNot: {
4483 // /* HeapReference<Object> */ out = *(base + offset)
4484 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4485 Location temp_loc = locations->GetTemp(0);
4486 // Note that a potential implicit null check is handled in this
4487 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4488 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4489 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4490 if (is_volatile) {
4491 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4492 }
4493 } else {
4494 __ movl(out.AsRegister<Register>(), Address(base, offset));
4495 codegen_->MaybeRecordImplicitNullCheck(instruction);
4496 if (is_volatile) {
4497 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4498 }
4499 // If read barriers are enabled, emit read barriers other than
4500 // Baker's using a slow path (and also unpoison the loaded
4501 // reference, if heap poisoning is enabled).
4502 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4503 }
4504 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004505 }
4506
4507 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004508 if (is_volatile) {
4509 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4510 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004511 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004512 __ movd(out.AsRegisterPairLow<Register>(), temp);
4513 __ psrlq(temp, Immediate(32));
4514 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4515 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004516 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004517 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004518 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004519 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4520 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004521 break;
4522 }
4523
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004524 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004525 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004526 break;
4527 }
4528
4529 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004530 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004531 break;
4532 }
4533
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004534 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004535 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004536 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004537 }
Calin Juravle52c48962014-12-16 17:02:57 +00004538
Roland Levillain7c1559a2015-12-15 10:55:36 +00004539 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4540 // Potential implicit null checks, in the case of reference or
4541 // long fields, are handled in the previous switch statement.
4542 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004543 codegen_->MaybeRecordImplicitNullCheck(instruction);
4544 }
4545
Calin Juravle52c48962014-12-16 17:02:57 +00004546 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004547 if (field_type == Primitive::kPrimNot) {
4548 // Memory barriers, in the case of references, are also handled
4549 // in the previous switch statement.
4550 } else {
4551 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4552 }
Roland Levillain4d027112015-07-01 15:41:14 +01004553 }
Calin Juravle52c48962014-12-16 17:02:57 +00004554}
4555
4556void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4557 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4558
4559 LocationSummary* locations =
4560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4561 locations->SetInAt(0, Location::RequiresRegister());
4562 bool is_volatile = field_info.IsVolatile();
4563 Primitive::Type field_type = field_info.GetFieldType();
4564 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4565 || (field_type == Primitive::kPrimByte);
4566
4567 // The register allocator does not support multiple
4568 // inputs that die at entry with one in a specific register.
4569 if (is_byte_type) {
4570 // Ensure the value is in a byte register.
4571 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004572 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004573 if (is_volatile && field_type == Primitive::kPrimDouble) {
4574 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4575 locations->SetInAt(1, Location::RequiresFpuRegister());
4576 } else {
4577 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4578 }
4579 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4580 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004581 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004582
Calin Juravle52c48962014-12-16 17:02:57 +00004583 // 64bits value can be atomically written to an address with movsd and an XMM register.
4584 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4585 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4586 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4587 // isolated cases when we need this it isn't worth adding the extra complexity.
4588 locations->AddTemp(Location::RequiresFpuRegister());
4589 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004590 } else {
4591 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4592
4593 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4594 // Temporary registers for the write barrier.
4595 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4596 // Ensure the card is in a byte register.
4597 locations->AddTemp(Location::RegisterLocation(ECX));
4598 }
Calin Juravle52c48962014-12-16 17:02:57 +00004599 }
4600}
4601
4602void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004603 const FieldInfo& field_info,
4604 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004605 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4606
4607 LocationSummary* locations = instruction->GetLocations();
4608 Register base = locations->InAt(0).AsRegister<Register>();
4609 Location value = locations->InAt(1);
4610 bool is_volatile = field_info.IsVolatile();
4611 Primitive::Type field_type = field_info.GetFieldType();
4612 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004613 bool needs_write_barrier =
4614 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004615
4616 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004617 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004618 }
4619
Mark Mendell81489372015-11-04 11:30:41 -05004620 bool maybe_record_implicit_null_check_done = false;
4621
Calin Juravle52c48962014-12-16 17:02:57 +00004622 switch (field_type) {
4623 case Primitive::kPrimBoolean:
4624 case Primitive::kPrimByte: {
4625 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4626 break;
4627 }
4628
4629 case Primitive::kPrimShort:
4630 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004631 if (value.IsConstant()) {
4632 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4633 __ movw(Address(base, offset), Immediate(v));
4634 } else {
4635 __ movw(Address(base, offset), value.AsRegister<Register>());
4636 }
Calin Juravle52c48962014-12-16 17:02:57 +00004637 break;
4638 }
4639
4640 case Primitive::kPrimInt:
4641 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004642 if (kPoisonHeapReferences && needs_write_barrier) {
4643 // Note that in the case where `value` is a null reference,
4644 // we do not enter this block, as the reference does not
4645 // need poisoning.
4646 DCHECK_EQ(field_type, Primitive::kPrimNot);
4647 Register temp = locations->GetTemp(0).AsRegister<Register>();
4648 __ movl(temp, value.AsRegister<Register>());
4649 __ PoisonHeapReference(temp);
4650 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004651 } else if (value.IsConstant()) {
4652 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4653 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004654 } else {
4655 __ movl(Address(base, offset), value.AsRegister<Register>());
4656 }
Calin Juravle52c48962014-12-16 17:02:57 +00004657 break;
4658 }
4659
4660 case Primitive::kPrimLong: {
4661 if (is_volatile) {
4662 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4663 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4664 __ movd(temp1, value.AsRegisterPairLow<Register>());
4665 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4666 __ punpckldq(temp1, temp2);
4667 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004668 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004669 } else if (value.IsConstant()) {
4670 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4671 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4672 codegen_->MaybeRecordImplicitNullCheck(instruction);
4673 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004674 } else {
4675 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004676 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004677 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4678 }
Mark Mendell81489372015-11-04 11:30:41 -05004679 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004680 break;
4681 }
4682
4683 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004684 if (value.IsConstant()) {
4685 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4686 __ movl(Address(base, offset), Immediate(v));
4687 } else {
4688 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4689 }
Calin Juravle52c48962014-12-16 17:02:57 +00004690 break;
4691 }
4692
4693 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004694 if (value.IsConstant()) {
4695 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4696 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4697 codegen_->MaybeRecordImplicitNullCheck(instruction);
4698 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4699 maybe_record_implicit_null_check_done = true;
4700 } else {
4701 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4702 }
Calin Juravle52c48962014-12-16 17:02:57 +00004703 break;
4704 }
4705
4706 case Primitive::kPrimVoid:
4707 LOG(FATAL) << "Unreachable type " << field_type;
4708 UNREACHABLE();
4709 }
4710
Mark Mendell81489372015-11-04 11:30:41 -05004711 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004712 codegen_->MaybeRecordImplicitNullCheck(instruction);
4713 }
4714
Roland Levillain4d027112015-07-01 15:41:14 +01004715 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004716 Register temp = locations->GetTemp(0).AsRegister<Register>();
4717 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004718 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004719 }
4720
Calin Juravle52c48962014-12-16 17:02:57 +00004721 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004722 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004723 }
4724}
4725
4726void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4727 HandleFieldGet(instruction, instruction->GetFieldInfo());
4728}
4729
4730void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4731 HandleFieldGet(instruction, instruction->GetFieldInfo());
4732}
4733
4734void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4735 HandleFieldSet(instruction, instruction->GetFieldInfo());
4736}
4737
4738void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004739 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004740}
4741
4742void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4743 HandleFieldSet(instruction, instruction->GetFieldInfo());
4744}
4745
4746void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004747 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004748}
4749
4750void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4751 HandleFieldGet(instruction, instruction->GetFieldInfo());
4752}
4753
4754void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4755 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004756}
4757
Calin Juravlee460d1d2015-09-29 04:52:17 +01004758void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4759 HUnresolvedInstanceFieldGet* instruction) {
4760 FieldAccessCallingConventionX86 calling_convention;
4761 codegen_->CreateUnresolvedFieldLocationSummary(
4762 instruction, instruction->GetFieldType(), calling_convention);
4763}
4764
4765void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4766 HUnresolvedInstanceFieldGet* instruction) {
4767 FieldAccessCallingConventionX86 calling_convention;
4768 codegen_->GenerateUnresolvedFieldAccess(instruction,
4769 instruction->GetFieldType(),
4770 instruction->GetFieldIndex(),
4771 instruction->GetDexPc(),
4772 calling_convention);
4773}
4774
4775void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4776 HUnresolvedInstanceFieldSet* instruction) {
4777 FieldAccessCallingConventionX86 calling_convention;
4778 codegen_->CreateUnresolvedFieldLocationSummary(
4779 instruction, instruction->GetFieldType(), calling_convention);
4780}
4781
4782void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4783 HUnresolvedInstanceFieldSet* instruction) {
4784 FieldAccessCallingConventionX86 calling_convention;
4785 codegen_->GenerateUnresolvedFieldAccess(instruction,
4786 instruction->GetFieldType(),
4787 instruction->GetFieldIndex(),
4788 instruction->GetDexPc(),
4789 calling_convention);
4790}
4791
4792void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4793 HUnresolvedStaticFieldGet* instruction) {
4794 FieldAccessCallingConventionX86 calling_convention;
4795 codegen_->CreateUnresolvedFieldLocationSummary(
4796 instruction, instruction->GetFieldType(), calling_convention);
4797}
4798
4799void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4800 HUnresolvedStaticFieldGet* instruction) {
4801 FieldAccessCallingConventionX86 calling_convention;
4802 codegen_->GenerateUnresolvedFieldAccess(instruction,
4803 instruction->GetFieldType(),
4804 instruction->GetFieldIndex(),
4805 instruction->GetDexPc(),
4806 calling_convention);
4807}
4808
4809void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4810 HUnresolvedStaticFieldSet* instruction) {
4811 FieldAccessCallingConventionX86 calling_convention;
4812 codegen_->CreateUnresolvedFieldLocationSummary(
4813 instruction, instruction->GetFieldType(), calling_convention);
4814}
4815
4816void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4817 HUnresolvedStaticFieldSet* instruction) {
4818 FieldAccessCallingConventionX86 calling_convention;
4819 codegen_->GenerateUnresolvedFieldAccess(instruction,
4820 instruction->GetFieldType(),
4821 instruction->GetFieldIndex(),
4822 instruction->GetDexPc(),
4823 calling_convention);
4824}
4825
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004827 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4828 ? LocationSummary::kCallOnSlowPath
4829 : LocationSummary::kNoCall;
4830 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4831 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004832 ? Location::RequiresRegister()
4833 : Location::Any();
4834 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004835 if (instruction->HasUses()) {
4836 locations->SetOut(Location::SameAsFirstInput());
4837 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004838}
4839
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004840void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4842 return;
4843 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004844 LocationSummary* locations = instruction->GetLocations();
4845 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004846
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004847 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4848 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4849}
4850
4851void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004852 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004853 codegen_->AddSlowPath(slow_path);
4854
4855 LocationSummary* locations = instruction->GetLocations();
4856 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004857
4858 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004859 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004860 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004861 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004862 } else {
4863 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004864 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004865 __ jmp(slow_path->GetEntryLabel());
4866 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004867 }
4868 __ j(kEqual, slow_path->GetEntryLabel());
4869}
4870
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004871void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004872 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004873 GenerateImplicitNullCheck(instruction);
4874 } else {
4875 GenerateExplicitNullCheck(instruction);
4876 }
4877}
4878
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004880 bool object_array_get_with_read_barrier =
4881 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004882 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004883 new (GetGraph()->GetArena()) LocationSummary(instruction,
4884 object_array_get_with_read_barrier ?
4885 LocationSummary::kCallOnSlowPath :
4886 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004887 locations->SetInAt(0, Location::RequiresRegister());
4888 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004889 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4890 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4891 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004892 // The output overlaps in case of long: we don't want the low move
4893 // to overwrite the array's location. Likewise, in the case of an
4894 // object array get with read barriers enabled, we do not want the
4895 // move to overwrite the array's location, as we need it to emit
4896 // the read barrier.
4897 locations->SetOut(
4898 Location::RequiresRegister(),
4899 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4900 Location::kOutputOverlap :
4901 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004902 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004903 // We need a temporary register for the read barrier marking slow
4904 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4905 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4906 locations->AddTemp(Location::RequiresRegister());
4907 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004908}
4909
4910void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4911 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004912 Location obj_loc = locations->InAt(0);
4913 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004914 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004915 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916
Calin Juravle77520bc2015-01-12 18:45:46 +00004917 Primitive::Type type = instruction->GetType();
4918 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004919 case Primitive::kPrimBoolean: {
4920 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004921 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004922 if (index.IsConstant()) {
4923 __ movzxb(out, Address(obj,
4924 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4925 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004926 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004927 }
4928 break;
4929 }
4930
4931 case Primitive::kPrimByte: {
4932 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004933 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004934 if (index.IsConstant()) {
4935 __ movsxb(out, Address(obj,
4936 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4937 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004938 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004939 }
4940 break;
4941 }
4942
4943 case Primitive::kPrimShort: {
4944 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004945 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004946 if (index.IsConstant()) {
4947 __ movsxw(out, Address(obj,
4948 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4949 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004950 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004951 }
4952 break;
4953 }
4954
4955 case Primitive::kPrimChar: {
4956 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004957 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004958 if (index.IsConstant()) {
4959 __ movzxw(out, Address(obj,
4960 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4961 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004962 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004963 }
4964 break;
4965 }
4966
Roland Levillain7c1559a2015-12-15 10:55:36 +00004967 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004969 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004970 if (index.IsConstant()) {
4971 __ movl(out, Address(obj,
4972 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4973 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004974 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004975 }
4976 break;
4977 }
4978
Roland Levillain7c1559a2015-12-15 10:55:36 +00004979 case Primitive::kPrimNot: {
4980 static_assert(
4981 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4982 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4983 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4984 // /* HeapReference<Object> */ out =
4985 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4986 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4987 Location temp = locations->GetTemp(0);
4988 // Note that a potential implicit null check is handled in this
4989 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4990 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4991 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4992 } else {
4993 Register out = out_loc.AsRegister<Register>();
4994 if (index.IsConstant()) {
4995 uint32_t offset =
4996 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4997 __ movl(out, Address(obj, offset));
4998 codegen_->MaybeRecordImplicitNullCheck(instruction);
4999 // If read barriers are enabled, emit read barriers other than
5000 // Baker's using a slow path (and also unpoison the loaded
5001 // reference, if heap poisoning is enabled).
5002 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5003 } else {
5004 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5005 codegen_->MaybeRecordImplicitNullCheck(instruction);
5006 // If read barriers are enabled, emit read barriers other than
5007 // Baker's using a slow path (and also unpoison the loaded
5008 // reference, if heap poisoning is enabled).
5009 codegen_->MaybeGenerateReadBarrierSlow(
5010 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5011 }
5012 }
5013 break;
5014 }
5015
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005016 case Primitive::kPrimLong: {
5017 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005018 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005019 if (index.IsConstant()) {
5020 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005021 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005022 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005023 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005024 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005025 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005026 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005027 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005028 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005029 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005030 }
5031 break;
5032 }
5033
Mark Mendell7c8d0092015-01-26 11:21:33 -05005034 case Primitive::kPrimFloat: {
5035 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005036 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005037 if (index.IsConstant()) {
5038 __ movss(out, Address(obj,
5039 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5040 } else {
5041 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5042 }
5043 break;
5044 }
5045
5046 case Primitive::kPrimDouble: {
5047 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005048 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005049 if (index.IsConstant()) {
5050 __ movsd(out, Address(obj,
5051 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5052 } else {
5053 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5054 }
5055 break;
5056 }
5057
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005059 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005060 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005062
Roland Levillain7c1559a2015-12-15 10:55:36 +00005063 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5064 // Potential implicit null checks, in the case of reference or
5065 // long arrays, are handled in the previous switch statement.
5066 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005067 codegen_->MaybeRecordImplicitNullCheck(instruction);
5068 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069}
5070
5071void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005072 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005073
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005074 bool needs_write_barrier =
5075 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005076 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5077 bool object_array_set_with_read_barrier =
5078 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005079
Nicolas Geoffray39468442014-09-02 15:17:15 +01005080 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5081 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005082 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5083 LocationSummary::kCallOnSlowPath :
5084 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005085
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005086 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5087 || (value_type == Primitive::kPrimByte);
5088 // We need the inputs to be different than the output in case of long operation.
5089 // In case of a byte operation, the register allocator does not support multiple
5090 // inputs that die at entry with one in a specific register.
5091 locations->SetInAt(0, Location::RequiresRegister());
5092 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5093 if (is_byte_type) {
5094 // Ensure the value is in a byte register.
5095 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5096 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005097 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005099 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5100 }
5101 if (needs_write_barrier) {
5102 // Temporary registers for the write barrier.
5103 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5104 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005105 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005106 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107}
5108
5109void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5110 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005111 Location array_loc = locations->InAt(0);
5112 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005113 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005114 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005115 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005116 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5117 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5118 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005119 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005120 bool needs_write_barrier =
5121 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122
5123 switch (value_type) {
5124 case Primitive::kPrimBoolean:
5125 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005126 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5127 Address address = index.IsConstant()
5128 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5129 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5130 if (value.IsRegister()) {
5131 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005132 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005133 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005135 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 break;
5137 }
5138
5139 case Primitive::kPrimShort:
5140 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005141 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5142 Address address = index.IsConstant()
5143 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5144 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5145 if (value.IsRegister()) {
5146 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005147 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005148 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005150 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151 break;
5152 }
5153
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005154 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005155 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5156 Address address = index.IsConstant()
5157 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5158 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005159
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005160 if (!value.IsRegister()) {
5161 // Just setting null.
5162 DCHECK(instruction->InputAt(2)->IsNullConstant());
5163 DCHECK(value.IsConstant()) << value;
5164 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005165 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005166 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005167 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005168 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005169 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005170
5171 DCHECK(needs_write_barrier);
5172 Register register_value = value.AsRegister<Register>();
5173 NearLabel done, not_null, do_put;
5174 SlowPathCode* slow_path = nullptr;
5175 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005176 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005177 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5178 codegen_->AddSlowPath(slow_path);
5179 if (instruction->GetValueCanBeNull()) {
5180 __ testl(register_value, register_value);
5181 __ j(kNotEqual, &not_null);
5182 __ movl(address, Immediate(0));
5183 codegen_->MaybeRecordImplicitNullCheck(instruction);
5184 __ jmp(&done);
5185 __ Bind(&not_null);
5186 }
5187
Roland Levillain0d5a2812015-11-13 10:07:31 +00005188 if (kEmitCompilerReadBarrier) {
5189 // When read barriers are enabled, the type checking
5190 // instrumentation requires two read barriers:
5191 //
5192 // __ movl(temp2, temp);
5193 // // /* HeapReference<Class> */ temp = temp->component_type_
5194 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005195 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005196 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5197 //
5198 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5199 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005200 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005201 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5202 //
5203 // __ cmpl(temp, temp2);
5204 //
5205 // However, the second read barrier may trash `temp`, as it
5206 // is a temporary register, and as such would not be saved
5207 // along with live registers before calling the runtime (nor
5208 // restored afterwards). So in this case, we bail out and
5209 // delegate the work to the array set slow path.
5210 //
5211 // TODO: Extend the register allocator to support a new
5212 // "(locally) live temp" location so as to avoid always
5213 // going into the slow path when read barriers are enabled.
5214 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005215 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005216 // /* HeapReference<Class> */ temp = array->klass_
5217 __ movl(temp, Address(array, class_offset));
5218 codegen_->MaybeRecordImplicitNullCheck(instruction);
5219 __ MaybeUnpoisonHeapReference(temp);
5220
5221 // /* HeapReference<Class> */ temp = temp->component_type_
5222 __ movl(temp, Address(temp, component_offset));
5223 // If heap poisoning is enabled, no need to unpoison `temp`
5224 // nor the object reference in `register_value->klass`, as
5225 // we are comparing two poisoned references.
5226 __ cmpl(temp, Address(register_value, class_offset));
5227
5228 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5229 __ j(kEqual, &do_put);
5230 // If heap poisoning is enabled, the `temp` reference has
5231 // not been unpoisoned yet; unpoison it now.
5232 __ MaybeUnpoisonHeapReference(temp);
5233
5234 // /* HeapReference<Class> */ temp = temp->super_class_
5235 __ movl(temp, Address(temp, super_offset));
5236 // If heap poisoning is enabled, no need to unpoison
5237 // `temp`, as we are comparing against null below.
5238 __ testl(temp, temp);
5239 __ j(kNotEqual, slow_path->GetEntryLabel());
5240 __ Bind(&do_put);
5241 } else {
5242 __ j(kNotEqual, slow_path->GetEntryLabel());
5243 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005244 }
5245 }
5246
5247 if (kPoisonHeapReferences) {
5248 __ movl(temp, register_value);
5249 __ PoisonHeapReference(temp);
5250 __ movl(address, temp);
5251 } else {
5252 __ movl(address, register_value);
5253 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005254 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005255 codegen_->MaybeRecordImplicitNullCheck(instruction);
5256 }
5257
5258 Register card = locations->GetTemp(1).AsRegister<Register>();
5259 codegen_->MarkGCCard(
5260 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5261 __ Bind(&done);
5262
5263 if (slow_path != nullptr) {
5264 __ Bind(slow_path->GetExitLabel());
5265 }
5266
5267 break;
5268 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005269
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005270 case Primitive::kPrimInt: {
5271 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5272 Address address = index.IsConstant()
5273 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5274 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5275 if (value.IsRegister()) {
5276 __ movl(address, value.AsRegister<Register>());
5277 } else {
5278 DCHECK(value.IsConstant()) << value;
5279 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5280 __ movl(address, Immediate(v));
5281 }
5282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 break;
5284 }
5285
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 case Primitive::kPrimLong: {
5287 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 if (index.IsConstant()) {
5289 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005290 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005291 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005294 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005295 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005296 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005299 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005300 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005301 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005302 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005304 value.AsRegisterPairLow<Register>());
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 Geoffray56b9ee62014-10-09 11:47:51 +01005307 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005308 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005309 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005310 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005311 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005312 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005313 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005315 Immediate(High32Bits(val)));
5316 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 }
5318 break;
5319 }
5320
Mark Mendell7c8d0092015-01-26 11:21:33 -05005321 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005322 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5323 Address address = index.IsConstant()
5324 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5325 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005326 if (value.IsFpuRegister()) {
5327 __ movss(address, value.AsFpuRegister<XmmRegister>());
5328 } else {
5329 DCHECK(value.IsConstant());
5330 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5331 __ movl(address, Immediate(v));
5332 }
5333 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005334 break;
5335 }
5336
5337 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5339 Address address = index.IsConstant()
5340 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5341 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005342 if (value.IsFpuRegister()) {
5343 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5344 } else {
5345 DCHECK(value.IsConstant());
5346 Address address_hi = index.IsConstant() ?
5347 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5348 offset + kX86WordSize) :
5349 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5350 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5351 __ movl(address, Immediate(Low32Bits(v)));
5352 codegen_->MaybeRecordImplicitNullCheck(instruction);
5353 __ movl(address_hi, Immediate(High32Bits(v)));
5354 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005355 break;
5356 }
5357
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005358 case Primitive::kPrimVoid:
5359 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005360 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005361 }
5362}
5363
5364void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5365 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005366 locations->SetInAt(0, Location::RequiresRegister());
5367 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005368}
5369
5370void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5371 LocationSummary* locations = instruction->GetLocations();
5372 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005373 Register obj = locations->InAt(0).AsRegister<Register>();
5374 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005375 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005376 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377}
5378
5379void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005380 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5381 ? LocationSummary::kCallOnSlowPath
5382 : LocationSummary::kNoCall;
5383 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005384 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005385 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005386 if (instruction->HasUses()) {
5387 locations->SetOut(Location::SameAsFirstInput());
5388 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005389}
5390
5391void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5392 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005393 Location index_loc = locations->InAt(0);
5394 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005395 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005396 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005397
Mark Mendell99dbd682015-04-22 16:18:52 -04005398 if (length_loc.IsConstant()) {
5399 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5400 if (index_loc.IsConstant()) {
5401 // BCE will remove the bounds check if we are guarenteed to pass.
5402 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5403 if (index < 0 || index >= length) {
5404 codegen_->AddSlowPath(slow_path);
5405 __ jmp(slow_path->GetEntryLabel());
5406 } else {
5407 // Some optimization after BCE may have generated this, and we should not
5408 // generate a bounds check if it is a valid range.
5409 }
5410 return;
5411 }
5412
5413 // We have to reverse the jump condition because the length is the constant.
5414 Register index_reg = index_loc.AsRegister<Register>();
5415 __ cmpl(index_reg, Immediate(length));
5416 codegen_->AddSlowPath(slow_path);
5417 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005418 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005419 Register length = length_loc.AsRegister<Register>();
5420 if (index_loc.IsConstant()) {
5421 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5422 __ cmpl(length, Immediate(value));
5423 } else {
5424 __ cmpl(length, index_loc.AsRegister<Register>());
5425 }
5426 codegen_->AddSlowPath(slow_path);
5427 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005428 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005429}
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>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005516 } else if (destination.IsFpuRegister()) {
5517 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005518 } else {
5519 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005520 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005521 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005522 } else if (source.IsRegisterPair()) {
5523 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5524 // Create stack space for 2 elements.
5525 __ subl(ESP, Immediate(2 * elem_size));
5526 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5527 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5528 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5529 // And remove the temporary stack space we allocated.
5530 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005531 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005532 if (destination.IsRegister()) {
5533 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5534 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005535 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005536 } else if (destination.IsRegisterPair()) {
5537 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5538 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5539 __ psrlq(src_reg, Immediate(32));
5540 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005541 } else if (destination.IsStackSlot()) {
5542 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5543 } else {
5544 DCHECK(destination.IsDoubleStackSlot());
5545 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5546 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005547 } else if (source.IsStackSlot()) {
5548 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005549 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005550 } else if (destination.IsFpuRegister()) {
5551 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005552 } else {
5553 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005554 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5555 }
5556 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005557 if (destination.IsRegisterPair()) {
5558 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5559 __ movl(destination.AsRegisterPairHigh<Register>(),
5560 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5561 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005562 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5563 } else {
5564 DCHECK(destination.IsDoubleStackSlot()) << destination;
5565 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005566 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005567 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005568 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005569 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005570 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005571 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005572 if (value == 0) {
5573 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5574 } else {
5575 __ movl(destination.AsRegister<Register>(), Immediate(value));
5576 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005577 } else {
5578 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005579 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005580 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005581 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005582 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005583 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005584 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005585 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005586 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5587 if (value == 0) {
5588 // Easy handling of 0.0.
5589 __ xorps(dest, dest);
5590 } else {
5591 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005592 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5593 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5594 __ movl(temp, Immediate(value));
5595 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005596 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005597 } else {
5598 DCHECK(destination.IsStackSlot()) << destination;
5599 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5600 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005601 } else if (constant->IsLongConstant()) {
5602 int64_t value = constant->AsLongConstant()->GetValue();
5603 int32_t low_value = Low32Bits(value);
5604 int32_t high_value = High32Bits(value);
5605 Immediate low(low_value);
5606 Immediate high(high_value);
5607 if (destination.IsDoubleStackSlot()) {
5608 __ movl(Address(ESP, destination.GetStackIndex()), low);
5609 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5610 } else {
5611 __ movl(destination.AsRegisterPairLow<Register>(), low);
5612 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5613 }
5614 } else {
5615 DCHECK(constant->IsDoubleConstant());
5616 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005617 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005618 int32_t low_value = Low32Bits(value);
5619 int32_t high_value = High32Bits(value);
5620 Immediate low(low_value);
5621 Immediate high(high_value);
5622 if (destination.IsFpuRegister()) {
5623 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5624 if (value == 0) {
5625 // Easy handling of 0.0.
5626 __ xorpd(dest, dest);
5627 } else {
5628 __ pushl(high);
5629 __ pushl(low);
5630 __ movsd(dest, Address(ESP, 0));
5631 __ addl(ESP, Immediate(8));
5632 }
5633 } else {
5634 DCHECK(destination.IsDoubleStackSlot()) << destination;
5635 __ movl(Address(ESP, destination.GetStackIndex()), low);
5636 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5637 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005638 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005639 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005640 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005641 }
5642}
5643
Mark Mendella5c19ce2015-04-01 12:51:05 -04005644void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005645 Register suggested_scratch = reg == EAX ? EBX : EAX;
5646 ScratchRegisterScope ensure_scratch(
5647 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5648
5649 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5650 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5651 __ movl(Address(ESP, mem + stack_offset), reg);
5652 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005653}
5654
Mark Mendell7c8d0092015-01-26 11:21:33 -05005655void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005656 ScratchRegisterScope ensure_scratch(
5657 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5658
5659 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5660 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5661 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5662 __ movss(Address(ESP, mem + stack_offset), reg);
5663 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005664}
5665
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005666void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005667 ScratchRegisterScope ensure_scratch1(
5668 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005669
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005670 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5671 ScratchRegisterScope ensure_scratch2(
5672 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005673
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005674 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5675 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5676 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5677 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5678 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5679 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005680}
5681
5682void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005683 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005684 Location source = move->GetSource();
5685 Location destination = move->GetDestination();
5686
5687 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005688 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5689 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5690 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5691 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5692 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005693 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005694 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005695 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005696 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005697 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5698 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005699 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5700 // Use XOR Swap algorithm to avoid a temporary.
5701 DCHECK_NE(source.reg(), destination.reg());
5702 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5703 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5704 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5705 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5706 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5707 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5708 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005709 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5710 // Take advantage of the 16 bytes in the XMM register.
5711 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5712 Address stack(ESP, destination.GetStackIndex());
5713 // Load the double into the high doubleword.
5714 __ movhpd(reg, stack);
5715
5716 // Store the low double into the destination.
5717 __ movsd(stack, reg);
5718
5719 // Move the high double to the low double.
5720 __ psrldq(reg, Immediate(8));
5721 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5722 // Take advantage of the 16 bytes in the XMM register.
5723 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5724 Address stack(ESP, source.GetStackIndex());
5725 // Load the double into the high doubleword.
5726 __ movhpd(reg, stack);
5727
5728 // Store the low double into the destination.
5729 __ movsd(stack, reg);
5730
5731 // Move the high double to the low double.
5732 __ psrldq(reg, Immediate(8));
5733 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5734 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5735 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005736 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005737 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005738 }
5739}
5740
5741void ParallelMoveResolverX86::SpillScratch(int reg) {
5742 __ pushl(static_cast<Register>(reg));
5743}
5744
5745void ParallelMoveResolverX86::RestoreScratch(int reg) {
5746 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005747}
5748
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005749void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005750 InvokeRuntimeCallingConvention calling_convention;
5751 CodeGenerator::CreateLoadClassLocationSummary(
5752 cls,
5753 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005754 Location::RegisterLocation(EAX),
5755 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005756}
5757
5758void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005759 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005760 if (cls->NeedsAccessCheck()) {
5761 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5762 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5763 cls,
5764 cls->GetDexPc(),
5765 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005766 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005767 return;
5768 }
5769
Roland Levillain0d5a2812015-11-13 10:07:31 +00005770 Location out_loc = locations->Out();
5771 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005772 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005773
Calin Juravle580b6092015-10-06 17:35:58 +01005774 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005775 DCHECK(!cls->CanCallRuntime());
5776 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005777 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5778 GenerateGcRootFieldLoad(
5779 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005780 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005781 // /* GcRoot<mirror::Class>[] */ out =
5782 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5783 __ movl(out, Address(current_method,
5784 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005785 // /* GcRoot<mirror::Class> */ out = out[type_index]
5786 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005787
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005788 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5789 DCHECK(cls->CanCallRuntime());
5790 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5791 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5792 codegen_->AddSlowPath(slow_path);
5793
5794 if (!cls->IsInDexCache()) {
5795 __ testl(out, out);
5796 __ j(kEqual, slow_path->GetEntryLabel());
5797 }
5798
5799 if (cls->MustGenerateClinitCheck()) {
5800 GenerateClassInitializationCheck(slow_path, out);
5801 } else {
5802 __ Bind(slow_path->GetExitLabel());
5803 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005804 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005805 }
5806}
5807
5808void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5809 LocationSummary* locations =
5810 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5811 locations->SetInAt(0, Location::RequiresRegister());
5812 if (check->HasUses()) {
5813 locations->SetOut(Location::SameAsFirstInput());
5814 }
5815}
5816
5817void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005818 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005819 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005820 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005821 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005822 GenerateClassInitializationCheck(slow_path,
5823 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005824}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005825
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005826void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005827 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005828 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5829 Immediate(mirror::Class::kStatusInitialized));
5830 __ j(kLess, slow_path->GetEntryLabel());
5831 __ Bind(slow_path->GetExitLabel());
5832 // No need for memory fence, thanks to the X86 memory model.
5833}
5834
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005835void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005836 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5837 ? LocationSummary::kCallOnSlowPath
5838 : LocationSummary::kNoCall;
5839 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005840 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005841 locations->SetOut(Location::RequiresRegister());
5842}
5843
5844void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005845 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005846 Location out_loc = locations->Out();
5847 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005848 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005849
Roland Levillain7c1559a2015-12-15 10:55:36 +00005850 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5851 GenerateGcRootFieldLoad(
5852 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005853 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005854 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005855 // /* GcRoot<mirror::String> */ out = out[string_index]
5856 GenerateGcRootFieldLoad(
5857 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005858
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005859 if (!load->IsInDexCache()) {
5860 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5861 codegen_->AddSlowPath(slow_path);
5862 __ testl(out, out);
5863 __ j(kEqual, slow_path->GetEntryLabel());
5864 __ Bind(slow_path->GetExitLabel());
5865 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005866}
5867
David Brazdilcb1c0552015-08-04 16:22:25 +01005868static Address GetExceptionTlsAddress() {
5869 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5870}
5871
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005872void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5873 LocationSummary* locations =
5874 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5875 locations->SetOut(Location::RequiresRegister());
5876}
5877
5878void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005879 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5880}
5881
5882void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5883 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5884}
5885
5886void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5887 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005888}
5889
5890void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5891 LocationSummary* locations =
5892 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5893 InvokeRuntimeCallingConvention calling_convention;
5894 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5895}
5896
5897void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005898 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5899 instruction,
5900 instruction->GetDexPc(),
5901 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005902 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005903}
5904
Roland Levillain7c1559a2015-12-15 10:55:36 +00005905static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5906 return kEmitCompilerReadBarrier &&
5907 (kUseBakerReadBarrier ||
5908 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5909 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5910 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5911}
5912
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005913void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005914 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005915 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5916 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005917 case TypeCheckKind::kExactCheck:
5918 case TypeCheckKind::kAbstractClassCheck:
5919 case TypeCheckKind::kClassHierarchyCheck:
5920 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005921 call_kind =
5922 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005923 break;
5924 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005925 case TypeCheckKind::kUnresolvedCheck:
5926 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005927 call_kind = LocationSummary::kCallOnSlowPath;
5928 break;
5929 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005930
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005931 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005932 locations->SetInAt(0, Location::RequiresRegister());
5933 locations->SetInAt(1, Location::Any());
5934 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5935 locations->SetOut(Location::RequiresRegister());
5936 // When read barriers are enabled, we need a temporary register for
5937 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005938 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005939 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005940 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005941}
5942
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005943void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005944 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005945 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005946 Location obj_loc = locations->InAt(0);
5947 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005948 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005949 Location out_loc = locations->Out();
5950 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005951 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00005952 locations->GetTemp(0) :
5953 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005954 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005955 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5956 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5957 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005958 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005959 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005960
5961 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005962 // Avoid null check if we know obj is not null.
5963 if (instruction->MustDoNullCheck()) {
5964 __ testl(obj, obj);
5965 __ j(kEqual, &zero);
5966 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005967
Roland Levillain0d5a2812015-11-13 10:07:31 +00005968 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005969 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005970
Roland Levillain7c1559a2015-12-15 10:55:36 +00005971 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005972 case TypeCheckKind::kExactCheck: {
5973 if (cls.IsRegister()) {
5974 __ cmpl(out, cls.AsRegister<Register>());
5975 } else {
5976 DCHECK(cls.IsStackSlot()) << cls;
5977 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5978 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005979
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005980 // Classes must be equal for the instanceof to succeed.
5981 __ j(kNotEqual, &zero);
5982 __ movl(out, Immediate(1));
5983 __ jmp(&done);
5984 break;
5985 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 case TypeCheckKind::kAbstractClassCheck: {
5988 // If the class is abstract, we eagerly fetch the super class of the
5989 // object to avoid doing a comparison we know will fail.
5990 NearLabel loop;
5991 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005992 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005993 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005994 __ testl(out, out);
5995 // If `out` is null, we use it for the result, and jump to `done`.
5996 __ j(kEqual, &done);
5997 if (cls.IsRegister()) {
5998 __ cmpl(out, cls.AsRegister<Register>());
5999 } else {
6000 DCHECK(cls.IsStackSlot()) << cls;
6001 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6002 }
6003 __ j(kNotEqual, &loop);
6004 __ movl(out, Immediate(1));
6005 if (zero.IsLinked()) {
6006 __ jmp(&done);
6007 }
6008 break;
6009 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 case TypeCheckKind::kClassHierarchyCheck: {
6012 // Walk over the class hierarchy to find a match.
6013 NearLabel loop, success;
6014 __ Bind(&loop);
6015 if (cls.IsRegister()) {
6016 __ cmpl(out, cls.AsRegister<Register>());
6017 } else {
6018 DCHECK(cls.IsStackSlot()) << cls;
6019 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6020 }
6021 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006022 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006023 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006024 __ testl(out, out);
6025 __ j(kNotEqual, &loop);
6026 // If `out` is null, we use it for the result, and jump to `done`.
6027 __ jmp(&done);
6028 __ Bind(&success);
6029 __ movl(out, Immediate(1));
6030 if (zero.IsLinked()) {
6031 __ jmp(&done);
6032 }
6033 break;
6034 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006035
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006036 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006037 // Do an exact check.
6038 NearLabel exact_check;
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 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006046 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006047 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006048 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006049 __ testl(out, out);
6050 // If `out` is null, we use it for the result, and jump to `done`.
6051 __ j(kEqual, &done);
6052 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6053 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006054 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006055 __ movl(out, Immediate(1));
6056 __ jmp(&done);
6057 break;
6058 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006059
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006060 case TypeCheckKind::kArrayCheck: {
6061 if (cls.IsRegister()) {
6062 __ cmpl(out, cls.AsRegister<Register>());
6063 } else {
6064 DCHECK(cls.IsStackSlot()) << cls;
6065 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6066 }
6067 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006068 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6069 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006070 codegen_->AddSlowPath(slow_path);
6071 __ j(kNotEqual, slow_path->GetEntryLabel());
6072 __ movl(out, Immediate(1));
6073 if (zero.IsLinked()) {
6074 __ jmp(&done);
6075 }
6076 break;
6077 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006078
Calin Juravle98893e12015-10-02 21:05:03 +01006079 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006080 case TypeCheckKind::kInterfaceCheck: {
6081 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006082 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006083 // cases.
6084 //
6085 // We cannot directly call the InstanceofNonTrivial runtime
6086 // entry point without resorting to a type checking slow path
6087 // here (i.e. by calling InvokeRuntime directly), as it would
6088 // require to assign fixed registers for the inputs of this
6089 // HInstanceOf instruction (following the runtime calling
6090 // convention), which might be cluttered by the potential first
6091 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006092 //
6093 // TODO: Introduce a new runtime entry point taking the object
6094 // to test (instead of its class) as argument, and let it deal
6095 // with the read barrier issues. This will let us refactor this
6096 // case of the `switch` code as it was previously (with a direct
6097 // call to the runtime not using a type checking slow path).
6098 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006099 DCHECK(locations->OnlyCallsOnSlowPath());
6100 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6101 /* is_fatal */ false);
6102 codegen_->AddSlowPath(slow_path);
6103 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 if (zero.IsLinked()) {
6105 __ jmp(&done);
6106 }
6107 break;
6108 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006109 }
6110
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006111 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006112 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006113 __ xorl(out, out);
6114 }
6115
6116 if (done.IsLinked()) {
6117 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006118 }
6119
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006120 if (slow_path != nullptr) {
6121 __ Bind(slow_path->GetExitLabel());
6122 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006123}
6124
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006125void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6127 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006128 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6129 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006130 case TypeCheckKind::kExactCheck:
6131 case TypeCheckKind::kAbstractClassCheck:
6132 case TypeCheckKind::kClassHierarchyCheck:
6133 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6135 LocationSummary::kCallOnSlowPath :
6136 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006137 break;
6138 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006139 case TypeCheckKind::kUnresolvedCheck:
6140 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006141 call_kind = LocationSummary::kCallOnSlowPath;
6142 break;
6143 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6145 locations->SetInAt(0, Location::RequiresRegister());
6146 locations->SetInAt(1, Location::Any());
6147 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6148 locations->AddTemp(Location::RequiresRegister());
6149 // When read barriers are enabled, we need an additional temporary
6150 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006151 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006153 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006154}
6155
6156void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006157 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006158 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159 Location obj_loc = locations->InAt(0);
6160 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006161 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006162 Location temp_loc = locations->GetTemp(0);
6163 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006164 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006165 locations->GetTemp(1) :
6166 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6168 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6169 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6170 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171
Roland Levillain0d5a2812015-11-13 10:07:31 +00006172 bool is_type_check_slow_path_fatal =
6173 (type_check_kind == TypeCheckKind::kExactCheck ||
6174 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6175 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6176 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6177 !instruction->CanThrowIntoCatchBlock();
6178 SlowPathCode* type_check_slow_path =
6179 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6180 is_type_check_slow_path_fatal);
6181 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006184 // Avoid null check if we know obj is not null.
6185 if (instruction->MustDoNullCheck()) {
6186 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006187 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006188 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189
Roland Levillain0d5a2812015-11-13 10:07:31 +00006190 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006191 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192
Roland Levillain0d5a2812015-11-13 10:07:31 +00006193 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006194 case TypeCheckKind::kExactCheck:
6195 case TypeCheckKind::kArrayCheck: {
6196 if (cls.IsRegister()) {
6197 __ cmpl(temp, cls.AsRegister<Register>());
6198 } else {
6199 DCHECK(cls.IsStackSlot()) << cls;
6200 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6201 }
6202 // Jump to slow path for throwing the exception or doing a
6203 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006205 break;
6206 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006208 case TypeCheckKind::kAbstractClassCheck: {
6209 // If the class is abstract, we eagerly fetch the super class of the
6210 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006212 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006213 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006214 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006215
6216 // If the class reference currently in `temp` is not null, jump
6217 // to the `compare_classes` label to compare it with the checked
6218 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006219 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220 __ j(kNotEqual, &compare_classes);
6221 // Otherwise, jump to the slow path to throw the exception.
6222 //
6223 // But before, move back the object's class into `temp` before
6224 // going into the slow path, as it has been overwritten in the
6225 // meantime.
6226 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006227 GenerateReferenceLoadTwoRegisters(
6228 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229 __ jmp(type_check_slow_path->GetEntryLabel());
6230
6231 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006232 if (cls.IsRegister()) {
6233 __ cmpl(temp, cls.AsRegister<Register>());
6234 } else {
6235 DCHECK(cls.IsStackSlot()) << cls;
6236 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6237 }
6238 __ j(kNotEqual, &loop);
6239 break;
6240 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006241
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006242 case TypeCheckKind::kClassHierarchyCheck: {
6243 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006244 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006245 __ Bind(&loop);
6246 if (cls.IsRegister()) {
6247 __ cmpl(temp, cls.AsRegister<Register>());
6248 } else {
6249 DCHECK(cls.IsStackSlot()) << cls;
6250 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6251 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006252 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253
Roland Levillain0d5a2812015-11-13 10:07:31 +00006254 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006255 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006256
6257 // If the class reference currently in `temp` is not null, jump
6258 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006259 __ testl(temp, temp);
6260 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261 // Otherwise, jump to the slow path to throw the exception.
6262 //
6263 // But before, move back the object's class into `temp` before
6264 // going into the slow path, as it has been overwritten in the
6265 // meantime.
6266 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006267 GenerateReferenceLoadTwoRegisters(
6268 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006269 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 break;
6271 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006272
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006273 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006274 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006276 if (cls.IsRegister()) {
6277 __ cmpl(temp, cls.AsRegister<Register>());
6278 } else {
6279 DCHECK(cls.IsStackSlot()) << cls;
6280 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6281 }
6282 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283
6284 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006286 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287
6288 // If the component type is not null (i.e. the object is indeed
6289 // an array), jump to label `check_non_primitive_component_type`
6290 // to further check that this component type is not a primitive
6291 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006292 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006293 __ j(kNotEqual, &check_non_primitive_component_type);
6294 // Otherwise, jump to the slow path to throw the exception.
6295 //
6296 // But before, move back the object's class into `temp` before
6297 // going into the slow path, as it has been overwritten in the
6298 // meantime.
6299 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006300 GenerateReferenceLoadTwoRegisters(
6301 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 __ jmp(type_check_slow_path->GetEntryLabel());
6303
6304 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006305 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006306 __ j(kEqual, &done);
6307 // Same comment as above regarding `temp` and the slow path.
6308 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006309 GenerateReferenceLoadTwoRegisters(
6310 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006312 break;
6313 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006314
Calin Juravle98893e12015-10-02 21:05:03 +01006315 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006317 // We always go into the type check slow path for the unresolved
6318 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006319 //
6320 // We cannot directly call the CheckCast runtime entry point
6321 // without resorting to a type checking slow path here (i.e. by
6322 // calling InvokeRuntime directly), as it would require to
6323 // assign fixed registers for the inputs of this HInstanceOf
6324 // instruction (following the runtime calling convention), which
6325 // might be cluttered by the potential first read barrier
6326 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006327 //
6328 // TODO: Introduce a new runtime entry point taking the object
6329 // to test (instead of its class) as argument, and let it deal
6330 // with the read barrier issues. This will let us refactor this
6331 // case of the `switch` code as it was previously (with a direct
6332 // call to the runtime not using a type checking slow path).
6333 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006334 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006335 break;
6336 }
6337 __ Bind(&done);
6338
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006340}
6341
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006342void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6343 LocationSummary* locations =
6344 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6345 InvokeRuntimeCallingConvention calling_convention;
6346 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6347}
6348
6349void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006350 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6351 : QUICK_ENTRY_POINT(pUnlockObject),
6352 instruction,
6353 instruction->GetDexPc(),
6354 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006355 if (instruction->IsEnter()) {
6356 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6357 } else {
6358 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6359 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006360}
6361
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006362void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6363void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6364void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6365
6366void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6367 LocationSummary* locations =
6368 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6369 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6370 || instruction->GetResultType() == Primitive::kPrimLong);
6371 locations->SetInAt(0, Location::RequiresRegister());
6372 locations->SetInAt(1, Location::Any());
6373 locations->SetOut(Location::SameAsFirstInput());
6374}
6375
6376void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6377 HandleBitwiseOperation(instruction);
6378}
6379
6380void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6381 HandleBitwiseOperation(instruction);
6382}
6383
6384void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6385 HandleBitwiseOperation(instruction);
6386}
6387
6388void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6389 LocationSummary* locations = instruction->GetLocations();
6390 Location first = locations->InAt(0);
6391 Location second = locations->InAt(1);
6392 DCHECK(first.Equals(locations->Out()));
6393
6394 if (instruction->GetResultType() == Primitive::kPrimInt) {
6395 if (second.IsRegister()) {
6396 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006397 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006398 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006399 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006400 } else {
6401 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006402 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006403 }
6404 } else if (second.IsConstant()) {
6405 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006406 __ andl(first.AsRegister<Register>(),
6407 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006408 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006409 __ orl(first.AsRegister<Register>(),
6410 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006411 } else {
6412 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006413 __ xorl(first.AsRegister<Register>(),
6414 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006415 }
6416 } else {
6417 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006418 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006419 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006420 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006421 } else {
6422 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006423 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006424 }
6425 }
6426 } else {
6427 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6428 if (second.IsRegisterPair()) {
6429 if (instruction->IsAnd()) {
6430 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6431 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6432 } else if (instruction->IsOr()) {
6433 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6434 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6435 } else {
6436 DCHECK(instruction->IsXor());
6437 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6438 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6439 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006440 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006441 if (instruction->IsAnd()) {
6442 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6443 __ andl(first.AsRegisterPairHigh<Register>(),
6444 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6445 } else if (instruction->IsOr()) {
6446 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6447 __ orl(first.AsRegisterPairHigh<Register>(),
6448 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6449 } else {
6450 DCHECK(instruction->IsXor());
6451 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6452 __ xorl(first.AsRegisterPairHigh<Register>(),
6453 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6454 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006455 } else {
6456 DCHECK(second.IsConstant()) << second;
6457 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006458 int32_t low_value = Low32Bits(value);
6459 int32_t high_value = High32Bits(value);
6460 Immediate low(low_value);
6461 Immediate high(high_value);
6462 Register first_low = first.AsRegisterPairLow<Register>();
6463 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006464 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006465 if (low_value == 0) {
6466 __ xorl(first_low, first_low);
6467 } else if (low_value != -1) {
6468 __ andl(first_low, low);
6469 }
6470 if (high_value == 0) {
6471 __ xorl(first_high, first_high);
6472 } else if (high_value != -1) {
6473 __ andl(first_high, high);
6474 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006475 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006476 if (low_value != 0) {
6477 __ orl(first_low, low);
6478 }
6479 if (high_value != 0) {
6480 __ orl(first_high, high);
6481 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006482 } else {
6483 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006484 if (low_value != 0) {
6485 __ xorl(first_low, low);
6486 }
6487 if (high_value != 0) {
6488 __ xorl(first_high, high);
6489 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006490 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006491 }
6492 }
6493}
6494
Roland Levillain7c1559a2015-12-15 10:55:36 +00006495void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6496 Location out,
6497 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006498 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006499 Register out_reg = out.AsRegister<Register>();
6500 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006501 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006502 if (kUseBakerReadBarrier) {
6503 // Load with fast path based Baker's read barrier.
6504 // /* HeapReference<Object> */ out = *(out + offset)
6505 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006506 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006507 } else {
6508 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006509 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006510 // in the following move operation, as we will need it for the
6511 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006512 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006513 // /* HeapReference<Object> */ out = *(out + offset)
6514 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006515 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006516 }
6517 } else {
6518 // Plain load with no read barrier.
6519 // /* HeapReference<Object> */ out = *(out + offset)
6520 __ movl(out_reg, Address(out_reg, offset));
6521 __ MaybeUnpoisonHeapReference(out_reg);
6522 }
6523}
6524
6525void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6526 Location out,
6527 Location obj,
6528 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006529 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006530 Register out_reg = out.AsRegister<Register>();
6531 Register obj_reg = obj.AsRegister<Register>();
6532 if (kEmitCompilerReadBarrier) {
6533 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006534 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006535 // Load with fast path based Baker's read barrier.
6536 // /* HeapReference<Object> */ out = *(obj + offset)
6537 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006538 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006539 } else {
6540 // Load with slow path based read barrier.
6541 // /* HeapReference<Object> */ out = *(obj + offset)
6542 __ movl(out_reg, Address(obj_reg, offset));
6543 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6544 }
6545 } else {
6546 // Plain load with no read barrier.
6547 // /* HeapReference<Object> */ out = *(obj + offset)
6548 __ movl(out_reg, Address(obj_reg, offset));
6549 __ MaybeUnpoisonHeapReference(out_reg);
6550 }
6551}
6552
6553void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6554 Location root,
6555 Register obj,
6556 uint32_t offset) {
6557 Register root_reg = root.AsRegister<Register>();
6558 if (kEmitCompilerReadBarrier) {
6559 if (kUseBakerReadBarrier) {
6560 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6561 // Baker's read barrier are used:
6562 //
6563 // root = obj.field;
6564 // if (Thread::Current()->GetIsGcMarking()) {
6565 // root = ReadBarrier::Mark(root)
6566 // }
6567
6568 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6569 __ movl(root_reg, Address(obj, offset));
6570 static_assert(
6571 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6572 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6573 "have different sizes.");
6574 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6575 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6576 "have different sizes.");
6577
6578 // Slow path used to mark the GC root `root`.
6579 SlowPathCode* slow_path =
6580 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6581 codegen_->AddSlowPath(slow_path);
6582
6583 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6584 Immediate(0));
6585 __ j(kNotEqual, slow_path->GetEntryLabel());
6586 __ Bind(slow_path->GetExitLabel());
6587 } else {
6588 // GC root loaded through a slow path for read barriers other
6589 // than Baker's.
6590 // /* GcRoot<mirror::Object>* */ root = obj + offset
6591 __ leal(root_reg, Address(obj, offset));
6592 // /* mirror::Object* */ root = root->Read()
6593 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6594 }
6595 } else {
6596 // Plain GC root load with no read barrier.
6597 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6598 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006599 // Note that GC roots are not affected by heap poisoning, thus we
6600 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006601 }
6602}
6603
6604void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6605 Location ref,
6606 Register obj,
6607 uint32_t offset,
6608 Location temp,
6609 bool needs_null_check) {
6610 DCHECK(kEmitCompilerReadBarrier);
6611 DCHECK(kUseBakerReadBarrier);
6612
6613 // /* HeapReference<Object> */ ref = *(obj + offset)
6614 Address src(obj, offset);
6615 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6616}
6617
6618void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6619 Location ref,
6620 Register obj,
6621 uint32_t data_offset,
6622 Location index,
6623 Location temp,
6624 bool needs_null_check) {
6625 DCHECK(kEmitCompilerReadBarrier);
6626 DCHECK(kUseBakerReadBarrier);
6627
6628 // /* HeapReference<Object> */ ref =
6629 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6630 Address src = index.IsConstant() ?
6631 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6632 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6633 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6634}
6635
6636void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6637 Location ref,
6638 Register obj,
6639 const Address& src,
6640 Location temp,
6641 bool needs_null_check) {
6642 DCHECK(kEmitCompilerReadBarrier);
6643 DCHECK(kUseBakerReadBarrier);
6644
6645 // In slow path based read barriers, the read barrier call is
6646 // inserted after the original load. However, in fast path based
6647 // Baker's read barriers, we need to perform the load of
6648 // mirror::Object::monitor_ *before* the original reference load.
6649 // This load-load ordering is required by the read barrier.
6650 // The fast path/slow path (for Baker's algorithm) should look like:
6651 //
6652 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6653 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6654 // HeapReference<Object> ref = *src; // Original reference load.
6655 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6656 // if (is_gray) {
6657 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6658 // }
6659 //
6660 // Note: the original implementation in ReadBarrier::Barrier is
6661 // slightly more complex as:
6662 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006663 // the high-bits of rb_state, which are expected to be all zeroes
6664 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6665 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006666 // - it performs additional checks that we do not do here for
6667 // performance reasons.
6668
6669 Register ref_reg = ref.AsRegister<Register>();
6670 Register temp_reg = temp.AsRegister<Register>();
6671 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6672
6673 // /* int32_t */ monitor = obj->monitor_
6674 __ movl(temp_reg, Address(obj, monitor_offset));
6675 if (needs_null_check) {
6676 MaybeRecordImplicitNullCheck(instruction);
6677 }
6678 // /* LockWord */ lock_word = LockWord(monitor)
6679 static_assert(sizeof(LockWord) == sizeof(int32_t),
6680 "art::LockWord and int32_t have different sizes.");
6681 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6682 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6683 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6684 static_assert(
6685 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6686 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6687
6688 // Load fence to prevent load-load reordering.
6689 // Note that this is a no-op, thanks to the x86 memory model.
6690 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6691
6692 // The actual reference load.
6693 // /* HeapReference<Object> */ ref = *src
6694 __ movl(ref_reg, src);
6695
6696 // Object* ref = ref_addr->AsMirrorPtr()
6697 __ MaybeUnpoisonHeapReference(ref_reg);
6698
6699 // Slow path used to mark the object `ref` when it is gray.
6700 SlowPathCode* slow_path =
6701 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6702 AddSlowPath(slow_path);
6703
6704 // if (rb_state == ReadBarrier::gray_ptr_)
6705 // ref = ReadBarrier::Mark(ref);
6706 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6707 __ j(kEqual, slow_path->GetEntryLabel());
6708 __ Bind(slow_path->GetExitLabel());
6709}
6710
6711void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6712 Location out,
6713 Location ref,
6714 Location obj,
6715 uint32_t offset,
6716 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006717 DCHECK(kEmitCompilerReadBarrier);
6718
Roland Levillain7c1559a2015-12-15 10:55:36 +00006719 // Insert a slow path based read barrier *after* the reference load.
6720 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 // If heap poisoning is enabled, the unpoisoning of the loaded
6722 // reference will be carried out by the runtime within the slow
6723 // path.
6724 //
6725 // Note that `ref` currently does not get unpoisoned (when heap
6726 // poisoning is enabled), which is alright as the `ref` argument is
6727 // not used by the artReadBarrierSlow entry point.
6728 //
6729 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6730 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6731 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6732 AddSlowPath(slow_path);
6733
Roland Levillain0d5a2812015-11-13 10:07:31 +00006734 __ jmp(slow_path->GetEntryLabel());
6735 __ Bind(slow_path->GetExitLabel());
6736}
6737
Roland Levillain7c1559a2015-12-15 10:55:36 +00006738void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6739 Location out,
6740 Location ref,
6741 Location obj,
6742 uint32_t offset,
6743 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006744 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006745 // Baker's read barriers shall be handled by the fast path
6746 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6747 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006748 // If heap poisoning is enabled, unpoisoning will be taken care of
6749 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006750 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006751 } else if (kPoisonHeapReferences) {
6752 __ UnpoisonHeapReference(out.AsRegister<Register>());
6753 }
6754}
6755
Roland Levillain7c1559a2015-12-15 10:55:36 +00006756void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6757 Location out,
6758 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006759 DCHECK(kEmitCompilerReadBarrier);
6760
Roland Levillain7c1559a2015-12-15 10:55:36 +00006761 // Insert a slow path based read barrier *after* the GC root load.
6762 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006763 // Note that GC roots are not affected by heap poisoning, so we do
6764 // not need to do anything special for this here.
6765 SlowPathCode* slow_path =
6766 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6767 AddSlowPath(slow_path);
6768
Roland Levillain0d5a2812015-11-13 10:07:31 +00006769 __ jmp(slow_path->GetEntryLabel());
6770 __ Bind(slow_path->GetExitLabel());
6771}
6772
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006773void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006774 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006775 LOG(FATAL) << "Unreachable";
6776}
6777
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006778void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006779 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006780 LOG(FATAL) << "Unreachable";
6781}
6782
Mark Mendellfe57faa2015-09-18 09:26:15 -04006783// Simple implementation of packed switch - generate cascaded compare/jumps.
6784void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6785 LocationSummary* locations =
6786 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6787 locations->SetInAt(0, Location::RequiresRegister());
6788}
6789
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006790void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6791 int32_t lower_bound,
6792 uint32_t num_entries,
6793 HBasicBlock* switch_block,
6794 HBasicBlock* default_block) {
6795 // Figure out the correct compare values and jump conditions.
6796 // Handle the first compare/branch as a special case because it might
6797 // jump to the default case.
6798 DCHECK_GT(num_entries, 2u);
6799 Condition first_condition;
6800 uint32_t index;
6801 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6802 if (lower_bound != 0) {
6803 first_condition = kLess;
6804 __ cmpl(value_reg, Immediate(lower_bound));
6805 __ j(first_condition, codegen_->GetLabelOf(default_block));
6806 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006807
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006808 index = 1;
6809 } else {
6810 // Handle all the compare/jumps below.
6811 first_condition = kBelow;
6812 index = 0;
6813 }
6814
6815 // Handle the rest of the compare/jumps.
6816 for (; index + 1 < num_entries; index += 2) {
6817 int32_t compare_to_value = lower_bound + index + 1;
6818 __ cmpl(value_reg, Immediate(compare_to_value));
6819 // Jump to successors[index] if value < case_value[index].
6820 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6821 // Jump to successors[index + 1] if value == case_value[index + 1].
6822 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6823 }
6824
6825 if (index != num_entries) {
6826 // There are an odd number of entries. Handle the last one.
6827 DCHECK_EQ(index + 1, num_entries);
6828 __ cmpl(value_reg, Immediate(lower_bound + index));
6829 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006830 }
6831
6832 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006833 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6834 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006835 }
6836}
6837
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006838void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6839 int32_t lower_bound = switch_instr->GetStartValue();
6840 uint32_t num_entries = switch_instr->GetNumEntries();
6841 LocationSummary* locations = switch_instr->GetLocations();
6842 Register value_reg = locations->InAt(0).AsRegister<Register>();
6843
6844 GenPackedSwitchWithCompares(value_reg,
6845 lower_bound,
6846 num_entries,
6847 switch_instr->GetBlock(),
6848 switch_instr->GetDefaultBlock());
6849}
6850
Mark Mendell805b3b52015-09-18 14:10:29 -04006851void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6852 LocationSummary* locations =
6853 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6854 locations->SetInAt(0, Location::RequiresRegister());
6855
6856 // Constant area pointer.
6857 locations->SetInAt(1, Location::RequiresRegister());
6858
6859 // And the temporary we need.
6860 locations->AddTemp(Location::RequiresRegister());
6861}
6862
6863void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6864 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006865 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006866 LocationSummary* locations = switch_instr->GetLocations();
6867 Register value_reg = locations->InAt(0).AsRegister<Register>();
6868 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6869
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006870 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6871 GenPackedSwitchWithCompares(value_reg,
6872 lower_bound,
6873 num_entries,
6874 switch_instr->GetBlock(),
6875 default_block);
6876 return;
6877 }
6878
Mark Mendell805b3b52015-09-18 14:10:29 -04006879 // Optimizing has a jump area.
6880 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6881 Register constant_area = locations->InAt(1).AsRegister<Register>();
6882
6883 // Remove the bias, if needed.
6884 if (lower_bound != 0) {
6885 __ leal(temp_reg, Address(value_reg, -lower_bound));
6886 value_reg = temp_reg;
6887 }
6888
6889 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006890 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006891 __ cmpl(value_reg, Immediate(num_entries - 1));
6892 __ j(kAbove, codegen_->GetLabelOf(default_block));
6893
6894 // We are in the range of the table.
6895 // Load (target-constant_area) from the jump table, indexing by the value.
6896 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6897
6898 // Compute the actual target address by adding in constant_area.
6899 __ addl(temp_reg, constant_area);
6900
6901 // And jump.
6902 __ jmp(temp_reg);
6903}
6904
Mark Mendell0616ae02015-04-17 12:49:27 -04006905void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6906 HX86ComputeBaseMethodAddress* insn) {
6907 LocationSummary* locations =
6908 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6909 locations->SetOut(Location::RequiresRegister());
6910}
6911
6912void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6913 HX86ComputeBaseMethodAddress* insn) {
6914 LocationSummary* locations = insn->GetLocations();
6915 Register reg = locations->Out().AsRegister<Register>();
6916
6917 // Generate call to next instruction.
6918 Label next_instruction;
6919 __ call(&next_instruction);
6920 __ Bind(&next_instruction);
6921
6922 // Remember this offset for later use with constant area.
6923 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6924
6925 // Grab the return address off the stack.
6926 __ popl(reg);
6927}
6928
6929void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6930 HX86LoadFromConstantTable* insn) {
6931 LocationSummary* locations =
6932 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6933
6934 locations->SetInAt(0, Location::RequiresRegister());
6935 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6936
6937 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00006938 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04006939 return;
6940 }
6941
6942 switch (insn->GetType()) {
6943 case Primitive::kPrimFloat:
6944 case Primitive::kPrimDouble:
6945 locations->SetOut(Location::RequiresFpuRegister());
6946 break;
6947
6948 case Primitive::kPrimInt:
6949 locations->SetOut(Location::RequiresRegister());
6950 break;
6951
6952 default:
6953 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6954 }
6955}
6956
6957void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00006958 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04006959 return;
6960 }
6961
6962 LocationSummary* locations = insn->GetLocations();
6963 Location out = locations->Out();
6964 Register const_area = locations->InAt(0).AsRegister<Register>();
6965 HConstant *value = insn->GetConstant();
6966
6967 switch (insn->GetType()) {
6968 case Primitive::kPrimFloat:
6969 __ movss(out.AsFpuRegister<XmmRegister>(),
6970 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6971 break;
6972
6973 case Primitive::kPrimDouble:
6974 __ movsd(out.AsFpuRegister<XmmRegister>(),
6975 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6976 break;
6977
6978 case Primitive::kPrimInt:
6979 __ movl(out.AsRegister<Register>(),
6980 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6981 break;
6982
6983 default:
6984 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6985 }
6986}
6987
Mark Mendell0616ae02015-04-17 12:49:27 -04006988/**
6989 * Class to handle late fixup of offsets into constant area.
6990 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006991class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006992 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006993 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6994 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6995
6996 protected:
6997 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6998
6999 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007000
7001 private:
7002 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7003 // Patch the correct offset for the instruction. The place to patch is the
7004 // last 4 bytes of the instruction.
7005 // The value to patch is the distance from the offset in the constant area
7006 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007007 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7008 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007009
7010 // Patch in the right value.
7011 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7012 }
7013
Mark Mendell0616ae02015-04-17 12:49:27 -04007014 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007015 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007016};
7017
Mark Mendell805b3b52015-09-18 14:10:29 -04007018/**
7019 * Class to handle late fixup of offsets to a jump table that will be created in the
7020 * constant area.
7021 */
7022class JumpTableRIPFixup : public RIPFixup {
7023 public:
7024 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7025 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7026
7027 void CreateJumpTable() {
7028 X86Assembler* assembler = codegen_->GetAssembler();
7029
7030 // Ensure that the reference to the jump table has the correct offset.
7031 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7032 SetOffset(offset_in_constant_table);
7033
7034 // The label values in the jump table are computed relative to the
7035 // instruction addressing the constant area.
7036 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7037
7038 // Populate the jump table with the correct values for the jump table.
7039 int32_t num_entries = switch_instr_->GetNumEntries();
7040 HBasicBlock* block = switch_instr_->GetBlock();
7041 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7042 // The value that we want is the target offset - the position of the table.
7043 for (int32_t i = 0; i < num_entries; i++) {
7044 HBasicBlock* b = successors[i];
7045 Label* l = codegen_->GetLabelOf(b);
7046 DCHECK(l->IsBound());
7047 int32_t offset_to_block = l->Position() - relative_offset;
7048 assembler->AppendInt32(offset_to_block);
7049 }
7050 }
7051
7052 private:
7053 const HX86PackedSwitch* switch_instr_;
7054};
7055
7056void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7057 // Generate the constant area if needed.
7058 X86Assembler* assembler = GetAssembler();
7059 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7060 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7061 // byte values.
7062 assembler->Align(4, 0);
7063 constant_area_start_ = assembler->CodeSize();
7064
7065 // Populate any jump tables.
7066 for (auto jump_table : fixups_to_jump_tables_) {
7067 jump_table->CreateJumpTable();
7068 }
7069
7070 // And now add the constant area to the generated code.
7071 assembler->AddConstantArea();
7072 }
7073
7074 // And finish up.
7075 CodeGenerator::Finalize(allocator);
7076}
7077
Mark Mendell0616ae02015-04-17 12:49:27 -04007078Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7079 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7080 return Address(reg, kDummy32BitOffset, fixup);
7081}
7082
7083Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7084 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7085 return Address(reg, kDummy32BitOffset, fixup);
7086}
7087
7088Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7089 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7090 return Address(reg, kDummy32BitOffset, fixup);
7091}
7092
7093Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7094 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7095 return Address(reg, kDummy32BitOffset, fixup);
7096}
7097
Aart Bika19616e2016-02-01 18:57:58 -08007098void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7099 if (value == 0) {
7100 __ xorl(dest, dest);
7101 } else {
7102 __ movl(dest, Immediate(value));
7103 }
7104}
7105
7106void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7107 if (value == 0) {
7108 __ testl(dest, dest);
7109 } else {
7110 __ cmpl(dest, Immediate(value));
7111 }
7112}
7113
Mark Mendell805b3b52015-09-18 14:10:29 -04007114Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7115 Register reg,
7116 Register value) {
7117 // Create a fixup to be used to create and address the jump table.
7118 JumpTableRIPFixup* table_fixup =
7119 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7120
7121 // We have to populate the jump tables.
7122 fixups_to_jump_tables_.push_back(table_fixup);
7123
7124 // We want a scaled address, as we are extracting the correct offset from the table.
7125 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7126}
7127
Andreas Gampe85b62f22015-09-09 13:15:38 -07007128// TODO: target as memory.
7129void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7130 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007131 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007132 return;
7133 }
7134
7135 DCHECK_NE(type, Primitive::kPrimVoid);
7136
7137 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7138 if (target.Equals(return_loc)) {
7139 return;
7140 }
7141
7142 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7143 // with the else branch.
7144 if (type == Primitive::kPrimLong) {
7145 HParallelMove parallel_move(GetGraph()->GetArena());
7146 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7147 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7148 GetMoveResolver()->EmitNativeCode(&parallel_move);
7149 } else {
7150 // Let the parallel move resolver take care of all of this.
7151 HParallelMove parallel_move(GetGraph()->GetArena());
7152 parallel_move.AddMove(return_loc, target, type, nullptr);
7153 GetMoveResolver()->EmitNativeCode(&parallel_move);
7154 }
7155}
7156
Roland Levillain4d027112015-07-01 15:41:14 +01007157#undef __
7158
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007159} // namespace x86
7160} // namespace art