blob: d76c6692f6b0e3e16fca2999fcb2751d53190abd [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
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001130void CodeGeneratorX86::Move(HInstruction* instruction, Location location, HInstruction* move_for) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001131 LocationSummary* locations = instruction->GetLocations();
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001132 if (instruction->IsCurrentMethod()) {
1133 Move32(location, Location::StackSlot(kCurrentMethodStackOffset));
1134 } else if (locations != nullptr && locations->Out().Equals(location)) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001135 return;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01001136 } else if (locations != nullptr && locations->Out().IsConstant()) {
Calin Juravlea21f5982014-11-13 15:53:04 +00001137 HConstant* const_to_move = locations->Out().GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001138 if (const_to_move->IsIntConstant() || const_to_move->IsNullConstant()) {
1139 Immediate imm(GetInt32ValueOf(const_to_move));
Calin Juravlea21f5982014-11-13 15:53:04 +00001140 if (location.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001141 __ movl(location.AsRegister<Register>(), imm);
Calin Juravlea21f5982014-11-13 15:53:04 +00001142 } else if (location.IsStackSlot()) {
1143 __ movl(Address(ESP, location.GetStackIndex()), imm);
1144 } else {
1145 DCHECK(location.IsConstant());
1146 DCHECK_EQ(location.GetConstant(), const_to_move);
1147 }
1148 } else if (const_to_move->IsLongConstant()) {
1149 int64_t value = const_to_move->AsLongConstant()->GetValue();
1150 if (location.IsRegisterPair()) {
1151 __ movl(location.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
1152 __ movl(location.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
1153 } else if (location.IsDoubleStackSlot()) {
1154 __ movl(Address(ESP, location.GetStackIndex()), Immediate(Low32Bits(value)));
Roland Levillain199f3362014-11-27 17:15:16 +00001155 __ movl(Address(ESP, location.GetHighStackIndex(kX86WordSize)),
1156 Immediate(High32Bits(value)));
Calin Juravlea21f5982014-11-13 15:53:04 +00001157 } else {
1158 DCHECK(location.IsConstant());
1159 DCHECK_EQ(location.GetConstant(), instruction);
1160 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001161 }
Nicolas Geoffrayf43083d2014-11-07 10:48:10 +00001162 } else if (instruction->IsTemporary()) {
1163 Location temp_location = GetTemporaryLocation(instruction->AsTemporary());
Calin Juravlef97f9fb2014-11-11 15:38:19 +00001164 if (temp_location.IsStackSlot()) {
1165 Move32(location, temp_location);
1166 } else {
1167 DCHECK(temp_location.IsDoubleStackSlot());
1168 Move64(location, temp_location);
1169 }
Roland Levillain476df552014-10-09 17:51:36 +01001170 } else if (instruction->IsLoadLocal()) {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001171 int slot = GetStackSlot(instruction->AsLoadLocal()->GetLocal());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001172 switch (instruction->GetType()) {
1173 case Primitive::kPrimBoolean:
1174 case Primitive::kPrimByte:
1175 case Primitive::kPrimChar:
1176 case Primitive::kPrimShort:
1177 case Primitive::kPrimInt:
1178 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001179 case Primitive::kPrimFloat:
1180 Move32(location, Location::StackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001181 break;
1182
1183 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001184 case Primitive::kPrimDouble:
1185 Move64(location, Location::DoubleStackSlot(slot));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001186 break;
1187
1188 default:
1189 LOG(FATAL) << "Unimplemented local type " << instruction->GetType();
1190 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001191 } else {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001192 DCHECK((instruction->GetNext() == move_for) || instruction->GetNext()->IsTemporary());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001193 switch (instruction->GetType()) {
1194 case Primitive::kPrimBoolean:
1195 case Primitive::kPrimByte:
1196 case Primitive::kPrimChar:
1197 case Primitive::kPrimShort:
1198 case Primitive::kPrimInt:
1199 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001200 case Primitive::kPrimFloat:
Calin Juravlea21f5982014-11-13 15:53:04 +00001201 Move32(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001202 break;
1203
1204 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 case Primitive::kPrimDouble:
Calin Juravlea21f5982014-11-13 15:53:04 +00001206 Move64(location, locations->Out());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001207 break;
1208
1209 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 LOG(FATAL) << "Unexpected type " << instruction->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001212 }
1213}
1214
Calin Juravle175dc732015-08-25 15:42:32 +01001215void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1216 DCHECK(location.IsRegister());
1217 __ movl(location.AsRegister<Register>(), Immediate(value));
1218}
1219
Calin Juravlee460d1d2015-09-29 04:52:17 +01001220void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1221 if (Primitive::Is64BitType(dst_type)) {
1222 Move64(dst, src);
1223 } else {
1224 Move32(dst, src);
1225 }
1226}
1227
1228void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1229 if (location.IsRegister()) {
1230 locations->AddTemp(location);
1231 } else if (location.IsRegisterPair()) {
1232 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1233 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1234 } else {
1235 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1236 }
1237}
1238
David Brazdilfc6a86a2015-06-26 10:33:45 +00001239void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001240 DCHECK(!successor->IsExitBlock());
1241
1242 HBasicBlock* block = got->GetBlock();
1243 HInstruction* previous = got->GetPrevious();
1244
1245 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001246 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001247 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1248 return;
1249 }
1250
1251 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1252 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1253 }
1254 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001255 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001256 }
1257}
1258
David Brazdilfc6a86a2015-06-26 10:33:45 +00001259void LocationsBuilderX86::VisitGoto(HGoto* got) {
1260 got->SetLocations(nullptr);
1261}
1262
1263void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1264 HandleGoto(got, got->GetSuccessor());
1265}
1266
1267void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1268 try_boundary->SetLocations(nullptr);
1269}
1270
1271void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1272 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1273 if (!successor->IsExitBlock()) {
1274 HandleGoto(try_boundary, successor);
1275 }
1276}
1277
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001278void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001279 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001280}
1281
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001282void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001283}
1284
Mark Mendell152408f2015-12-31 12:28:50 -05001285template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001286void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001287 LabelType* true_label,
1288 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001289 if (cond->IsFPConditionTrueIfNaN()) {
1290 __ j(kUnordered, true_label);
1291 } else if (cond->IsFPConditionFalseIfNaN()) {
1292 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001293 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001294 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001295}
1296
Mark Mendell152408f2015-12-31 12:28:50 -05001297template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001298void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001299 LabelType* true_label,
1300 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001301 LocationSummary* locations = cond->GetLocations();
1302 Location left = locations->InAt(0);
1303 Location right = locations->InAt(1);
1304 IfCondition if_cond = cond->GetCondition();
1305
Mark Mendellc4701932015-04-10 13:18:51 -04001306 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001307 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001308 IfCondition true_high_cond = if_cond;
1309 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001310 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001311
1312 // Set the conditions for the test, remembering that == needs to be
1313 // decided using the low words.
1314 switch (if_cond) {
1315 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001316 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001317 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001318 break;
1319 case kCondLT:
1320 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001321 break;
1322 case kCondLE:
1323 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001324 break;
1325 case kCondGT:
1326 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001327 break;
1328 case kCondGE:
1329 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001330 break;
Aart Bike9f37602015-10-09 11:15:55 -07001331 case kCondB:
1332 false_high_cond = kCondA;
1333 break;
1334 case kCondBE:
1335 true_high_cond = kCondB;
1336 break;
1337 case kCondA:
1338 false_high_cond = kCondB;
1339 break;
1340 case kCondAE:
1341 true_high_cond = kCondA;
1342 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001343 }
1344
1345 if (right.IsConstant()) {
1346 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001347 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001348 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001349
1350 if (val_high == 0) {
1351 __ testl(left_high, left_high);
1352 } else {
1353 __ cmpl(left_high, Immediate(val_high));
1354 }
1355 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001356 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001357 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001358 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001359 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001360 __ j(X86Condition(true_high_cond), true_label);
1361 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001362 }
1363 // Must be equal high, so compare the lows.
1364 if (val_low == 0) {
1365 __ testl(left_low, left_low);
1366 } else {
1367 __ cmpl(left_low, Immediate(val_low));
1368 }
1369 } else {
Mark Mendellc4701932015-04-10 13:18:51 -04001370 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001371 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001372
1373 __ cmpl(left_high, right_high);
1374 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001375 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001376 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001377 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001378 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001379 __ j(X86Condition(true_high_cond), true_label);
1380 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001381 }
1382 // Must be equal high, so compare the lows.
1383 __ cmpl(left_low, right_low);
1384 }
1385 // The last comparison might be unsigned.
1386 __ j(final_condition, true_label);
1387}
1388
Mark Mendell152408f2015-12-31 12:28:50 -05001389template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001390void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001391 LabelType* true_target_in,
1392 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001393 // Generated branching requires both targets to be explicit. If either of the
1394 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001395 LabelType fallthrough_target;
1396 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1397 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001398
Mark Mendellc4701932015-04-10 13:18:51 -04001399 LocationSummary* locations = condition->GetLocations();
1400 Location left = locations->InAt(0);
1401 Location right = locations->InAt(1);
1402
Mark Mendellc4701932015-04-10 13:18:51 -04001403 Primitive::Type type = condition->InputAt(0)->GetType();
1404 switch (type) {
1405 case Primitive::kPrimLong:
1406 GenerateLongComparesAndJumps(condition, true_target, false_target);
1407 break;
1408 case Primitive::kPrimFloat:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001409 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001410 GenerateFPJumps(condition, true_target, false_target);
1411 break;
1412 case Primitive::kPrimDouble:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001413 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001414 GenerateFPJumps(condition, true_target, false_target);
1415 break;
1416 default:
1417 LOG(FATAL) << "Unexpected compare type " << type;
1418 }
1419
David Brazdil0debae72015-11-12 18:37:00 +00001420 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001421 __ jmp(false_target);
1422 }
David Brazdil0debae72015-11-12 18:37:00 +00001423
1424 if (fallthrough_target.IsLinked()) {
1425 __ Bind(&fallthrough_target);
1426 }
Mark Mendellc4701932015-04-10 13:18:51 -04001427}
1428
David Brazdil0debae72015-11-12 18:37:00 +00001429static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1430 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1431 // are set only strictly before `branch`. We can't use the eflags on long/FP
1432 // conditions if they are materialized due to the complex branching.
1433 return cond->IsCondition() &&
1434 cond->GetNext() == branch &&
1435 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1436 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1437}
1438
Mark Mendell152408f2015-12-31 12:28:50 -05001439template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001440void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001441 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001442 LabelType* true_target,
1443 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001444 HInstruction* cond = instruction->InputAt(condition_input_index);
1445
1446 if (true_target == nullptr && false_target == nullptr) {
1447 // Nothing to do. The code always falls through.
1448 return;
1449 } else if (cond->IsIntConstant()) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001450 // Constant condition, statically compared against 1.
David Brazdil0debae72015-11-12 18:37:00 +00001451 if (cond->AsIntConstant()->IsOne()) {
1452 if (true_target != nullptr) {
1453 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001454 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001455 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001456 DCHECK(cond->AsIntConstant()->IsZero());
1457 if (false_target != nullptr) {
1458 __ jmp(false_target);
1459 }
1460 }
1461 return;
1462 }
1463
1464 // The following code generates these patterns:
1465 // (1) true_target == nullptr && false_target != nullptr
1466 // - opposite condition true => branch to false_target
1467 // (2) true_target != nullptr && false_target == nullptr
1468 // - condition true => branch to true_target
1469 // (3) true_target != nullptr && false_target != nullptr
1470 // - condition true => branch to true_target
1471 // - branch to false_target
1472 if (IsBooleanValueOrMaterializedCondition(cond)) {
1473 if (AreEflagsSetFrom(cond, instruction)) {
1474 if (true_target == nullptr) {
1475 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1476 } else {
1477 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1478 }
1479 } else {
1480 // Materialized condition, compare against 0.
1481 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1482 if (lhs.IsRegister()) {
1483 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1484 } else {
1485 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1486 }
1487 if (true_target == nullptr) {
1488 __ j(kEqual, false_target);
1489 } else {
1490 __ j(kNotEqual, true_target);
1491 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001492 }
1493 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001494 // Condition has not been materialized, use its inputs as the comparison and
1495 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001496 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001497
1498 // If this is a long or FP comparison that has been folded into
1499 // the HCondition, generate the comparison directly.
1500 Primitive::Type type = condition->InputAt(0)->GetType();
1501 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1502 GenerateCompareTestAndBranch(condition, true_target, false_target);
1503 return;
1504 }
1505
1506 Location lhs = condition->GetLocations()->InAt(0);
1507 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001508 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001509 if (rhs.IsRegister()) {
1510 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1511 } else if (rhs.IsConstant()) {
1512 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1513 if (constant == 0) {
1514 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001515 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001516 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001517 }
1518 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001519 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1520 }
1521 if (true_target == nullptr) {
1522 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1523 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001524 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001525 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001526 }
David Brazdil0debae72015-11-12 18:37:00 +00001527
1528 // If neither branch falls through (case 3), the conditional branch to `true_target`
1529 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1530 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001531 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001532 }
1533}
1534
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001535void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001536 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1537 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001538 locations->SetInAt(0, Location::Any());
1539 }
1540}
1541
1542void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001543 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1544 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1545 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1546 nullptr : codegen_->GetLabelOf(true_successor);
1547 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1548 nullptr : codegen_->GetLabelOf(false_successor);
1549 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001550}
1551
1552void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1553 LocationSummary* locations = new (GetGraph()->GetArena())
1554 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001555 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001556 locations->SetInAt(0, Location::Any());
1557 }
1558}
1559
1560void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001561 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00001562 GenerateTestAndBranch(deoptimize,
1563 /* condition_input_index */ 0,
1564 slow_path->GetEntryLabel(),
Mark Mendell152408f2015-12-31 12:28:50 -05001565 /* false_target */ static_cast<Label*>(nullptr));
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001566}
1567
David Srbecky0cf44932015-12-09 14:09:59 +00001568void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1569 new (GetGraph()->GetArena()) LocationSummary(info);
1570}
1571
1572void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
David Srbeckyb7070a22016-01-08 18:13:53 +00001573 if (codegen_->HasStackMapAtCurrentPc()) {
1574 // Ensure that we do not collide with the stack map of the previous instruction.
1575 __ nop();
1576 }
David Srbecky0cf44932015-12-09 14:09:59 +00001577 codegen_->RecordPcInfo(info, info->GetDexPc());
1578}
1579
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001580void LocationsBuilderX86::VisitLocal(HLocal* local) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001581 local->SetLocations(nullptr);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001582}
1583
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001584void InstructionCodeGeneratorX86::VisitLocal(HLocal* local) {
1585 DCHECK_EQ(local->GetBlock(), GetGraph()->GetEntryBlock());
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001586}
1587
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001588void LocationsBuilderX86::VisitLoadLocal(HLoadLocal* local) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001589 local->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001590}
1591
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001592void InstructionCodeGeneratorX86::VisitLoadLocal(HLoadLocal* load ATTRIBUTE_UNUSED) {
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001593 // Nothing to do, this is driven by the code generator.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001594}
1595
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001596void LocationsBuilderX86::VisitStoreLocal(HStoreLocal* store) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001597 LocationSummary* locations =
1598 new (GetGraph()->GetArena()) LocationSummary(store, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001599 switch (store->InputAt(1)->GetType()) {
1600 case Primitive::kPrimBoolean:
1601 case Primitive::kPrimByte:
1602 case Primitive::kPrimChar:
1603 case Primitive::kPrimShort:
1604 case Primitive::kPrimInt:
1605 case Primitive::kPrimNot:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001606 case Primitive::kPrimFloat:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001607 locations->SetInAt(1, Location::StackSlot(codegen_->GetStackSlot(store->GetLocal())));
1608 break;
1609
1610 case Primitive::kPrimLong:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001611 case Primitive::kPrimDouble:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001612 locations->SetInAt(1, Location::DoubleStackSlot(codegen_->GetStackSlot(store->GetLocal())));
1613 break;
1614
1615 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001616 LOG(FATAL) << "Unknown local type " << store->InputAt(1)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001617 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001618}
1619
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001620void InstructionCodeGeneratorX86::VisitStoreLocal(HStoreLocal* store ATTRIBUTE_UNUSED) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001621}
1622
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001623void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001624 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001625 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001626 // Handle the long/FP comparisons made in instruction simplification.
1627 switch (cond->InputAt(0)->GetType()) {
1628 case Primitive::kPrimLong: {
1629 locations->SetInAt(0, Location::RequiresRegister());
1630 locations->SetInAt(1, Location::RegisterOrConstant(cond->InputAt(1)));
1631 if (cond->NeedsMaterialization()) {
1632 locations->SetOut(Location::RequiresRegister());
1633 }
1634 break;
1635 }
1636 case Primitive::kPrimFloat:
1637 case Primitive::kPrimDouble: {
1638 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001639 locations->SetInAt(1, Location::RequiresFpuRegister());
Mark Mendellc4701932015-04-10 13:18:51 -04001640 if (cond->NeedsMaterialization()) {
1641 locations->SetOut(Location::RequiresRegister());
1642 }
1643 break;
1644 }
1645 default:
1646 locations->SetInAt(0, Location::RequiresRegister());
1647 locations->SetInAt(1, Location::Any());
1648 if (cond->NeedsMaterialization()) {
1649 // We need a byte register.
1650 locations->SetOut(Location::RegisterLocation(ECX));
1651 }
1652 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001653 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001654}
1655
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001656void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
Mark Mendellc4701932015-04-10 13:18:51 -04001657 if (!cond->NeedsMaterialization()) {
1658 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001659 }
Mark Mendellc4701932015-04-10 13:18:51 -04001660
1661 LocationSummary* locations = cond->GetLocations();
1662 Location lhs = locations->InAt(0);
1663 Location rhs = locations->InAt(1);
1664 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001665 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001666
1667 switch (cond->InputAt(0)->GetType()) {
1668 default: {
1669 // Integer case.
1670
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001671 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001672 __ xorl(reg, reg);
1673
1674 if (rhs.IsRegister()) {
1675 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1676 } else if (rhs.IsConstant()) {
1677 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1678 if (constant == 0) {
1679 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1680 } else {
1681 __ cmpl(lhs.AsRegister<Register>(), Immediate(constant));
1682 }
1683 } else {
1684 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1685 }
Aart Bike9f37602015-10-09 11:15:55 -07001686 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001687 return;
1688 }
1689 case Primitive::kPrimLong:
1690 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1691 break;
1692 case Primitive::kPrimFloat:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001693 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001694 GenerateFPJumps(cond, &true_label, &false_label);
1695 break;
1696 case Primitive::kPrimDouble:
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00001697 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
Mark Mendellc4701932015-04-10 13:18:51 -04001698 GenerateFPJumps(cond, &true_label, &false_label);
1699 break;
1700 }
1701
1702 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001703 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001704
Roland Levillain4fa13f62015-07-06 18:11:54 +01001705 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001706 __ Bind(&false_label);
1707 __ xorl(reg, reg);
1708 __ jmp(&done_label);
1709
Roland Levillain4fa13f62015-07-06 18:11:54 +01001710 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001711 __ Bind(&true_label);
1712 __ movl(reg, Immediate(1));
1713 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001714}
1715
1716void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001717 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001718}
1719
1720void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001721 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001722}
1723
1724void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001725 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001726}
1727
1728void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001729 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001730}
1731
1732void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001733 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001734}
1735
1736void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001737 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001738}
1739
1740void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001741 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001742}
1743
1744void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001745 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001746}
1747
1748void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001749 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001750}
1751
1752void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001753 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001754}
1755
1756void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001757 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001758}
1759
1760void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001761 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001762}
1763
Aart Bike9f37602015-10-09 11:15:55 -07001764void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001765 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001766}
1767
1768void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001769 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001770}
1771
1772void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001773 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001774}
1775
1776void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001777 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001778}
1779
1780void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001781 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001782}
1783
1784void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001785 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001786}
1787
1788void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001789 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001790}
1791
1792void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001793 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001794}
1795
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001796void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001797 LocationSummary* locations =
1798 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001799 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001800}
1801
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001802void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001803 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001804}
1805
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001806void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1807 LocationSummary* locations =
1808 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1809 locations->SetOut(Location::ConstantLocation(constant));
1810}
1811
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001812void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001813 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001814}
1815
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001816void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001817 LocationSummary* locations =
1818 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001819 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001820}
1821
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001822void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001823 // Will be generated at use site.
1824}
1825
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001826void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1827 LocationSummary* locations =
1828 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1829 locations->SetOut(Location::ConstantLocation(constant));
1830}
1831
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001832void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001833 // Will be generated at use site.
1834}
1835
1836void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1837 LocationSummary* locations =
1838 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1839 locations->SetOut(Location::ConstantLocation(constant));
1840}
1841
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001842void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001843 // Will be generated at use site.
1844}
1845
Calin Juravle27df7582015-04-17 19:12:31 +01001846void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1847 memory_barrier->SetLocations(nullptr);
1848}
1849
1850void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001851 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001852}
1853
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001854void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001855 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001856}
1857
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001858void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001859 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001860}
1861
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001862void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001863 LocationSummary* locations =
1864 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001865 switch (ret->InputAt(0)->GetType()) {
1866 case Primitive::kPrimBoolean:
1867 case Primitive::kPrimByte:
1868 case Primitive::kPrimChar:
1869 case Primitive::kPrimShort:
1870 case Primitive::kPrimInt:
1871 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001872 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001873 break;
1874
1875 case Primitive::kPrimLong:
1876 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001877 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
1879
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001880 case Primitive::kPrimFloat:
1881 case Primitive::kPrimDouble:
1882 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001883 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001884 break;
1885
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001886 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001887 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001889}
1890
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001891void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001892 if (kIsDebugBuild) {
1893 switch (ret->InputAt(0)->GetType()) {
1894 case Primitive::kPrimBoolean:
1895 case Primitive::kPrimByte:
1896 case Primitive::kPrimChar:
1897 case Primitive::kPrimShort:
1898 case Primitive::kPrimInt:
1899 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001900 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001901 break;
1902
1903 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001904 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1905 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906 break;
1907
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001908 case Primitive::kPrimFloat:
1909 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001910 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001911 break;
1912
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001913 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001914 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001915 }
1916 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001917 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001918}
1919
Calin Juravle175dc732015-08-25 15:42:32 +01001920void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1921 // The trampoline uses the same calling convention as dex calling conventions,
1922 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1923 // the method_idx.
1924 HandleInvoke(invoke);
1925}
1926
1927void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1928 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1929}
1930
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001931void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001932 // Explicit clinit checks triggered by static invokes must have been pruned by
1933 // art::PrepareForRegisterAllocation.
1934 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001935
Mark Mendellfb8d2792015-03-31 22:16:59 -04001936 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001937 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001938 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001939 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001940 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001941 return;
1942 }
1943
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001944 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001945
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001946 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1947 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001948 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001949 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001950}
1951
Mark Mendell09ed1a32015-03-25 08:30:06 -04001952static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1953 if (invoke->GetLocations()->Intrinsified()) {
1954 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1955 intrinsic.Dispatch(invoke);
1956 return true;
1957 }
1958 return false;
1959}
1960
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001961void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001962 // Explicit clinit checks triggered by static invokes must have been pruned by
1963 // art::PrepareForRegisterAllocation.
1964 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001965
Mark Mendell09ed1a32015-03-25 08:30:06 -04001966 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1967 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001968 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001969
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001970 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001971 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001972 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001973 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001974}
1975
1976void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001977 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1978 if (intrinsic.TryDispatch(invoke)) {
1979 return;
1980 }
1981
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001982 HandleInvoke(invoke);
1983}
1984
1985void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001986 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001987 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001988}
1989
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001990void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001991 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1992 return;
1993 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001994
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001995 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001996 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001997 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001998}
1999
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002000void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002001 // This call to HandleInvoke allocates a temporary (core) register
2002 // which is also used to transfer the hidden argument from FP to
2003 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002004 HandleInvoke(invoke);
2005 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002006 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002007}
2008
2009void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2010 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002011 LocationSummary* locations = invoke->GetLocations();
2012 Register temp = locations->GetTemp(0).AsRegister<Register>();
2013 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002014 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2015 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016 Location receiver = locations->InAt(0);
2017 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2018
Roland Levillain0d5a2812015-11-13 10:07:31 +00002019 // Set the hidden argument. This is safe to do this here, as XMM7
2020 // won't be modified thereafter, before the `call` instruction.
2021 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002022 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002023 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002024
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025 if (receiver.IsStackSlot()) {
2026 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002027 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028 __ movl(temp, Address(temp, class_offset));
2029 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002030 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002032 }
Roland Levillain4d027112015-07-01 15:41:14 +01002033 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002034 // Instead of simply (possibly) unpoisoning `temp` here, we should
2035 // emit a read barrier for the previous class reference load.
2036 // However this is not required in practice, as this is an
2037 // intermediate/temporary reference and because the current
2038 // concurrent copying collector keeps the from-space memory
2039 // intact/accessible until the end of the marking phase (the
2040 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002041 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002042 // temp = temp->GetImtEntryAt(method_offset);
2043 __ movl(temp, Address(temp, method_offset));
2044 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002045 __ call(Address(temp,
2046 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002047
2048 DCHECK(!codegen_->IsLeafMethod());
2049 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2050}
2051
Roland Levillain88cb1752014-10-20 16:36:47 +01002052void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2053 LocationSummary* locations =
2054 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2055 switch (neg->GetResultType()) {
2056 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002057 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002058 locations->SetInAt(0, Location::RequiresRegister());
2059 locations->SetOut(Location::SameAsFirstInput());
2060 break;
2061
Roland Levillain88cb1752014-10-20 16:36:47 +01002062 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002063 locations->SetInAt(0, Location::RequiresFpuRegister());
2064 locations->SetOut(Location::SameAsFirstInput());
2065 locations->AddTemp(Location::RequiresRegister());
2066 locations->AddTemp(Location::RequiresFpuRegister());
2067 break;
2068
Roland Levillain88cb1752014-10-20 16:36:47 +01002069 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002070 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002071 locations->SetOut(Location::SameAsFirstInput());
2072 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002073 break;
2074
2075 default:
2076 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2077 }
2078}
2079
2080void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2081 LocationSummary* locations = neg->GetLocations();
2082 Location out = locations->Out();
2083 Location in = locations->InAt(0);
2084 switch (neg->GetResultType()) {
2085 case Primitive::kPrimInt:
2086 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002087 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002088 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002089 break;
2090
2091 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002092 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002093 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002094 __ negl(out.AsRegisterPairLow<Register>());
2095 // Negation is similar to subtraction from zero. The least
2096 // significant byte triggers a borrow when it is different from
2097 // zero; to take it into account, add 1 to the most significant
2098 // byte if the carry flag (CF) is set to 1 after the first NEGL
2099 // operation.
2100 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2101 __ negl(out.AsRegisterPairHigh<Register>());
2102 break;
2103
Roland Levillain5368c212014-11-27 15:03:41 +00002104 case Primitive::kPrimFloat: {
2105 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002106 Register constant = locations->GetTemp(0).AsRegister<Register>();
2107 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002108 // Implement float negation with an exclusive or with value
2109 // 0x80000000 (mask for bit 31, representing the sign of a
2110 // single-precision floating-point number).
2111 __ movl(constant, Immediate(INT32_C(0x80000000)));
2112 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002113 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002114 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002115 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002116
Roland Levillain5368c212014-11-27 15:03:41 +00002117 case Primitive::kPrimDouble: {
2118 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002119 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002120 // Implement double negation with an exclusive or with value
2121 // 0x8000000000000000 (mask for bit 63, representing the sign of
2122 // a double-precision floating-point number).
2123 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002125 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002126 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002127
2128 default:
2129 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2130 }
2131}
2132
Roland Levillaindff1f282014-11-05 14:15:05 +00002133void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002134 Primitive::Type result_type = conversion->GetResultType();
2135 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002136 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002137
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002138 // The float-to-long and double-to-long type conversions rely on a
2139 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002140 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002141 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2142 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002143 ? LocationSummary::kCall
2144 : LocationSummary::kNoCall;
2145 LocationSummary* locations =
2146 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2147
David Brazdilb2bd1c52015-03-25 11:17:37 +00002148 // The Java language does not allow treating boolean as an integral type but
2149 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002150
Roland Levillaindff1f282014-11-05 14:15:05 +00002151 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002152 case Primitive::kPrimByte:
2153 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002154 case Primitive::kPrimBoolean:
2155 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002156 case Primitive::kPrimShort:
2157 case Primitive::kPrimInt:
2158 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002159 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002160 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2161 // Make the output overlap to please the register allocator. This greatly simplifies
2162 // the validation of the linear scan implementation
2163 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002164 break;
2165
2166 default:
2167 LOG(FATAL) << "Unexpected type conversion from " << input_type
2168 << " to " << result_type;
2169 }
2170 break;
2171
Roland Levillain01a8d712014-11-14 16:27:39 +00002172 case Primitive::kPrimShort:
2173 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002174 case Primitive::kPrimBoolean:
2175 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002176 case Primitive::kPrimByte:
2177 case Primitive::kPrimInt:
2178 case Primitive::kPrimChar:
2179 // Processing a Dex `int-to-short' instruction.
2180 locations->SetInAt(0, Location::Any());
2181 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2182 break;
2183
2184 default:
2185 LOG(FATAL) << "Unexpected type conversion from " << input_type
2186 << " to " << result_type;
2187 }
2188 break;
2189
Roland Levillain946e1432014-11-11 17:35:19 +00002190 case Primitive::kPrimInt:
2191 switch (input_type) {
2192 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002193 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002194 locations->SetInAt(0, Location::Any());
2195 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2196 break;
2197
2198 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002199 // Processing a Dex `float-to-int' instruction.
2200 locations->SetInAt(0, Location::RequiresFpuRegister());
2201 locations->SetOut(Location::RequiresRegister());
2202 locations->AddTemp(Location::RequiresFpuRegister());
2203 break;
2204
Roland Levillain946e1432014-11-11 17:35:19 +00002205 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002206 // Processing a Dex `double-to-int' instruction.
2207 locations->SetInAt(0, Location::RequiresFpuRegister());
2208 locations->SetOut(Location::RequiresRegister());
2209 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002210 break;
2211
2212 default:
2213 LOG(FATAL) << "Unexpected type conversion from " << input_type
2214 << " to " << result_type;
2215 }
2216 break;
2217
Roland Levillaindff1f282014-11-05 14:15:05 +00002218 case Primitive::kPrimLong:
2219 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002220 case Primitive::kPrimBoolean:
2221 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002222 case Primitive::kPrimByte:
2223 case Primitive::kPrimShort:
2224 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002225 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002226 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002227 locations->SetInAt(0, Location::RegisterLocation(EAX));
2228 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2229 break;
2230
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002231 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002232 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002233 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002234 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002235 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2236 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2237
Vladimir Marko949c91f2015-01-27 10:48:44 +00002238 // The runtime helper puts the result in EAX, EDX.
2239 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002240 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002241 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002242
2243 default:
2244 LOG(FATAL) << "Unexpected type conversion from " << input_type
2245 << " to " << result_type;
2246 }
2247 break;
2248
Roland Levillain981e4542014-11-14 11:47:14 +00002249 case Primitive::kPrimChar:
2250 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002251 case Primitive::kPrimBoolean:
2252 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002253 case Primitive::kPrimByte:
2254 case Primitive::kPrimShort:
2255 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002256 // Processing a Dex `int-to-char' instruction.
2257 locations->SetInAt(0, Location::Any());
2258 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2259 break;
2260
2261 default:
2262 LOG(FATAL) << "Unexpected type conversion from " << input_type
2263 << " to " << result_type;
2264 }
2265 break;
2266
Roland Levillaindff1f282014-11-05 14:15:05 +00002267 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002268 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002269 case Primitive::kPrimBoolean:
2270 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002271 case Primitive::kPrimByte:
2272 case Primitive::kPrimShort:
2273 case Primitive::kPrimInt:
2274 case Primitive::kPrimChar:
2275 // Processing a Dex `int-to-float' instruction.
2276 locations->SetInAt(0, Location::RequiresRegister());
2277 locations->SetOut(Location::RequiresFpuRegister());
2278 break;
2279
2280 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002281 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002282 locations->SetInAt(0, Location::Any());
2283 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002284 break;
2285
Roland Levillaincff13742014-11-17 14:32:17 +00002286 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002287 // Processing a Dex `double-to-float' instruction.
2288 locations->SetInAt(0, Location::RequiresFpuRegister());
2289 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002290 break;
2291
2292 default:
2293 LOG(FATAL) << "Unexpected type conversion from " << input_type
2294 << " to " << result_type;
2295 };
2296 break;
2297
Roland Levillaindff1f282014-11-05 14:15:05 +00002298 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002299 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002300 case Primitive::kPrimBoolean:
2301 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002302 case Primitive::kPrimByte:
2303 case Primitive::kPrimShort:
2304 case Primitive::kPrimInt:
2305 case Primitive::kPrimChar:
2306 // Processing a Dex `int-to-double' instruction.
2307 locations->SetInAt(0, Location::RequiresRegister());
2308 locations->SetOut(Location::RequiresFpuRegister());
2309 break;
2310
2311 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002312 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002313 locations->SetInAt(0, Location::Any());
2314 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002315 break;
2316
Roland Levillaincff13742014-11-17 14:32:17 +00002317 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002318 // Processing a Dex `float-to-double' instruction.
2319 locations->SetInAt(0, Location::RequiresFpuRegister());
2320 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002321 break;
2322
2323 default:
2324 LOG(FATAL) << "Unexpected type conversion from " << input_type
2325 << " to " << result_type;
2326 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002327 break;
2328
2329 default:
2330 LOG(FATAL) << "Unexpected type conversion from " << input_type
2331 << " to " << result_type;
2332 }
2333}
2334
2335void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2336 LocationSummary* locations = conversion->GetLocations();
2337 Location out = locations->Out();
2338 Location in = locations->InAt(0);
2339 Primitive::Type result_type = conversion->GetResultType();
2340 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002341 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002342 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002343 case Primitive::kPrimByte:
2344 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002345 case Primitive::kPrimBoolean:
2346 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002347 case Primitive::kPrimShort:
2348 case Primitive::kPrimInt:
2349 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002350 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002351 if (in.IsRegister()) {
2352 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002353 } else {
2354 DCHECK(in.GetConstant()->IsIntConstant());
2355 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2356 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2357 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002358 break;
2359
2360 default:
2361 LOG(FATAL) << "Unexpected type conversion from " << input_type
2362 << " to " << result_type;
2363 }
2364 break;
2365
Roland Levillain01a8d712014-11-14 16:27:39 +00002366 case Primitive::kPrimShort:
2367 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002368 case Primitive::kPrimBoolean:
2369 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002370 case Primitive::kPrimByte:
2371 case Primitive::kPrimInt:
2372 case Primitive::kPrimChar:
2373 // Processing a Dex `int-to-short' instruction.
2374 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002375 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002376 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002377 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002378 } else {
2379 DCHECK(in.GetConstant()->IsIntConstant());
2380 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002381 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002382 }
2383 break;
2384
2385 default:
2386 LOG(FATAL) << "Unexpected type conversion from " << input_type
2387 << " to " << result_type;
2388 }
2389 break;
2390
Roland Levillain946e1432014-11-11 17:35:19 +00002391 case Primitive::kPrimInt:
2392 switch (input_type) {
2393 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002394 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002395 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002396 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002397 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002398 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002399 } else {
2400 DCHECK(in.IsConstant());
2401 DCHECK(in.GetConstant()->IsLongConstant());
2402 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002403 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002404 }
2405 break;
2406
Roland Levillain3f8f9362014-12-02 17:45:01 +00002407 case Primitive::kPrimFloat: {
2408 // Processing a Dex `float-to-int' instruction.
2409 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2410 Register output = out.AsRegister<Register>();
2411 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002412 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002413
2414 __ movl(output, Immediate(kPrimIntMax));
2415 // temp = int-to-float(output)
2416 __ cvtsi2ss(temp, output);
2417 // if input >= temp goto done
2418 __ comiss(input, temp);
2419 __ j(kAboveEqual, &done);
2420 // if input == NaN goto nan
2421 __ j(kUnordered, &nan);
2422 // output = float-to-int-truncate(input)
2423 __ cvttss2si(output, input);
2424 __ jmp(&done);
2425 __ Bind(&nan);
2426 // output = 0
2427 __ xorl(output, output);
2428 __ Bind(&done);
2429 break;
2430 }
2431
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002432 case Primitive::kPrimDouble: {
2433 // Processing a Dex `double-to-int' instruction.
2434 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2435 Register output = out.AsRegister<Register>();
2436 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002437 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002438
2439 __ movl(output, Immediate(kPrimIntMax));
2440 // temp = int-to-double(output)
2441 __ cvtsi2sd(temp, output);
2442 // if input >= temp goto done
2443 __ comisd(input, temp);
2444 __ j(kAboveEqual, &done);
2445 // if input == NaN goto nan
2446 __ j(kUnordered, &nan);
2447 // output = double-to-int-truncate(input)
2448 __ cvttsd2si(output, input);
2449 __ jmp(&done);
2450 __ Bind(&nan);
2451 // output = 0
2452 __ xorl(output, output);
2453 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002454 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002455 }
Roland Levillain946e1432014-11-11 17:35:19 +00002456
2457 default:
2458 LOG(FATAL) << "Unexpected type conversion from " << input_type
2459 << " to " << result_type;
2460 }
2461 break;
2462
Roland Levillaindff1f282014-11-05 14:15:05 +00002463 case Primitive::kPrimLong:
2464 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002465 case Primitive::kPrimBoolean:
2466 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002467 case Primitive::kPrimByte:
2468 case Primitive::kPrimShort:
2469 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002470 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002471 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002472 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2473 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002474 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002475 __ cdq();
2476 break;
2477
2478 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002479 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002480 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2481 conversion,
2482 conversion->GetDexPc(),
2483 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002484 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002485 break;
2486
Roland Levillaindff1f282014-11-05 14:15:05 +00002487 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002488 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002489 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2490 conversion,
2491 conversion->GetDexPc(),
2492 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002493 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002494 break;
2495
2496 default:
2497 LOG(FATAL) << "Unexpected type conversion from " << input_type
2498 << " to " << result_type;
2499 }
2500 break;
2501
Roland Levillain981e4542014-11-14 11:47:14 +00002502 case Primitive::kPrimChar:
2503 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002504 case Primitive::kPrimBoolean:
2505 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002506 case Primitive::kPrimByte:
2507 case Primitive::kPrimShort:
2508 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002509 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2510 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002511 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002512 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002513 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002514 } else {
2515 DCHECK(in.GetConstant()->IsIntConstant());
2516 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002517 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002518 }
2519 break;
2520
2521 default:
2522 LOG(FATAL) << "Unexpected type conversion from " << input_type
2523 << " to " << result_type;
2524 }
2525 break;
2526
Roland Levillaindff1f282014-11-05 14:15:05 +00002527 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002528 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002529 case Primitive::kPrimBoolean:
2530 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002531 case Primitive::kPrimByte:
2532 case Primitive::kPrimShort:
2533 case Primitive::kPrimInt:
2534 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002535 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002536 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002537 break;
2538
Roland Levillain6d0e4832014-11-27 18:31:21 +00002539 case Primitive::kPrimLong: {
2540 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002541 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002542
Roland Levillain232ade02015-04-20 15:14:36 +01002543 // Create stack space for the call to
2544 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2545 // TODO: enhance register allocator to ask for stack temporaries.
2546 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2547 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2548 __ subl(ESP, Immediate(adjustment));
2549 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002550
Roland Levillain232ade02015-04-20 15:14:36 +01002551 // Load the value to the FP stack, using temporaries if needed.
2552 PushOntoFPStack(in, 0, adjustment, false, true);
2553
2554 if (out.IsStackSlot()) {
2555 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2556 } else {
2557 __ fstps(Address(ESP, 0));
2558 Location stack_temp = Location::StackSlot(0);
2559 codegen_->Move32(out, stack_temp);
2560 }
2561
2562 // Remove the temporary stack space we allocated.
2563 if (adjustment != 0) {
2564 __ addl(ESP, Immediate(adjustment));
2565 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002566 break;
2567 }
2568
Roland Levillaincff13742014-11-17 14:32:17 +00002569 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002570 // Processing a Dex `double-to-float' instruction.
2571 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002572 break;
2573
2574 default:
2575 LOG(FATAL) << "Unexpected type conversion from " << input_type
2576 << " to " << result_type;
2577 };
2578 break;
2579
Roland Levillaindff1f282014-11-05 14:15:05 +00002580 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002581 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002582 case Primitive::kPrimBoolean:
2583 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002584 case Primitive::kPrimByte:
2585 case Primitive::kPrimShort:
2586 case Primitive::kPrimInt:
2587 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002588 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002589 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002590 break;
2591
Roland Levillain647b9ed2014-11-27 12:06:00 +00002592 case Primitive::kPrimLong: {
2593 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002594 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002595
Roland Levillain232ade02015-04-20 15:14:36 +01002596 // Create stack space for the call to
2597 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2598 // TODO: enhance register allocator to ask for stack temporaries.
2599 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2600 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2601 __ subl(ESP, Immediate(adjustment));
2602 }
2603
2604 // Load the value to the FP stack, using temporaries if needed.
2605 PushOntoFPStack(in, 0, adjustment, false, true);
2606
2607 if (out.IsDoubleStackSlot()) {
2608 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2609 } else {
2610 __ fstpl(Address(ESP, 0));
2611 Location stack_temp = Location::DoubleStackSlot(0);
2612 codegen_->Move64(out, stack_temp);
2613 }
2614
2615 // Remove the temporary stack space we allocated.
2616 if (adjustment != 0) {
2617 __ addl(ESP, Immediate(adjustment));
2618 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002619 break;
2620 }
2621
Roland Levillaincff13742014-11-17 14:32:17 +00002622 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002623 // Processing a Dex `float-to-double' instruction.
2624 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002625 break;
2626
2627 default:
2628 LOG(FATAL) << "Unexpected type conversion from " << input_type
2629 << " to " << result_type;
2630 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002631 break;
2632
2633 default:
2634 LOG(FATAL) << "Unexpected type conversion from " << input_type
2635 << " to " << result_type;
2636 }
2637}
2638
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002639void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002640 LocationSummary* locations =
2641 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002642 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002643 case Primitive::kPrimInt: {
2644 locations->SetInAt(0, Location::RequiresRegister());
2645 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2646 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2647 break;
2648 }
2649
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002650 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002651 locations->SetInAt(0, Location::RequiresRegister());
2652 locations->SetInAt(1, Location::Any());
2653 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002654 break;
2655 }
2656
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002657 case Primitive::kPrimFloat:
2658 case Primitive::kPrimDouble: {
2659 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002660 locations->SetInAt(1, Location::Any());
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();
2724 DCHECK(!const_area->NeedsMaterialization());
2725 __ 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();
2741 DCHECK(!const_area->NeedsMaterialization());
2742 __ 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());
Mark Mendell0616ae02015-04-17 12:49:27 -04002772 locations->SetInAt(1, Location::Any());
Calin Juravle11351682014-10-23 15:38:15 +01002773 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002774 break;
Calin Juravle11351682014-10-23 15:38:15 +01002775 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002776
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002777 default:
Calin Juravle11351682014-10-23 15:38:15 +01002778 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002779 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002780}
2781
2782void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2783 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002784 Location first = locations->InAt(0);
2785 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002786 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002787 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002788 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002789 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002790 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002791 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002792 __ subl(first.AsRegister<Register>(),
2793 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002794 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002795 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002796 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002797 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002798 }
2799
2800 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002801 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002802 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2803 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002804 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002805 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002806 __ sbbl(first.AsRegisterPairHigh<Register>(),
2807 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002808 } else {
2809 DCHECK(second.IsConstant()) << second;
2810 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2811 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2812 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002813 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002814 break;
2815 }
2816
Calin Juravle11351682014-10-23 15:38:15 +01002817 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002818 if (second.IsFpuRegister()) {
2819 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2820 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2821 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2822 DCHECK(!const_area->NeedsMaterialization());
2823 __ subss(first.AsFpuRegister<XmmRegister>(),
2824 codegen_->LiteralFloatAddress(
2825 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2826 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2827 } else {
2828 DCHECK(second.IsStackSlot());
2829 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2830 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002831 break;
Calin Juravle11351682014-10-23 15:38:15 +01002832 }
2833
2834 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002835 if (second.IsFpuRegister()) {
2836 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2837 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2838 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
2839 DCHECK(!const_area->NeedsMaterialization());
2840 __ subsd(first.AsFpuRegister<XmmRegister>(),
2841 codegen_->LiteralDoubleAddress(
2842 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2843 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2844 } else {
2845 DCHECK(second.IsDoubleStackSlot());
2846 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2847 }
Calin Juravle11351682014-10-23 15:38:15 +01002848 break;
2849 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002850
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002851 default:
Calin Juravle11351682014-10-23 15:38:15 +01002852 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002853 }
2854}
2855
Calin Juravle34bacdf2014-10-07 20:23:36 +01002856void LocationsBuilderX86::VisitMul(HMul* mul) {
2857 LocationSummary* locations =
2858 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2859 switch (mul->GetResultType()) {
2860 case Primitive::kPrimInt:
2861 locations->SetInAt(0, Location::RequiresRegister());
2862 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002863 if (mul->InputAt(1)->IsIntConstant()) {
2864 // Can use 3 operand multiply.
2865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2866 } else {
2867 locations->SetOut(Location::SameAsFirstInput());
2868 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002869 break;
2870 case Primitive::kPrimLong: {
2871 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002872 locations->SetInAt(1, Location::Any());
2873 locations->SetOut(Location::SameAsFirstInput());
2874 // Needed for imul on 32bits with 64bits output.
2875 locations->AddTemp(Location::RegisterLocation(EAX));
2876 locations->AddTemp(Location::RegisterLocation(EDX));
2877 break;
2878 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002879 case Primitive::kPrimFloat:
2880 case Primitive::kPrimDouble: {
2881 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04002882 locations->SetInAt(1, Location::Any());
Calin Juravleb5bfa962014-10-21 18:02:24 +01002883 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002884 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002885 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002886
2887 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002888 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002889 }
2890}
2891
2892void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2893 LocationSummary* locations = mul->GetLocations();
2894 Location first = locations->InAt(0);
2895 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002896 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002897
2898 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002899 case Primitive::kPrimInt:
2900 // The constant may have ended up in a register, so test explicitly to avoid
2901 // problems where the output may not be the same as the first operand.
2902 if (mul->InputAt(1)->IsIntConstant()) {
2903 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2904 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
2905 } else if (second.IsRegister()) {
2906 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002907 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002908 } else {
2909 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002910 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002911 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01002912 }
2913 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01002914
2915 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01002916 Register in1_hi = first.AsRegisterPairHigh<Register>();
2917 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002918 Register eax = locations->GetTemp(0).AsRegister<Register>();
2919 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002920
2921 DCHECK_EQ(EAX, eax);
2922 DCHECK_EQ(EDX, edx);
2923
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002924 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01002925 // output: in1
2926 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
2927 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
2928 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002929 if (second.IsConstant()) {
2930 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002931
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002932 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2933 int32_t low_value = Low32Bits(value);
2934 int32_t high_value = High32Bits(value);
2935 Immediate low(low_value);
2936 Immediate high(high_value);
2937
2938 __ movl(eax, high);
2939 // eax <- in1.lo * in2.hi
2940 __ imull(eax, in1_lo);
2941 // in1.hi <- in1.hi * in2.lo
2942 __ imull(in1_hi, low);
2943 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2944 __ addl(in1_hi, eax);
2945 // move in2_lo to eax to prepare for double precision
2946 __ movl(eax, low);
2947 // edx:eax <- in1.lo * in2.lo
2948 __ mull(in1_lo);
2949 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2950 __ addl(in1_hi, edx);
2951 // in1.lo <- (in1.lo * in2.lo)[31:0];
2952 __ movl(in1_lo, eax);
2953 } else if (second.IsRegisterPair()) {
2954 Register in2_hi = second.AsRegisterPairHigh<Register>();
2955 Register in2_lo = second.AsRegisterPairLow<Register>();
2956
2957 __ movl(eax, in2_hi);
2958 // eax <- in1.lo * in2.hi
2959 __ imull(eax, in1_lo);
2960 // in1.hi <- in1.hi * in2.lo
2961 __ imull(in1_hi, in2_lo);
2962 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2963 __ addl(in1_hi, eax);
2964 // move in1_lo to eax to prepare for double precision
2965 __ movl(eax, in1_lo);
2966 // edx:eax <- in1.lo * in2.lo
2967 __ mull(in2_lo);
2968 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2969 __ addl(in1_hi, edx);
2970 // in1.lo <- (in1.lo * in2.lo)[31:0];
2971 __ movl(in1_lo, eax);
2972 } else {
2973 DCHECK(second.IsDoubleStackSlot()) << second;
2974 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
2975 Address in2_lo(ESP, second.GetStackIndex());
2976
2977 __ movl(eax, in2_hi);
2978 // eax <- in1.lo * in2.hi
2979 __ imull(eax, in1_lo);
2980 // in1.hi <- in1.hi * in2.lo
2981 __ imull(in1_hi, in2_lo);
2982 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
2983 __ addl(in1_hi, eax);
2984 // move in1_lo to eax to prepare for double precision
2985 __ movl(eax, in1_lo);
2986 // edx:eax <- in1.lo * in2.lo
2987 __ mull(in2_lo);
2988 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
2989 __ addl(in1_hi, edx);
2990 // in1.lo <- (in1.lo * in2.lo)[31:0];
2991 __ movl(in1_lo, eax);
2992 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002993
2994 break;
2995 }
2996
Calin Juravleb5bfa962014-10-21 18:02:24 +01002997 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002998 DCHECK(first.Equals(locations->Out()));
2999 if (second.IsFpuRegister()) {
3000 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3001 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3002 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3003 DCHECK(!const_area->NeedsMaterialization());
3004 __ mulss(first.AsFpuRegister<XmmRegister>(),
3005 codegen_->LiteralFloatAddress(
3006 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3007 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3008 } else {
3009 DCHECK(second.IsStackSlot());
3010 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3011 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003013 }
3014
3015 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003016 DCHECK(first.Equals(locations->Out()));
3017 if (second.IsFpuRegister()) {
3018 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3019 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3020 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
3021 DCHECK(!const_area->NeedsMaterialization());
3022 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3023 codegen_->LiteralDoubleAddress(
3024 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3025 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3026 } else {
3027 DCHECK(second.IsDoubleStackSlot());
3028 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3029 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003030 break;
3031 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003032
3033 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003034 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003035 }
3036}
3037
Roland Levillain232ade02015-04-20 15:14:36 +01003038void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3039 uint32_t temp_offset,
3040 uint32_t stack_adjustment,
3041 bool is_fp,
3042 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003043 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003044 DCHECK(!is_wide);
3045 if (is_fp) {
3046 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3047 } else {
3048 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3049 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003050 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003051 DCHECK(is_wide);
3052 if (is_fp) {
3053 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3054 } else {
3055 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3056 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003057 } else {
3058 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003059 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003060 Location stack_temp = Location::StackSlot(temp_offset);
3061 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003062 if (is_fp) {
3063 __ flds(Address(ESP, temp_offset));
3064 } else {
3065 __ filds(Address(ESP, temp_offset));
3066 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003067 } else {
3068 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3069 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003070 if (is_fp) {
3071 __ fldl(Address(ESP, temp_offset));
3072 } else {
3073 __ fildl(Address(ESP, temp_offset));
3074 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003075 }
3076 }
3077}
3078
3079void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3080 Primitive::Type type = rem->GetResultType();
3081 bool is_float = type == Primitive::kPrimFloat;
3082 size_t elem_size = Primitive::ComponentSize(type);
3083 LocationSummary* locations = rem->GetLocations();
3084 Location first = locations->InAt(0);
3085 Location second = locations->InAt(1);
3086 Location out = locations->Out();
3087
3088 // Create stack space for 2 elements.
3089 // TODO: enhance register allocator to ask for stack temporaries.
3090 __ subl(ESP, Immediate(2 * elem_size));
3091
3092 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003093 const bool is_wide = !is_float;
3094 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3095 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003096
3097 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003098 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003099 __ Bind(&retry);
3100 __ fprem();
3101
3102 // Move FP status to AX.
3103 __ fstsw();
3104
3105 // And see if the argument reduction is complete. This is signaled by the
3106 // C2 FPU flag bit set to 0.
3107 __ andl(EAX, Immediate(kC2ConditionMask));
3108 __ j(kNotEqual, &retry);
3109
3110 // We have settled on the final value. Retrieve it into an XMM register.
3111 // Store FP top of stack to real stack.
3112 if (is_float) {
3113 __ fsts(Address(ESP, 0));
3114 } else {
3115 __ fstl(Address(ESP, 0));
3116 }
3117
3118 // Pop the 2 items from the FP stack.
3119 __ fucompp();
3120
3121 // Load the value from the stack into an XMM register.
3122 DCHECK(out.IsFpuRegister()) << out;
3123 if (is_float) {
3124 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3125 } else {
3126 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3127 }
3128
3129 // And remove the temporary stack space we allocated.
3130 __ addl(ESP, Immediate(2 * elem_size));
3131}
3132
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003133
3134void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3135 DCHECK(instruction->IsDiv() || instruction->IsRem());
3136
3137 LocationSummary* locations = instruction->GetLocations();
3138 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003139 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003140
3141 Register out_register = locations->Out().AsRegister<Register>();
3142 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003143 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003144
3145 DCHECK(imm == 1 || imm == -1);
3146
3147 if (instruction->IsRem()) {
3148 __ xorl(out_register, out_register);
3149 } else {
3150 __ movl(out_register, input_register);
3151 if (imm == -1) {
3152 __ negl(out_register);
3153 }
3154 }
3155}
3156
3157
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003158void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003159 LocationSummary* locations = instruction->GetLocations();
3160
3161 Register out_register = locations->Out().AsRegister<Register>();
3162 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003163 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003164 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3165 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003166
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003167 Register num = locations->GetTemp(0).AsRegister<Register>();
3168
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003169 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003170 __ testl(input_register, input_register);
3171 __ cmovl(kGreaterEqual, num, input_register);
3172 int shift = CTZ(imm);
3173 __ sarl(num, Immediate(shift));
3174
3175 if (imm < 0) {
3176 __ negl(num);
3177 }
3178
3179 __ movl(out_register, num);
3180}
3181
3182void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3183 DCHECK(instruction->IsDiv() || instruction->IsRem());
3184
3185 LocationSummary* locations = instruction->GetLocations();
3186 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3187
3188 Register eax = locations->InAt(0).AsRegister<Register>();
3189 Register out = locations->Out().AsRegister<Register>();
3190 Register num;
3191 Register edx;
3192
3193 if (instruction->IsDiv()) {
3194 edx = locations->GetTemp(0).AsRegister<Register>();
3195 num = locations->GetTemp(1).AsRegister<Register>();
3196 } else {
3197 edx = locations->Out().AsRegister<Register>();
3198 num = locations->GetTemp(0).AsRegister<Register>();
3199 }
3200
3201 DCHECK_EQ(EAX, eax);
3202 DCHECK_EQ(EDX, edx);
3203 if (instruction->IsDiv()) {
3204 DCHECK_EQ(EAX, out);
3205 } else {
3206 DCHECK_EQ(EDX, out);
3207 }
3208
3209 int64_t magic;
3210 int shift;
3211 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3212
Mark Mendell0c9497d2015-08-21 09:30:05 -04003213 NearLabel ndiv;
3214 NearLabel end;
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003215 // If numerator is 0, the result is 0, no computation needed.
3216 __ testl(eax, eax);
3217 __ j(kNotEqual, &ndiv);
3218
3219 __ xorl(out, out);
3220 __ jmp(&end);
3221
3222 __ Bind(&ndiv);
3223
3224 // Save the numerator.
3225 __ movl(num, eax);
3226
3227 // EAX = magic
3228 __ movl(eax, Immediate(magic));
3229
3230 // EDX:EAX = magic * numerator
3231 __ imull(num);
3232
3233 if (imm > 0 && magic < 0) {
3234 // EDX += num
3235 __ addl(edx, num);
3236 } else if (imm < 0 && magic > 0) {
3237 __ subl(edx, num);
3238 }
3239
3240 // Shift if needed.
3241 if (shift != 0) {
3242 __ sarl(edx, Immediate(shift));
3243 }
3244
3245 // EDX += 1 if EDX < 0
3246 __ movl(eax, edx);
3247 __ shrl(edx, Immediate(31));
3248 __ addl(edx, eax);
3249
3250 if (instruction->IsRem()) {
3251 __ movl(eax, num);
3252 __ imull(edx, Immediate(imm));
3253 __ subl(eax, edx);
3254 __ movl(edx, eax);
3255 } else {
3256 __ movl(eax, edx);
3257 }
3258 __ Bind(&end);
3259}
3260
Calin Juravlebacfec32014-11-14 15:54:36 +00003261void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3262 DCHECK(instruction->IsDiv() || instruction->IsRem());
3263
3264 LocationSummary* locations = instruction->GetLocations();
3265 Location out = locations->Out();
3266 Location first = locations->InAt(0);
3267 Location second = locations->InAt(1);
3268 bool is_div = instruction->IsDiv();
3269
3270 switch (instruction->GetResultType()) {
3271 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003272 DCHECK_EQ(EAX, first.AsRegister<Register>());
3273 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003274
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003275 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003276 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003277
3278 if (imm == 0) {
3279 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3280 } else if (imm == 1 || imm == -1) {
3281 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003282 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003283 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003284 } else {
3285 DCHECK(imm <= -2 || imm >= 2);
3286 GenerateDivRemWithAnyConstant(instruction);
3287 }
3288 } else {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003289 SlowPathCode* slow_path =
Roland Levillain199f3362014-11-27 17:15:16 +00003290 new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(out.AsRegister<Register>(),
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003291 is_div);
3292 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003293
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003294 Register second_reg = second.AsRegister<Register>();
3295 // 0x80000000/-1 triggers an arithmetic exception!
3296 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3297 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003298
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003299 __ cmpl(second_reg, Immediate(-1));
3300 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003301
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003302 // edx:eax <- sign-extended of eax
3303 __ cdq();
3304 // eax = quotient, edx = remainder
3305 __ idivl(second_reg);
3306 __ Bind(slow_path->GetExitLabel());
3307 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003308 break;
3309 }
3310
3311 case Primitive::kPrimLong: {
3312 InvokeRuntimeCallingConvention calling_convention;
3313 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3314 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3315 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3316 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3317 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3318 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3319
3320 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003321 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3322 instruction,
3323 instruction->GetDexPc(),
3324 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003325 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003326 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003327 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3328 instruction,
3329 instruction->GetDexPc(),
3330 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003331 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003332 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003333 break;
3334 }
3335
3336 default:
3337 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3338 }
3339}
3340
Calin Juravle7c4954d2014-10-28 16:57:40 +00003341void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003342 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003343 ? LocationSummary::kCall
3344 : LocationSummary::kNoCall;
3345 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3346
Calin Juravle7c4954d2014-10-28 16:57:40 +00003347 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003348 case Primitive::kPrimInt: {
3349 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003350 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003351 locations->SetOut(Location::SameAsFirstInput());
3352 // Intel uses edx:eax as the dividend.
3353 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003354 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3355 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3356 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003357 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003358 locations->AddTemp(Location::RequiresRegister());
3359 }
Calin Juravled0d48522014-11-04 16:40:20 +00003360 break;
3361 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003362 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003363 InvokeRuntimeCallingConvention calling_convention;
3364 locations->SetInAt(0, Location::RegisterPairLocation(
3365 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3366 locations->SetInAt(1, Location::RegisterPairLocation(
3367 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3368 // Runtime helper puts the result in EAX, EDX.
3369 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003370 break;
3371 }
3372 case Primitive::kPrimFloat:
3373 case Primitive::kPrimDouble: {
3374 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0616ae02015-04-17 12:49:27 -04003375 locations->SetInAt(1, Location::Any());
Calin Juravle7c4954d2014-10-28 16:57:40 +00003376 locations->SetOut(Location::SameAsFirstInput());
3377 break;
3378 }
3379
3380 default:
3381 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3382 }
3383}
3384
3385void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3386 LocationSummary* locations = div->GetLocations();
3387 Location first = locations->InAt(0);
3388 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003389
3390 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003391 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003392 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003393 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003394 break;
3395 }
3396
3397 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003398 if (second.IsFpuRegister()) {
3399 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3400 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3401 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3402 DCHECK(!const_area->NeedsMaterialization());
3403 __ divss(first.AsFpuRegister<XmmRegister>(),
3404 codegen_->LiteralFloatAddress(
3405 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3406 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3407 } else {
3408 DCHECK(second.IsStackSlot());
3409 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3410 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003411 break;
3412 }
3413
3414 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003415 if (second.IsFpuRegister()) {
3416 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3417 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3418 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
3419 DCHECK(!const_area->NeedsMaterialization());
3420 __ divsd(first.AsFpuRegister<XmmRegister>(),
3421 codegen_->LiteralDoubleAddress(
3422 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3423 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3424 } else {
3425 DCHECK(second.IsDoubleStackSlot());
3426 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3427 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003428 break;
3429 }
3430
3431 default:
3432 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3433 }
3434}
3435
Calin Juravlebacfec32014-11-14 15:54:36 +00003436void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003437 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003438
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003439 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3440 ? LocationSummary::kCall
3441 : LocationSummary::kNoCall;
3442 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003443
Calin Juravled2ec87d2014-12-08 14:24:46 +00003444 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003445 case Primitive::kPrimInt: {
3446 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003448 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003449 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3450 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3451 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003452 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453 locations->AddTemp(Location::RequiresRegister());
3454 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003455 break;
3456 }
3457 case Primitive::kPrimLong: {
3458 InvokeRuntimeCallingConvention calling_convention;
3459 locations->SetInAt(0, Location::RegisterPairLocation(
3460 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3461 locations->SetInAt(1, Location::RegisterPairLocation(
3462 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3463 // Runtime helper puts the result in EAX, EDX.
3464 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3465 break;
3466 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003467 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003468 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003469 locations->SetInAt(0, Location::Any());
3470 locations->SetInAt(1, Location::Any());
3471 locations->SetOut(Location::RequiresFpuRegister());
3472 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003473 break;
3474 }
3475
3476 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003477 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003478 }
3479}
3480
3481void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3482 Primitive::Type type = rem->GetResultType();
3483 switch (type) {
3484 case Primitive::kPrimInt:
3485 case Primitive::kPrimLong: {
3486 GenerateDivRemIntegral(rem);
3487 break;
3488 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003489 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003490 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003491 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003492 break;
3493 }
3494 default:
3495 LOG(FATAL) << "Unexpected rem type " << type;
3496 }
3497}
3498
Calin Juravled0d48522014-11-04 16:40:20 +00003499void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003500 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3501 ? LocationSummary::kCallOnSlowPath
3502 : LocationSummary::kNoCall;
3503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003504 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003505 case Primitive::kPrimByte:
3506 case Primitive::kPrimChar:
3507 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003508 case Primitive::kPrimInt: {
3509 locations->SetInAt(0, Location::Any());
3510 break;
3511 }
3512 case Primitive::kPrimLong: {
3513 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3514 if (!instruction->IsConstant()) {
3515 locations->AddTemp(Location::RequiresRegister());
3516 }
3517 break;
3518 }
3519 default:
3520 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3521 }
Calin Juravled0d48522014-11-04 16:40:20 +00003522 if (instruction->HasUses()) {
3523 locations->SetOut(Location::SameAsFirstInput());
3524 }
3525}
3526
3527void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003528 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003529 codegen_->AddSlowPath(slow_path);
3530
3531 LocationSummary* locations = instruction->GetLocations();
3532 Location value = locations->InAt(0);
3533
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003534 switch (instruction->GetType()) {
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003535 case Primitive::kPrimByte:
3536 case Primitive::kPrimChar:
3537 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003538 case Primitive::kPrimInt: {
3539 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003540 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003541 __ j(kEqual, slow_path->GetEntryLabel());
3542 } else if (value.IsStackSlot()) {
3543 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3544 __ j(kEqual, slow_path->GetEntryLabel());
3545 } else {
3546 DCHECK(value.IsConstant()) << value;
3547 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3548 __ jmp(slow_path->GetEntryLabel());
3549 }
3550 }
3551 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003552 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003553 case Primitive::kPrimLong: {
3554 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003555 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003556 __ movl(temp, value.AsRegisterPairLow<Register>());
3557 __ orl(temp, value.AsRegisterPairHigh<Register>());
3558 __ j(kEqual, slow_path->GetEntryLabel());
3559 } else {
3560 DCHECK(value.IsConstant()) << value;
3561 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3562 __ jmp(slow_path->GetEntryLabel());
3563 }
3564 }
3565 break;
3566 }
3567 default:
3568 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003569 }
Calin Juravled0d48522014-11-04 16:40:20 +00003570}
3571
Calin Juravle9aec02f2014-11-18 23:06:35 +00003572void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3573 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3574
3575 LocationSummary* locations =
3576 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3577
3578 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003579 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003580 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003581 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003582 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003583 // The shift count needs to be in CL or a constant.
3584 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003585 locations->SetOut(Location::SameAsFirstInput());
3586 break;
3587 }
3588 default:
3589 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3590 }
3591}
3592
3593void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3594 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3595
3596 LocationSummary* locations = op->GetLocations();
3597 Location first = locations->InAt(0);
3598 Location second = locations->InAt(1);
3599 DCHECK(first.Equals(locations->Out()));
3600
3601 switch (op->GetResultType()) {
3602 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003603 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003604 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003605 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003606 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003607 DCHECK_EQ(ECX, second_reg);
3608 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003609 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003610 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003611 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003612 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003613 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003614 }
3615 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003616 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue;
3617 if (shift == 0) {
3618 return;
3619 }
3620 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003621 if (op->IsShl()) {
3622 __ shll(first_reg, imm);
3623 } else if (op->IsShr()) {
3624 __ sarl(first_reg, imm);
3625 } else {
3626 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003627 }
3628 }
3629 break;
3630 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003631 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003632 if (second.IsRegister()) {
3633 Register second_reg = second.AsRegister<Register>();
3634 DCHECK_EQ(ECX, second_reg);
3635 if (op->IsShl()) {
3636 GenerateShlLong(first, second_reg);
3637 } else if (op->IsShr()) {
3638 GenerateShrLong(first, second_reg);
3639 } else {
3640 GenerateUShrLong(first, second_reg);
3641 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003642 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003643 // Shift by a constant.
3644 int shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftValue;
3645 // Nothing to do if the shift is 0, as the input is already the output.
3646 if (shift != 0) {
3647 if (op->IsShl()) {
3648 GenerateShlLong(first, shift);
3649 } else if (op->IsShr()) {
3650 GenerateShrLong(first, shift);
3651 } else {
3652 GenerateUShrLong(first, shift);
3653 }
3654 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003655 }
3656 break;
3657 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003658 default:
3659 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3660 }
3661}
3662
Mark P Mendell73945692015-04-29 14:56:17 +00003663void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3664 Register low = loc.AsRegisterPairLow<Register>();
3665 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003666 if (shift == 1) {
3667 // This is just an addition.
3668 __ addl(low, low);
3669 __ adcl(high, high);
3670 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003671 // Shift by 32 is easy. High gets low, and low gets 0.
3672 codegen_->EmitParallelMoves(
3673 loc.ToLow(),
3674 loc.ToHigh(),
3675 Primitive::kPrimInt,
3676 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3677 loc.ToLow(),
3678 Primitive::kPrimInt);
3679 } else if (shift > 32) {
3680 // Low part becomes 0. High part is low part << (shift-32).
3681 __ movl(high, low);
3682 __ shll(high, Immediate(shift - 32));
3683 __ xorl(low, low);
3684 } else {
3685 // Between 1 and 31.
3686 __ shld(high, low, Immediate(shift));
3687 __ shll(low, Immediate(shift));
3688 }
3689}
3690
Calin Juravle9aec02f2014-11-18 23:06:35 +00003691void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003692 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003693 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3694 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3695 __ testl(shifter, Immediate(32));
3696 __ j(kEqual, &done);
3697 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3698 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3699 __ Bind(&done);
3700}
3701
Mark P Mendell73945692015-04-29 14:56:17 +00003702void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3703 Register low = loc.AsRegisterPairLow<Register>();
3704 Register high = loc.AsRegisterPairHigh<Register>();
3705 if (shift == 32) {
3706 // Need to copy the sign.
3707 DCHECK_NE(low, high);
3708 __ movl(low, high);
3709 __ sarl(high, Immediate(31));
3710 } else if (shift > 32) {
3711 DCHECK_NE(low, high);
3712 // High part becomes sign. Low part is shifted by shift - 32.
3713 __ movl(low, high);
3714 __ sarl(high, Immediate(31));
3715 __ sarl(low, Immediate(shift - 32));
3716 } else {
3717 // Between 1 and 31.
3718 __ shrd(low, high, Immediate(shift));
3719 __ sarl(high, Immediate(shift));
3720 }
3721}
3722
Calin Juravle9aec02f2014-11-18 23:06:35 +00003723void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003724 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003725 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3726 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3727 __ testl(shifter, Immediate(32));
3728 __ j(kEqual, &done);
3729 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3730 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3731 __ Bind(&done);
3732}
3733
Mark P Mendell73945692015-04-29 14:56:17 +00003734void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3735 Register low = loc.AsRegisterPairLow<Register>();
3736 Register high = loc.AsRegisterPairHigh<Register>();
3737 if (shift == 32) {
3738 // Shift by 32 is easy. Low gets high, and high gets 0.
3739 codegen_->EmitParallelMoves(
3740 loc.ToHigh(),
3741 loc.ToLow(),
3742 Primitive::kPrimInt,
3743 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3744 loc.ToHigh(),
3745 Primitive::kPrimInt);
3746 } else if (shift > 32) {
3747 // Low part is high >> (shift - 32). High part becomes 0.
3748 __ movl(low, high);
3749 __ shrl(low, Immediate(shift - 32));
3750 __ xorl(high, high);
3751 } else {
3752 // Between 1 and 31.
3753 __ shrd(low, high, Immediate(shift));
3754 __ shrl(high, Immediate(shift));
3755 }
3756}
3757
Calin Juravle9aec02f2014-11-18 23:06:35 +00003758void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003759 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003760 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3761 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3762 __ testl(shifter, Immediate(32));
3763 __ j(kEqual, &done);
3764 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3765 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3766 __ Bind(&done);
3767}
3768
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003769void LocationsBuilderX86::VisitRor(HRor* ror) {
3770 LocationSummary* locations =
3771 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3772
3773 switch (ror->GetResultType()) {
3774 case Primitive::kPrimLong:
3775 // Add the temporary needed.
3776 locations->AddTemp(Location::RequiresRegister());
3777 FALLTHROUGH_INTENDED;
3778 case Primitive::kPrimInt:
3779 locations->SetInAt(0, Location::RequiresRegister());
3780 // The shift count needs to be in CL (unless it is a constant).
3781 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3782 locations->SetOut(Location::SameAsFirstInput());
3783 break;
3784 default:
3785 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3786 UNREACHABLE();
3787 }
3788}
3789
3790void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3791 LocationSummary* locations = ror->GetLocations();
3792 Location first = locations->InAt(0);
3793 Location second = locations->InAt(1);
3794
3795 if (ror->GetResultType() == Primitive::kPrimInt) {
3796 Register first_reg = first.AsRegister<Register>();
3797 if (second.IsRegister()) {
3798 Register second_reg = second.AsRegister<Register>();
3799 __ rorl(first_reg, second_reg);
3800 } else {
3801 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftValue);
3802 __ rorl(first_reg, imm);
3803 }
3804 return;
3805 }
3806
3807 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3808 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3809 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3810 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3811 if (second.IsRegister()) {
3812 Register second_reg = second.AsRegister<Register>();
3813 DCHECK_EQ(second_reg, ECX);
3814 __ movl(temp_reg, first_reg_hi);
3815 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3816 __ shrd(first_reg_lo, temp_reg, second_reg);
3817 __ movl(temp_reg, first_reg_hi);
3818 __ testl(second_reg, Immediate(32));
3819 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3820 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3821 } else {
3822 int32_t shift_amt =
3823 CodeGenerator::GetInt64ValueOf(second.GetConstant()) & kMaxLongShiftValue;
3824 if (shift_amt == 0) {
3825 // Already fine.
3826 return;
3827 }
3828 if (shift_amt == 32) {
3829 // Just swap.
3830 __ movl(temp_reg, first_reg_lo);
3831 __ movl(first_reg_lo, first_reg_hi);
3832 __ movl(first_reg_hi, temp_reg);
3833 return;
3834 }
3835
3836 Immediate imm(shift_amt);
3837 // Save the constents of the low value.
3838 __ movl(temp_reg, first_reg_lo);
3839
3840 // Shift right into low, feeding bits from high.
3841 __ shrd(first_reg_lo, first_reg_hi, imm);
3842
3843 // Shift right into high, feeding bits from the original low.
3844 __ shrd(first_reg_hi, temp_reg, imm);
3845
3846 // Swap if needed.
3847 if (shift_amt > 32) {
3848 __ movl(temp_reg, first_reg_lo);
3849 __ movl(first_reg_lo, first_reg_hi);
3850 __ movl(first_reg_hi, temp_reg);
3851 }
3852 }
3853}
3854
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855void LocationsBuilderX86::VisitShl(HShl* shl) {
3856 HandleShift(shl);
3857}
3858
3859void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3860 HandleShift(shl);
3861}
3862
3863void LocationsBuilderX86::VisitShr(HShr* shr) {
3864 HandleShift(shr);
3865}
3866
3867void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3868 HandleShift(shr);
3869}
3870
3871void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3872 HandleShift(ushr);
3873}
3874
3875void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3876 HandleShift(ushr);
3877}
3878
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003879void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003880 LocationSummary* locations =
3881 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003882 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003883 if (instruction->IsStringAlloc()) {
3884 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3885 } else {
3886 InvokeRuntimeCallingConvention calling_convention;
3887 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3888 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3889 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003890}
3891
3892void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003893 // Note: if heap poisoning is enabled, the entry point takes cares
3894 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003895 if (instruction->IsStringAlloc()) {
3896 // String is allocated through StringFactory. Call NewEmptyString entry point.
3897 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3898 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3899 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3900 __ call(Address(temp, code_offset.Int32Value()));
3901 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3902 } else {
3903 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3904 instruction,
3905 instruction->GetDexPc(),
3906 nullptr);
3907 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3908 DCHECK(!codegen_->IsLeafMethod());
3909 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003910}
3911
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003912void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
3913 LocationSummary* locations =
3914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
3915 locations->SetOut(Location::RegisterLocation(EAX));
3916 InvokeRuntimeCallingConvention calling_convention;
3917 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08003918 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01003919 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003920}
3921
3922void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
3923 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003924 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01003925 // Note: if heap poisoning is enabled, the entry point takes cares
3926 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01003927 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3928 instruction,
3929 instruction->GetDexPc(),
3930 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003931 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01003932 DCHECK(!codegen_->IsLeafMethod());
3933}
3934
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003935void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003936 LocationSummary* locations =
3937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003938 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
3939 if (location.IsStackSlot()) {
3940 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
3941 } else if (location.IsDoubleStackSlot()) {
3942 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003943 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01003944 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003945}
3946
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01003947void InstructionCodeGeneratorX86::VisitParameterValue(
3948 HParameterValue* instruction ATTRIBUTE_UNUSED) {
3949}
3950
3951void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
3952 LocationSummary* locations =
3953 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3954 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
3955}
3956
3957void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003958}
3959
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00003960void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
3961 LocationSummary* locations =
3962 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
3963 locations->SetInAt(0, Location::RequiresRegister());
3964 locations->SetOut(Location::RequiresRegister());
3965}
3966
3967void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
3968 LocationSummary* locations = instruction->GetLocations();
3969 uint32_t method_offset = 0;
3970 if (instruction->GetTableKind() == HClassTableGet::kVTable) {
3971 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
3972 instruction->GetIndex(), kX86PointerSize).SizeValue();
3973 } else {
3974 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
3975 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
3976 }
3977 __ movl(locations->Out().AsRegister<Register>(),
3978 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
3979}
3980
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003981void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003982 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003983 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003984 locations->SetInAt(0, Location::RequiresRegister());
3985 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01003986}
3987
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003988void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
3989 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01003990 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003991 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01003992 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00003993 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003994 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00003995 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01003996 break;
3997
3998 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01003999 __ notl(out.AsRegisterPairLow<Register>());
4000 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004001 break;
4002
4003 default:
4004 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4005 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004006}
4007
David Brazdil66d126e2015-04-03 16:02:44 +01004008void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4009 LocationSummary* locations =
4010 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4011 locations->SetInAt(0, Location::RequiresRegister());
4012 locations->SetOut(Location::SameAsFirstInput());
4013}
4014
4015void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004016 LocationSummary* locations = bool_not->GetLocations();
4017 Location in = locations->InAt(0);
4018 Location out = locations->Out();
4019 DCHECK(in.Equals(out));
4020 __ xorl(out.AsRegister<Register>(), Immediate(1));
4021}
4022
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004023void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004024 LocationSummary* locations =
4025 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004026 switch (compare->InputAt(0)->GetType()) {
4027 case Primitive::kPrimLong: {
4028 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004029 locations->SetInAt(1, Location::Any());
4030 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4031 break;
4032 }
4033 case Primitive::kPrimFloat:
4034 case Primitive::kPrimDouble: {
4035 locations->SetInAt(0, Location::RequiresFpuRegister());
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00004036 locations->SetInAt(1, Location::RequiresFpuRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004037 locations->SetOut(Location::RequiresRegister());
4038 break;
4039 }
4040 default:
4041 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4042 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004043}
4044
4045void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004046 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004047 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004048 Location left = locations->InAt(0);
4049 Location right = locations->InAt(1);
4050
Mark Mendell0c9497d2015-08-21 09:30:05 -04004051 NearLabel less, greater, done;
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004052 switch (compare->InputAt(0)->GetType()) {
4053 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004054 Register left_low = left.AsRegisterPairLow<Register>();
4055 Register left_high = left.AsRegisterPairHigh<Register>();
4056 int32_t val_low = 0;
4057 int32_t val_high = 0;
4058 bool right_is_const = false;
4059
4060 if (right.IsConstant()) {
4061 DCHECK(right.GetConstant()->IsLongConstant());
4062 right_is_const = true;
4063 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4064 val_low = Low32Bits(val);
4065 val_high = High32Bits(val);
4066 }
4067
Calin Juravleddb7df22014-11-25 20:56:51 +00004068 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004069 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004070 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004071 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004072 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004073 DCHECK(right_is_const) << right;
4074 if (val_high == 0) {
4075 __ testl(left_high, left_high);
4076 } else {
4077 __ cmpl(left_high, Immediate(val_high));
4078 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004079 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004080 __ j(kLess, &less); // Signed compare.
4081 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004082 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004083 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004084 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004085 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004086 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004087 DCHECK(right_is_const) << right;
4088 if (val_low == 0) {
4089 __ testl(left_low, left_low);
4090 } else {
4091 __ cmpl(left_low, Immediate(val_low));
4092 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004093 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004094 break;
4095 }
4096 case Primitive::kPrimFloat: {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00004097 __ ucomiss(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004098 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
4099 break;
4100 }
4101 case Primitive::kPrimDouble: {
Nicolas Geoffraycf8d1bb2016-01-25 09:43:30 +00004102 __ ucomisd(left.AsFpuRegister<XmmRegister>(), right.AsFpuRegister<XmmRegister>());
Calin Juravleddb7df22014-11-25 20:56:51 +00004103 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004104 break;
4105 }
4106 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004107 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004108 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004109 __ movl(out, Immediate(0));
4110 __ j(kEqual, &done);
4111 __ j(kBelow, &less); // kBelow is for CF (unsigned & floats).
4112
4113 __ Bind(&greater);
4114 __ movl(out, Immediate(1));
4115 __ jmp(&done);
4116
4117 __ Bind(&less);
4118 __ movl(out, Immediate(-1));
4119
4120 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004121}
4122
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004123void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004124 LocationSummary* locations =
4125 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004126 for (size_t i = 0, e = instruction->InputCount(); i < e; ++i) {
4127 locations->SetInAt(i, Location::Any());
4128 }
4129 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004130}
4131
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004132void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004133 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004134}
4135
Roland Levillain7c1559a2015-12-15 10:55:36 +00004136void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004137 /*
4138 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4139 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4140 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4141 */
4142 switch (kind) {
4143 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004144 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004145 break;
4146 }
4147 case MemBarrierKind::kAnyStore:
4148 case MemBarrierKind::kLoadAny:
4149 case MemBarrierKind::kStoreStore: {
4150 // nop
4151 break;
4152 }
4153 default:
4154 LOG(FATAL) << "Unexpected memory barrier " << kind;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004155 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004156}
4157
Vladimir Markodc151b22015-10-15 18:02:30 +01004158HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4159 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4160 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004161 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4162
4163 // We disable pc-relative load when there is an irreducible loop, as the optimization
4164 // is incompatible with it.
4165 if (GetGraph()->HasIrreducibleLoops() &&
4166 (dispatch_info.method_load_kind ==
4167 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4168 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4169 }
4170 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004171 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4172 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4173 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4174 // (Though the direct CALL ptr16:32 is available for consideration).
4175 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004176 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004177 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004178 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004179 0u
4180 };
4181 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004182 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004183 }
4184}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004186Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4187 Register temp) {
4188 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004189 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004190 if (!invoke->GetLocations()->Intrinsified()) {
4191 return location.AsRegister<Register>();
4192 }
4193 // For intrinsics we allow any location, so it may be on the stack.
4194 if (!location.IsRegister()) {
4195 __ movl(temp, Address(ESP, location.GetStackIndex()));
4196 return temp;
4197 }
4198 // For register locations, check if the register was saved. If so, get it from the stack.
4199 // Note: There is a chance that the register was saved but not overwritten, so we could
4200 // save one load. However, since this is just an intrinsic slow path we prefer this
4201 // simple and more robust approach rather that trying to determine if that's the case.
4202 SlowPathCode* slow_path = GetCurrentSlowPath();
4203 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
4204 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4205 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4206 __ movl(temp, Address(ESP, stack_offset));
4207 return temp;
4208 }
4209 return location.AsRegister<Register>();
4210}
4211
Vladimir Marko58155012015-08-19 12:49:41 +00004212void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4213 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4214 switch (invoke->GetMethodLoadKind()) {
4215 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4216 // temp = thread->string_init_entrypoint
4217 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4218 break;
4219 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004220 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004221 break;
4222 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4223 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4224 break;
4225 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
4226 __ movl(temp.AsRegister<Register>(), Immediate(0)); // Placeholder.
4227 method_patches_.emplace_back(invoke->GetTargetMethod());
4228 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4229 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004230 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4231 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4232 temp.AsRegister<Register>());
4233 uint32_t offset = invoke->GetDexCacheArrayOffset();
4234 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
4235 // Add the patch entry and bind its label at the end of the instruction.
4236 pc_relative_dex_cache_patches_.emplace_back(*invoke->GetTargetMethod().dex_file, offset);
4237 __ Bind(&pc_relative_dex_cache_patches_.back().label);
4238 break;
4239 }
Vladimir Marko58155012015-08-19 12:49:41 +00004240 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004241 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004242 Register method_reg;
4243 Register reg = temp.AsRegister<Register>();
4244 if (current_method.IsRegister()) {
4245 method_reg = current_method.AsRegister<Register>();
4246 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004247 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004248 DCHECK(!current_method.IsValid());
4249 method_reg = reg;
4250 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4251 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004252 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004253 __ movl(reg, Address(method_reg,
4254 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004255 // temp = temp[index_in_cache]
4256 uint32_t index_in_cache = invoke->GetTargetMethod().dex_method_index;
4257 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4258 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004259 }
Vladimir Marko58155012015-08-19 12:49:41 +00004260 }
4261
4262 switch (invoke->GetCodePtrLocation()) {
4263 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4264 __ call(GetFrameEntryLabel());
4265 break;
4266 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4267 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4268 Label* label = &relative_call_patches_.back().label;
4269 __ call(label); // Bind to the patch label, override at link time.
4270 __ Bind(label); // Bind the label at the end of the "call" insn.
4271 break;
4272 }
4273 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4274 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004275 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4276 LOG(FATAL) << "Unsupported";
4277 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004278 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4279 // (callee_method + offset_of_quick_compiled_code)()
4280 __ call(Address(callee_method.AsRegister<Register>(),
4281 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4282 kX86WordSize).Int32Value()));
4283 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004284 }
4285
4286 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004287}
4288
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004289void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4290 Register temp = temp_in.AsRegister<Register>();
4291 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4292 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004293
4294 // Use the calling convention instead of the location of the receiver, as
4295 // intrinsics may have put the receiver in a different register. In the intrinsics
4296 // slow path, the arguments have been moved to the right place, so here we are
4297 // guaranteed that the receiver is the first register of the calling convention.
4298 InvokeDexCallingConvention calling_convention;
4299 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004300 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004301 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004302 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004303 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004304 // Instead of simply (possibly) unpoisoning `temp` here, we should
4305 // emit a read barrier for the previous class reference load.
4306 // However this is not required in practice, as this is an
4307 // intermediate/temporary reference and because the current
4308 // concurrent copying collector keeps the from-space memory
4309 // intact/accessible until the end of the marking phase (the
4310 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004311 __ MaybeUnpoisonHeapReference(temp);
4312 // temp = temp->GetMethodAt(method_offset);
4313 __ movl(temp, Address(temp, method_offset));
4314 // call temp->GetEntryPoint();
4315 __ call(Address(
4316 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4317}
4318
Vladimir Marko58155012015-08-19 12:49:41 +00004319void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4320 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004321 size_t size =
4322 method_patches_.size() +
4323 relative_call_patches_.size() +
4324 pc_relative_dex_cache_patches_.size();
4325 linker_patches->reserve(size);
4326 // The label points to the end of the "movl" insn but the literal offset for method
4327 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4328 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004329 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004330 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004331 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4332 info.target_method.dex_file,
4333 info.target_method.dex_method_index));
4334 }
4335 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004336 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004337 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4338 info.target_method.dex_file,
4339 info.target_method.dex_method_index));
4340 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004341 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4342 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4343 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4344 &info.target_dex_file,
4345 GetMethodAddressOffset(),
4346 info.element_offset));
4347 }
Vladimir Marko58155012015-08-19 12:49:41 +00004348}
4349
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004350void CodeGeneratorX86::MarkGCCard(Register temp,
4351 Register card,
4352 Register object,
4353 Register value,
4354 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004355 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004356 if (value_can_be_null) {
4357 __ testl(value, value);
4358 __ j(kEqual, &is_null);
4359 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004360 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4361 __ movl(temp, object);
4362 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004363 __ movb(Address(temp, card, TIMES_1, 0),
4364 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004365 if (value_can_be_null) {
4366 __ Bind(&is_null);
4367 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004368}
4369
Calin Juravle52c48962014-12-16 17:02:57 +00004370void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4371 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004372
4373 bool object_field_get_with_read_barrier =
4374 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004375 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004376 new (GetGraph()->GetArena()) LocationSummary(instruction,
4377 kEmitCompilerReadBarrier ?
4378 LocationSummary::kCallOnSlowPath :
4379 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004380 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004381
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004382 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4383 locations->SetOut(Location::RequiresFpuRegister());
4384 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004385 // The output overlaps in case of long: we don't want the low move
4386 // to overwrite the object's location. Likewise, in the case of
4387 // an object field get with read barriers enabled, we do not want
4388 // the move to overwrite the object's location, as we need it to emit
4389 // the read barrier.
4390 locations->SetOut(
4391 Location::RequiresRegister(),
4392 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4393 Location::kOutputOverlap :
4394 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004395 }
Calin Juravle52c48962014-12-16 17:02:57 +00004396
4397 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4398 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004399 // So we use an XMM register as a temp to achieve atomicity (first
4400 // load the temp into the XMM and then copy the XMM into the
4401 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004402 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004403 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4404 // We need a temporary register for the read barrier marking slow
4405 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4406 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004407 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004408}
4409
Calin Juravle52c48962014-12-16 17:02:57 +00004410void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4411 const FieldInfo& field_info) {
4412 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004413
Calin Juravle52c48962014-12-16 17:02:57 +00004414 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004415 Location base_loc = locations->InAt(0);
4416 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004417 Location out = locations->Out();
4418 bool is_volatile = field_info.IsVolatile();
4419 Primitive::Type field_type = field_info.GetFieldType();
4420 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4421
4422 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004423 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004424 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004425 break;
4426 }
4427
4428 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004429 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004430 break;
4431 }
4432
4433 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004434 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004435 break;
4436 }
4437
4438 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004439 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440 break;
4441 }
4442
4443 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004444 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004445 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004446
4447 case Primitive::kPrimNot: {
4448 // /* HeapReference<Object> */ out = *(base + offset)
4449 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4450 Location temp_loc = locations->GetTemp(0);
4451 // Note that a potential implicit null check is handled in this
4452 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4453 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4454 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4455 if (is_volatile) {
4456 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4457 }
4458 } else {
4459 __ movl(out.AsRegister<Register>(), Address(base, offset));
4460 codegen_->MaybeRecordImplicitNullCheck(instruction);
4461 if (is_volatile) {
4462 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4463 }
4464 // If read barriers are enabled, emit read barriers other than
4465 // Baker's using a slow path (and also unpoison the loaded
4466 // reference, if heap poisoning is enabled).
4467 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4468 }
4469 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004470 }
4471
4472 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004473 if (is_volatile) {
4474 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4475 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004476 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004477 __ movd(out.AsRegisterPairLow<Register>(), temp);
4478 __ psrlq(temp, Immediate(32));
4479 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4480 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004481 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004482 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004483 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004484 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4485 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004486 break;
4487 }
4488
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004489 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004490 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004491 break;
4492 }
4493
4494 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004495 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004496 break;
4497 }
4498
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004499 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004500 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004501 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004502 }
Calin Juravle52c48962014-12-16 17:02:57 +00004503
Roland Levillain7c1559a2015-12-15 10:55:36 +00004504 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4505 // Potential implicit null checks, in the case of reference or
4506 // long fields, are handled in the previous switch statement.
4507 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004508 codegen_->MaybeRecordImplicitNullCheck(instruction);
4509 }
4510
Calin Juravle52c48962014-12-16 17:02:57 +00004511 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004512 if (field_type == Primitive::kPrimNot) {
4513 // Memory barriers, in the case of references, are also handled
4514 // in the previous switch statement.
4515 } else {
4516 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4517 }
Roland Levillain4d027112015-07-01 15:41:14 +01004518 }
Calin Juravle52c48962014-12-16 17:02:57 +00004519}
4520
4521void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4522 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4523
4524 LocationSummary* locations =
4525 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4526 locations->SetInAt(0, Location::RequiresRegister());
4527 bool is_volatile = field_info.IsVolatile();
4528 Primitive::Type field_type = field_info.GetFieldType();
4529 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4530 || (field_type == Primitive::kPrimByte);
4531
4532 // The register allocator does not support multiple
4533 // inputs that die at entry with one in a specific register.
4534 if (is_byte_type) {
4535 // Ensure the value is in a byte register.
4536 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004537 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004538 if (is_volatile && field_type == Primitive::kPrimDouble) {
4539 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4540 locations->SetInAt(1, Location::RequiresFpuRegister());
4541 } else {
4542 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4543 }
4544 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4545 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004546 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004547
Calin Juravle52c48962014-12-16 17:02:57 +00004548 // 64bits value can be atomically written to an address with movsd and an XMM register.
4549 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4550 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4551 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4552 // isolated cases when we need this it isn't worth adding the extra complexity.
4553 locations->AddTemp(Location::RequiresFpuRegister());
4554 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004555 } else {
4556 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4557
4558 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4559 // Temporary registers for the write barrier.
4560 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4561 // Ensure the card is in a byte register.
4562 locations->AddTemp(Location::RegisterLocation(ECX));
4563 }
Calin Juravle52c48962014-12-16 17:02:57 +00004564 }
4565}
4566
4567void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004568 const FieldInfo& field_info,
4569 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004570 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4571
4572 LocationSummary* locations = instruction->GetLocations();
4573 Register base = locations->InAt(0).AsRegister<Register>();
4574 Location value = locations->InAt(1);
4575 bool is_volatile = field_info.IsVolatile();
4576 Primitive::Type field_type = field_info.GetFieldType();
4577 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004578 bool needs_write_barrier =
4579 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004580
4581 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004582 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004583 }
4584
Mark Mendell81489372015-11-04 11:30:41 -05004585 bool maybe_record_implicit_null_check_done = false;
4586
Calin Juravle52c48962014-12-16 17:02:57 +00004587 switch (field_type) {
4588 case Primitive::kPrimBoolean:
4589 case Primitive::kPrimByte: {
4590 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4591 break;
4592 }
4593
4594 case Primitive::kPrimShort:
4595 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004596 if (value.IsConstant()) {
4597 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4598 __ movw(Address(base, offset), Immediate(v));
4599 } else {
4600 __ movw(Address(base, offset), value.AsRegister<Register>());
4601 }
Calin Juravle52c48962014-12-16 17:02:57 +00004602 break;
4603 }
4604
4605 case Primitive::kPrimInt:
4606 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004607 if (kPoisonHeapReferences && needs_write_barrier) {
4608 // Note that in the case where `value` is a null reference,
4609 // we do not enter this block, as the reference does not
4610 // need poisoning.
4611 DCHECK_EQ(field_type, Primitive::kPrimNot);
4612 Register temp = locations->GetTemp(0).AsRegister<Register>();
4613 __ movl(temp, value.AsRegister<Register>());
4614 __ PoisonHeapReference(temp);
4615 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004616 } else if (value.IsConstant()) {
4617 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4618 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004619 } else {
4620 __ movl(Address(base, offset), value.AsRegister<Register>());
4621 }
Calin Juravle52c48962014-12-16 17:02:57 +00004622 break;
4623 }
4624
4625 case Primitive::kPrimLong: {
4626 if (is_volatile) {
4627 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4628 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4629 __ movd(temp1, value.AsRegisterPairLow<Register>());
4630 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4631 __ punpckldq(temp1, temp2);
4632 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004633 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004634 } else if (value.IsConstant()) {
4635 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4636 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4637 codegen_->MaybeRecordImplicitNullCheck(instruction);
4638 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004639 } else {
4640 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004641 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004642 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4643 }
Mark Mendell81489372015-11-04 11:30:41 -05004644 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004645 break;
4646 }
4647
4648 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004649 if (value.IsConstant()) {
4650 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4651 __ movl(Address(base, offset), Immediate(v));
4652 } else {
4653 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4654 }
Calin Juravle52c48962014-12-16 17:02:57 +00004655 break;
4656 }
4657
4658 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004659 if (value.IsConstant()) {
4660 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4661 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4662 codegen_->MaybeRecordImplicitNullCheck(instruction);
4663 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4664 maybe_record_implicit_null_check_done = true;
4665 } else {
4666 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4667 }
Calin Juravle52c48962014-12-16 17:02:57 +00004668 break;
4669 }
4670
4671 case Primitive::kPrimVoid:
4672 LOG(FATAL) << "Unreachable type " << field_type;
4673 UNREACHABLE();
4674 }
4675
Mark Mendell81489372015-11-04 11:30:41 -05004676 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
4678 }
4679
Roland Levillain4d027112015-07-01 15:41:14 +01004680 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004681 Register temp = locations->GetTemp(0).AsRegister<Register>();
4682 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004683 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004684 }
4685
Calin Juravle52c48962014-12-16 17:02:57 +00004686 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004687 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004688 }
4689}
4690
4691void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4692 HandleFieldGet(instruction, instruction->GetFieldInfo());
4693}
4694
4695void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4696 HandleFieldGet(instruction, instruction->GetFieldInfo());
4697}
4698
4699void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4700 HandleFieldSet(instruction, instruction->GetFieldInfo());
4701}
4702
4703void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004704 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004705}
4706
4707void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4708 HandleFieldSet(instruction, instruction->GetFieldInfo());
4709}
4710
4711void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004712 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004713}
4714
4715void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4716 HandleFieldGet(instruction, instruction->GetFieldInfo());
4717}
4718
4719void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4720 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004721}
4722
Calin Juravlee460d1d2015-09-29 04:52:17 +01004723void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4724 HUnresolvedInstanceFieldGet* instruction) {
4725 FieldAccessCallingConventionX86 calling_convention;
4726 codegen_->CreateUnresolvedFieldLocationSummary(
4727 instruction, instruction->GetFieldType(), calling_convention);
4728}
4729
4730void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4731 HUnresolvedInstanceFieldGet* instruction) {
4732 FieldAccessCallingConventionX86 calling_convention;
4733 codegen_->GenerateUnresolvedFieldAccess(instruction,
4734 instruction->GetFieldType(),
4735 instruction->GetFieldIndex(),
4736 instruction->GetDexPc(),
4737 calling_convention);
4738}
4739
4740void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4741 HUnresolvedInstanceFieldSet* instruction) {
4742 FieldAccessCallingConventionX86 calling_convention;
4743 codegen_->CreateUnresolvedFieldLocationSummary(
4744 instruction, instruction->GetFieldType(), calling_convention);
4745}
4746
4747void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4748 HUnresolvedInstanceFieldSet* instruction) {
4749 FieldAccessCallingConventionX86 calling_convention;
4750 codegen_->GenerateUnresolvedFieldAccess(instruction,
4751 instruction->GetFieldType(),
4752 instruction->GetFieldIndex(),
4753 instruction->GetDexPc(),
4754 calling_convention);
4755}
4756
4757void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4758 HUnresolvedStaticFieldGet* instruction) {
4759 FieldAccessCallingConventionX86 calling_convention;
4760 codegen_->CreateUnresolvedFieldLocationSummary(
4761 instruction, instruction->GetFieldType(), calling_convention);
4762}
4763
4764void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4765 HUnresolvedStaticFieldGet* instruction) {
4766 FieldAccessCallingConventionX86 calling_convention;
4767 codegen_->GenerateUnresolvedFieldAccess(instruction,
4768 instruction->GetFieldType(),
4769 instruction->GetFieldIndex(),
4770 instruction->GetDexPc(),
4771 calling_convention);
4772}
4773
4774void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4775 HUnresolvedStaticFieldSet* instruction) {
4776 FieldAccessCallingConventionX86 calling_convention;
4777 codegen_->CreateUnresolvedFieldLocationSummary(
4778 instruction, instruction->GetFieldType(), calling_convention);
4779}
4780
4781void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4782 HUnresolvedStaticFieldSet* instruction) {
4783 FieldAccessCallingConventionX86 calling_convention;
4784 codegen_->GenerateUnresolvedFieldAccess(instruction,
4785 instruction->GetFieldType(),
4786 instruction->GetFieldIndex(),
4787 instruction->GetDexPc(),
4788 calling_convention);
4789}
4790
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004791void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004792 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4793 ? LocationSummary::kCallOnSlowPath
4794 : LocationSummary::kNoCall;
4795 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4796 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004797 ? Location::RequiresRegister()
4798 : Location::Any();
4799 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004800 if (instruction->HasUses()) {
4801 locations->SetOut(Location::SameAsFirstInput());
4802 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004803}
4804
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004805void InstructionCodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004806 if (codegen_->CanMoveNullCheckToUser(instruction)) {
4807 return;
4808 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004809 LocationSummary* locations = instruction->GetLocations();
4810 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004811
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004812 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
4813 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4814}
4815
4816void InstructionCodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004817 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004818 codegen_->AddSlowPath(slow_path);
4819
4820 LocationSummary* locations = instruction->GetLocations();
4821 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004822
4823 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004824 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004825 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004826 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004827 } else {
4828 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004829 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004830 __ jmp(slow_path->GetEntryLabel());
4831 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004832 }
4833 __ j(kEqual, slow_path->GetEntryLabel());
4834}
4835
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004836void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004837 if (codegen_->IsImplicitNullCheckAllowed(instruction)) {
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004838 GenerateImplicitNullCheck(instruction);
4839 } else {
4840 GenerateExplicitNullCheck(instruction);
4841 }
4842}
4843
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004844void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004845 bool object_array_get_with_read_barrier =
4846 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004847 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004848 new (GetGraph()->GetArena()) LocationSummary(instruction,
4849 object_array_get_with_read_barrier ?
4850 LocationSummary::kCallOnSlowPath :
4851 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004852 locations->SetInAt(0, Location::RequiresRegister());
4853 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004854 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4855 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4856 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004857 // The output overlaps in case of long: we don't want the low move
4858 // to overwrite the array's location. Likewise, in the case of an
4859 // object array get with read barriers enabled, we do not want the
4860 // move to overwrite the array's location, as we need it to emit
4861 // the read barrier.
4862 locations->SetOut(
4863 Location::RequiresRegister(),
4864 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
4865 Location::kOutputOverlap :
4866 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004867 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00004868 // We need a temporary register for the read barrier marking slow
4869 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
4870 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
4871 locations->AddTemp(Location::RequiresRegister());
4872 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004873}
4874
4875void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
4876 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004877 Location obj_loc = locations->InAt(0);
4878 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004879 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004880 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004881
Calin Juravle77520bc2015-01-12 18:45:46 +00004882 Primitive::Type type = instruction->GetType();
4883 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004884 case Primitive::kPrimBoolean: {
4885 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004886 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004887 if (index.IsConstant()) {
4888 __ movzxb(out, Address(obj,
4889 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4890 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004891 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004892 }
4893 break;
4894 }
4895
4896 case Primitive::kPrimByte: {
4897 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004898 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004899 if (index.IsConstant()) {
4900 __ movsxb(out, Address(obj,
4901 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
4902 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004903 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004904 }
4905 break;
4906 }
4907
4908 case Primitive::kPrimShort: {
4909 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004910 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004911 if (index.IsConstant()) {
4912 __ movsxw(out, Address(obj,
4913 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4914 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004915 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004916 }
4917 break;
4918 }
4919
4920 case Primitive::kPrimChar: {
4921 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004922 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004923 if (index.IsConstant()) {
4924 __ movzxw(out, Address(obj,
4925 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
4926 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004927 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004928 }
4929 break;
4930 }
4931
Roland Levillain7c1559a2015-12-15 10:55:36 +00004932 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004933 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004934 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004935 if (index.IsConstant()) {
4936 __ movl(out, Address(obj,
4937 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
4938 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00004939 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004940 }
4941 break;
4942 }
4943
Roland Levillain7c1559a2015-12-15 10:55:36 +00004944 case Primitive::kPrimNot: {
4945 static_assert(
4946 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
4947 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
4948 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
4949 // /* HeapReference<Object> */ out =
4950 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
4951 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4952 Location temp = locations->GetTemp(0);
4953 // Note that a potential implicit null check is handled in this
4954 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
4955 codegen_->GenerateArrayLoadWithBakerReadBarrier(
4956 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
4957 } else {
4958 Register out = out_loc.AsRegister<Register>();
4959 if (index.IsConstant()) {
4960 uint32_t offset =
4961 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
4962 __ movl(out, Address(obj, offset));
4963 codegen_->MaybeRecordImplicitNullCheck(instruction);
4964 // If read barriers are enabled, emit read barriers other than
4965 // Baker's using a slow path (and also unpoison the loaded
4966 // reference, if heap poisoning is enabled).
4967 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
4968 } else {
4969 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
4970 codegen_->MaybeRecordImplicitNullCheck(instruction);
4971 // If read barriers are enabled, emit read barriers other than
4972 // Baker's using a slow path (and also unpoison the loaded
4973 // reference, if heap poisoning is enabled).
4974 codegen_->MaybeGenerateReadBarrierSlow(
4975 instruction, out_loc, out_loc, obj_loc, data_offset, index);
4976 }
4977 }
4978 break;
4979 }
4980
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004981 case Primitive::kPrimLong: {
4982 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00004983 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004984 if (index.IsConstant()) {
4985 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004986 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004987 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004988 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004989 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004990 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004991 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004992 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004993 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00004994 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004995 }
4996 break;
4997 }
4998
Mark Mendell7c8d0092015-01-26 11:21:33 -05004999 case Primitive::kPrimFloat: {
5000 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005001 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005002 if (index.IsConstant()) {
5003 __ movss(out, Address(obj,
5004 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5005 } else {
5006 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5007 }
5008 break;
5009 }
5010
5011 case Primitive::kPrimDouble: {
5012 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005013 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005014 if (index.IsConstant()) {
5015 __ movsd(out, Address(obj,
5016 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5017 } else {
5018 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5019 }
5020 break;
5021 }
5022
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005023 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005024 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005025 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005027
Roland Levillain7c1559a2015-12-15 10:55:36 +00005028 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5029 // Potential implicit null checks, in the case of reference or
5030 // long arrays, are handled in the previous switch statement.
5031 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005032 codegen_->MaybeRecordImplicitNullCheck(instruction);
5033 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034}
5035
5036void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005037 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005038
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005039 bool needs_write_barrier =
5040 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005041 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5042 bool object_array_set_with_read_barrier =
5043 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005044
Nicolas Geoffray39468442014-09-02 15:17:15 +01005045 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5046 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005047 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5048 LocationSummary::kCallOnSlowPath :
5049 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005050
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005051 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5052 || (value_type == Primitive::kPrimByte);
5053 // We need the inputs to be different than the output in case of long operation.
5054 // In case of a byte operation, the register allocator does not support multiple
5055 // inputs that die at entry with one in a specific register.
5056 locations->SetInAt(0, Location::RequiresRegister());
5057 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5058 if (is_byte_type) {
5059 // Ensure the value is in a byte register.
5060 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5061 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005062 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005064 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5065 }
5066 if (needs_write_barrier) {
5067 // Temporary registers for the write barrier.
5068 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5069 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005070 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005072}
5073
5074void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5075 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005076 Location array_loc = locations->InAt(0);
5077 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005079 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005080 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005081 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5082 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5083 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005084 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005085 bool needs_write_barrier =
5086 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005087
5088 switch (value_type) {
5089 case Primitive::kPrimBoolean:
5090 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005091 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5092 Address address = index.IsConstant()
5093 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5094 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5095 if (value.IsRegister()) {
5096 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005098 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005099 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005100 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005101 break;
5102 }
5103
5104 case Primitive::kPrimShort:
5105 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005106 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5107 Address address = index.IsConstant()
5108 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5109 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5110 if (value.IsRegister()) {
5111 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005113 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005114 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005115 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005116 break;
5117 }
5118
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005119 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005120 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5121 Address address = index.IsConstant()
5122 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5123 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005124
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005125 if (!value.IsRegister()) {
5126 // Just setting null.
5127 DCHECK(instruction->InputAt(2)->IsNullConstant());
5128 DCHECK(value.IsConstant()) << value;
5129 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005130 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005131 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005132 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005133 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005134 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005135
5136 DCHECK(needs_write_barrier);
5137 Register register_value = value.AsRegister<Register>();
5138 NearLabel done, not_null, do_put;
5139 SlowPathCode* slow_path = nullptr;
5140 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005141 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005142 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5143 codegen_->AddSlowPath(slow_path);
5144 if (instruction->GetValueCanBeNull()) {
5145 __ testl(register_value, register_value);
5146 __ j(kNotEqual, &not_null);
5147 __ movl(address, Immediate(0));
5148 codegen_->MaybeRecordImplicitNullCheck(instruction);
5149 __ jmp(&done);
5150 __ Bind(&not_null);
5151 }
5152
Roland Levillain0d5a2812015-11-13 10:07:31 +00005153 if (kEmitCompilerReadBarrier) {
5154 // When read barriers are enabled, the type checking
5155 // instrumentation requires two read barriers:
5156 //
5157 // __ movl(temp2, temp);
5158 // // /* HeapReference<Class> */ temp = temp->component_type_
5159 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005160 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005161 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5162 //
5163 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5164 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005165 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005166 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5167 //
5168 // __ cmpl(temp, temp2);
5169 //
5170 // However, the second read barrier may trash `temp`, as it
5171 // is a temporary register, and as such would not be saved
5172 // along with live registers before calling the runtime (nor
5173 // restored afterwards). So in this case, we bail out and
5174 // delegate the work to the array set slow path.
5175 //
5176 // TODO: Extend the register allocator to support a new
5177 // "(locally) live temp" location so as to avoid always
5178 // going into the slow path when read barriers are enabled.
5179 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005180 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005181 // /* HeapReference<Class> */ temp = array->klass_
5182 __ movl(temp, Address(array, class_offset));
5183 codegen_->MaybeRecordImplicitNullCheck(instruction);
5184 __ MaybeUnpoisonHeapReference(temp);
5185
5186 // /* HeapReference<Class> */ temp = temp->component_type_
5187 __ movl(temp, Address(temp, component_offset));
5188 // If heap poisoning is enabled, no need to unpoison `temp`
5189 // nor the object reference in `register_value->klass`, as
5190 // we are comparing two poisoned references.
5191 __ cmpl(temp, Address(register_value, class_offset));
5192
5193 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5194 __ j(kEqual, &do_put);
5195 // If heap poisoning is enabled, the `temp` reference has
5196 // not been unpoisoned yet; unpoison it now.
5197 __ MaybeUnpoisonHeapReference(temp);
5198
5199 // /* HeapReference<Class> */ temp = temp->super_class_
5200 __ movl(temp, Address(temp, super_offset));
5201 // If heap poisoning is enabled, no need to unpoison
5202 // `temp`, as we are comparing against null below.
5203 __ testl(temp, temp);
5204 __ j(kNotEqual, slow_path->GetEntryLabel());
5205 __ Bind(&do_put);
5206 } else {
5207 __ j(kNotEqual, slow_path->GetEntryLabel());
5208 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 }
5210 }
5211
5212 if (kPoisonHeapReferences) {
5213 __ movl(temp, register_value);
5214 __ PoisonHeapReference(temp);
5215 __ movl(address, temp);
5216 } else {
5217 __ movl(address, register_value);
5218 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005219 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005220 codegen_->MaybeRecordImplicitNullCheck(instruction);
5221 }
5222
5223 Register card = locations->GetTemp(1).AsRegister<Register>();
5224 codegen_->MarkGCCard(
5225 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5226 __ Bind(&done);
5227
5228 if (slow_path != nullptr) {
5229 __ Bind(slow_path->GetExitLabel());
5230 }
5231
5232 break;
5233 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005234
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005235 case Primitive::kPrimInt: {
5236 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5237 Address address = index.IsConstant()
5238 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5239 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5240 if (value.IsRegister()) {
5241 __ movl(address, value.AsRegister<Register>());
5242 } else {
5243 DCHECK(value.IsConstant()) << value;
5244 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5245 __ movl(address, Immediate(v));
5246 }
5247 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005248 break;
5249 }
5250
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005251 case Primitive::kPrimLong: {
5252 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 if (index.IsConstant()) {
5254 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005255 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005257 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005258 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005259 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005260 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005261 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005262 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005263 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005264 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005265 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005266 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005267 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005268 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005269 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005270 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005272 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005273 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005274 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005275 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005276 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005277 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005278 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005279 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005280 Immediate(High32Bits(val)));
5281 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005282 }
5283 break;
5284 }
5285
Mark Mendell7c8d0092015-01-26 11:21:33 -05005286 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5288 Address address = index.IsConstant()
5289 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5290 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005291 if (value.IsFpuRegister()) {
5292 __ movss(address, value.AsFpuRegister<XmmRegister>());
5293 } else {
5294 DCHECK(value.IsConstant());
5295 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5296 __ movl(address, Immediate(v));
5297 }
5298 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005299 break;
5300 }
5301
5302 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5304 Address address = index.IsConstant()
5305 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5306 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005307 if (value.IsFpuRegister()) {
5308 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5309 } else {
5310 DCHECK(value.IsConstant());
5311 Address address_hi = index.IsConstant() ?
5312 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5313 offset + kX86WordSize) :
5314 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5315 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5316 __ movl(address, Immediate(Low32Bits(v)));
5317 codegen_->MaybeRecordImplicitNullCheck(instruction);
5318 __ movl(address_hi, Immediate(High32Bits(v)));
5319 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005320 break;
5321 }
5322
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005323 case Primitive::kPrimVoid:
5324 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005325 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005326 }
5327}
5328
5329void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5330 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005331 locations->SetInAt(0, Location::RequiresRegister());
5332 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005333}
5334
5335void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5336 LocationSummary* locations = instruction->GetLocations();
5337 uint32_t offset = mirror::Array::LengthOffset().Uint32Value();
Roland Levillain271ab9c2014-11-27 15:23:57 +00005338 Register obj = locations->InAt(0).AsRegister<Register>();
5339 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005340 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005341 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342}
5343
5344void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005345 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5346 ? LocationSummary::kCallOnSlowPath
5347 : LocationSummary::kNoCall;
5348 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005349 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005350 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005351 if (instruction->HasUses()) {
5352 locations->SetOut(Location::SameAsFirstInput());
5353 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005354}
5355
5356void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5357 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005358 Location index_loc = locations->InAt(0);
5359 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005360 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005361 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005362
Mark Mendell99dbd682015-04-22 16:18:52 -04005363 if (length_loc.IsConstant()) {
5364 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5365 if (index_loc.IsConstant()) {
5366 // BCE will remove the bounds check if we are guarenteed to pass.
5367 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5368 if (index < 0 || index >= length) {
5369 codegen_->AddSlowPath(slow_path);
5370 __ jmp(slow_path->GetEntryLabel());
5371 } else {
5372 // Some optimization after BCE may have generated this, and we should not
5373 // generate a bounds check if it is a valid range.
5374 }
5375 return;
5376 }
5377
5378 // We have to reverse the jump condition because the length is the constant.
5379 Register index_reg = index_loc.AsRegister<Register>();
5380 __ cmpl(index_reg, Immediate(length));
5381 codegen_->AddSlowPath(slow_path);
5382 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005383 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005384 Register length = length_loc.AsRegister<Register>();
5385 if (index_loc.IsConstant()) {
5386 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5387 __ cmpl(length, Immediate(value));
5388 } else {
5389 __ cmpl(length, index_loc.AsRegister<Register>());
5390 }
5391 codegen_->AddSlowPath(slow_path);
5392 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005393 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394}
5395
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005396void LocationsBuilderX86::VisitTemporary(HTemporary* temp) {
5397 temp->SetLocations(nullptr);
5398}
5399
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005400void InstructionCodeGeneratorX86::VisitTemporary(HTemporary* temp ATTRIBUTE_UNUSED) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005401 // Nothing to do, this is driven by the code generator.
5402}
5403
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005404void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005405 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005406}
5407
5408void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005409 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5410}
5411
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005412void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5413 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5414}
5415
5416void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005417 HBasicBlock* block = instruction->GetBlock();
5418 if (block->GetLoopInformation() != nullptr) {
5419 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5420 // The back edge will generate the suspend check.
5421 return;
5422 }
5423 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5424 // The goto will generate the suspend check.
5425 return;
5426 }
5427 GenerateSuspendCheck(instruction, nullptr);
5428}
5429
5430void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5431 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005432 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005433 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5434 if (slow_path == nullptr) {
5435 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5436 instruction->SetSlowPath(slow_path);
5437 codegen_->AddSlowPath(slow_path);
5438 if (successor != nullptr) {
5439 DCHECK(successor->IsLoopHeader());
5440 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5441 }
5442 } else {
5443 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5444 }
5445
Roland Levillain7c1559a2015-12-15 10:55:36 +00005446 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5447 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005448 if (successor == nullptr) {
5449 __ j(kNotEqual, slow_path->GetEntryLabel());
5450 __ Bind(slow_path->GetReturnLabel());
5451 } else {
5452 __ j(kEqual, codegen_->GetLabelOf(successor));
5453 __ jmp(slow_path->GetEntryLabel());
5454 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005455}
5456
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005457X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5458 return codegen_->GetAssembler();
5459}
5460
Mark Mendell7c8d0092015-01-26 11:21:33 -05005461void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005462 ScratchRegisterScope ensure_scratch(
5463 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5464 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5465 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5466 __ movl(temp_reg, Address(ESP, src + stack_offset));
5467 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005468}
5469
5470void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005471 ScratchRegisterScope ensure_scratch(
5472 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5473 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5474 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5475 __ movl(temp_reg, Address(ESP, src + stack_offset));
5476 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5477 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5478 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005479}
5480
5481void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005482 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005483 Location source = move->GetSource();
5484 Location destination = move->GetDestination();
5485
5486 if (source.IsRegister()) {
5487 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005488 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005489 } else {
5490 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005491 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005492 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005493 } else if (source.IsFpuRegister()) {
5494 if (destination.IsFpuRegister()) {
5495 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5496 } else if (destination.IsStackSlot()) {
5497 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5498 } else {
5499 DCHECK(destination.IsDoubleStackSlot());
5500 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5501 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005502 } else if (source.IsStackSlot()) {
5503 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005504 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005505 } else if (destination.IsFpuRegister()) {
5506 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005507 } else {
5508 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005509 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5510 }
5511 } else if (source.IsDoubleStackSlot()) {
5512 if (destination.IsFpuRegister()) {
5513 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5514 } else {
5515 DCHECK(destination.IsDoubleStackSlot()) << destination;
5516 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005517 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005518 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005519 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005520 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005521 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005522 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005523 if (value == 0) {
5524 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5525 } else {
5526 __ movl(destination.AsRegister<Register>(), Immediate(value));
5527 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005528 } else {
5529 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005530 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005531 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005532 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005533 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005534 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005535 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005536 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005537 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5538 if (value == 0) {
5539 // Easy handling of 0.0.
5540 __ xorps(dest, dest);
5541 } else {
5542 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005543 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5544 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5545 __ movl(temp, Immediate(value));
5546 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005547 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005548 } else {
5549 DCHECK(destination.IsStackSlot()) << destination;
5550 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5551 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005552 } else if (constant->IsLongConstant()) {
5553 int64_t value = constant->AsLongConstant()->GetValue();
5554 int32_t low_value = Low32Bits(value);
5555 int32_t high_value = High32Bits(value);
5556 Immediate low(low_value);
5557 Immediate high(high_value);
5558 if (destination.IsDoubleStackSlot()) {
5559 __ movl(Address(ESP, destination.GetStackIndex()), low);
5560 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5561 } else {
5562 __ movl(destination.AsRegisterPairLow<Register>(), low);
5563 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5564 }
5565 } else {
5566 DCHECK(constant->IsDoubleConstant());
5567 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005568 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005569 int32_t low_value = Low32Bits(value);
5570 int32_t high_value = High32Bits(value);
5571 Immediate low(low_value);
5572 Immediate high(high_value);
5573 if (destination.IsFpuRegister()) {
5574 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5575 if (value == 0) {
5576 // Easy handling of 0.0.
5577 __ xorpd(dest, dest);
5578 } else {
5579 __ pushl(high);
5580 __ pushl(low);
5581 __ movsd(dest, Address(ESP, 0));
5582 __ addl(ESP, Immediate(8));
5583 }
5584 } else {
5585 DCHECK(destination.IsDoubleStackSlot()) << destination;
5586 __ movl(Address(ESP, destination.GetStackIndex()), low);
5587 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5588 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005589 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005590 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005591 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005592 }
5593}
5594
Mark Mendella5c19ce2015-04-01 12:51:05 -04005595void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005596 Register suggested_scratch = reg == EAX ? EBX : EAX;
5597 ScratchRegisterScope ensure_scratch(
5598 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5599
5600 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5601 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5602 __ movl(Address(ESP, mem + stack_offset), reg);
5603 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005604}
5605
Mark Mendell7c8d0092015-01-26 11:21:33 -05005606void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005607 ScratchRegisterScope ensure_scratch(
5608 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5609
5610 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5611 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5612 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5613 __ movss(Address(ESP, mem + stack_offset), reg);
5614 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005615}
5616
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005617void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005618 ScratchRegisterScope ensure_scratch1(
5619 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005620
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005621 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5622 ScratchRegisterScope ensure_scratch2(
5623 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005624
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005625 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5626 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5627 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5628 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5629 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5630 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005631}
5632
5633void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005634 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005635 Location source = move->GetSource();
5636 Location destination = move->GetDestination();
5637
5638 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005639 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5640 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5641 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5642 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5643 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005644 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005645 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005646 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005647 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005648 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5649 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005650 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5651 // Use XOR Swap algorithm to avoid a temporary.
5652 DCHECK_NE(source.reg(), destination.reg());
5653 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5654 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5655 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5656 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5657 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5658 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5659 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005660 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5661 // Take advantage of the 16 bytes in the XMM register.
5662 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5663 Address stack(ESP, destination.GetStackIndex());
5664 // Load the double into the high doubleword.
5665 __ movhpd(reg, stack);
5666
5667 // Store the low double into the destination.
5668 __ movsd(stack, reg);
5669
5670 // Move the high double to the low double.
5671 __ psrldq(reg, Immediate(8));
5672 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5673 // Take advantage of the 16 bytes in the XMM register.
5674 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5675 Address stack(ESP, source.GetStackIndex());
5676 // Load the double into the high doubleword.
5677 __ movhpd(reg, stack);
5678
5679 // Store the low double into the destination.
5680 __ movsd(stack, reg);
5681
5682 // Move the high double to the low double.
5683 __ psrldq(reg, Immediate(8));
5684 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5685 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5686 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005687 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005688 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005689 }
5690}
5691
5692void ParallelMoveResolverX86::SpillScratch(int reg) {
5693 __ pushl(static_cast<Register>(reg));
5694}
5695
5696void ParallelMoveResolverX86::RestoreScratch(int reg) {
5697 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005698}
5699
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005700void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005701 InvokeRuntimeCallingConvention calling_convention;
5702 CodeGenerator::CreateLoadClassLocationSummary(
5703 cls,
5704 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005705 Location::RegisterLocation(EAX),
5706 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005707}
5708
5709void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005710 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005711 if (cls->NeedsAccessCheck()) {
5712 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5713 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5714 cls,
5715 cls->GetDexPc(),
5716 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005717 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005718 return;
5719 }
5720
Roland Levillain0d5a2812015-11-13 10:07:31 +00005721 Location out_loc = locations->Out();
5722 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005723 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005724
Calin Juravle580b6092015-10-06 17:35:58 +01005725 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005726 DCHECK(!cls->CanCallRuntime());
5727 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005728 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5729 GenerateGcRootFieldLoad(
5730 cls, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005731 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005732 // /* GcRoot<mirror::Class>[] */ out =
5733 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5734 __ movl(out, Address(current_method,
5735 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005736 // /* GcRoot<mirror::Class> */ out = out[type_index]
5737 GenerateGcRootFieldLoad(cls, out_loc, out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex()));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005738
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005739 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5740 DCHECK(cls->CanCallRuntime());
5741 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5742 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5743 codegen_->AddSlowPath(slow_path);
5744
5745 if (!cls->IsInDexCache()) {
5746 __ testl(out, out);
5747 __ j(kEqual, slow_path->GetEntryLabel());
5748 }
5749
5750 if (cls->MustGenerateClinitCheck()) {
5751 GenerateClassInitializationCheck(slow_path, out);
5752 } else {
5753 __ Bind(slow_path->GetExitLabel());
5754 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005755 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005756 }
5757}
5758
5759void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5760 LocationSummary* locations =
5761 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5762 locations->SetInAt(0, Location::RequiresRegister());
5763 if (check->HasUses()) {
5764 locations->SetOut(Location::SameAsFirstInput());
5765 }
5766}
5767
5768void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005769 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005770 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005771 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005772 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005773 GenerateClassInitializationCheck(slow_path,
5774 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005775}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005776
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005777void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005778 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005779 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5780 Immediate(mirror::Class::kStatusInitialized));
5781 __ j(kLess, slow_path->GetEntryLabel());
5782 __ Bind(slow_path->GetExitLabel());
5783 // No need for memory fence, thanks to the X86 memory model.
5784}
5785
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005786void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005787 LocationSummary::CallKind call_kind = (!load->IsInDexCache() || kEmitCompilerReadBarrier)
5788 ? LocationSummary::kCallOnSlowPath
5789 : LocationSummary::kNoCall;
5790 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005791 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005792 locations->SetOut(Location::RequiresRegister());
5793}
5794
5795void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005796 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005797 Location out_loc = locations->Out();
5798 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01005799 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005800
Roland Levillain7c1559a2015-12-15 10:55:36 +00005801 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5802 GenerateGcRootFieldLoad(
5803 load, out_loc, current_method, ArtMethod::DeclaringClassOffset().Int32Value());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005804 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
Mathieu Chartiereace4582014-11-24 18:29:54 -08005805 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005806 // /* GcRoot<mirror::String> */ out = out[string_index]
5807 GenerateGcRootFieldLoad(
5808 load, out_loc, out, CodeGenerator::GetCacheOffset(load->GetStringIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00005809
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005810 if (!load->IsInDexCache()) {
5811 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
5812 codegen_->AddSlowPath(slow_path);
5813 __ testl(out, out);
5814 __ j(kEqual, slow_path->GetEntryLabel());
5815 __ Bind(slow_path->GetExitLabel());
5816 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005817}
5818
David Brazdilcb1c0552015-08-04 16:22:25 +01005819static Address GetExceptionTlsAddress() {
5820 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
5821}
5822
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005823void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
5824 LocationSummary* locations =
5825 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
5826 locations->SetOut(Location::RequiresRegister());
5827}
5828
5829void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01005830 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
5831}
5832
5833void LocationsBuilderX86::VisitClearException(HClearException* clear) {
5834 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
5835}
5836
5837void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
5838 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005839}
5840
5841void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
5842 LocationSummary* locations =
5843 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
5844 InvokeRuntimeCallingConvention calling_convention;
5845 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5846}
5847
5848void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01005849 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
5850 instruction,
5851 instruction->GetDexPc(),
5852 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005853 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00005854}
5855
Roland Levillain7c1559a2015-12-15 10:55:36 +00005856static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
5857 return kEmitCompilerReadBarrier &&
5858 (kUseBakerReadBarrier ||
5859 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
5860 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
5861 type_check_kind == TypeCheckKind::kArrayObjectCheck);
5862}
5863
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005864void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005865 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00005866 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
5867 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005868 case TypeCheckKind::kExactCheck:
5869 case TypeCheckKind::kAbstractClassCheck:
5870 case TypeCheckKind::kClassHierarchyCheck:
5871 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005872 call_kind =
5873 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005874 break;
5875 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00005876 case TypeCheckKind::kUnresolvedCheck:
5877 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005878 call_kind = LocationSummary::kCallOnSlowPath;
5879 break;
5880 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005882 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005883 locations->SetInAt(0, Location::RequiresRegister());
5884 locations->SetInAt(1, Location::Any());
5885 // Note that TypeCheckSlowPathX86 uses this "out" register too.
5886 locations->SetOut(Location::RequiresRegister());
5887 // When read barriers are enabled, we need a temporary register for
5888 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00005889 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005891 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005892}
5893
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00005894void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005895 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005896 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 Location obj_loc = locations->InAt(0);
5898 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005899 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005900 Location out_loc = locations->Out();
5901 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005902 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00005903 locations->GetTemp(0) :
5904 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005905 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005906 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5907 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
5908 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07005909 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005910 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00005911
5912 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01005913 // Avoid null check if we know obj is not null.
5914 if (instruction->MustDoNullCheck()) {
5915 __ testl(obj, obj);
5916 __ j(kEqual, &zero);
5917 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005918
Roland Levillain0d5a2812015-11-13 10:07:31 +00005919 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005920 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005921
Roland Levillain7c1559a2015-12-15 10:55:36 +00005922 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005923 case TypeCheckKind::kExactCheck: {
5924 if (cls.IsRegister()) {
5925 __ cmpl(out, cls.AsRegister<Register>());
5926 } else {
5927 DCHECK(cls.IsStackSlot()) << cls;
5928 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5929 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005930
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005931 // Classes must be equal for the instanceof to succeed.
5932 __ j(kNotEqual, &zero);
5933 __ movl(out, Immediate(1));
5934 __ jmp(&done);
5935 break;
5936 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005937
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005938 case TypeCheckKind::kAbstractClassCheck: {
5939 // If the class is abstract, we eagerly fetch the super class of the
5940 // object to avoid doing a comparison we know will fail.
5941 NearLabel loop;
5942 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005943 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005944 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005945 __ testl(out, out);
5946 // If `out` is null, we use it for the result, and jump to `done`.
5947 __ j(kEqual, &done);
5948 if (cls.IsRegister()) {
5949 __ cmpl(out, cls.AsRegister<Register>());
5950 } else {
5951 DCHECK(cls.IsStackSlot()) << cls;
5952 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5953 }
5954 __ j(kNotEqual, &loop);
5955 __ movl(out, Immediate(1));
5956 if (zero.IsLinked()) {
5957 __ jmp(&done);
5958 }
5959 break;
5960 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005961
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005962 case TypeCheckKind::kClassHierarchyCheck: {
5963 // Walk over the class hierarchy to find a match.
5964 NearLabel loop, success;
5965 __ Bind(&loop);
5966 if (cls.IsRegister()) {
5967 __ cmpl(out, cls.AsRegister<Register>());
5968 } else {
5969 DCHECK(cls.IsStackSlot()) << cls;
5970 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5971 }
5972 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005973 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005974 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005975 __ testl(out, out);
5976 __ j(kNotEqual, &loop);
5977 // If `out` is null, we use it for the result, and jump to `done`.
5978 __ jmp(&done);
5979 __ Bind(&success);
5980 __ movl(out, Immediate(1));
5981 if (zero.IsLinked()) {
5982 __ jmp(&done);
5983 }
5984 break;
5985 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005986
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00005987 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01005988 // Do an exact check.
5989 NearLabel exact_check;
5990 if (cls.IsRegister()) {
5991 __ cmpl(out, cls.AsRegister<Register>());
5992 } else {
5993 DCHECK(cls.IsStackSlot()) << cls;
5994 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
5995 }
5996 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005997 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005998 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00005999 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006000 __ testl(out, out);
6001 // If `out` is null, we use it for the result, and jump to `done`.
6002 __ j(kEqual, &done);
6003 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6004 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006005 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006006 __ movl(out, Immediate(1));
6007 __ jmp(&done);
6008 break;
6009 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006010
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006011 case TypeCheckKind::kArrayCheck: {
6012 if (cls.IsRegister()) {
6013 __ cmpl(out, cls.AsRegister<Register>());
6014 } else {
6015 DCHECK(cls.IsStackSlot()) << cls;
6016 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6017 }
6018 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006019 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6020 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006021 codegen_->AddSlowPath(slow_path);
6022 __ j(kNotEqual, slow_path->GetEntryLabel());
6023 __ movl(out, Immediate(1));
6024 if (zero.IsLinked()) {
6025 __ jmp(&done);
6026 }
6027 break;
6028 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006029
Calin Juravle98893e12015-10-02 21:05:03 +01006030 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006031 case TypeCheckKind::kInterfaceCheck: {
6032 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006033 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006034 // cases.
6035 //
6036 // We cannot directly call the InstanceofNonTrivial runtime
6037 // entry point without resorting to a type checking slow path
6038 // here (i.e. by calling InvokeRuntime directly), as it would
6039 // require to assign fixed registers for the inputs of this
6040 // HInstanceOf instruction (following the runtime calling
6041 // convention), which might be cluttered by the potential first
6042 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006043 //
6044 // TODO: Introduce a new runtime entry point taking the object
6045 // to test (instead of its class) as argument, and let it deal
6046 // with the read barrier issues. This will let us refactor this
6047 // case of the `switch` code as it was previously (with a direct
6048 // call to the runtime not using a type checking slow path).
6049 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006050 DCHECK(locations->OnlyCallsOnSlowPath());
6051 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6052 /* is_fatal */ false);
6053 codegen_->AddSlowPath(slow_path);
6054 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006055 if (zero.IsLinked()) {
6056 __ jmp(&done);
6057 }
6058 break;
6059 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006060 }
6061
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006062 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006063 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006064 __ xorl(out, out);
6065 }
6066
6067 if (done.IsLinked()) {
6068 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006069 }
6070
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006071 if (slow_path != nullptr) {
6072 __ Bind(slow_path->GetExitLabel());
6073 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006074}
6075
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006076void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006077 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6078 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006079 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6080 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006081 case TypeCheckKind::kExactCheck:
6082 case TypeCheckKind::kAbstractClassCheck:
6083 case TypeCheckKind::kClassHierarchyCheck:
6084 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006085 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6086 LocationSummary::kCallOnSlowPath :
6087 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006088 break;
6089 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006090 case TypeCheckKind::kUnresolvedCheck:
6091 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006092 call_kind = LocationSummary::kCallOnSlowPath;
6093 break;
6094 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006095 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6096 locations->SetInAt(0, Location::RequiresRegister());
6097 locations->SetInAt(1, Location::Any());
6098 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6099 locations->AddTemp(Location::RequiresRegister());
6100 // When read barriers are enabled, we need an additional temporary
6101 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006102 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006103 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006104 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006105}
6106
6107void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006108 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006109 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006110 Location obj_loc = locations->InAt(0);
6111 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006112 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006113 Location temp_loc = locations->GetTemp(0);
6114 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006115 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006116 locations->GetTemp(1) :
6117 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006118 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6119 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6120 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6121 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006122
Roland Levillain0d5a2812015-11-13 10:07:31 +00006123 bool is_type_check_slow_path_fatal =
6124 (type_check_kind == TypeCheckKind::kExactCheck ||
6125 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6126 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6127 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6128 !instruction->CanThrowIntoCatchBlock();
6129 SlowPathCode* type_check_slow_path =
6130 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6131 is_type_check_slow_path_fatal);
6132 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006135 // Avoid null check if we know obj is not null.
6136 if (instruction->MustDoNullCheck()) {
6137 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006139 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006140
Roland Levillain0d5a2812015-11-13 10:07:31 +00006141 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006142 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006143
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006145 case TypeCheckKind::kExactCheck:
6146 case TypeCheckKind::kArrayCheck: {
6147 if (cls.IsRegister()) {
6148 __ cmpl(temp, cls.AsRegister<Register>());
6149 } else {
6150 DCHECK(cls.IsStackSlot()) << cls;
6151 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6152 }
6153 // Jump to slow path for throwing the exception or doing a
6154 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006155 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 break;
6157 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006158
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159 case TypeCheckKind::kAbstractClassCheck: {
6160 // If the class is abstract, we eagerly fetch the super class of the
6161 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006162 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006163 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006164 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006165 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006166
6167 // If the class reference currently in `temp` is not null, jump
6168 // to the `compare_classes` label to compare it with the checked
6169 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006170 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006171 __ j(kNotEqual, &compare_classes);
6172 // Otherwise, jump to the slow path to throw the exception.
6173 //
6174 // But before, move back the object's class into `temp` before
6175 // going into the slow path, as it has been overwritten in the
6176 // meantime.
6177 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006178 GenerateReferenceLoadTwoRegisters(
6179 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006180 __ jmp(type_check_slow_path->GetEntryLabel());
6181
6182 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006183 if (cls.IsRegister()) {
6184 __ cmpl(temp, cls.AsRegister<Register>());
6185 } else {
6186 DCHECK(cls.IsStackSlot()) << cls;
6187 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6188 }
6189 __ j(kNotEqual, &loop);
6190 break;
6191 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006192
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006193 case TypeCheckKind::kClassHierarchyCheck: {
6194 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006195 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196 __ Bind(&loop);
6197 if (cls.IsRegister()) {
6198 __ cmpl(temp, cls.AsRegister<Register>());
6199 } else {
6200 DCHECK(cls.IsStackSlot()) << cls;
6201 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6202 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006203 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006206 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207
6208 // If the class reference currently in `temp` is not null, jump
6209 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006210 __ testl(temp, temp);
6211 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006212 // Otherwise, jump to the slow path to throw the exception.
6213 //
6214 // But before, move back the object's class into `temp` before
6215 // going into the slow path, as it has been overwritten in the
6216 // meantime.
6217 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006218 GenerateReferenceLoadTwoRegisters(
6219 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006220 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006221 break;
6222 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006223
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006224 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006225 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006226 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006227 if (cls.IsRegister()) {
6228 __ cmpl(temp, cls.AsRegister<Register>());
6229 } else {
6230 DCHECK(cls.IsStackSlot()) << cls;
6231 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6232 }
6233 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006234
6235 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006236 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006237 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006238
6239 // If the component type is not null (i.e. the object is indeed
6240 // an array), jump to label `check_non_primitive_component_type`
6241 // to further check that this component type is not a primitive
6242 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006244 __ j(kNotEqual, &check_non_primitive_component_type);
6245 // Otherwise, jump to the slow path to throw the exception.
6246 //
6247 // But before, move back the object's class into `temp` before
6248 // going into the slow path, as it has been overwritten in the
6249 // meantime.
6250 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006251 GenerateReferenceLoadTwoRegisters(
6252 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006253 __ jmp(type_check_slow_path->GetEntryLabel());
6254
6255 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006256 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006257 __ j(kEqual, &done);
6258 // Same comment as above regarding `temp` and the slow path.
6259 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006260 GenerateReferenceLoadTwoRegisters(
6261 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006263 break;
6264 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265
Calin Juravle98893e12015-10-02 21:05:03 +01006266 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006268 // We always go into the type check slow path for the unresolved
6269 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006270 //
6271 // We cannot directly call the CheckCast runtime entry point
6272 // without resorting to a type checking slow path here (i.e. by
6273 // calling InvokeRuntime directly), as it would require to
6274 // assign fixed registers for the inputs of this HInstanceOf
6275 // instruction (following the runtime calling convention), which
6276 // might be cluttered by the potential first read barrier
6277 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006278 //
6279 // TODO: Introduce a new runtime entry point taking the object
6280 // to test (instead of its class) as argument, and let it deal
6281 // with the read barrier issues. This will let us refactor this
6282 // case of the `switch` code as it was previously (with a direct
6283 // call to the runtime not using a type checking slow path).
6284 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006286 break;
6287 }
6288 __ Bind(&done);
6289
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006291}
6292
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006293void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6294 LocationSummary* locations =
6295 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6296 InvokeRuntimeCallingConvention calling_convention;
6297 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6298}
6299
6300void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006301 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6302 : QUICK_ENTRY_POINT(pUnlockObject),
6303 instruction,
6304 instruction->GetDexPc(),
6305 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006306 if (instruction->IsEnter()) {
6307 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6308 } else {
6309 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6310 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006311}
6312
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006313void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6314void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6315void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6316
6317void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6318 LocationSummary* locations =
6319 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6320 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6321 || instruction->GetResultType() == Primitive::kPrimLong);
6322 locations->SetInAt(0, Location::RequiresRegister());
6323 locations->SetInAt(1, Location::Any());
6324 locations->SetOut(Location::SameAsFirstInput());
6325}
6326
6327void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6328 HandleBitwiseOperation(instruction);
6329}
6330
6331void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6332 HandleBitwiseOperation(instruction);
6333}
6334
6335void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6336 HandleBitwiseOperation(instruction);
6337}
6338
6339void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6340 LocationSummary* locations = instruction->GetLocations();
6341 Location first = locations->InAt(0);
6342 Location second = locations->InAt(1);
6343 DCHECK(first.Equals(locations->Out()));
6344
6345 if (instruction->GetResultType() == Primitive::kPrimInt) {
6346 if (second.IsRegister()) {
6347 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006348 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006349 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006350 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006351 } else {
6352 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006353 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006354 }
6355 } else if (second.IsConstant()) {
6356 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006357 __ andl(first.AsRegister<Register>(),
6358 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006359 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006360 __ orl(first.AsRegister<Register>(),
6361 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006362 } else {
6363 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006364 __ xorl(first.AsRegister<Register>(),
6365 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006366 }
6367 } else {
6368 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006369 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006370 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006371 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006372 } else {
6373 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006374 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006375 }
6376 }
6377 } else {
6378 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6379 if (second.IsRegisterPair()) {
6380 if (instruction->IsAnd()) {
6381 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6382 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6383 } else if (instruction->IsOr()) {
6384 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6385 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6386 } else {
6387 DCHECK(instruction->IsXor());
6388 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6389 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6390 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006391 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006392 if (instruction->IsAnd()) {
6393 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6394 __ andl(first.AsRegisterPairHigh<Register>(),
6395 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6396 } else if (instruction->IsOr()) {
6397 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6398 __ orl(first.AsRegisterPairHigh<Register>(),
6399 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6400 } else {
6401 DCHECK(instruction->IsXor());
6402 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6403 __ xorl(first.AsRegisterPairHigh<Register>(),
6404 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6405 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006406 } else {
6407 DCHECK(second.IsConstant()) << second;
6408 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006409 int32_t low_value = Low32Bits(value);
6410 int32_t high_value = High32Bits(value);
6411 Immediate low(low_value);
6412 Immediate high(high_value);
6413 Register first_low = first.AsRegisterPairLow<Register>();
6414 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006415 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006416 if (low_value == 0) {
6417 __ xorl(first_low, first_low);
6418 } else if (low_value != -1) {
6419 __ andl(first_low, low);
6420 }
6421 if (high_value == 0) {
6422 __ xorl(first_high, first_high);
6423 } else if (high_value != -1) {
6424 __ andl(first_high, high);
6425 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006426 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006427 if (low_value != 0) {
6428 __ orl(first_low, low);
6429 }
6430 if (high_value != 0) {
6431 __ orl(first_high, high);
6432 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006433 } else {
6434 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006435 if (low_value != 0) {
6436 __ xorl(first_low, low);
6437 }
6438 if (high_value != 0) {
6439 __ xorl(first_high, high);
6440 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006441 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006442 }
6443 }
6444}
6445
Roland Levillain7c1559a2015-12-15 10:55:36 +00006446void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6447 Location out,
6448 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006449 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006450 Register out_reg = out.AsRegister<Register>();
6451 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006452 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006453 if (kUseBakerReadBarrier) {
6454 // Load with fast path based Baker's read barrier.
6455 // /* HeapReference<Object> */ out = *(out + offset)
6456 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006457 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006458 } else {
6459 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006460 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006461 // in the following move operation, as we will need it for the
6462 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006463 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006464 // /* HeapReference<Object> */ out = *(out + offset)
6465 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006466 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006467 }
6468 } else {
6469 // Plain load with no read barrier.
6470 // /* HeapReference<Object> */ out = *(out + offset)
6471 __ movl(out_reg, Address(out_reg, offset));
6472 __ MaybeUnpoisonHeapReference(out_reg);
6473 }
6474}
6475
6476void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6477 Location out,
6478 Location obj,
6479 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006480 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006481 Register out_reg = out.AsRegister<Register>();
6482 Register obj_reg = obj.AsRegister<Register>();
6483 if (kEmitCompilerReadBarrier) {
6484 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006485 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006486 // Load with fast path based Baker's read barrier.
6487 // /* HeapReference<Object> */ out = *(obj + offset)
6488 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006489 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006490 } else {
6491 // Load with slow path based read barrier.
6492 // /* HeapReference<Object> */ out = *(obj + offset)
6493 __ movl(out_reg, Address(obj_reg, offset));
6494 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6495 }
6496 } else {
6497 // Plain load with no read barrier.
6498 // /* HeapReference<Object> */ out = *(obj + offset)
6499 __ movl(out_reg, Address(obj_reg, offset));
6500 __ MaybeUnpoisonHeapReference(out_reg);
6501 }
6502}
6503
6504void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6505 Location root,
6506 Register obj,
6507 uint32_t offset) {
6508 Register root_reg = root.AsRegister<Register>();
6509 if (kEmitCompilerReadBarrier) {
6510 if (kUseBakerReadBarrier) {
6511 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6512 // Baker's read barrier are used:
6513 //
6514 // root = obj.field;
6515 // if (Thread::Current()->GetIsGcMarking()) {
6516 // root = ReadBarrier::Mark(root)
6517 // }
6518
6519 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6520 __ movl(root_reg, Address(obj, offset));
6521 static_assert(
6522 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6523 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6524 "have different sizes.");
6525 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6526 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6527 "have different sizes.");
6528
6529 // Slow path used to mark the GC root `root`.
6530 SlowPathCode* slow_path =
6531 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6532 codegen_->AddSlowPath(slow_path);
6533
6534 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6535 Immediate(0));
6536 __ j(kNotEqual, slow_path->GetEntryLabel());
6537 __ Bind(slow_path->GetExitLabel());
6538 } else {
6539 // GC root loaded through a slow path for read barriers other
6540 // than Baker's.
6541 // /* GcRoot<mirror::Object>* */ root = obj + offset
6542 __ leal(root_reg, Address(obj, offset));
6543 // /* mirror::Object* */ root = root->Read()
6544 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6545 }
6546 } else {
6547 // Plain GC root load with no read barrier.
6548 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6549 __ movl(root_reg, Address(obj, offset));
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006550 // Note that GC roots are not affected by heap poisoning, thus we
6551 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006552 }
6553}
6554
6555void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6556 Location ref,
6557 Register obj,
6558 uint32_t offset,
6559 Location temp,
6560 bool needs_null_check) {
6561 DCHECK(kEmitCompilerReadBarrier);
6562 DCHECK(kUseBakerReadBarrier);
6563
6564 // /* HeapReference<Object> */ ref = *(obj + offset)
6565 Address src(obj, offset);
6566 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6567}
6568
6569void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6570 Location ref,
6571 Register obj,
6572 uint32_t data_offset,
6573 Location index,
6574 Location temp,
6575 bool needs_null_check) {
6576 DCHECK(kEmitCompilerReadBarrier);
6577 DCHECK(kUseBakerReadBarrier);
6578
6579 // /* HeapReference<Object> */ ref =
6580 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6581 Address src = index.IsConstant() ?
6582 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6583 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6584 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6585}
6586
6587void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6588 Location ref,
6589 Register obj,
6590 const Address& src,
6591 Location temp,
6592 bool needs_null_check) {
6593 DCHECK(kEmitCompilerReadBarrier);
6594 DCHECK(kUseBakerReadBarrier);
6595
6596 // In slow path based read barriers, the read barrier call is
6597 // inserted after the original load. However, in fast path based
6598 // Baker's read barriers, we need to perform the load of
6599 // mirror::Object::monitor_ *before* the original reference load.
6600 // This load-load ordering is required by the read barrier.
6601 // The fast path/slow path (for Baker's algorithm) should look like:
6602 //
6603 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6604 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6605 // HeapReference<Object> ref = *src; // Original reference load.
6606 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6607 // if (is_gray) {
6608 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6609 // }
6610 //
6611 // Note: the original implementation in ReadBarrier::Barrier is
6612 // slightly more complex as:
6613 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006614 // the high-bits of rb_state, which are expected to be all zeroes
6615 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6616 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006617 // - it performs additional checks that we do not do here for
6618 // performance reasons.
6619
6620 Register ref_reg = ref.AsRegister<Register>();
6621 Register temp_reg = temp.AsRegister<Register>();
6622 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6623
6624 // /* int32_t */ monitor = obj->monitor_
6625 __ movl(temp_reg, Address(obj, monitor_offset));
6626 if (needs_null_check) {
6627 MaybeRecordImplicitNullCheck(instruction);
6628 }
6629 // /* LockWord */ lock_word = LockWord(monitor)
6630 static_assert(sizeof(LockWord) == sizeof(int32_t),
6631 "art::LockWord and int32_t have different sizes.");
6632 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6633 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6634 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6635 static_assert(
6636 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6637 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6638
6639 // Load fence to prevent load-load reordering.
6640 // Note that this is a no-op, thanks to the x86 memory model.
6641 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6642
6643 // The actual reference load.
6644 // /* HeapReference<Object> */ ref = *src
6645 __ movl(ref_reg, src);
6646
6647 // Object* ref = ref_addr->AsMirrorPtr()
6648 __ MaybeUnpoisonHeapReference(ref_reg);
6649
6650 // Slow path used to mark the object `ref` when it is gray.
6651 SlowPathCode* slow_path =
6652 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6653 AddSlowPath(slow_path);
6654
6655 // if (rb_state == ReadBarrier::gray_ptr_)
6656 // ref = ReadBarrier::Mark(ref);
6657 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6658 __ j(kEqual, slow_path->GetEntryLabel());
6659 __ Bind(slow_path->GetExitLabel());
6660}
6661
6662void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6663 Location out,
6664 Location ref,
6665 Location obj,
6666 uint32_t offset,
6667 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006668 DCHECK(kEmitCompilerReadBarrier);
6669
Roland Levillain7c1559a2015-12-15 10:55:36 +00006670 // Insert a slow path based read barrier *after* the reference load.
6671 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006672 // If heap poisoning is enabled, the unpoisoning of the loaded
6673 // reference will be carried out by the runtime within the slow
6674 // path.
6675 //
6676 // Note that `ref` currently does not get unpoisoned (when heap
6677 // poisoning is enabled), which is alright as the `ref` argument is
6678 // not used by the artReadBarrierSlow entry point.
6679 //
6680 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6681 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6682 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6683 AddSlowPath(slow_path);
6684
Roland Levillain0d5a2812015-11-13 10:07:31 +00006685 __ jmp(slow_path->GetEntryLabel());
6686 __ Bind(slow_path->GetExitLabel());
6687}
6688
Roland Levillain7c1559a2015-12-15 10:55:36 +00006689void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6690 Location out,
6691 Location ref,
6692 Location obj,
6693 uint32_t offset,
6694 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006695 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006696 // Baker's read barriers shall be handled by the fast path
6697 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6698 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006699 // If heap poisoning is enabled, unpoisoning will be taken care of
6700 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006701 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006702 } else if (kPoisonHeapReferences) {
6703 __ UnpoisonHeapReference(out.AsRegister<Register>());
6704 }
6705}
6706
Roland Levillain7c1559a2015-12-15 10:55:36 +00006707void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6708 Location out,
6709 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006710 DCHECK(kEmitCompilerReadBarrier);
6711
Roland Levillain7c1559a2015-12-15 10:55:36 +00006712 // Insert a slow path based read barrier *after* the GC root load.
6713 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006714 // Note that GC roots are not affected by heap poisoning, so we do
6715 // not need to do anything special for this here.
6716 SlowPathCode* slow_path =
6717 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6718 AddSlowPath(slow_path);
6719
Roland Levillain0d5a2812015-11-13 10:07:31 +00006720 __ jmp(slow_path->GetEntryLabel());
6721 __ Bind(slow_path->GetExitLabel());
6722}
6723
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006724void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006725 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006726 LOG(FATAL) << "Unreachable";
6727}
6728
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006729void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006730 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006731 LOG(FATAL) << "Unreachable";
6732}
6733
Mark Mendellfe57faa2015-09-18 09:26:15 -04006734// Simple implementation of packed switch - generate cascaded compare/jumps.
6735void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6736 LocationSummary* locations =
6737 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6738 locations->SetInAt(0, Location::RequiresRegister());
6739}
6740
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006741void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
6742 int32_t lower_bound,
6743 uint32_t num_entries,
6744 HBasicBlock* switch_block,
6745 HBasicBlock* default_block) {
6746 // Figure out the correct compare values and jump conditions.
6747 // Handle the first compare/branch as a special case because it might
6748 // jump to the default case.
6749 DCHECK_GT(num_entries, 2u);
6750 Condition first_condition;
6751 uint32_t index;
6752 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
6753 if (lower_bound != 0) {
6754 first_condition = kLess;
6755 __ cmpl(value_reg, Immediate(lower_bound));
6756 __ j(first_condition, codegen_->GetLabelOf(default_block));
6757 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006758
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006759 index = 1;
6760 } else {
6761 // Handle all the compare/jumps below.
6762 first_condition = kBelow;
6763 index = 0;
6764 }
6765
6766 // Handle the rest of the compare/jumps.
6767 for (; index + 1 < num_entries; index += 2) {
6768 int32_t compare_to_value = lower_bound + index + 1;
6769 __ cmpl(value_reg, Immediate(compare_to_value));
6770 // Jump to successors[index] if value < case_value[index].
6771 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
6772 // Jump to successors[index + 1] if value == case_value[index + 1].
6773 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
6774 }
6775
6776 if (index != num_entries) {
6777 // There are an odd number of entries. Handle the last one.
6778 DCHECK_EQ(index + 1, num_entries);
6779 __ cmpl(value_reg, Immediate(lower_bound + index));
6780 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006781 }
6782
6783 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006784 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
6785 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04006786 }
6787}
6788
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006789void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
6790 int32_t lower_bound = switch_instr->GetStartValue();
6791 uint32_t num_entries = switch_instr->GetNumEntries();
6792 LocationSummary* locations = switch_instr->GetLocations();
6793 Register value_reg = locations->InAt(0).AsRegister<Register>();
6794
6795 GenPackedSwitchWithCompares(value_reg,
6796 lower_bound,
6797 num_entries,
6798 switch_instr->GetBlock(),
6799 switch_instr->GetDefaultBlock());
6800}
6801
Mark Mendell805b3b52015-09-18 14:10:29 -04006802void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6803 LocationSummary* locations =
6804 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
6805 locations->SetInAt(0, Location::RequiresRegister());
6806
6807 // Constant area pointer.
6808 locations->SetInAt(1, Location::RequiresRegister());
6809
6810 // And the temporary we need.
6811 locations->AddTemp(Location::RequiresRegister());
6812}
6813
6814void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
6815 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006816 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04006817 LocationSummary* locations = switch_instr->GetLocations();
6818 Register value_reg = locations->InAt(0).AsRegister<Register>();
6819 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
6820
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006821 if (num_entries <= kPackedSwitchJumpTableThreshold) {
6822 GenPackedSwitchWithCompares(value_reg,
6823 lower_bound,
6824 num_entries,
6825 switch_instr->GetBlock(),
6826 default_block);
6827 return;
6828 }
6829
Mark Mendell805b3b52015-09-18 14:10:29 -04006830 // Optimizing has a jump area.
6831 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
6832 Register constant_area = locations->InAt(1).AsRegister<Register>();
6833
6834 // Remove the bias, if needed.
6835 if (lower_bound != 0) {
6836 __ leal(temp_reg, Address(value_reg, -lower_bound));
6837 value_reg = temp_reg;
6838 }
6839
6840 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00006841 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04006842 __ cmpl(value_reg, Immediate(num_entries - 1));
6843 __ j(kAbove, codegen_->GetLabelOf(default_block));
6844
6845 // We are in the range of the table.
6846 // Load (target-constant_area) from the jump table, indexing by the value.
6847 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
6848
6849 // Compute the actual target address by adding in constant_area.
6850 __ addl(temp_reg, constant_area);
6851
6852 // And jump.
6853 __ jmp(temp_reg);
6854}
6855
Mark Mendell0616ae02015-04-17 12:49:27 -04006856void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
6857 HX86ComputeBaseMethodAddress* insn) {
6858 LocationSummary* locations =
6859 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6860 locations->SetOut(Location::RequiresRegister());
6861}
6862
6863void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
6864 HX86ComputeBaseMethodAddress* insn) {
6865 LocationSummary* locations = insn->GetLocations();
6866 Register reg = locations->Out().AsRegister<Register>();
6867
6868 // Generate call to next instruction.
6869 Label next_instruction;
6870 __ call(&next_instruction);
6871 __ Bind(&next_instruction);
6872
6873 // Remember this offset for later use with constant area.
6874 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
6875
6876 // Grab the return address off the stack.
6877 __ popl(reg);
6878}
6879
6880void LocationsBuilderX86::VisitX86LoadFromConstantTable(
6881 HX86LoadFromConstantTable* insn) {
6882 LocationSummary* locations =
6883 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
6884
6885 locations->SetInAt(0, Location::RequiresRegister());
6886 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
6887
6888 // If we don't need to be materialized, we only need the inputs to be set.
6889 if (!insn->NeedsMaterialization()) {
6890 return;
6891 }
6892
6893 switch (insn->GetType()) {
6894 case Primitive::kPrimFloat:
6895 case Primitive::kPrimDouble:
6896 locations->SetOut(Location::RequiresFpuRegister());
6897 break;
6898
6899 case Primitive::kPrimInt:
6900 locations->SetOut(Location::RequiresRegister());
6901 break;
6902
6903 default:
6904 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6905 }
6906}
6907
6908void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
6909 if (!insn->NeedsMaterialization()) {
6910 return;
6911 }
6912
6913 LocationSummary* locations = insn->GetLocations();
6914 Location out = locations->Out();
6915 Register const_area = locations->InAt(0).AsRegister<Register>();
6916 HConstant *value = insn->GetConstant();
6917
6918 switch (insn->GetType()) {
6919 case Primitive::kPrimFloat:
6920 __ movss(out.AsFpuRegister<XmmRegister>(),
6921 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
6922 break;
6923
6924 case Primitive::kPrimDouble:
6925 __ movsd(out.AsFpuRegister<XmmRegister>(),
6926 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
6927 break;
6928
6929 case Primitive::kPrimInt:
6930 __ movl(out.AsRegister<Register>(),
6931 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
6932 break;
6933
6934 default:
6935 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
6936 }
6937}
6938
Mark Mendell0616ae02015-04-17 12:49:27 -04006939/**
6940 * Class to handle late fixup of offsets into constant area.
6941 */
Vladimir Marko5233f932015-09-29 19:01:15 +01006942class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04006943 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04006944 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
6945 : codegen_(&codegen), offset_into_constant_area_(offset) {}
6946
6947 protected:
6948 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
6949
6950 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006951
6952 private:
6953 void Process(const MemoryRegion& region, int pos) OVERRIDE {
6954 // Patch the correct offset for the instruction. The place to patch is the
6955 // last 4 bytes of the instruction.
6956 // The value to patch is the distance from the offset in the constant area
6957 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04006958 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
6959 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04006960
6961 // Patch in the right value.
6962 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
6963 }
6964
Mark Mendell0616ae02015-04-17 12:49:27 -04006965 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04006966 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04006967};
6968
Mark Mendell805b3b52015-09-18 14:10:29 -04006969/**
6970 * Class to handle late fixup of offsets to a jump table that will be created in the
6971 * constant area.
6972 */
6973class JumpTableRIPFixup : public RIPFixup {
6974 public:
6975 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
6976 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
6977
6978 void CreateJumpTable() {
6979 X86Assembler* assembler = codegen_->GetAssembler();
6980
6981 // Ensure that the reference to the jump table has the correct offset.
6982 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
6983 SetOffset(offset_in_constant_table);
6984
6985 // The label values in the jump table are computed relative to the
6986 // instruction addressing the constant area.
6987 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
6988
6989 // Populate the jump table with the correct values for the jump table.
6990 int32_t num_entries = switch_instr_->GetNumEntries();
6991 HBasicBlock* block = switch_instr_->GetBlock();
6992 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
6993 // The value that we want is the target offset - the position of the table.
6994 for (int32_t i = 0; i < num_entries; i++) {
6995 HBasicBlock* b = successors[i];
6996 Label* l = codegen_->GetLabelOf(b);
6997 DCHECK(l->IsBound());
6998 int32_t offset_to_block = l->Position() - relative_offset;
6999 assembler->AppendInt32(offset_to_block);
7000 }
7001 }
7002
7003 private:
7004 const HX86PackedSwitch* switch_instr_;
7005};
7006
7007void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7008 // Generate the constant area if needed.
7009 X86Assembler* assembler = GetAssembler();
7010 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7011 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7012 // byte values.
7013 assembler->Align(4, 0);
7014 constant_area_start_ = assembler->CodeSize();
7015
7016 // Populate any jump tables.
7017 for (auto jump_table : fixups_to_jump_tables_) {
7018 jump_table->CreateJumpTable();
7019 }
7020
7021 // And now add the constant area to the generated code.
7022 assembler->AddConstantArea();
7023 }
7024
7025 // And finish up.
7026 CodeGenerator::Finalize(allocator);
7027}
7028
Mark Mendell0616ae02015-04-17 12:49:27 -04007029Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7030 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7031 return Address(reg, kDummy32BitOffset, fixup);
7032}
7033
7034Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7035 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7036 return Address(reg, kDummy32BitOffset, fixup);
7037}
7038
7039Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7040 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7041 return Address(reg, kDummy32BitOffset, fixup);
7042}
7043
7044Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7045 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7046 return Address(reg, kDummy32BitOffset, fixup);
7047}
7048
Mark Mendell805b3b52015-09-18 14:10:29 -04007049Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7050 Register reg,
7051 Register value) {
7052 // Create a fixup to be used to create and address the jump table.
7053 JumpTableRIPFixup* table_fixup =
7054 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7055
7056 // We have to populate the jump tables.
7057 fixups_to_jump_tables_.push_back(table_fixup);
7058
7059 // We want a scaled address, as we are extracting the correct offset from the table.
7060 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7061}
7062
Andreas Gampe85b62f22015-09-09 13:15:38 -07007063// TODO: target as memory.
7064void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7065 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007066 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007067 return;
7068 }
7069
7070 DCHECK_NE(type, Primitive::kPrimVoid);
7071
7072 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7073 if (target.Equals(return_loc)) {
7074 return;
7075 }
7076
7077 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7078 // with the else branch.
7079 if (type == Primitive::kPrimLong) {
7080 HParallelMove parallel_move(GetGraph()->GetArena());
7081 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7082 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7083 GetMoveResolver()->EmitNativeCode(&parallel_move);
7084 } else {
7085 // Let the parallel move resolver take care of all of this.
7086 HParallelMove parallel_move(GetGraph()->GetArena());
7087 parallel_move.AddMove(return_loc, target, type, nullptr);
7088 GetMoveResolver()->EmitNativeCode(&parallel_move);
7089 }
7090}
7091
Roland Levillain4d027112015-07-01 15:41:14 +01007092#undef __
7093
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007094} // namespace x86
7095} // namespace art