blob: 9d0092b67497d11fca3814259a6cb551ef37914b [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"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070050// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
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:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(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 Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000087 if (instruction_->CanThrowIntoCatchBlock()) {
88 // Live registers will be restored in the catch block if caught.
89 SaveLiveRegisters(codegen, instruction_->GetLocations());
90 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000144 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100147 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100148 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100149 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
150 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100151 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
152 instruction_,
153 instruction_->GetDexPc(),
154 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000155 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 }
157
Alexandre Rames8158f282015-08-07 10:26:17 +0100158 bool IsFatal() const OVERRIDE { return true; }
159
Alexandre Rames9931f312015-06-19 14:47:01 +0100160 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
164};
165
Andreas Gampe85b62f22015-09-09 13:15:38 -0700166class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000168 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000169 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000171 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000174 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100175 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
176 instruction_,
177 instruction_->GetDexPc(),
178 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000179 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000180 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100181 if (successor_ == nullptr) {
182 __ jmp(GetReturnLabel());
183 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100184 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 }
187
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100188 Label* GetReturnLabel() {
189 DCHECK(successor_ == nullptr);
190 return &return_label_;
191 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100193 HBasicBlock* GetSuccessor() const {
194 return successor_;
195 }
196
Alexandre Rames9931f312015-06-19 14:47:01 +0100197 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
198
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201 Label return_label_;
202
203 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
204};
205
Andreas Gampe85b62f22015-09-09 13:15:38 -0700206class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000208 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000209
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000210 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 LocationSummary* locations = instruction_->GetLocations();
212 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
213
214 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
215 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000216 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000217
218 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000219 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
220 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100221 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
222 instruction_,
223 instruction_->GetDexPc(),
224 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000225 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000226 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228
229 __ jmp(GetExitLabel());
230 }
231
Alexandre Rames9931f312015-06-19 14:47:01 +0100232 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
233
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000234 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000235 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
236};
237
Andreas Gampe85b62f22015-09-09 13:15:38 -0700238class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 public:
240 LoadClassSlowPathX86(HLoadClass* cls,
241 HInstruction* at,
242 uint32_t dex_pc,
243 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000244 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
246 }
247
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000248 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 LocationSummary* locations = at_->GetLocations();
250 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
251 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253
254 InvokeRuntimeCallingConvention calling_convention;
255 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100256 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
257 : QUICK_ENTRY_POINT(pInitializeType),
258 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000259 if (do_clinit_) {
260 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
261 } else {
262 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
263 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264
265 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000266 Location out = locations->Out();
267 if (out.IsValid()) {
268 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
269 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000272 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 __ jmp(GetExitLabel());
274 }
275
Alexandre Rames9931f312015-06-19 14:47:01 +0100276 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
277
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 private:
279 // The class this slow path will load.
280 HLoadClass* const cls_;
281
282 // The instruction where this slow path is happening.
283 // (Might be the load class or an initialization check).
284 HInstruction* const at_;
285
286 // The dex PC of `at_`.
287 const uint32_t dex_pc_;
288
289 // Whether to initialize the class.
290 const bool do_clinit_;
291
292 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
293};
294
Andreas Gampe85b62f22015-09-09 13:15:38 -0700295class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000297 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000298 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000300 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100302 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
303 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304 DCHECK(instruction_->IsCheckCast()
305 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
307 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
308 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000309
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000310 if (!is_fatal_) {
311 SaveLiveRegisters(codegen, locations);
312 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
314 // We're moving two locations to locations that could overlap, so we need a parallel
315 // move resolver.
316 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000317 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100318 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000319 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100320 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100322 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
323 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100326 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
327 instruction_,
328 instruction_->GetDexPc(),
329 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000330 CheckEntrypointTypes<
331 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 } else {
333 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100334 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
335 instruction_,
336 instruction_->GetDexPc(),
337 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000338 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000339 }
340
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341 if (!is_fatal_) {
342 if (instruction_->IsInstanceOf()) {
343 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
344 }
345 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000346
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000347 __ jmp(GetExitLabel());
348 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000349 }
350
Alexandre Rames9931f312015-06-19 14:47:01 +0100351 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000352 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100353
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356
357 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
358};
359
Andreas Gampe85b62f22015-09-09 13:15:38 -0700360class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 public:
Aart Bik42249c32016-01-07 15:33:50 -0800362 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000363 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700364
365 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100366 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 __ Bind(GetEntryLabel());
368 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100369 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
370 instruction_,
371 instruction_->GetDexPc(),
372 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000373 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 }
375
Alexandre Rames9931f312015-06-19 14:47:01 +0100376 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
377
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
380};
381
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100382class ArraySetSlowPathX86 : public SlowPathCode {
383 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
387 LocationSummary* locations = instruction_->GetLocations();
388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, locations);
390
391 InvokeRuntimeCallingConvention calling_convention;
392 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
393 parallel_move.AddMove(
394 locations->InAt(0),
395 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
396 Primitive::kPrimNot,
397 nullptr);
398 parallel_move.AddMove(
399 locations->InAt(1),
400 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
401 Primitive::kPrimInt,
402 nullptr);
403 parallel_move.AddMove(
404 locations->InAt(2),
405 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
406 Primitive::kPrimNot,
407 nullptr);
408 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
409
410 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
411 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
412 instruction_,
413 instruction_->GetDexPc(),
414 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000415 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416 RestoreLiveRegisters(codegen, locations);
417 __ jmp(GetExitLabel());
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
421
422 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
424};
425
Roland Levillain7c1559a2015-12-15 10:55:36 +0000426// Slow path marking an object during a read barrier.
427class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
428 public:
429 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000430 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000431 DCHECK(kEmitCompilerReadBarrier);
432 }
433
434 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
435
436 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
437 LocationSummary* locations = instruction_->GetLocations();
438 Register reg_out = out_.AsRegister<Register>();
439 DCHECK(locations->CanCall());
440 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
441 DCHECK(instruction_->IsInstanceFieldGet() ||
442 instruction_->IsStaticFieldGet() ||
443 instruction_->IsArrayGet() ||
444 instruction_->IsLoadClass() ||
445 instruction_->IsLoadString() ||
446 instruction_->IsInstanceOf() ||
447 instruction_->IsCheckCast())
448 << "Unexpected instruction in read barrier marking slow path: "
449 << instruction_->DebugName();
450
451 __ Bind(GetEntryLabel());
452 SaveLiveRegisters(codegen, locations);
453
454 InvokeRuntimeCallingConvention calling_convention;
455 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
456 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
457 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
458 instruction_,
459 instruction_->GetDexPc(),
460 this);
461 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
462 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
463
464 RestoreLiveRegisters(codegen, locations);
465 __ jmp(GetExitLabel());
466 }
467
468 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000469 const Location out_;
470 const Location obj_;
471
472 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
473};
474
Roland Levillain0d5a2812015-11-13 10:07:31 +0000475// Slow path generating a read barrier for a heap reference.
476class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
477 public:
478 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
479 Location out,
480 Location ref,
481 Location obj,
482 uint32_t offset,
483 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000484 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000485 out_(out),
486 ref_(ref),
487 obj_(obj),
488 offset_(offset),
489 index_(index) {
490 DCHECK(kEmitCompilerReadBarrier);
491 // If `obj` is equal to `out` or `ref`, it means the initial object
492 // has been overwritten by (or after) the heap object reference load
493 // to be instrumented, e.g.:
494 //
495 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000496 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000497 //
498 // In that case, we have lost the information about the original
499 // object, and the emitted read barrier cannot work properly.
500 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
501 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
502 }
503
504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
505 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
506 LocationSummary* locations = instruction_->GetLocations();
507 Register reg_out = out_.AsRegister<Register>();
508 DCHECK(locations->CanCall());
509 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
510 DCHECK(!instruction_->IsInvoke() ||
511 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000512 instruction_->GetLocations()->Intrinsified()))
513 << "Unexpected instruction in read barrier for heap reference slow path: "
514 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000515
516 __ Bind(GetEntryLabel());
517 SaveLiveRegisters(codegen, locations);
518
519 // We may have to change the index's value, but as `index_` is a
520 // constant member (like other "inputs" of this slow path),
521 // introduce a copy of it, `index`.
522 Location index = index_;
523 if (index_.IsValid()) {
524 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
525 if (instruction_->IsArrayGet()) {
526 // Compute the actual memory offset and store it in `index`.
527 Register index_reg = index_.AsRegister<Register>();
528 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
529 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
530 // We are about to change the value of `index_reg` (see the
531 // calls to art::x86::X86Assembler::shll and
532 // art::x86::X86Assembler::AddImmediate below), but it has
533 // not been saved by the previous call to
534 // art::SlowPathCode::SaveLiveRegisters, as it is a
535 // callee-save register --
536 // art::SlowPathCode::SaveLiveRegisters does not consider
537 // callee-save registers, as it has been designed with the
538 // assumption that callee-save registers are supposed to be
539 // handled by the called function. So, as a callee-save
540 // register, `index_reg` _would_ eventually be saved onto
541 // the stack, but it would be too late: we would have
542 // changed its value earlier. Therefore, we manually save
543 // it here into another freely available register,
544 // `free_reg`, chosen of course among the caller-save
545 // registers (as a callee-save `free_reg` register would
546 // exhibit the same problem).
547 //
548 // Note we could have requested a temporary register from
549 // the register allocator instead; but we prefer not to, as
550 // this is a slow path, and we know we can find a
551 // caller-save register that is available.
552 Register free_reg = FindAvailableCallerSaveRegister(codegen);
553 __ movl(free_reg, index_reg);
554 index_reg = free_reg;
555 index = Location::RegisterLocation(index_reg);
556 } else {
557 // The initial register stored in `index_` has already been
558 // saved in the call to art::SlowPathCode::SaveLiveRegisters
559 // (as it is not a callee-save register), so we can freely
560 // use it.
561 }
562 // Shifting the index value contained in `index_reg` by the scale
563 // factor (2) cannot overflow in practice, as the runtime is
564 // unable to allocate object arrays with a size larger than
565 // 2^26 - 1 (that is, 2^28 - 4 bytes).
566 __ shll(index_reg, Immediate(TIMES_4));
567 static_assert(
568 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
569 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
570 __ AddImmediate(index_reg, Immediate(offset_));
571 } else {
572 DCHECK(instruction_->IsInvoke());
573 DCHECK(instruction_->GetLocations()->Intrinsified());
574 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
575 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
576 << instruction_->AsInvoke()->GetIntrinsic();
577 DCHECK_EQ(offset_, 0U);
578 DCHECK(index_.IsRegisterPair());
579 // UnsafeGet's offset location is a register pair, the low
580 // part contains the correct offset.
581 index = index_.ToLow();
582 }
583 }
584
585 // We're moving two or three locations to locations that could
586 // overlap, so we need a parallel move resolver.
587 InvokeRuntimeCallingConvention calling_convention;
588 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
589 parallel_move.AddMove(ref_,
590 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
591 Primitive::kPrimNot,
592 nullptr);
593 parallel_move.AddMove(obj_,
594 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
595 Primitive::kPrimNot,
596 nullptr);
597 if (index.IsValid()) {
598 parallel_move.AddMove(index,
599 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
600 Primitive::kPrimInt,
601 nullptr);
602 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
603 } else {
604 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
605 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
606 }
607 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
608 instruction_,
609 instruction_->GetDexPc(),
610 this);
611 CheckEntrypointTypes<
612 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
613 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
614
615 RestoreLiveRegisters(codegen, locations);
616 __ jmp(GetExitLabel());
617 }
618
619 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
620
621 private:
622 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
623 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
624 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
625 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
626 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
627 return static_cast<Register>(i);
628 }
629 }
630 // We shall never fail to find a free caller-save register, as
631 // there are more than two core caller-save registers on x86
632 // (meaning it is possible to find one which is different from
633 // `ref` and `obj`).
634 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
635 LOG(FATAL) << "Could not find a free caller-save register";
636 UNREACHABLE();
637 }
638
Roland Levillain0d5a2812015-11-13 10:07:31 +0000639 const Location out_;
640 const Location ref_;
641 const Location obj_;
642 const uint32_t offset_;
643 // An additional location containing an index to an array.
644 // Only used for HArrayGet and the UnsafeGetObject &
645 // UnsafeGetObjectVolatile intrinsics.
646 const Location index_;
647
648 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
649};
650
651// Slow path generating a read barrier for a GC root.
652class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
653 public:
654 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000655 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000656 DCHECK(kEmitCompilerReadBarrier);
657 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000658
659 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
660 LocationSummary* locations = instruction_->GetLocations();
661 Register reg_out = out_.AsRegister<Register>();
662 DCHECK(locations->CanCall());
663 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000664 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
665 << "Unexpected instruction in read barrier for GC root slow path: "
666 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000667
668 __ Bind(GetEntryLabel());
669 SaveLiveRegisters(codegen, locations);
670
671 InvokeRuntimeCallingConvention calling_convention;
672 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
673 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
674 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
675 instruction_,
676 instruction_->GetDexPc(),
677 this);
678 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
679 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
680
681 RestoreLiveRegisters(codegen, locations);
682 __ jmp(GetExitLabel());
683 }
684
685 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
686
687 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000688 const Location out_;
689 const Location root_;
690
691 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
692};
693
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100694#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700695// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
696#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100697
Aart Bike9f37602015-10-09 11:15:55 -0700698inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700699 switch (cond) {
700 case kCondEQ: return kEqual;
701 case kCondNE: return kNotEqual;
702 case kCondLT: return kLess;
703 case kCondLE: return kLessEqual;
704 case kCondGT: return kGreater;
705 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700706 case kCondB: return kBelow;
707 case kCondBE: return kBelowEqual;
708 case kCondA: return kAbove;
709 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700710 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100711 LOG(FATAL) << "Unreachable";
712 UNREACHABLE();
713}
714
Aart Bike9f37602015-10-09 11:15:55 -0700715// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100716inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
717 switch (cond) {
718 case kCondEQ: return kEqual;
719 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700720 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100721 case kCondLT: return kBelow;
722 case kCondLE: return kBelowEqual;
723 case kCondGT: return kAbove;
724 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700725 // Unsigned remain unchanged.
726 case kCondB: return kBelow;
727 case kCondBE: return kBelowEqual;
728 case kCondA: return kAbove;
729 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 }
731 LOG(FATAL) << "Unreachable";
732 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700733}
734
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100736 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100737}
738
739void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100740 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100741}
742
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100743size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
744 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
745 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100746}
747
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100748size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
749 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
750 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100751}
752
Mark Mendell7c8d0092015-01-26 11:21:33 -0500753size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
754 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
755 return GetFloatingPointSpillSlotSize();
756}
757
758size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
759 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
760 return GetFloatingPointSpillSlotSize();
761}
762
Calin Juravle175dc732015-08-25 15:42:32 +0100763void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
764 HInstruction* instruction,
765 uint32_t dex_pc,
766 SlowPathCode* slow_path) {
767 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
768 instruction,
769 dex_pc,
770 slow_path);
771}
772
773void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100774 HInstruction* instruction,
775 uint32_t dex_pc,
776 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100777 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100778 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100779 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100780}
781
Mark Mendellfb8d2792015-03-31 22:16:59 -0400782CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000783 const X86InstructionSetFeatures& isa_features,
784 const CompilerOptions& compiler_options,
785 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500786 : CodeGenerator(graph,
787 kNumberOfCpuRegisters,
788 kNumberOfXmmRegisters,
789 kNumberOfRegisterPairs,
790 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
791 arraysize(kCoreCalleeSaves))
792 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100793 0,
794 compiler_options,
795 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100796 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100797 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100798 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400799 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100800 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000801 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100802 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400803 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000804 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000805 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
806 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100807 constant_area_start_(-1),
808 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
809 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000810 // Use a fake return address register to mimic Quick.
811 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100812}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100813
David Brazdil58282f42016-01-14 12:45:10 +0000814void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100815 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100816 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100817
818 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100819 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100820
Calin Juravle34bacdf2014-10-07 20:23:36 +0100821 UpdateBlockedPairRegisters();
822}
823
824void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
825 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
826 X86ManagedRegister current =
827 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
828 if (blocked_core_registers_[current.AsRegisterPairLow()]
829 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
830 blocked_register_pairs_[i] = true;
831 }
832 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100833}
834
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100835InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800836 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100837 assembler_(codegen->GetAssembler()),
838 codegen_(codegen) {}
839
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100840static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100841 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100842}
843
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000844void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100845 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000846 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000847 bool skip_overflow_check =
848 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000849 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000850
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000851 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100852 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100853 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100854 }
855
Mark Mendell5f874182015-03-04 15:42:45 -0500856 if (HasEmptyFrame()) {
857 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000858 }
Mark Mendell5f874182015-03-04 15:42:45 -0500859
860 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
861 Register reg = kCoreCalleeSaves[i];
862 if (allocated_registers_.ContainsCoreRegister(reg)) {
863 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100864 __ cfi().AdjustCFAOffset(kX86WordSize);
865 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500866 }
867 }
868
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100869 int adjust = GetFrameSize() - FrameEntrySpillSize();
870 __ subl(ESP, Immediate(adjust));
871 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100872 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000873}
874
875void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100876 __ cfi().RememberState();
877 if (!HasEmptyFrame()) {
878 int adjust = GetFrameSize() - FrameEntrySpillSize();
879 __ addl(ESP, Immediate(adjust));
880 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500881
David Srbeckyc34dc932015-04-12 09:27:43 +0100882 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
883 Register reg = kCoreCalleeSaves[i];
884 if (allocated_registers_.ContainsCoreRegister(reg)) {
885 __ popl(reg);
886 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
887 __ cfi().Restore(DWARFReg(reg));
888 }
Mark Mendell5f874182015-03-04 15:42:45 -0500889 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000890 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100891 __ ret();
892 __ cfi().RestoreState();
893 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000894}
895
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100896void CodeGeneratorX86::Bind(HBasicBlock* block) {
897 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000898}
899
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100900Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
901 switch (type) {
902 case Primitive::kPrimBoolean:
903 case Primitive::kPrimByte:
904 case Primitive::kPrimChar:
905 case Primitive::kPrimShort:
906 case Primitive::kPrimInt:
907 case Primitive::kPrimNot:
908 return Location::RegisterLocation(EAX);
909
910 case Primitive::kPrimLong:
911 return Location::RegisterPairLocation(EAX, EDX);
912
913 case Primitive::kPrimVoid:
914 return Location::NoLocation();
915
916 case Primitive::kPrimDouble:
917 case Primitive::kPrimFloat:
918 return Location::FpuRegisterLocation(XMM0);
919 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100920
921 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100922}
923
924Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
925 return Location::RegisterLocation(kMethodRegisterArgument);
926}
927
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100928Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100929 switch (type) {
930 case Primitive::kPrimBoolean:
931 case Primitive::kPrimByte:
932 case Primitive::kPrimChar:
933 case Primitive::kPrimShort:
934 case Primitive::kPrimInt:
935 case Primitive::kPrimNot: {
936 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000937 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100938 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100939 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100940 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000941 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100942 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100943 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100944
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000945 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100946 uint32_t index = gp_index_;
947 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000948 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100949 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100950 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
951 calling_convention.GetRegisterPairAt(index));
952 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100953 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000954 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
955 }
956 }
957
958 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100959 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000960 stack_index_++;
961 if (index < calling_convention.GetNumberOfFpuRegisters()) {
962 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
963 } else {
964 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
965 }
966 }
967
968 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100969 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000970 stack_index_ += 2;
971 if (index < calling_convention.GetNumberOfFpuRegisters()) {
972 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
973 } else {
974 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100975 }
976 }
977
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100978 case Primitive::kPrimVoid:
979 LOG(FATAL) << "Unexpected parameter type " << type;
980 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100981 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000982 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100983}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100984
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100985void CodeGeneratorX86::Move32(Location destination, Location source) {
986 if (source.Equals(destination)) {
987 return;
988 }
989 if (destination.IsRegister()) {
990 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000991 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100992 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000993 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100994 } else {
995 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000996 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100997 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100998 } else if (destination.IsFpuRegister()) {
999 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001000 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001001 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001002 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001003 } else {
1004 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001005 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001006 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001008 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001009 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001010 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001011 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001012 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001013 } else if (source.IsConstant()) {
1014 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001015 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001016 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017 } else {
1018 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001019 __ pushl(Address(ESP, source.GetStackIndex()));
1020 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001021 }
1022 }
1023}
1024
1025void CodeGeneratorX86::Move64(Location destination, Location source) {
1026 if (source.Equals(destination)) {
1027 return;
1028 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001029 if (destination.IsRegisterPair()) {
1030 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001031 EmitParallelMoves(
1032 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1033 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001034 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001035 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001036 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1037 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001038 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001039 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1040 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1041 __ psrlq(src_reg, Immediate(32));
1042 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001043 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001044 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001045 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001046 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1047 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001048 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1049 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001050 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001051 if (source.IsFpuRegister()) {
1052 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1053 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001054 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001055 } else if (source.IsRegisterPair()) {
1056 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1057 // Create stack space for 2 elements.
1058 __ subl(ESP, Immediate(2 * elem_size));
1059 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1060 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1061 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1062 // And remove the temporary stack space we allocated.
1063 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001064 } else {
1065 LOG(FATAL) << "Unimplemented";
1066 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001067 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001068 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001069 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001070 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001071 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001072 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001073 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001075 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001076 } else if (source.IsConstant()) {
1077 HConstant* constant = source.GetConstant();
1078 int64_t value;
1079 if (constant->IsLongConstant()) {
1080 value = constant->AsLongConstant()->GetValue();
1081 } else {
1082 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001083 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001084 }
1085 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1086 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001087 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001088 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001089 EmitParallelMoves(
1090 Location::StackSlot(source.GetStackIndex()),
1091 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001092 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001093 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001094 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1095 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001096 }
1097 }
1098}
1099
Calin Juravle175dc732015-08-25 15:42:32 +01001100void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1101 DCHECK(location.IsRegister());
1102 __ movl(location.AsRegister<Register>(), Immediate(value));
1103}
1104
Calin Juravlee460d1d2015-09-29 04:52:17 +01001105void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001106 HParallelMove move(GetGraph()->GetArena());
1107 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1108 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1109 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001110 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001111 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001112 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001113 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001114}
1115
1116void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1117 if (location.IsRegister()) {
1118 locations->AddTemp(location);
1119 } else if (location.IsRegisterPair()) {
1120 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1121 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1122 } else {
1123 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1124 }
1125}
1126
David Brazdilfc6a86a2015-06-26 10:33:45 +00001127void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001128 DCHECK(!successor->IsExitBlock());
1129
1130 HBasicBlock* block = got->GetBlock();
1131 HInstruction* previous = got->GetPrevious();
1132
1133 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001134 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001135 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1136 return;
1137 }
1138
1139 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1140 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1141 }
1142 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001143 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001144 }
1145}
1146
David Brazdilfc6a86a2015-06-26 10:33:45 +00001147void LocationsBuilderX86::VisitGoto(HGoto* got) {
1148 got->SetLocations(nullptr);
1149}
1150
1151void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1152 HandleGoto(got, got->GetSuccessor());
1153}
1154
1155void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1156 try_boundary->SetLocations(nullptr);
1157}
1158
1159void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1160 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1161 if (!successor->IsExitBlock()) {
1162 HandleGoto(try_boundary, successor);
1163 }
1164}
1165
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001166void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001167 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001168}
1169
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001170void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001171}
1172
Mark Mendell152408f2015-12-31 12:28:50 -05001173template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001174void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001175 LabelType* true_label,
1176 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001177 if (cond->IsFPConditionTrueIfNaN()) {
1178 __ j(kUnordered, true_label);
1179 } else if (cond->IsFPConditionFalseIfNaN()) {
1180 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001181 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001182 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001183}
1184
Mark Mendell152408f2015-12-31 12:28:50 -05001185template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001186void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001187 LabelType* true_label,
1188 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001189 LocationSummary* locations = cond->GetLocations();
1190 Location left = locations->InAt(0);
1191 Location right = locations->InAt(1);
1192 IfCondition if_cond = cond->GetCondition();
1193
Mark Mendellc4701932015-04-10 13:18:51 -04001194 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001195 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001196 IfCondition true_high_cond = if_cond;
1197 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001198 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001199
1200 // Set the conditions for the test, remembering that == needs to be
1201 // decided using the low words.
1202 switch (if_cond) {
1203 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001204 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001205 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001206 break;
1207 case kCondLT:
1208 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001209 break;
1210 case kCondLE:
1211 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001212 break;
1213 case kCondGT:
1214 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001215 break;
1216 case kCondGE:
1217 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001218 break;
Aart Bike9f37602015-10-09 11:15:55 -07001219 case kCondB:
1220 false_high_cond = kCondA;
1221 break;
1222 case kCondBE:
1223 true_high_cond = kCondB;
1224 break;
1225 case kCondA:
1226 false_high_cond = kCondB;
1227 break;
1228 case kCondAE:
1229 true_high_cond = kCondA;
1230 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001231 }
1232
1233 if (right.IsConstant()) {
1234 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001235 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001236 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001237
Aart Bika19616e2016-02-01 18:57:58 -08001238 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001239 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001240 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001241 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001242 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001243 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001244 __ j(X86Condition(true_high_cond), true_label);
1245 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001246 }
1247 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001248 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001249 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001250 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001251 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001252
1253 __ cmpl(left_high, right_high);
1254 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001255 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001256 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001257 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001258 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001259 __ j(X86Condition(true_high_cond), true_label);
1260 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001261 }
1262 // Must be equal high, so compare the lows.
1263 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001264 } else {
1265 DCHECK(right.IsDoubleStackSlot());
1266 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1267 if (if_cond == kCondNE) {
1268 __ j(X86Condition(true_high_cond), true_label);
1269 } else if (if_cond == kCondEQ) {
1270 __ j(X86Condition(false_high_cond), false_label);
1271 } else {
1272 __ j(X86Condition(true_high_cond), true_label);
1273 __ j(X86Condition(false_high_cond), false_label);
1274 }
1275 // Must be equal high, so compare the lows.
1276 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001277 }
1278 // The last comparison might be unsigned.
1279 __ j(final_condition, true_label);
1280}
1281
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001282void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1283 Location rhs,
1284 HInstruction* insn,
1285 bool is_double) {
1286 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1287 if (is_double) {
1288 if (rhs.IsFpuRegister()) {
1289 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1290 } else if (const_area != nullptr) {
1291 DCHECK(const_area->IsEmittedAtUseSite());
1292 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1293 codegen_->LiteralDoubleAddress(
1294 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1295 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1296 } else {
1297 DCHECK(rhs.IsDoubleStackSlot());
1298 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1299 }
1300 } else {
1301 if (rhs.IsFpuRegister()) {
1302 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1303 } else if (const_area != nullptr) {
1304 DCHECK(const_area->IsEmittedAtUseSite());
1305 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1306 codegen_->LiteralFloatAddress(
1307 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1308 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1309 } else {
1310 DCHECK(rhs.IsStackSlot());
1311 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1312 }
1313 }
1314}
1315
Mark Mendell152408f2015-12-31 12:28:50 -05001316template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001317void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001318 LabelType* true_target_in,
1319 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001320 // Generated branching requires both targets to be explicit. If either of the
1321 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001322 LabelType fallthrough_target;
1323 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1324 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001325
Mark Mendellc4701932015-04-10 13:18:51 -04001326 LocationSummary* locations = condition->GetLocations();
1327 Location left = locations->InAt(0);
1328 Location right = locations->InAt(1);
1329
Mark Mendellc4701932015-04-10 13:18:51 -04001330 Primitive::Type type = condition->InputAt(0)->GetType();
1331 switch (type) {
1332 case Primitive::kPrimLong:
1333 GenerateLongComparesAndJumps(condition, true_target, false_target);
1334 break;
1335 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001336 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001337 GenerateFPJumps(condition, true_target, false_target);
1338 break;
1339 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001340 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001341 GenerateFPJumps(condition, true_target, false_target);
1342 break;
1343 default:
1344 LOG(FATAL) << "Unexpected compare type " << type;
1345 }
1346
David Brazdil0debae72015-11-12 18:37:00 +00001347 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001348 __ jmp(false_target);
1349 }
David Brazdil0debae72015-11-12 18:37:00 +00001350
1351 if (fallthrough_target.IsLinked()) {
1352 __ Bind(&fallthrough_target);
1353 }
Mark Mendellc4701932015-04-10 13:18:51 -04001354}
1355
David Brazdil0debae72015-11-12 18:37:00 +00001356static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1357 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1358 // are set only strictly before `branch`. We can't use the eflags on long/FP
1359 // conditions if they are materialized due to the complex branching.
1360 return cond->IsCondition() &&
1361 cond->GetNext() == branch &&
1362 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1363 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1364}
1365
Mark Mendell152408f2015-12-31 12:28:50 -05001366template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001367void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001368 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001369 LabelType* true_target,
1370 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001371 HInstruction* cond = instruction->InputAt(condition_input_index);
1372
1373 if (true_target == nullptr && false_target == nullptr) {
1374 // Nothing to do. The code always falls through.
1375 return;
1376 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001377 // Constant condition, statically compared against "true" (integer value 1).
1378 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001379 if (true_target != nullptr) {
1380 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001381 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001382 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001383 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001384 if (false_target != nullptr) {
1385 __ jmp(false_target);
1386 }
1387 }
1388 return;
1389 }
1390
1391 // The following code generates these patterns:
1392 // (1) true_target == nullptr && false_target != nullptr
1393 // - opposite condition true => branch to false_target
1394 // (2) true_target != nullptr && false_target == nullptr
1395 // - condition true => branch to true_target
1396 // (3) true_target != nullptr && false_target != nullptr
1397 // - condition true => branch to true_target
1398 // - branch to false_target
1399 if (IsBooleanValueOrMaterializedCondition(cond)) {
1400 if (AreEflagsSetFrom(cond, instruction)) {
1401 if (true_target == nullptr) {
1402 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1403 } else {
1404 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1405 }
1406 } else {
1407 // Materialized condition, compare against 0.
1408 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1409 if (lhs.IsRegister()) {
1410 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1411 } else {
1412 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1413 }
1414 if (true_target == nullptr) {
1415 __ j(kEqual, false_target);
1416 } else {
1417 __ j(kNotEqual, true_target);
1418 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001419 }
1420 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001421 // Condition has not been materialized, use its inputs as the comparison and
1422 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001423 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001424
1425 // If this is a long or FP comparison that has been folded into
1426 // the HCondition, generate the comparison directly.
1427 Primitive::Type type = condition->InputAt(0)->GetType();
1428 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1429 GenerateCompareTestAndBranch(condition, true_target, false_target);
1430 return;
1431 }
1432
1433 Location lhs = condition->GetLocations()->InAt(0);
1434 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001435 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001436 if (rhs.IsRegister()) {
1437 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1438 } else if (rhs.IsConstant()) {
1439 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001440 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001441 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001442 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1443 }
1444 if (true_target == nullptr) {
1445 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1446 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001447 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001448 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001449 }
David Brazdil0debae72015-11-12 18:37:00 +00001450
1451 // If neither branch falls through (case 3), the conditional branch to `true_target`
1452 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1453 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001454 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001455 }
1456}
1457
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001458void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001459 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1460 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001461 locations->SetInAt(0, Location::Any());
1462 }
1463}
1464
1465void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001466 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1467 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1468 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1469 nullptr : codegen_->GetLabelOf(true_successor);
1470 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1471 nullptr : codegen_->GetLabelOf(false_successor);
1472 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001473}
1474
1475void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1476 LocationSummary* locations = new (GetGraph()->GetArena())
1477 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001478 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001479 locations->SetInAt(0, Location::Any());
1480 }
1481}
1482
1483void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001484 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001485 GenerateTestAndBranch<Label>(deoptimize,
1486 /* condition_input_index */ 0,
1487 slow_path->GetEntryLabel(),
1488 /* false_target */ nullptr);
1489}
1490
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001491static bool SelectCanUseCMOV(HSelect* select) {
1492 // There are no conditional move instructions for XMMs.
1493 if (Primitive::IsFloatingPointType(select->GetType())) {
1494 return false;
1495 }
1496
1497 // A FP condition doesn't generate the single CC that we need.
1498 // In 32 bit mode, a long condition doesn't generate a single CC either.
1499 HInstruction* condition = select->GetCondition();
1500 if (condition->IsCondition()) {
1501 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1502 if (compare_type == Primitive::kPrimLong ||
1503 Primitive::IsFloatingPointType(compare_type)) {
1504 return false;
1505 }
1506 }
1507
1508 // We can generate a CMOV for this Select.
1509 return true;
1510}
1511
David Brazdil74eb1b22015-12-14 11:44:01 +00001512void LocationsBuilderX86::VisitSelect(HSelect* select) {
1513 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001514 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001515 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001516 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001517 } else {
1518 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001519 if (SelectCanUseCMOV(select)) {
1520 if (select->InputAt(1)->IsConstant()) {
1521 // Cmov can't handle a constant value.
1522 locations->SetInAt(1, Location::RequiresRegister());
1523 } else {
1524 locations->SetInAt(1, Location::Any());
1525 }
1526 } else {
1527 locations->SetInAt(1, Location::Any());
1528 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001529 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001530 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1531 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001532 }
1533 locations->SetOut(Location::SameAsFirstInput());
1534}
1535
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001536void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1537 Register lhs_reg = lhs.AsRegister<Register>();
1538 if (rhs.IsConstant()) {
1539 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1540 codegen_->Compare32BitValue(lhs_reg, value);
1541 } else if (rhs.IsStackSlot()) {
1542 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1543 } else {
1544 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1545 }
1546}
1547
David Brazdil74eb1b22015-12-14 11:44:01 +00001548void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1549 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001550 DCHECK(locations->InAt(0).Equals(locations->Out()));
1551 if (SelectCanUseCMOV(select)) {
1552 // If both the condition and the source types are integer, we can generate
1553 // a CMOV to implement Select.
1554
1555 HInstruction* select_condition = select->GetCondition();
1556 Condition cond = kNotEqual;
1557
1558 // Figure out how to test the 'condition'.
1559 if (select_condition->IsCondition()) {
1560 HCondition* condition = select_condition->AsCondition();
1561 if (!condition->IsEmittedAtUseSite()) {
1562 // This was a previously materialized condition.
1563 // Can we use the existing condition code?
1564 if (AreEflagsSetFrom(condition, select)) {
1565 // Materialization was the previous instruction. Condition codes are right.
1566 cond = X86Condition(condition->GetCondition());
1567 } else {
1568 // No, we have to recreate the condition code.
1569 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1570 __ testl(cond_reg, cond_reg);
1571 }
1572 } else {
1573 // We can't handle FP or long here.
1574 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1575 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1576 LocationSummary* cond_locations = condition->GetLocations();
1577 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1578 cond = X86Condition(condition->GetCondition());
1579 }
1580 } else {
1581 // Must be a boolean condition, which needs to be compared to 0.
1582 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1583 __ testl(cond_reg, cond_reg);
1584 }
1585
1586 // If the condition is true, overwrite the output, which already contains false.
1587 Location false_loc = locations->InAt(0);
1588 Location true_loc = locations->InAt(1);
1589 if (select->GetType() == Primitive::kPrimLong) {
1590 // 64 bit conditional move.
1591 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1592 Register false_low = false_loc.AsRegisterPairLow<Register>();
1593 if (true_loc.IsRegisterPair()) {
1594 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1595 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1596 } else {
1597 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1598 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1599 }
1600 } else {
1601 // 32 bit conditional move.
1602 Register false_reg = false_loc.AsRegister<Register>();
1603 if (true_loc.IsRegister()) {
1604 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1605 } else {
1606 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1607 }
1608 }
1609 } else {
1610 NearLabel false_target;
1611 GenerateTestAndBranch<NearLabel>(
1612 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1613 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1614 __ Bind(&false_target);
1615 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001616}
1617
David Srbecky0cf44932015-12-09 14:09:59 +00001618void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1619 new (GetGraph()->GetArena()) LocationSummary(info);
1620}
1621
David Srbeckyd28f4a02016-03-14 17:14:24 +00001622void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1623 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001624}
1625
1626void CodeGeneratorX86::GenerateNop() {
1627 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001628}
1629
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001630void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001631 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001632 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001633 // Handle the long/FP comparisons made in instruction simplification.
1634 switch (cond->InputAt(0)->GetType()) {
1635 case Primitive::kPrimLong: {
1636 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001637 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001638 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001639 locations->SetOut(Location::RequiresRegister());
1640 }
1641 break;
1642 }
1643 case Primitive::kPrimFloat:
1644 case Primitive::kPrimDouble: {
1645 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001646 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1647 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1648 } else if (cond->InputAt(1)->IsConstant()) {
1649 locations->SetInAt(1, Location::RequiresFpuRegister());
1650 } else {
1651 locations->SetInAt(1, Location::Any());
1652 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001653 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001654 locations->SetOut(Location::RequiresRegister());
1655 }
1656 break;
1657 }
1658 default:
1659 locations->SetInAt(0, Location::RequiresRegister());
1660 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001661 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001662 // We need a byte register.
1663 locations->SetOut(Location::RegisterLocation(ECX));
1664 }
1665 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001666 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001667}
1668
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001669void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001670 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001671 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001672 }
Mark Mendellc4701932015-04-10 13:18:51 -04001673
1674 LocationSummary* locations = cond->GetLocations();
1675 Location lhs = locations->InAt(0);
1676 Location rhs = locations->InAt(1);
1677 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001678 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001679
1680 switch (cond->InputAt(0)->GetType()) {
1681 default: {
1682 // Integer case.
1683
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001684 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001685 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001686 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001687 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001688 return;
1689 }
1690 case Primitive::kPrimLong:
1691 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1692 break;
1693 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001694 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001695 GenerateFPJumps(cond, &true_label, &false_label);
1696 break;
1697 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001698 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001699 GenerateFPJumps(cond, &true_label, &false_label);
1700 break;
1701 }
1702
1703 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001704 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001705
Roland Levillain4fa13f62015-07-06 18:11:54 +01001706 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001707 __ Bind(&false_label);
1708 __ xorl(reg, reg);
1709 __ jmp(&done_label);
1710
Roland Levillain4fa13f62015-07-06 18:11:54 +01001711 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001712 __ Bind(&true_label);
1713 __ movl(reg, Immediate(1));
1714 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001715}
1716
1717void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001718 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001719}
1720
1721void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001723}
1724
1725void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001731}
1732
1733void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001735}
1736
1737void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001739}
1740
1741void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001743}
1744
1745void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001763}
1764
Aart Bike9f37602015-10-09 11:15:55 -07001765void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001767}
1768
1769void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001771}
1772
1773void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001775}
1776
1777void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001779}
1780
1781void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001783}
1784
1785void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001787}
1788
1789void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001791}
1792
1793void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001795}
1796
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001797void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001798 LocationSummary* locations =
1799 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001800 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001801}
1802
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001803void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001804 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001805}
1806
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001807void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1810 locations->SetOut(Location::ConstantLocation(constant));
1811}
1812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001814 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001815}
1816
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001817void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001820 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001821}
1822
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001823void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001824 // Will be generated at use site.
1825}
1826
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001827void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1828 LocationSummary* locations =
1829 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1830 locations->SetOut(Location::ConstantLocation(constant));
1831}
1832
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001833void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001834 // Will be generated at use site.
1835}
1836
1837void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1838 LocationSummary* locations =
1839 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1840 locations->SetOut(Location::ConstantLocation(constant));
1841}
1842
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001843void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001844 // Will be generated at use site.
1845}
1846
Calin Juravle27df7582015-04-17 19:12:31 +01001847void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1848 memory_barrier->SetLocations(nullptr);
1849}
1850
1851void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001852 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001853}
1854
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001855void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001856 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001857}
1858
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001859void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001860 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001861}
1862
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001863void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001864 LocationSummary* locations =
1865 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001866 switch (ret->InputAt(0)->GetType()) {
1867 case Primitive::kPrimBoolean:
1868 case Primitive::kPrimByte:
1869 case Primitive::kPrimChar:
1870 case Primitive::kPrimShort:
1871 case Primitive::kPrimInt:
1872 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001873 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001874 break;
1875
1876 case Primitive::kPrimLong:
1877 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001878 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001879 break;
1880
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001881 case Primitive::kPrimFloat:
1882 case Primitive::kPrimDouble:
1883 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001884 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 break;
1886
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001887 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001888 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001890}
1891
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001892void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893 if (kIsDebugBuild) {
1894 switch (ret->InputAt(0)->GetType()) {
1895 case Primitive::kPrimBoolean:
1896 case Primitive::kPrimByte:
1897 case Primitive::kPrimChar:
1898 case Primitive::kPrimShort:
1899 case Primitive::kPrimInt:
1900 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001901 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001902 break;
1903
1904 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001905 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1906 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001907 break;
1908
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001909 case Primitive::kPrimFloat:
1910 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001911 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001912 break;
1913
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001914 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001915 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001916 }
1917 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001918 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001919}
1920
Calin Juravle175dc732015-08-25 15:42:32 +01001921void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1922 // The trampoline uses the same calling convention as dex calling conventions,
1923 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1924 // the method_idx.
1925 HandleInvoke(invoke);
1926}
1927
1928void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1929 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1930}
1931
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001932void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001933 // Explicit clinit checks triggered by static invokes must have been pruned by
1934 // art::PrepareForRegisterAllocation.
1935 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001936
Mark Mendellfb8d2792015-03-31 22:16:59 -04001937 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001938 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001939 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001940 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001941 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001942 return;
1943 }
1944
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001945 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001946
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001947 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1948 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001949 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001950 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001951}
1952
Mark Mendell09ed1a32015-03-25 08:30:06 -04001953static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1954 if (invoke->GetLocations()->Intrinsified()) {
1955 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1956 intrinsic.Dispatch(invoke);
1957 return true;
1958 }
1959 return false;
1960}
1961
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001962void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001963 // Explicit clinit checks triggered by static invokes must have been pruned by
1964 // art::PrepareForRegisterAllocation.
1965 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001966
Mark Mendell09ed1a32015-03-25 08:30:06 -04001967 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1968 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001969 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001970
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001971 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001972 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001973 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001974 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001975}
1976
1977void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001978 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1979 if (intrinsic.TryDispatch(invoke)) {
1980 return;
1981 }
1982
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001983 HandleInvoke(invoke);
1984}
1985
1986void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001987 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001988 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001989}
1990
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001991void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001992 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1993 return;
1994 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001995
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001996 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001997 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001998 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001999}
2000
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002001void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002002 // This call to HandleInvoke allocates a temporary (core) register
2003 // which is also used to transfer the hidden argument from FP to
2004 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002005 HandleInvoke(invoke);
2006 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002007 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002008}
2009
2010void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2011 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002012 LocationSummary* locations = invoke->GetLocations();
2013 Register temp = locations->GetTemp(0).AsRegister<Register>();
2014 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002015 Location receiver = locations->InAt(0);
2016 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2017
Roland Levillain0d5a2812015-11-13 10:07:31 +00002018 // Set the hidden argument. This is safe to do this here, as XMM7
2019 // won't be modified thereafter, before the `call` instruction.
2020 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002021 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002022 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002024 if (receiver.IsStackSlot()) {
2025 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027 __ movl(temp, Address(temp, class_offset));
2028 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002029 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002030 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002031 }
Roland Levillain4d027112015-07-01 15:41:14 +01002032 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002033 // Instead of simply (possibly) unpoisoning `temp` here, we should
2034 // emit a read barrier for the previous class reference load.
2035 // However this is not required in practice, as this is an
2036 // intermediate/temporary reference and because the current
2037 // concurrent copying collector keeps the from-space memory
2038 // intact/accessible until the end of the marking phase (the
2039 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002040 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03002041 // temp = temp->GetAddressOfIMT()
2042 __ movl(temp,
2043 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002044 // temp = temp->GetImtEntryAt(method_offset);
Nelli Kimbadee982016-05-13 13:08:53 +03002045 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
2046 invoke->GetImtIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002047 __ movl(temp, Address(temp, method_offset));
2048 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002049 __ call(Address(temp,
2050 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051
2052 DCHECK(!codegen_->IsLeafMethod());
2053 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2054}
2055
Roland Levillain88cb1752014-10-20 16:36:47 +01002056void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2057 LocationSummary* locations =
2058 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2059 switch (neg->GetResultType()) {
2060 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002061 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002062 locations->SetInAt(0, Location::RequiresRegister());
2063 locations->SetOut(Location::SameAsFirstInput());
2064 break;
2065
Roland Levillain88cb1752014-10-20 16:36:47 +01002066 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002067 locations->SetInAt(0, Location::RequiresFpuRegister());
2068 locations->SetOut(Location::SameAsFirstInput());
2069 locations->AddTemp(Location::RequiresRegister());
2070 locations->AddTemp(Location::RequiresFpuRegister());
2071 break;
2072
Roland Levillain88cb1752014-10-20 16:36:47 +01002073 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002074 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002075 locations->SetOut(Location::SameAsFirstInput());
2076 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002077 break;
2078
2079 default:
2080 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2081 }
2082}
2083
2084void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2085 LocationSummary* locations = neg->GetLocations();
2086 Location out = locations->Out();
2087 Location in = locations->InAt(0);
2088 switch (neg->GetResultType()) {
2089 case Primitive::kPrimInt:
2090 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002091 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002092 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002093 break;
2094
2095 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002096 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002097 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002098 __ negl(out.AsRegisterPairLow<Register>());
2099 // Negation is similar to subtraction from zero. The least
2100 // significant byte triggers a borrow when it is different from
2101 // zero; to take it into account, add 1 to the most significant
2102 // byte if the carry flag (CF) is set to 1 after the first NEGL
2103 // operation.
2104 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2105 __ negl(out.AsRegisterPairHigh<Register>());
2106 break;
2107
Roland Levillain5368c212014-11-27 15:03:41 +00002108 case Primitive::kPrimFloat: {
2109 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002110 Register constant = locations->GetTemp(0).AsRegister<Register>();
2111 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002112 // Implement float negation with an exclusive or with value
2113 // 0x80000000 (mask for bit 31, representing the sign of a
2114 // single-precision floating-point number).
2115 __ movl(constant, Immediate(INT32_C(0x80000000)));
2116 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002117 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002118 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002119 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002120
Roland Levillain5368c212014-11-27 15:03:41 +00002121 case Primitive::kPrimDouble: {
2122 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002123 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002124 // Implement double negation with an exclusive or with value
2125 // 0x8000000000000000 (mask for bit 63, representing the sign of
2126 // a double-precision floating-point number).
2127 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002128 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002129 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002130 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002131
2132 default:
2133 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2134 }
2135}
2136
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002137void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2138 LocationSummary* locations =
2139 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2140 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2141 locations->SetInAt(0, Location::RequiresFpuRegister());
2142 locations->SetInAt(1, Location::RequiresRegister());
2143 locations->SetOut(Location::SameAsFirstInput());
2144 locations->AddTemp(Location::RequiresFpuRegister());
2145}
2146
2147void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2148 LocationSummary* locations = neg->GetLocations();
2149 Location out = locations->Out();
2150 DCHECK(locations->InAt(0).Equals(out));
2151
2152 Register constant_area = locations->InAt(1).AsRegister<Register>();
2153 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2154 if (neg->GetType() == Primitive::kPrimFloat) {
2155 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2156 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2157 } else {
2158 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2159 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2160 }
2161}
2162
Roland Levillaindff1f282014-11-05 14:15:05 +00002163void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002164 Primitive::Type result_type = conversion->GetResultType();
2165 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002166 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002167
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002168 // The float-to-long and double-to-long type conversions rely on a
2169 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002170 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002171 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2172 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002173 ? LocationSummary::kCall
2174 : LocationSummary::kNoCall;
2175 LocationSummary* locations =
2176 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2177
David Brazdilb2bd1c52015-03-25 11:17:37 +00002178 // The Java language does not allow treating boolean as an integral type but
2179 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002180
Roland Levillaindff1f282014-11-05 14:15:05 +00002181 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002182 case Primitive::kPrimByte:
2183 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002184 case Primitive::kPrimLong: {
2185 // Type conversion from long to byte is a result of code transformations.
2186 HInstruction* input = conversion->InputAt(0);
2187 Location input_location = input->IsConstant()
2188 ? Location::ConstantLocation(input->AsConstant())
2189 : Location::RegisterPairLocation(EAX, EDX);
2190 locations->SetInAt(0, input_location);
2191 // Make the output overlap to please the register allocator. This greatly simplifies
2192 // the validation of the linear scan implementation
2193 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2194 break;
2195 }
David Brazdil46e2a392015-03-16 17:31:52 +00002196 case Primitive::kPrimBoolean:
2197 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002198 case Primitive::kPrimShort:
2199 case Primitive::kPrimInt:
2200 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002201 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002202 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2203 // Make the output overlap to please the register allocator. This greatly simplifies
2204 // the validation of the linear scan implementation
2205 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002206 break;
2207
2208 default:
2209 LOG(FATAL) << "Unexpected type conversion from " << input_type
2210 << " to " << result_type;
2211 }
2212 break;
2213
Roland Levillain01a8d712014-11-14 16:27:39 +00002214 case Primitive::kPrimShort:
2215 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002216 case Primitive::kPrimLong:
2217 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002218 case Primitive::kPrimBoolean:
2219 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002220 case Primitive::kPrimByte:
2221 case Primitive::kPrimInt:
2222 case Primitive::kPrimChar:
2223 // Processing a Dex `int-to-short' instruction.
2224 locations->SetInAt(0, Location::Any());
2225 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2226 break;
2227
2228 default:
2229 LOG(FATAL) << "Unexpected type conversion from " << input_type
2230 << " to " << result_type;
2231 }
2232 break;
2233
Roland Levillain946e1432014-11-11 17:35:19 +00002234 case Primitive::kPrimInt:
2235 switch (input_type) {
2236 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002237 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002238 locations->SetInAt(0, Location::Any());
2239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2240 break;
2241
2242 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002243 // Processing a Dex `float-to-int' instruction.
2244 locations->SetInAt(0, Location::RequiresFpuRegister());
2245 locations->SetOut(Location::RequiresRegister());
2246 locations->AddTemp(Location::RequiresFpuRegister());
2247 break;
2248
Roland Levillain946e1432014-11-11 17:35:19 +00002249 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002250 // Processing a Dex `double-to-int' instruction.
2251 locations->SetInAt(0, Location::RequiresFpuRegister());
2252 locations->SetOut(Location::RequiresRegister());
2253 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002254 break;
2255
2256 default:
2257 LOG(FATAL) << "Unexpected type conversion from " << input_type
2258 << " to " << result_type;
2259 }
2260 break;
2261
Roland Levillaindff1f282014-11-05 14:15:05 +00002262 case Primitive::kPrimLong:
2263 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002264 case Primitive::kPrimBoolean:
2265 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 case Primitive::kPrimByte:
2267 case Primitive::kPrimShort:
2268 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002269 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002270 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002271 locations->SetInAt(0, Location::RegisterLocation(EAX));
2272 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2273 break;
2274
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002275 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002276 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002277 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002278 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002279 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2280 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2281
Vladimir Marko949c91f2015-01-27 10:48:44 +00002282 // The runtime helper puts the result in EAX, EDX.
2283 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002284 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002285 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002286
2287 default:
2288 LOG(FATAL) << "Unexpected type conversion from " << input_type
2289 << " to " << result_type;
2290 }
2291 break;
2292
Roland Levillain981e4542014-11-14 11:47:14 +00002293 case Primitive::kPrimChar:
2294 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002295 case Primitive::kPrimLong:
2296 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002297 case Primitive::kPrimBoolean:
2298 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002299 case Primitive::kPrimByte:
2300 case Primitive::kPrimShort:
2301 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002302 // Processing a Dex `int-to-char' instruction.
2303 locations->SetInAt(0, Location::Any());
2304 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2305 break;
2306
2307 default:
2308 LOG(FATAL) << "Unexpected type conversion from " << input_type
2309 << " to " << result_type;
2310 }
2311 break;
2312
Roland Levillaindff1f282014-11-05 14:15:05 +00002313 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002314 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002315 case Primitive::kPrimBoolean:
2316 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002317 case Primitive::kPrimByte:
2318 case Primitive::kPrimShort:
2319 case Primitive::kPrimInt:
2320 case Primitive::kPrimChar:
2321 // Processing a Dex `int-to-float' instruction.
2322 locations->SetInAt(0, Location::RequiresRegister());
2323 locations->SetOut(Location::RequiresFpuRegister());
2324 break;
2325
2326 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002327 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002328 locations->SetInAt(0, Location::Any());
2329 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002330 break;
2331
Roland Levillaincff13742014-11-17 14:32:17 +00002332 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002333 // Processing a Dex `double-to-float' instruction.
2334 locations->SetInAt(0, Location::RequiresFpuRegister());
2335 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002336 break;
2337
2338 default:
2339 LOG(FATAL) << "Unexpected type conversion from " << input_type
2340 << " to " << result_type;
2341 };
2342 break;
2343
Roland Levillaindff1f282014-11-05 14:15:05 +00002344 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002345 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002346 case Primitive::kPrimBoolean:
2347 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002348 case Primitive::kPrimByte:
2349 case Primitive::kPrimShort:
2350 case Primitive::kPrimInt:
2351 case Primitive::kPrimChar:
2352 // Processing a Dex `int-to-double' instruction.
2353 locations->SetInAt(0, Location::RequiresRegister());
2354 locations->SetOut(Location::RequiresFpuRegister());
2355 break;
2356
2357 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002358 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002359 locations->SetInAt(0, Location::Any());
2360 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002361 break;
2362
Roland Levillaincff13742014-11-17 14:32:17 +00002363 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002364 // Processing a Dex `float-to-double' instruction.
2365 locations->SetInAt(0, Location::RequiresFpuRegister());
2366 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002367 break;
2368
2369 default:
2370 LOG(FATAL) << "Unexpected type conversion from " << input_type
2371 << " to " << result_type;
2372 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002373 break;
2374
2375 default:
2376 LOG(FATAL) << "Unexpected type conversion from " << input_type
2377 << " to " << result_type;
2378 }
2379}
2380
2381void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2382 LocationSummary* locations = conversion->GetLocations();
2383 Location out = locations->Out();
2384 Location in = locations->InAt(0);
2385 Primitive::Type result_type = conversion->GetResultType();
2386 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002387 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002388 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002389 case Primitive::kPrimByte:
2390 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002391 case Primitive::kPrimLong:
2392 // Type conversion from long to byte is a result of code transformations.
2393 if (in.IsRegisterPair()) {
2394 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2395 } else {
2396 DCHECK(in.GetConstant()->IsLongConstant());
2397 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2398 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2399 }
2400 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002401 case Primitive::kPrimBoolean:
2402 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002403 case Primitive::kPrimShort:
2404 case Primitive::kPrimInt:
2405 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002406 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002407 if (in.IsRegister()) {
2408 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002409 } else {
2410 DCHECK(in.GetConstant()->IsIntConstant());
2411 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2412 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2413 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002414 break;
2415
2416 default:
2417 LOG(FATAL) << "Unexpected type conversion from " << input_type
2418 << " to " << result_type;
2419 }
2420 break;
2421
Roland Levillain01a8d712014-11-14 16:27:39 +00002422 case Primitive::kPrimShort:
2423 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002424 case Primitive::kPrimLong:
2425 // Type conversion from long to short is a result of code transformations.
2426 if (in.IsRegisterPair()) {
2427 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2428 } else if (in.IsDoubleStackSlot()) {
2429 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2430 } else {
2431 DCHECK(in.GetConstant()->IsLongConstant());
2432 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2433 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2434 }
2435 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002436 case Primitive::kPrimBoolean:
2437 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002438 case Primitive::kPrimByte:
2439 case Primitive::kPrimInt:
2440 case Primitive::kPrimChar:
2441 // Processing a Dex `int-to-short' instruction.
2442 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002443 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002444 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002445 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002446 } else {
2447 DCHECK(in.GetConstant()->IsIntConstant());
2448 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002450 }
2451 break;
2452
2453 default:
2454 LOG(FATAL) << "Unexpected type conversion from " << input_type
2455 << " to " << result_type;
2456 }
2457 break;
2458
Roland Levillain946e1432014-11-11 17:35:19 +00002459 case Primitive::kPrimInt:
2460 switch (input_type) {
2461 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002462 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002463 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002464 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002465 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002466 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002467 } else {
2468 DCHECK(in.IsConstant());
2469 DCHECK(in.GetConstant()->IsLongConstant());
2470 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002471 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002472 }
2473 break;
2474
Roland Levillain3f8f9362014-12-02 17:45:01 +00002475 case Primitive::kPrimFloat: {
2476 // Processing a Dex `float-to-int' instruction.
2477 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2478 Register output = out.AsRegister<Register>();
2479 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002480 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002481
2482 __ movl(output, Immediate(kPrimIntMax));
2483 // temp = int-to-float(output)
2484 __ cvtsi2ss(temp, output);
2485 // if input >= temp goto done
2486 __ comiss(input, temp);
2487 __ j(kAboveEqual, &done);
2488 // if input == NaN goto nan
2489 __ j(kUnordered, &nan);
2490 // output = float-to-int-truncate(input)
2491 __ cvttss2si(output, input);
2492 __ jmp(&done);
2493 __ Bind(&nan);
2494 // output = 0
2495 __ xorl(output, output);
2496 __ Bind(&done);
2497 break;
2498 }
2499
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002500 case Primitive::kPrimDouble: {
2501 // Processing a Dex `double-to-int' instruction.
2502 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2503 Register output = out.AsRegister<Register>();
2504 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002505 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002506
2507 __ movl(output, Immediate(kPrimIntMax));
2508 // temp = int-to-double(output)
2509 __ cvtsi2sd(temp, output);
2510 // if input >= temp goto done
2511 __ comisd(input, temp);
2512 __ j(kAboveEqual, &done);
2513 // if input == NaN goto nan
2514 __ j(kUnordered, &nan);
2515 // output = double-to-int-truncate(input)
2516 __ cvttsd2si(output, input);
2517 __ jmp(&done);
2518 __ Bind(&nan);
2519 // output = 0
2520 __ xorl(output, output);
2521 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002522 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002523 }
Roland Levillain946e1432014-11-11 17:35:19 +00002524
2525 default:
2526 LOG(FATAL) << "Unexpected type conversion from " << input_type
2527 << " to " << result_type;
2528 }
2529 break;
2530
Roland Levillaindff1f282014-11-05 14:15:05 +00002531 case Primitive::kPrimLong:
2532 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002533 case Primitive::kPrimBoolean:
2534 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 case Primitive::kPrimByte:
2536 case Primitive::kPrimShort:
2537 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002538 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002539 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002540 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2541 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002542 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002543 __ cdq();
2544 break;
2545
2546 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002547 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002548 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2549 conversion,
2550 conversion->GetDexPc(),
2551 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002552 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002553 break;
2554
Roland Levillaindff1f282014-11-05 14:15:05 +00002555 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002556 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002557 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2558 conversion,
2559 conversion->GetDexPc(),
2560 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002561 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002562 break;
2563
2564 default:
2565 LOG(FATAL) << "Unexpected type conversion from " << input_type
2566 << " to " << result_type;
2567 }
2568 break;
2569
Roland Levillain981e4542014-11-14 11:47:14 +00002570 case Primitive::kPrimChar:
2571 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002572 case Primitive::kPrimLong:
2573 // Type conversion from long to short is a result of code transformations.
2574 if (in.IsRegisterPair()) {
2575 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2576 } else if (in.IsDoubleStackSlot()) {
2577 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2578 } else {
2579 DCHECK(in.GetConstant()->IsLongConstant());
2580 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2581 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2582 }
2583 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002584 case Primitive::kPrimBoolean:
2585 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002586 case Primitive::kPrimByte:
2587 case Primitive::kPrimShort:
2588 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002589 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2590 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002591 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002592 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002593 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002594 } else {
2595 DCHECK(in.GetConstant()->IsIntConstant());
2596 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002598 }
2599 break;
2600
2601 default:
2602 LOG(FATAL) << "Unexpected type conversion from " << input_type
2603 << " to " << result_type;
2604 }
2605 break;
2606
Roland Levillaindff1f282014-11-05 14:15:05 +00002607 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002608 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002609 case Primitive::kPrimBoolean:
2610 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002611 case Primitive::kPrimByte:
2612 case Primitive::kPrimShort:
2613 case Primitive::kPrimInt:
2614 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002615 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002616 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002617 break;
2618
Roland Levillain6d0e4832014-11-27 18:31:21 +00002619 case Primitive::kPrimLong: {
2620 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002621 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002622
Roland Levillain232ade02015-04-20 15:14:36 +01002623 // Create stack space for the call to
2624 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2625 // TODO: enhance register allocator to ask for stack temporaries.
2626 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2627 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2628 __ subl(ESP, Immediate(adjustment));
2629 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002630
Roland Levillain232ade02015-04-20 15:14:36 +01002631 // Load the value to the FP stack, using temporaries if needed.
2632 PushOntoFPStack(in, 0, adjustment, false, true);
2633
2634 if (out.IsStackSlot()) {
2635 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2636 } else {
2637 __ fstps(Address(ESP, 0));
2638 Location stack_temp = Location::StackSlot(0);
2639 codegen_->Move32(out, stack_temp);
2640 }
2641
2642 // Remove the temporary stack space we allocated.
2643 if (adjustment != 0) {
2644 __ addl(ESP, Immediate(adjustment));
2645 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002646 break;
2647 }
2648
Roland Levillaincff13742014-11-17 14:32:17 +00002649 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002650 // Processing a Dex `double-to-float' instruction.
2651 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002652 break;
2653
2654 default:
2655 LOG(FATAL) << "Unexpected type conversion from " << input_type
2656 << " to " << result_type;
2657 };
2658 break;
2659
Roland Levillaindff1f282014-11-05 14:15:05 +00002660 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002661 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002662 case Primitive::kPrimBoolean:
2663 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002664 case Primitive::kPrimByte:
2665 case Primitive::kPrimShort:
2666 case Primitive::kPrimInt:
2667 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002668 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002669 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002670 break;
2671
Roland Levillain647b9ed2014-11-27 12:06:00 +00002672 case Primitive::kPrimLong: {
2673 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002674 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002675
Roland Levillain232ade02015-04-20 15:14:36 +01002676 // Create stack space for the call to
2677 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2678 // TODO: enhance register allocator to ask for stack temporaries.
2679 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2680 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2681 __ subl(ESP, Immediate(adjustment));
2682 }
2683
2684 // Load the value to the FP stack, using temporaries if needed.
2685 PushOntoFPStack(in, 0, adjustment, false, true);
2686
2687 if (out.IsDoubleStackSlot()) {
2688 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2689 } else {
2690 __ fstpl(Address(ESP, 0));
2691 Location stack_temp = Location::DoubleStackSlot(0);
2692 codegen_->Move64(out, stack_temp);
2693 }
2694
2695 // Remove the temporary stack space we allocated.
2696 if (adjustment != 0) {
2697 __ addl(ESP, Immediate(adjustment));
2698 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002699 break;
2700 }
2701
Roland Levillaincff13742014-11-17 14:32:17 +00002702 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002703 // Processing a Dex `float-to-double' instruction.
2704 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002705 break;
2706
2707 default:
2708 LOG(FATAL) << "Unexpected type conversion from " << input_type
2709 << " to " << result_type;
2710 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002711 break;
2712
2713 default:
2714 LOG(FATAL) << "Unexpected type conversion from " << input_type
2715 << " to " << result_type;
2716 }
2717}
2718
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002719void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002720 LocationSummary* locations =
2721 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002722 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002723 case Primitive::kPrimInt: {
2724 locations->SetInAt(0, Location::RequiresRegister());
2725 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2726 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2727 break;
2728 }
2729
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002730 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002731 locations->SetInAt(0, Location::RequiresRegister());
2732 locations->SetInAt(1, Location::Any());
2733 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002734 break;
2735 }
2736
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002737 case Primitive::kPrimFloat:
2738 case Primitive::kPrimDouble: {
2739 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002740 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2741 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002742 } else if (add->InputAt(1)->IsConstant()) {
2743 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002744 } else {
2745 locations->SetInAt(1, Location::Any());
2746 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002747 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002748 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002749 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002750
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002751 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002752 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2753 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002754 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002755}
2756
2757void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2758 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002759 Location first = locations->InAt(0);
2760 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002761 Location out = locations->Out();
2762
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002763 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002764 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002765 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002766 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2767 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002768 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2769 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002770 } else {
2771 __ leal(out.AsRegister<Register>(), Address(
2772 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2773 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002774 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002775 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2776 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2777 __ addl(out.AsRegister<Register>(), Immediate(value));
2778 } else {
2779 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2780 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002781 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002782 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002784 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002785 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002786 }
2787
2788 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002789 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002790 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2791 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002792 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002793 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2794 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002795 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002796 } else {
2797 DCHECK(second.IsConstant()) << second;
2798 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2799 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2800 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002801 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002802 break;
2803 }
2804
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002805 case Primitive::kPrimFloat: {
2806 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002807 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002808 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2809 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002810 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002811 __ addss(first.AsFpuRegister<XmmRegister>(),
2812 codegen_->LiteralFloatAddress(
2813 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2814 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2815 } else {
2816 DCHECK(second.IsStackSlot());
2817 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002818 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002819 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002820 }
2821
2822 case Primitive::kPrimDouble: {
2823 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002824 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002825 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2826 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002827 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002828 __ addsd(first.AsFpuRegister<XmmRegister>(),
2829 codegen_->LiteralDoubleAddress(
2830 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2831 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2832 } else {
2833 DCHECK(second.IsDoubleStackSlot());
2834 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002835 }
2836 break;
2837 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002838
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002839 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002840 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002841 }
2842}
2843
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002844void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002845 LocationSummary* locations =
2846 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002847 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002848 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002849 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002850 locations->SetInAt(0, Location::RequiresRegister());
2851 locations->SetInAt(1, Location::Any());
2852 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002853 break;
2854 }
Calin Juravle11351682014-10-23 15:38:15 +01002855 case Primitive::kPrimFloat:
2856 case Primitive::kPrimDouble: {
2857 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002858 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2859 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002860 } else if (sub->InputAt(1)->IsConstant()) {
2861 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002862 } else {
2863 locations->SetInAt(1, Location::Any());
2864 }
Calin Juravle11351682014-10-23 15:38:15 +01002865 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002866 break;
Calin Juravle11351682014-10-23 15:38:15 +01002867 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002868
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002869 default:
Calin Juravle11351682014-10-23 15:38:15 +01002870 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002871 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002872}
2873
2874void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2875 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002876 Location first = locations->InAt(0);
2877 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002878 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002879 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002880 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002881 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002882 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002883 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002884 __ subl(first.AsRegister<Register>(),
2885 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002886 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002887 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002888 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002889 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002890 }
2891
2892 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002893 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002894 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2895 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002896 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002897 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002898 __ sbbl(first.AsRegisterPairHigh<Register>(),
2899 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002900 } else {
2901 DCHECK(second.IsConstant()) << second;
2902 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2903 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2904 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002905 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002906 break;
2907 }
2908
Calin Juravle11351682014-10-23 15:38:15 +01002909 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002910 if (second.IsFpuRegister()) {
2911 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2912 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2913 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002914 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002915 __ subss(first.AsFpuRegister<XmmRegister>(),
2916 codegen_->LiteralFloatAddress(
2917 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2918 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2919 } else {
2920 DCHECK(second.IsStackSlot());
2921 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2922 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002923 break;
Calin Juravle11351682014-10-23 15:38:15 +01002924 }
2925
2926 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002927 if (second.IsFpuRegister()) {
2928 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2929 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2930 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002931 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002932 __ subsd(first.AsFpuRegister<XmmRegister>(),
2933 codegen_->LiteralDoubleAddress(
2934 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2935 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2936 } else {
2937 DCHECK(second.IsDoubleStackSlot());
2938 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2939 }
Calin Juravle11351682014-10-23 15:38:15 +01002940 break;
2941 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002942
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002943 default:
Calin Juravle11351682014-10-23 15:38:15 +01002944 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002945 }
2946}
2947
Calin Juravle34bacdf2014-10-07 20:23:36 +01002948void LocationsBuilderX86::VisitMul(HMul* mul) {
2949 LocationSummary* locations =
2950 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2951 switch (mul->GetResultType()) {
2952 case Primitive::kPrimInt:
2953 locations->SetInAt(0, Location::RequiresRegister());
2954 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002955 if (mul->InputAt(1)->IsIntConstant()) {
2956 // Can use 3 operand multiply.
2957 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2958 } else {
2959 locations->SetOut(Location::SameAsFirstInput());
2960 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002961 break;
2962 case Primitive::kPrimLong: {
2963 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002964 locations->SetInAt(1, Location::Any());
2965 locations->SetOut(Location::SameAsFirstInput());
2966 // Needed for imul on 32bits with 64bits output.
2967 locations->AddTemp(Location::RegisterLocation(EAX));
2968 locations->AddTemp(Location::RegisterLocation(EDX));
2969 break;
2970 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002971 case Primitive::kPrimFloat:
2972 case Primitive::kPrimDouble: {
2973 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002974 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2975 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002976 } else if (mul->InputAt(1)->IsConstant()) {
2977 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002978 } else {
2979 locations->SetInAt(1, Location::Any());
2980 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002981 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002982 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002983 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002984
2985 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002986 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002987 }
2988}
2989
2990void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2991 LocationSummary* locations = mul->GetLocations();
2992 Location first = locations->InAt(0);
2993 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002994 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002995
2996 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002997 case Primitive::kPrimInt:
2998 // The constant may have ended up in a register, so test explicitly to avoid
2999 // problems where the output may not be the same as the first operand.
3000 if (mul->InputAt(1)->IsIntConstant()) {
3001 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3002 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3003 } else if (second.IsRegister()) {
3004 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003005 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003006 } else {
3007 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003008 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003009 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003010 }
3011 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003012
3013 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003014 Register in1_hi = first.AsRegisterPairHigh<Register>();
3015 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003016 Register eax = locations->GetTemp(0).AsRegister<Register>();
3017 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003018
3019 DCHECK_EQ(EAX, eax);
3020 DCHECK_EQ(EDX, edx);
3021
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003022 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003023 // output: in1
3024 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3025 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3026 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003027 if (second.IsConstant()) {
3028 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003029
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003030 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3031 int32_t low_value = Low32Bits(value);
3032 int32_t high_value = High32Bits(value);
3033 Immediate low(low_value);
3034 Immediate high(high_value);
3035
3036 __ movl(eax, high);
3037 // eax <- in1.lo * in2.hi
3038 __ imull(eax, in1_lo);
3039 // in1.hi <- in1.hi * in2.lo
3040 __ imull(in1_hi, low);
3041 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3042 __ addl(in1_hi, eax);
3043 // move in2_lo to eax to prepare for double precision
3044 __ movl(eax, low);
3045 // edx:eax <- in1.lo * in2.lo
3046 __ mull(in1_lo);
3047 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3048 __ addl(in1_hi, edx);
3049 // in1.lo <- (in1.lo * in2.lo)[31:0];
3050 __ movl(in1_lo, eax);
3051 } else if (second.IsRegisterPair()) {
3052 Register in2_hi = second.AsRegisterPairHigh<Register>();
3053 Register in2_lo = second.AsRegisterPairLow<Register>();
3054
3055 __ movl(eax, in2_hi);
3056 // eax <- in1.lo * in2.hi
3057 __ imull(eax, in1_lo);
3058 // in1.hi <- in1.hi * in2.lo
3059 __ imull(in1_hi, in2_lo);
3060 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3061 __ addl(in1_hi, eax);
3062 // move in1_lo to eax to prepare for double precision
3063 __ movl(eax, in1_lo);
3064 // edx:eax <- in1.lo * in2.lo
3065 __ mull(in2_lo);
3066 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3067 __ addl(in1_hi, edx);
3068 // in1.lo <- (in1.lo * in2.lo)[31:0];
3069 __ movl(in1_lo, eax);
3070 } else {
3071 DCHECK(second.IsDoubleStackSlot()) << second;
3072 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3073 Address in2_lo(ESP, second.GetStackIndex());
3074
3075 __ movl(eax, in2_hi);
3076 // eax <- in1.lo * in2.hi
3077 __ imull(eax, in1_lo);
3078 // in1.hi <- in1.hi * in2.lo
3079 __ imull(in1_hi, in2_lo);
3080 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3081 __ addl(in1_hi, eax);
3082 // move in1_lo to eax to prepare for double precision
3083 __ movl(eax, in1_lo);
3084 // edx:eax <- in1.lo * in2.lo
3085 __ mull(in2_lo);
3086 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3087 __ addl(in1_hi, edx);
3088 // in1.lo <- (in1.lo * in2.lo)[31:0];
3089 __ movl(in1_lo, eax);
3090 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003091
3092 break;
3093 }
3094
Calin Juravleb5bfa962014-10-21 18:02:24 +01003095 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003096 DCHECK(first.Equals(locations->Out()));
3097 if (second.IsFpuRegister()) {
3098 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3099 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3100 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003101 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003102 __ mulss(first.AsFpuRegister<XmmRegister>(),
3103 codegen_->LiteralFloatAddress(
3104 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3105 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3106 } else {
3107 DCHECK(second.IsStackSlot());
3108 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3109 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003110 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003111 }
3112
3113 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003114 DCHECK(first.Equals(locations->Out()));
3115 if (second.IsFpuRegister()) {
3116 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3117 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3118 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003119 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003120 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3121 codegen_->LiteralDoubleAddress(
3122 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3123 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3124 } else {
3125 DCHECK(second.IsDoubleStackSlot());
3126 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3127 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003128 break;
3129 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003130
3131 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003132 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003133 }
3134}
3135
Roland Levillain232ade02015-04-20 15:14:36 +01003136void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3137 uint32_t temp_offset,
3138 uint32_t stack_adjustment,
3139 bool is_fp,
3140 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003141 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003142 DCHECK(!is_wide);
3143 if (is_fp) {
3144 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3145 } else {
3146 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3147 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003148 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003149 DCHECK(is_wide);
3150 if (is_fp) {
3151 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3152 } else {
3153 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3154 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003155 } else {
3156 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003157 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003158 Location stack_temp = Location::StackSlot(temp_offset);
3159 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003160 if (is_fp) {
3161 __ flds(Address(ESP, temp_offset));
3162 } else {
3163 __ filds(Address(ESP, temp_offset));
3164 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003165 } else {
3166 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3167 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003168 if (is_fp) {
3169 __ fldl(Address(ESP, temp_offset));
3170 } else {
3171 __ fildl(Address(ESP, temp_offset));
3172 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003173 }
3174 }
3175}
3176
3177void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3178 Primitive::Type type = rem->GetResultType();
3179 bool is_float = type == Primitive::kPrimFloat;
3180 size_t elem_size = Primitive::ComponentSize(type);
3181 LocationSummary* locations = rem->GetLocations();
3182 Location first = locations->InAt(0);
3183 Location second = locations->InAt(1);
3184 Location out = locations->Out();
3185
3186 // Create stack space for 2 elements.
3187 // TODO: enhance register allocator to ask for stack temporaries.
3188 __ subl(ESP, Immediate(2 * elem_size));
3189
3190 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003191 const bool is_wide = !is_float;
3192 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3193 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003194
3195 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003196 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003197 __ Bind(&retry);
3198 __ fprem();
3199
3200 // Move FP status to AX.
3201 __ fstsw();
3202
3203 // And see if the argument reduction is complete. This is signaled by the
3204 // C2 FPU flag bit set to 0.
3205 __ andl(EAX, Immediate(kC2ConditionMask));
3206 __ j(kNotEqual, &retry);
3207
3208 // We have settled on the final value. Retrieve it into an XMM register.
3209 // Store FP top of stack to real stack.
3210 if (is_float) {
3211 __ fsts(Address(ESP, 0));
3212 } else {
3213 __ fstl(Address(ESP, 0));
3214 }
3215
3216 // Pop the 2 items from the FP stack.
3217 __ fucompp();
3218
3219 // Load the value from the stack into an XMM register.
3220 DCHECK(out.IsFpuRegister()) << out;
3221 if (is_float) {
3222 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3223 } else {
3224 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3225 }
3226
3227 // And remove the temporary stack space we allocated.
3228 __ addl(ESP, Immediate(2 * elem_size));
3229}
3230
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003231
3232void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3233 DCHECK(instruction->IsDiv() || instruction->IsRem());
3234
3235 LocationSummary* locations = instruction->GetLocations();
3236 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003237 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003238
3239 Register out_register = locations->Out().AsRegister<Register>();
3240 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003241 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003242
3243 DCHECK(imm == 1 || imm == -1);
3244
3245 if (instruction->IsRem()) {
3246 __ xorl(out_register, out_register);
3247 } else {
3248 __ movl(out_register, input_register);
3249 if (imm == -1) {
3250 __ negl(out_register);
3251 }
3252 }
3253}
3254
3255
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003256void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003257 LocationSummary* locations = instruction->GetLocations();
3258
3259 Register out_register = locations->Out().AsRegister<Register>();
3260 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003261 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003262 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3263 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003264
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003265 Register num = locations->GetTemp(0).AsRegister<Register>();
3266
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003267 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003268 __ testl(input_register, input_register);
3269 __ cmovl(kGreaterEqual, num, input_register);
3270 int shift = CTZ(imm);
3271 __ sarl(num, Immediate(shift));
3272
3273 if (imm < 0) {
3274 __ negl(num);
3275 }
3276
3277 __ movl(out_register, num);
3278}
3279
3280void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3281 DCHECK(instruction->IsDiv() || instruction->IsRem());
3282
3283 LocationSummary* locations = instruction->GetLocations();
3284 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3285
3286 Register eax = locations->InAt(0).AsRegister<Register>();
3287 Register out = locations->Out().AsRegister<Register>();
3288 Register num;
3289 Register edx;
3290
3291 if (instruction->IsDiv()) {
3292 edx = locations->GetTemp(0).AsRegister<Register>();
3293 num = locations->GetTemp(1).AsRegister<Register>();
3294 } else {
3295 edx = locations->Out().AsRegister<Register>();
3296 num = locations->GetTemp(0).AsRegister<Register>();
3297 }
3298
3299 DCHECK_EQ(EAX, eax);
3300 DCHECK_EQ(EDX, edx);
3301 if (instruction->IsDiv()) {
3302 DCHECK_EQ(EAX, out);
3303 } else {
3304 DCHECK_EQ(EDX, out);
3305 }
3306
3307 int64_t magic;
3308 int shift;
3309 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3310
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003311 // Save the numerator.
3312 __ movl(num, eax);
3313
3314 // EAX = magic
3315 __ movl(eax, Immediate(magic));
3316
3317 // EDX:EAX = magic * numerator
3318 __ imull(num);
3319
3320 if (imm > 0 && magic < 0) {
3321 // EDX += num
3322 __ addl(edx, num);
3323 } else if (imm < 0 && magic > 0) {
3324 __ subl(edx, num);
3325 }
3326
3327 // Shift if needed.
3328 if (shift != 0) {
3329 __ sarl(edx, Immediate(shift));
3330 }
3331
3332 // EDX += 1 if EDX < 0
3333 __ movl(eax, edx);
3334 __ shrl(edx, Immediate(31));
3335 __ addl(edx, eax);
3336
3337 if (instruction->IsRem()) {
3338 __ movl(eax, num);
3339 __ imull(edx, Immediate(imm));
3340 __ subl(eax, edx);
3341 __ movl(edx, eax);
3342 } else {
3343 __ movl(eax, edx);
3344 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003345}
3346
Calin Juravlebacfec32014-11-14 15:54:36 +00003347void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3348 DCHECK(instruction->IsDiv() || instruction->IsRem());
3349
3350 LocationSummary* locations = instruction->GetLocations();
3351 Location out = locations->Out();
3352 Location first = locations->InAt(0);
3353 Location second = locations->InAt(1);
3354 bool is_div = instruction->IsDiv();
3355
3356 switch (instruction->GetResultType()) {
3357 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003358 DCHECK_EQ(EAX, first.AsRegister<Register>());
3359 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003360
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003361 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003362 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003363
3364 if (imm == 0) {
3365 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3366 } else if (imm == 1 || imm == -1) {
3367 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003368 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003369 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003370 } else {
3371 DCHECK(imm <= -2 || imm >= 2);
3372 GenerateDivRemWithAnyConstant(instruction);
3373 }
3374 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003375 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3376 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003377 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003378
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003379 Register second_reg = second.AsRegister<Register>();
3380 // 0x80000000/-1 triggers an arithmetic exception!
3381 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3382 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003383
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003384 __ cmpl(second_reg, Immediate(-1));
3385 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003386
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003387 // edx:eax <- sign-extended of eax
3388 __ cdq();
3389 // eax = quotient, edx = remainder
3390 __ idivl(second_reg);
3391 __ Bind(slow_path->GetExitLabel());
3392 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003393 break;
3394 }
3395
3396 case Primitive::kPrimLong: {
3397 InvokeRuntimeCallingConvention calling_convention;
3398 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3399 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3400 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3401 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3402 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3403 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3404
3405 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003406 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3407 instruction,
3408 instruction->GetDexPc(),
3409 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003410 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003411 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003412 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3413 instruction,
3414 instruction->GetDexPc(),
3415 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003416 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003417 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003418 break;
3419 }
3420
3421 default:
3422 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3423 }
3424}
3425
Calin Juravle7c4954d2014-10-28 16:57:40 +00003426void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003427 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003428 ? LocationSummary::kCall
3429 : LocationSummary::kNoCall;
3430 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3431
Calin Juravle7c4954d2014-10-28 16:57:40 +00003432 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003433 case Primitive::kPrimInt: {
3434 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003435 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003436 locations->SetOut(Location::SameAsFirstInput());
3437 // Intel uses edx:eax as the dividend.
3438 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3440 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3441 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003442 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003443 locations->AddTemp(Location::RequiresRegister());
3444 }
Calin Juravled0d48522014-11-04 16:40:20 +00003445 break;
3446 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003447 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003448 InvokeRuntimeCallingConvention calling_convention;
3449 locations->SetInAt(0, Location::RegisterPairLocation(
3450 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3451 locations->SetInAt(1, Location::RegisterPairLocation(
3452 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3453 // Runtime helper puts the result in EAX, EDX.
3454 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003455 break;
3456 }
3457 case Primitive::kPrimFloat:
3458 case Primitive::kPrimDouble: {
3459 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003460 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3461 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003462 } else if (div->InputAt(1)->IsConstant()) {
3463 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003464 } else {
3465 locations->SetInAt(1, Location::Any());
3466 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003467 locations->SetOut(Location::SameAsFirstInput());
3468 break;
3469 }
3470
3471 default:
3472 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3473 }
3474}
3475
3476void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3477 LocationSummary* locations = div->GetLocations();
3478 Location first = locations->InAt(0);
3479 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003480
3481 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003482 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003483 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003484 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003485 break;
3486 }
3487
3488 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003489 if (second.IsFpuRegister()) {
3490 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3491 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3492 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003493 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003494 __ divss(first.AsFpuRegister<XmmRegister>(),
3495 codegen_->LiteralFloatAddress(
3496 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3497 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3498 } else {
3499 DCHECK(second.IsStackSlot());
3500 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3501 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003502 break;
3503 }
3504
3505 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003506 if (second.IsFpuRegister()) {
3507 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3508 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3509 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003510 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003511 __ divsd(first.AsFpuRegister<XmmRegister>(),
3512 codegen_->LiteralDoubleAddress(
3513 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3514 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3515 } else {
3516 DCHECK(second.IsDoubleStackSlot());
3517 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3518 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003519 break;
3520 }
3521
3522 default:
3523 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3524 }
3525}
3526
Calin Juravlebacfec32014-11-14 15:54:36 +00003527void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003528 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003529
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003530 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3531 ? LocationSummary::kCall
3532 : LocationSummary::kNoCall;
3533 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003534
Calin Juravled2ec87d2014-12-08 14:24:46 +00003535 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 case Primitive::kPrimInt: {
3537 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003538 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003539 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003540 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3541 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3542 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003543 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 locations->AddTemp(Location::RequiresRegister());
3545 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003546 break;
3547 }
3548 case Primitive::kPrimLong: {
3549 InvokeRuntimeCallingConvention calling_convention;
3550 locations->SetInAt(0, Location::RegisterPairLocation(
3551 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3552 locations->SetInAt(1, Location::RegisterPairLocation(
3553 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3554 // Runtime helper puts the result in EAX, EDX.
3555 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3556 break;
3557 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003558 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003559 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003560 locations->SetInAt(0, Location::Any());
3561 locations->SetInAt(1, Location::Any());
3562 locations->SetOut(Location::RequiresFpuRegister());
3563 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003564 break;
3565 }
3566
3567 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003568 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003569 }
3570}
3571
3572void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3573 Primitive::Type type = rem->GetResultType();
3574 switch (type) {
3575 case Primitive::kPrimInt:
3576 case Primitive::kPrimLong: {
3577 GenerateDivRemIntegral(rem);
3578 break;
3579 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003580 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003581 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003582 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003583 break;
3584 }
3585 default:
3586 LOG(FATAL) << "Unexpected rem type " << type;
3587 }
3588}
3589
Calin Juravled0d48522014-11-04 16:40:20 +00003590void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003591 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3592 ? LocationSummary::kCallOnSlowPath
3593 : LocationSummary::kNoCall;
3594 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003595 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003596 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003597 case Primitive::kPrimByte:
3598 case Primitive::kPrimChar:
3599 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003600 case Primitive::kPrimInt: {
3601 locations->SetInAt(0, Location::Any());
3602 break;
3603 }
3604 case Primitive::kPrimLong: {
3605 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3606 if (!instruction->IsConstant()) {
3607 locations->AddTemp(Location::RequiresRegister());
3608 }
3609 break;
3610 }
3611 default:
3612 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3613 }
Calin Juravled0d48522014-11-04 16:40:20 +00003614 if (instruction->HasUses()) {
3615 locations->SetOut(Location::SameAsFirstInput());
3616 }
3617}
3618
3619void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003620 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003621 codegen_->AddSlowPath(slow_path);
3622
3623 LocationSummary* locations = instruction->GetLocations();
3624 Location value = locations->InAt(0);
3625
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003626 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003627 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003628 case Primitive::kPrimByte:
3629 case Primitive::kPrimChar:
3630 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003631 case Primitive::kPrimInt: {
3632 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003633 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003634 __ j(kEqual, slow_path->GetEntryLabel());
3635 } else if (value.IsStackSlot()) {
3636 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3637 __ j(kEqual, slow_path->GetEntryLabel());
3638 } else {
3639 DCHECK(value.IsConstant()) << value;
3640 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3641 __ jmp(slow_path->GetEntryLabel());
3642 }
3643 }
3644 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003645 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 case Primitive::kPrimLong: {
3647 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003648 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003649 __ movl(temp, value.AsRegisterPairLow<Register>());
3650 __ orl(temp, value.AsRegisterPairHigh<Register>());
3651 __ j(kEqual, slow_path->GetEntryLabel());
3652 } else {
3653 DCHECK(value.IsConstant()) << value;
3654 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3655 __ jmp(slow_path->GetEntryLabel());
3656 }
3657 }
3658 break;
3659 }
3660 default:
3661 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003662 }
Calin Juravled0d48522014-11-04 16:40:20 +00003663}
3664
Calin Juravle9aec02f2014-11-18 23:06:35 +00003665void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3666 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3667
3668 LocationSummary* locations =
3669 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3670
3671 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003672 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003673 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003674 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003675 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003676 // The shift count needs to be in CL or a constant.
3677 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003678 locations->SetOut(Location::SameAsFirstInput());
3679 break;
3680 }
3681 default:
3682 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3683 }
3684}
3685
3686void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3687 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3688
3689 LocationSummary* locations = op->GetLocations();
3690 Location first = locations->InAt(0);
3691 Location second = locations->InAt(1);
3692 DCHECK(first.Equals(locations->Out()));
3693
3694 switch (op->GetResultType()) {
3695 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003696 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003697 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003698 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003699 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003700 DCHECK_EQ(ECX, second_reg);
3701 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003702 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003703 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003704 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003705 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003706 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003707 }
3708 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003709 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003710 if (shift == 0) {
3711 return;
3712 }
3713 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003714 if (op->IsShl()) {
3715 __ shll(first_reg, imm);
3716 } else if (op->IsShr()) {
3717 __ sarl(first_reg, imm);
3718 } else {
3719 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003720 }
3721 }
3722 break;
3723 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003724 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003725 if (second.IsRegister()) {
3726 Register second_reg = second.AsRegister<Register>();
3727 DCHECK_EQ(ECX, second_reg);
3728 if (op->IsShl()) {
3729 GenerateShlLong(first, second_reg);
3730 } else if (op->IsShr()) {
3731 GenerateShrLong(first, second_reg);
3732 } else {
3733 GenerateUShrLong(first, second_reg);
3734 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003735 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003736 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003737 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003738 // Nothing to do if the shift is 0, as the input is already the output.
3739 if (shift != 0) {
3740 if (op->IsShl()) {
3741 GenerateShlLong(first, shift);
3742 } else if (op->IsShr()) {
3743 GenerateShrLong(first, shift);
3744 } else {
3745 GenerateUShrLong(first, shift);
3746 }
3747 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003748 }
3749 break;
3750 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003751 default:
3752 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3753 }
3754}
3755
Mark P Mendell73945692015-04-29 14:56:17 +00003756void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3757 Register low = loc.AsRegisterPairLow<Register>();
3758 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003759 if (shift == 1) {
3760 // This is just an addition.
3761 __ addl(low, low);
3762 __ adcl(high, high);
3763 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003764 // Shift by 32 is easy. High gets low, and low gets 0.
3765 codegen_->EmitParallelMoves(
3766 loc.ToLow(),
3767 loc.ToHigh(),
3768 Primitive::kPrimInt,
3769 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3770 loc.ToLow(),
3771 Primitive::kPrimInt);
3772 } else if (shift > 32) {
3773 // Low part becomes 0. High part is low part << (shift-32).
3774 __ movl(high, low);
3775 __ shll(high, Immediate(shift - 32));
3776 __ xorl(low, low);
3777 } else {
3778 // Between 1 and 31.
3779 __ shld(high, low, Immediate(shift));
3780 __ shll(low, Immediate(shift));
3781 }
3782}
3783
Calin Juravle9aec02f2014-11-18 23:06:35 +00003784void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003785 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003786 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3787 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3788 __ testl(shifter, Immediate(32));
3789 __ j(kEqual, &done);
3790 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3791 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3792 __ Bind(&done);
3793}
3794
Mark P Mendell73945692015-04-29 14:56:17 +00003795void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3796 Register low = loc.AsRegisterPairLow<Register>();
3797 Register high = loc.AsRegisterPairHigh<Register>();
3798 if (shift == 32) {
3799 // Need to copy the sign.
3800 DCHECK_NE(low, high);
3801 __ movl(low, high);
3802 __ sarl(high, Immediate(31));
3803 } else if (shift > 32) {
3804 DCHECK_NE(low, high);
3805 // High part becomes sign. Low part is shifted by shift - 32.
3806 __ movl(low, high);
3807 __ sarl(high, Immediate(31));
3808 __ sarl(low, Immediate(shift - 32));
3809 } else {
3810 // Between 1 and 31.
3811 __ shrd(low, high, Immediate(shift));
3812 __ sarl(high, Immediate(shift));
3813 }
3814}
3815
Calin Juravle9aec02f2014-11-18 23:06:35 +00003816void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003817 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003818 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3819 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3820 __ testl(shifter, Immediate(32));
3821 __ j(kEqual, &done);
3822 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3823 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3824 __ Bind(&done);
3825}
3826
Mark P Mendell73945692015-04-29 14:56:17 +00003827void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3828 Register low = loc.AsRegisterPairLow<Register>();
3829 Register high = loc.AsRegisterPairHigh<Register>();
3830 if (shift == 32) {
3831 // Shift by 32 is easy. Low gets high, and high gets 0.
3832 codegen_->EmitParallelMoves(
3833 loc.ToHigh(),
3834 loc.ToLow(),
3835 Primitive::kPrimInt,
3836 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3837 loc.ToHigh(),
3838 Primitive::kPrimInt);
3839 } else if (shift > 32) {
3840 // Low part is high >> (shift - 32). High part becomes 0.
3841 __ movl(low, high);
3842 __ shrl(low, Immediate(shift - 32));
3843 __ xorl(high, high);
3844 } else {
3845 // Between 1 and 31.
3846 __ shrd(low, high, Immediate(shift));
3847 __ shrl(high, Immediate(shift));
3848 }
3849}
3850
Calin Juravle9aec02f2014-11-18 23:06:35 +00003851void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003852 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003853 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3854 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3855 __ testl(shifter, Immediate(32));
3856 __ j(kEqual, &done);
3857 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3858 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3859 __ Bind(&done);
3860}
3861
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003862void LocationsBuilderX86::VisitRor(HRor* ror) {
3863 LocationSummary* locations =
3864 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3865
3866 switch (ror->GetResultType()) {
3867 case Primitive::kPrimLong:
3868 // Add the temporary needed.
3869 locations->AddTemp(Location::RequiresRegister());
3870 FALLTHROUGH_INTENDED;
3871 case Primitive::kPrimInt:
3872 locations->SetInAt(0, Location::RequiresRegister());
3873 // The shift count needs to be in CL (unless it is a constant).
3874 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3875 locations->SetOut(Location::SameAsFirstInput());
3876 break;
3877 default:
3878 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3879 UNREACHABLE();
3880 }
3881}
3882
3883void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3884 LocationSummary* locations = ror->GetLocations();
3885 Location first = locations->InAt(0);
3886 Location second = locations->InAt(1);
3887
3888 if (ror->GetResultType() == Primitive::kPrimInt) {
3889 Register first_reg = first.AsRegister<Register>();
3890 if (second.IsRegister()) {
3891 Register second_reg = second.AsRegister<Register>();
3892 __ rorl(first_reg, second_reg);
3893 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003894 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003895 __ rorl(first_reg, imm);
3896 }
3897 return;
3898 }
3899
3900 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3901 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3902 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3903 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3904 if (second.IsRegister()) {
3905 Register second_reg = second.AsRegister<Register>();
3906 DCHECK_EQ(second_reg, ECX);
3907 __ movl(temp_reg, first_reg_hi);
3908 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3909 __ shrd(first_reg_lo, temp_reg, second_reg);
3910 __ movl(temp_reg, first_reg_hi);
3911 __ testl(second_reg, Immediate(32));
3912 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3913 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3914 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003915 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003916 if (shift_amt == 0) {
3917 // Already fine.
3918 return;
3919 }
3920 if (shift_amt == 32) {
3921 // Just swap.
3922 __ movl(temp_reg, first_reg_lo);
3923 __ movl(first_reg_lo, first_reg_hi);
3924 __ movl(first_reg_hi, temp_reg);
3925 return;
3926 }
3927
3928 Immediate imm(shift_amt);
3929 // Save the constents of the low value.
3930 __ movl(temp_reg, first_reg_lo);
3931
3932 // Shift right into low, feeding bits from high.
3933 __ shrd(first_reg_lo, first_reg_hi, imm);
3934
3935 // Shift right into high, feeding bits from the original low.
3936 __ shrd(first_reg_hi, temp_reg, imm);
3937
3938 // Swap if needed.
3939 if (shift_amt > 32) {
3940 __ movl(temp_reg, first_reg_lo);
3941 __ movl(first_reg_lo, first_reg_hi);
3942 __ movl(first_reg_hi, temp_reg);
3943 }
3944 }
3945}
3946
Calin Juravle9aec02f2014-11-18 23:06:35 +00003947void LocationsBuilderX86::VisitShl(HShl* shl) {
3948 HandleShift(shl);
3949}
3950
3951void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3952 HandleShift(shl);
3953}
3954
3955void LocationsBuilderX86::VisitShr(HShr* shr) {
3956 HandleShift(shr);
3957}
3958
3959void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3960 HandleShift(shr);
3961}
3962
3963void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3964 HandleShift(ushr);
3965}
3966
3967void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3968 HandleShift(ushr);
3969}
3970
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003971void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003972 LocationSummary* locations =
3973 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003974 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003975 if (instruction->IsStringAlloc()) {
3976 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3977 } else {
3978 InvokeRuntimeCallingConvention calling_convention;
3979 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3980 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3981 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003982}
3983
3984void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003985 // Note: if heap poisoning is enabled, the entry point takes cares
3986 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003987 if (instruction->IsStringAlloc()) {
3988 // String is allocated through StringFactory. Call NewEmptyString entry point.
3989 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3990 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3991 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3992 __ call(Address(temp, code_offset.Int32Value()));
3993 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3994 } else {
3995 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3996 instruction,
3997 instruction->GetDexPc(),
3998 nullptr);
3999 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4000 DCHECK(!codegen_->IsLeafMethod());
4001 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004002}
4003
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004004void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4005 LocationSummary* locations =
4006 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4007 locations->SetOut(Location::RegisterLocation(EAX));
4008 InvokeRuntimeCallingConvention calling_convention;
4009 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004010 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004011 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004012}
4013
4014void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4015 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004016 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004017 // Note: if heap poisoning is enabled, the entry point takes cares
4018 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004019 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4020 instruction,
4021 instruction->GetDexPc(),
4022 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004023 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004024 DCHECK(!codegen_->IsLeafMethod());
4025}
4026
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004027void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004028 LocationSummary* locations =
4029 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004030 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4031 if (location.IsStackSlot()) {
4032 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4033 } else if (location.IsDoubleStackSlot()) {
4034 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004035 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004036 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004037}
4038
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004039void InstructionCodeGeneratorX86::VisitParameterValue(
4040 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4041}
4042
4043void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4044 LocationSummary* locations =
4045 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4046 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4047}
4048
4049void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004050}
4051
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004052void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4053 LocationSummary* locations =
4054 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4055 locations->SetInAt(0, Location::RequiresRegister());
4056 locations->SetOut(Location::RequiresRegister());
4057}
4058
4059void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4060 LocationSummary* locations = instruction->GetLocations();
4061 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004062 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004063 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4064 instruction->GetIndex(), kX86PointerSize).SizeValue();
4065 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03004066 __ movl(locations->InAt(0).AsRegister<Register>(),
4067 Address(locations->InAt(0).AsRegister<Register>(),
4068 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4069 // temp = temp->GetImtEntryAt(method_offset);
4070 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
4071 instruction->GetIndex() % ImTable::kSize, kX86PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004072 }
4073 __ movl(locations->Out().AsRegister<Register>(),
4074 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4075}
4076
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004077void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004078 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004079 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004080 locations->SetInAt(0, Location::RequiresRegister());
4081 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004082}
4083
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004084void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4085 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004086 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004087 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004088 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004089 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004090 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004091 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004092 break;
4093
4094 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004095 __ notl(out.AsRegisterPairLow<Register>());
4096 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004097 break;
4098
4099 default:
4100 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4101 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004102}
4103
David Brazdil66d126e2015-04-03 16:02:44 +01004104void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4105 LocationSummary* locations =
4106 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4107 locations->SetInAt(0, Location::RequiresRegister());
4108 locations->SetOut(Location::SameAsFirstInput());
4109}
4110
4111void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004112 LocationSummary* locations = bool_not->GetLocations();
4113 Location in = locations->InAt(0);
4114 Location out = locations->Out();
4115 DCHECK(in.Equals(out));
4116 __ xorl(out.AsRegister<Register>(), Immediate(1));
4117}
4118
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004119void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004120 LocationSummary* locations =
4121 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004122 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004123 case Primitive::kPrimBoolean:
4124 case Primitive::kPrimByte:
4125 case Primitive::kPrimShort:
4126 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004127 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004128 case Primitive::kPrimLong: {
4129 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004130 locations->SetInAt(1, Location::Any());
4131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4132 break;
4133 }
4134 case Primitive::kPrimFloat:
4135 case Primitive::kPrimDouble: {
4136 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004137 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4138 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4139 } else if (compare->InputAt(1)->IsConstant()) {
4140 locations->SetInAt(1, Location::RequiresFpuRegister());
4141 } else {
4142 locations->SetInAt(1, Location::Any());
4143 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004144 locations->SetOut(Location::RequiresRegister());
4145 break;
4146 }
4147 default:
4148 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4149 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004150}
4151
4152void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004153 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004154 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004155 Location left = locations->InAt(0);
4156 Location right = locations->InAt(1);
4157
Mark Mendell0c9497d2015-08-21 09:30:05 -04004158 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004159 Condition less_cond = kLess;
4160
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004161 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004162 case Primitive::kPrimBoolean:
4163 case Primitive::kPrimByte:
4164 case Primitive::kPrimShort:
4165 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004166 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004167 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004168 break;
4169 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004170 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004171 Register left_low = left.AsRegisterPairLow<Register>();
4172 Register left_high = left.AsRegisterPairHigh<Register>();
4173 int32_t val_low = 0;
4174 int32_t val_high = 0;
4175 bool right_is_const = false;
4176
4177 if (right.IsConstant()) {
4178 DCHECK(right.GetConstant()->IsLongConstant());
4179 right_is_const = true;
4180 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4181 val_low = Low32Bits(val);
4182 val_high = High32Bits(val);
4183 }
4184
Calin Juravleddb7df22014-11-25 20:56:51 +00004185 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004186 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004187 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004188 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004189 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004190 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004191 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004192 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004193 __ j(kLess, &less); // Signed compare.
4194 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004195 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004196 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004197 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004198 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004199 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004200 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004201 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004202 }
Aart Bika19616e2016-02-01 18:57:58 -08004203 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004204 break;
4205 }
4206 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004207 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004208 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004209 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004210 break;
4211 }
4212 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004213 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004214 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004215 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004216 break;
4217 }
4218 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004219 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004220 }
Aart Bika19616e2016-02-01 18:57:58 -08004221
Calin Juravleddb7df22014-11-25 20:56:51 +00004222 __ movl(out, Immediate(0));
4223 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004224 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004225
4226 __ Bind(&greater);
4227 __ movl(out, Immediate(1));
4228 __ jmp(&done);
4229
4230 __ Bind(&less);
4231 __ movl(out, Immediate(-1));
4232
4233 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004234}
4235
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004236void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004237 LocationSummary* locations =
4238 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004239 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004240 locations->SetInAt(i, Location::Any());
4241 }
4242 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004243}
4244
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004245void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004246 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004247}
4248
Roland Levillain7c1559a2015-12-15 10:55:36 +00004249void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004250 /*
4251 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4252 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4253 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4254 */
4255 switch (kind) {
4256 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004257 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004258 break;
4259 }
4260 case MemBarrierKind::kAnyStore:
4261 case MemBarrierKind::kLoadAny:
4262 case MemBarrierKind::kStoreStore: {
4263 // nop
4264 break;
4265 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004266 case MemBarrierKind::kNTStoreStore:
4267 // Non-Temporal Store/Store needs an explicit fence.
4268 MemoryFence(/* non-temporal */ true);
4269 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004270 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004271}
4272
Vladimir Markodc151b22015-10-15 18:02:30 +01004273HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4274 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4275 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004276 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4277
4278 // We disable pc-relative load when there is an irreducible loop, as the optimization
4279 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004280 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4281 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004282 if (GetGraph()->HasIrreducibleLoops() &&
4283 (dispatch_info.method_load_kind ==
4284 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4285 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4286 }
4287 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004288 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4289 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4290 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4291 // (Though the direct CALL ptr16:32 is available for consideration).
4292 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004293 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004294 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004295 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004296 0u
4297 };
4298 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004299 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004300 }
4301}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004302
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004303Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4304 Register temp) {
4305 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004306 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004307 if (!invoke->GetLocations()->Intrinsified()) {
4308 return location.AsRegister<Register>();
4309 }
4310 // For intrinsics we allow any location, so it may be on the stack.
4311 if (!location.IsRegister()) {
4312 __ movl(temp, Address(ESP, location.GetStackIndex()));
4313 return temp;
4314 }
4315 // For register locations, check if the register was saved. If so, get it from the stack.
4316 // Note: There is a chance that the register was saved but not overwritten, so we could
4317 // save one load. However, since this is just an intrinsic slow path we prefer this
4318 // simple and more robust approach rather that trying to determine if that's the case.
4319 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004320 if (slow_path != nullptr) {
4321 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4322 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4323 __ movl(temp, Address(ESP, stack_offset));
4324 return temp;
4325 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004326 }
4327 return location.AsRegister<Register>();
4328}
4329
Serguei Katkov288c7a82016-05-16 11:53:15 +06004330Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4331 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004332 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4333 switch (invoke->GetMethodLoadKind()) {
4334 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4335 // temp = thread->string_init_entrypoint
4336 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4337 break;
4338 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004339 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004340 break;
4341 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4342 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4343 break;
4344 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004345 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004346 method_patches_.emplace_back(invoke->GetTargetMethod());
4347 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4348 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004349 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4350 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4351 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004352 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004353 // Bind a new fixup label at the end of the "movl" insn.
4354 uint32_t offset = invoke->GetDexCacheArrayOffset();
4355 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004356 break;
4357 }
Vladimir Marko58155012015-08-19 12:49:41 +00004358 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004359 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004360 Register method_reg;
4361 Register reg = temp.AsRegister<Register>();
4362 if (current_method.IsRegister()) {
4363 method_reg = current_method.AsRegister<Register>();
4364 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004365 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004366 DCHECK(!current_method.IsValid());
4367 method_reg = reg;
4368 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4369 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004370 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004371 __ movl(reg, Address(method_reg,
4372 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004373 // temp = temp[index_in_cache];
4374 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4375 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004376 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4377 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004378 }
Vladimir Marko58155012015-08-19 12:49:41 +00004379 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004380 return callee_method;
4381}
4382
4383void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4384 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004385
4386 switch (invoke->GetCodePtrLocation()) {
4387 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4388 __ call(GetFrameEntryLabel());
4389 break;
4390 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4391 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4392 Label* label = &relative_call_patches_.back().label;
4393 __ call(label); // Bind to the patch label, override at link time.
4394 __ Bind(label); // Bind the label at the end of the "call" insn.
4395 break;
4396 }
4397 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4398 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004399 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4400 LOG(FATAL) << "Unsupported";
4401 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004402 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4403 // (callee_method + offset_of_quick_compiled_code)()
4404 __ call(Address(callee_method.AsRegister<Register>(),
4405 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4406 kX86WordSize).Int32Value()));
4407 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004408 }
4409
4410 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004411}
4412
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004413void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4414 Register temp = temp_in.AsRegister<Register>();
4415 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4416 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004417
4418 // Use the calling convention instead of the location of the receiver, as
4419 // intrinsics may have put the receiver in a different register. In the intrinsics
4420 // slow path, the arguments have been moved to the right place, so here we are
4421 // guaranteed that the receiver is the first register of the calling convention.
4422 InvokeDexCallingConvention calling_convention;
4423 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004424 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004425 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004426 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004427 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004428 // Instead of simply (possibly) unpoisoning `temp` here, we should
4429 // emit a read barrier for the previous class reference load.
4430 // However this is not required in practice, as this is an
4431 // intermediate/temporary reference and because the current
4432 // concurrent copying collector keeps the from-space memory
4433 // intact/accessible until the end of the marking phase (the
4434 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004435 __ MaybeUnpoisonHeapReference(temp);
4436 // temp = temp->GetMethodAt(method_offset);
4437 __ movl(temp, Address(temp, method_offset));
4438 // call temp->GetEntryPoint();
4439 __ call(Address(
4440 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4441}
4442
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004443void CodeGeneratorX86::RecordSimplePatch() {
4444 if (GetCompilerOptions().GetIncludePatchInformation()) {
4445 simple_patches_.emplace_back();
4446 __ Bind(&simple_patches_.back());
4447 }
4448}
4449
4450void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4451 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4452 __ Bind(&string_patches_.back().label);
4453}
4454
4455Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4456 uint32_t element_offset) {
4457 // Add the patch entry and bind its label at the end of the instruction.
4458 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4459 return &pc_relative_dex_cache_patches_.back().label;
4460}
4461
Vladimir Marko58155012015-08-19 12:49:41 +00004462void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4463 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004464 size_t size =
4465 method_patches_.size() +
4466 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004467 pc_relative_dex_cache_patches_.size() +
4468 simple_patches_.size() +
4469 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004470 linker_patches->reserve(size);
4471 // The label points to the end of the "movl" insn but the literal offset for method
4472 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4473 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004474 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004475 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004476 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4477 info.target_method.dex_file,
4478 info.target_method.dex_method_index));
4479 }
4480 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004481 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004482 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4483 info.target_method.dex_file,
4484 info.target_method.dex_method_index));
4485 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004486 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4487 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4488 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4489 &info.target_dex_file,
4490 GetMethodAddressOffset(),
4491 info.element_offset));
4492 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004493 for (const Label& label : simple_patches_) {
4494 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4495 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4496 }
4497 if (GetCompilerOptions().GetCompilePic()) {
4498 for (const StringPatchInfo<Label>& info : string_patches_) {
4499 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4500 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4501 &info.dex_file,
4502 GetMethodAddressOffset(),
4503 info.string_index));
4504 }
4505 } else {
4506 for (const StringPatchInfo<Label>& info : string_patches_) {
4507 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4508 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4509 &info.dex_file,
4510 info.string_index));
4511 }
4512 }
Vladimir Marko58155012015-08-19 12:49:41 +00004513}
4514
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004515void CodeGeneratorX86::MarkGCCard(Register temp,
4516 Register card,
4517 Register object,
4518 Register value,
4519 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004520 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004521 if (value_can_be_null) {
4522 __ testl(value, value);
4523 __ j(kEqual, &is_null);
4524 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004525 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4526 __ movl(temp, object);
4527 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004528 __ movb(Address(temp, card, TIMES_1, 0),
4529 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004530 if (value_can_be_null) {
4531 __ Bind(&is_null);
4532 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004533}
4534
Calin Juravle52c48962014-12-16 17:02:57 +00004535void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4536 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004537
4538 bool object_field_get_with_read_barrier =
4539 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004540 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004541 new (GetGraph()->GetArena()) LocationSummary(instruction,
4542 kEmitCompilerReadBarrier ?
4543 LocationSummary::kCallOnSlowPath :
4544 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004545 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004546
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004547 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4548 locations->SetOut(Location::RequiresFpuRegister());
4549 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004550 // The output overlaps in case of long: we don't want the low move
4551 // to overwrite the object's location. Likewise, in the case of
4552 // an object field get with read barriers enabled, we do not want
4553 // the move to overwrite the object's location, as we need it to emit
4554 // the read barrier.
4555 locations->SetOut(
4556 Location::RequiresRegister(),
4557 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4558 Location::kOutputOverlap :
4559 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004560 }
Calin Juravle52c48962014-12-16 17:02:57 +00004561
4562 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4563 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004564 // So we use an XMM register as a temp to achieve atomicity (first
4565 // load the temp into the XMM and then copy the XMM into the
4566 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004567 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004568 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4569 // We need a temporary register for the read barrier marking slow
4570 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4571 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004572 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004573}
4574
Calin Juravle52c48962014-12-16 17:02:57 +00004575void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4576 const FieldInfo& field_info) {
4577 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004578
Calin Juravle52c48962014-12-16 17:02:57 +00004579 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004580 Location base_loc = locations->InAt(0);
4581 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004582 Location out = locations->Out();
4583 bool is_volatile = field_info.IsVolatile();
4584 Primitive::Type field_type = field_info.GetFieldType();
4585 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4586
4587 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004588 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004589 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004590 break;
4591 }
4592
4593 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004594 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004595 break;
4596 }
4597
4598 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004599 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 break;
4601 }
4602
4603 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004604 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605 break;
4606 }
4607
4608 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004609 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004610 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004611
4612 case Primitive::kPrimNot: {
4613 // /* HeapReference<Object> */ out = *(base + offset)
4614 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4615 Location temp_loc = locations->GetTemp(0);
4616 // Note that a potential implicit null check is handled in this
4617 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4618 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4619 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4620 if (is_volatile) {
4621 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4622 }
4623 } else {
4624 __ movl(out.AsRegister<Register>(), Address(base, offset));
4625 codegen_->MaybeRecordImplicitNullCheck(instruction);
4626 if (is_volatile) {
4627 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4628 }
4629 // If read barriers are enabled, emit read barriers other than
4630 // Baker's using a slow path (and also unpoison the loaded
4631 // reference, if heap poisoning is enabled).
4632 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4633 }
4634 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004635 }
4636
4637 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004638 if (is_volatile) {
4639 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4640 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004641 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004642 __ movd(out.AsRegisterPairLow<Register>(), temp);
4643 __ psrlq(temp, Immediate(32));
4644 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4645 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004646 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004647 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004648 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004649 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4650 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004651 break;
4652 }
4653
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004654 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004655 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004656 break;
4657 }
4658
4659 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004660 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004661 break;
4662 }
4663
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004664 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004665 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004666 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004667 }
Calin Juravle52c48962014-12-16 17:02:57 +00004668
Roland Levillain7c1559a2015-12-15 10:55:36 +00004669 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4670 // Potential implicit null checks, in the case of reference or
4671 // long fields, are handled in the previous switch statement.
4672 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004673 codegen_->MaybeRecordImplicitNullCheck(instruction);
4674 }
4675
Calin Juravle52c48962014-12-16 17:02:57 +00004676 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004677 if (field_type == Primitive::kPrimNot) {
4678 // Memory barriers, in the case of references, are also handled
4679 // in the previous switch statement.
4680 } else {
4681 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4682 }
Roland Levillain4d027112015-07-01 15:41:14 +01004683 }
Calin Juravle52c48962014-12-16 17:02:57 +00004684}
4685
4686void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4687 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4688
4689 LocationSummary* locations =
4690 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4691 locations->SetInAt(0, Location::RequiresRegister());
4692 bool is_volatile = field_info.IsVolatile();
4693 Primitive::Type field_type = field_info.GetFieldType();
4694 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4695 || (field_type == Primitive::kPrimByte);
4696
4697 // The register allocator does not support multiple
4698 // inputs that die at entry with one in a specific register.
4699 if (is_byte_type) {
4700 // Ensure the value is in a byte register.
4701 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004702 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004703 if (is_volatile && field_type == Primitive::kPrimDouble) {
4704 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4705 locations->SetInAt(1, Location::RequiresFpuRegister());
4706 } else {
4707 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4708 }
4709 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4710 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004711 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004712
Calin Juravle52c48962014-12-16 17:02:57 +00004713 // 64bits value can be atomically written to an address with movsd and an XMM register.
4714 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4715 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4716 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4717 // isolated cases when we need this it isn't worth adding the extra complexity.
4718 locations->AddTemp(Location::RequiresFpuRegister());
4719 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004720 } else {
4721 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4722
4723 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4724 // Temporary registers for the write barrier.
4725 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4726 // Ensure the card is in a byte register.
4727 locations->AddTemp(Location::RegisterLocation(ECX));
4728 }
Calin Juravle52c48962014-12-16 17:02:57 +00004729 }
4730}
4731
4732void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004733 const FieldInfo& field_info,
4734 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004735 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4736
4737 LocationSummary* locations = instruction->GetLocations();
4738 Register base = locations->InAt(0).AsRegister<Register>();
4739 Location value = locations->InAt(1);
4740 bool is_volatile = field_info.IsVolatile();
4741 Primitive::Type field_type = field_info.GetFieldType();
4742 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004743 bool needs_write_barrier =
4744 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004745
4746 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004747 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004748 }
4749
Mark Mendell81489372015-11-04 11:30:41 -05004750 bool maybe_record_implicit_null_check_done = false;
4751
Calin Juravle52c48962014-12-16 17:02:57 +00004752 switch (field_type) {
4753 case Primitive::kPrimBoolean:
4754 case Primitive::kPrimByte: {
4755 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4756 break;
4757 }
4758
4759 case Primitive::kPrimShort:
4760 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004761 if (value.IsConstant()) {
4762 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4763 __ movw(Address(base, offset), Immediate(v));
4764 } else {
4765 __ movw(Address(base, offset), value.AsRegister<Register>());
4766 }
Calin Juravle52c48962014-12-16 17:02:57 +00004767 break;
4768 }
4769
4770 case Primitive::kPrimInt:
4771 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004772 if (kPoisonHeapReferences && needs_write_barrier) {
4773 // Note that in the case where `value` is a null reference,
4774 // we do not enter this block, as the reference does not
4775 // need poisoning.
4776 DCHECK_EQ(field_type, Primitive::kPrimNot);
4777 Register temp = locations->GetTemp(0).AsRegister<Register>();
4778 __ movl(temp, value.AsRegister<Register>());
4779 __ PoisonHeapReference(temp);
4780 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004781 } else if (value.IsConstant()) {
4782 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4783 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004784 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004785 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004786 __ movl(Address(base, offset), value.AsRegister<Register>());
4787 }
Calin Juravle52c48962014-12-16 17:02:57 +00004788 break;
4789 }
4790
4791 case Primitive::kPrimLong: {
4792 if (is_volatile) {
4793 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4794 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4795 __ movd(temp1, value.AsRegisterPairLow<Register>());
4796 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4797 __ punpckldq(temp1, temp2);
4798 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004799 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004800 } else if (value.IsConstant()) {
4801 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4802 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4803 codegen_->MaybeRecordImplicitNullCheck(instruction);
4804 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004805 } else {
4806 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004807 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004808 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4809 }
Mark Mendell81489372015-11-04 11:30:41 -05004810 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004811 break;
4812 }
4813
4814 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004815 if (value.IsConstant()) {
4816 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4817 __ movl(Address(base, offset), Immediate(v));
4818 } else {
4819 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4820 }
Calin Juravle52c48962014-12-16 17:02:57 +00004821 break;
4822 }
4823
4824 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004825 if (value.IsConstant()) {
4826 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4827 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4828 codegen_->MaybeRecordImplicitNullCheck(instruction);
4829 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4830 maybe_record_implicit_null_check_done = true;
4831 } else {
4832 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4833 }
Calin Juravle52c48962014-12-16 17:02:57 +00004834 break;
4835 }
4836
4837 case Primitive::kPrimVoid:
4838 LOG(FATAL) << "Unreachable type " << field_type;
4839 UNREACHABLE();
4840 }
4841
Mark Mendell81489372015-11-04 11:30:41 -05004842 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004843 codegen_->MaybeRecordImplicitNullCheck(instruction);
4844 }
4845
Roland Levillain4d027112015-07-01 15:41:14 +01004846 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004847 Register temp = locations->GetTemp(0).AsRegister<Register>();
4848 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004849 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004850 }
4851
Calin Juravle52c48962014-12-16 17:02:57 +00004852 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004853 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004854 }
4855}
4856
4857void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4858 HandleFieldGet(instruction, instruction->GetFieldInfo());
4859}
4860
4861void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4862 HandleFieldGet(instruction, instruction->GetFieldInfo());
4863}
4864
4865void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4866 HandleFieldSet(instruction, instruction->GetFieldInfo());
4867}
4868
4869void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004870 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004871}
4872
4873void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4874 HandleFieldSet(instruction, instruction->GetFieldInfo());
4875}
4876
4877void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004878 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004879}
4880
4881void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4882 HandleFieldGet(instruction, instruction->GetFieldInfo());
4883}
4884
4885void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4886 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004887}
4888
Calin Juravlee460d1d2015-09-29 04:52:17 +01004889void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4890 HUnresolvedInstanceFieldGet* instruction) {
4891 FieldAccessCallingConventionX86 calling_convention;
4892 codegen_->CreateUnresolvedFieldLocationSummary(
4893 instruction, instruction->GetFieldType(), calling_convention);
4894}
4895
4896void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4897 HUnresolvedInstanceFieldGet* instruction) {
4898 FieldAccessCallingConventionX86 calling_convention;
4899 codegen_->GenerateUnresolvedFieldAccess(instruction,
4900 instruction->GetFieldType(),
4901 instruction->GetFieldIndex(),
4902 instruction->GetDexPc(),
4903 calling_convention);
4904}
4905
4906void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4907 HUnresolvedInstanceFieldSet* instruction) {
4908 FieldAccessCallingConventionX86 calling_convention;
4909 codegen_->CreateUnresolvedFieldLocationSummary(
4910 instruction, instruction->GetFieldType(), calling_convention);
4911}
4912
4913void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4914 HUnresolvedInstanceFieldSet* instruction) {
4915 FieldAccessCallingConventionX86 calling_convention;
4916 codegen_->GenerateUnresolvedFieldAccess(instruction,
4917 instruction->GetFieldType(),
4918 instruction->GetFieldIndex(),
4919 instruction->GetDexPc(),
4920 calling_convention);
4921}
4922
4923void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4924 HUnresolvedStaticFieldGet* instruction) {
4925 FieldAccessCallingConventionX86 calling_convention;
4926 codegen_->CreateUnresolvedFieldLocationSummary(
4927 instruction, instruction->GetFieldType(), calling_convention);
4928}
4929
4930void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4931 HUnresolvedStaticFieldGet* instruction) {
4932 FieldAccessCallingConventionX86 calling_convention;
4933 codegen_->GenerateUnresolvedFieldAccess(instruction,
4934 instruction->GetFieldType(),
4935 instruction->GetFieldIndex(),
4936 instruction->GetDexPc(),
4937 calling_convention);
4938}
4939
4940void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4941 HUnresolvedStaticFieldSet* instruction) {
4942 FieldAccessCallingConventionX86 calling_convention;
4943 codegen_->CreateUnresolvedFieldLocationSummary(
4944 instruction, instruction->GetFieldType(), calling_convention);
4945}
4946
4947void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4948 HUnresolvedStaticFieldSet* instruction) {
4949 FieldAccessCallingConventionX86 calling_convention;
4950 codegen_->GenerateUnresolvedFieldAccess(instruction,
4951 instruction->GetFieldType(),
4952 instruction->GetFieldIndex(),
4953 instruction->GetDexPc(),
4954 calling_convention);
4955}
4956
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004957void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004958 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4959 ? LocationSummary::kCallOnSlowPath
4960 : LocationSummary::kNoCall;
4961 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4962 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004963 ? Location::RequiresRegister()
4964 : Location::Any();
4965 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004966 if (instruction->HasUses()) {
4967 locations->SetOut(Location::SameAsFirstInput());
4968 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004969}
4970
Calin Juravle2ae48182016-03-16 14:05:09 +00004971void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4972 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004973 return;
4974 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004975 LocationSummary* locations = instruction->GetLocations();
4976 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004977
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004978 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004979 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004980}
4981
Calin Juravle2ae48182016-03-16 14:05:09 +00004982void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004983 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004984 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985
4986 LocationSummary* locations = instruction->GetLocations();
4987 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004988
4989 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004990 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004991 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004992 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004993 } else {
4994 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004995 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004996 __ jmp(slow_path->GetEntryLabel());
4997 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998 }
4999 __ j(kEqual, slow_path->GetEntryLabel());
5000}
5001
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005002void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005003 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005004}
5005
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005006void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005007 bool object_array_get_with_read_barrier =
5008 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005009 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005010 new (GetGraph()->GetArena()) LocationSummary(instruction,
5011 object_array_get_with_read_barrier ?
5012 LocationSummary::kCallOnSlowPath :
5013 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005014 locations->SetInAt(0, Location::RequiresRegister());
5015 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005016 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5017 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5018 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005019 // The output overlaps in case of long: we don't want the low move
5020 // to overwrite the array's location. Likewise, in the case of an
5021 // object array get with read barriers enabled, we do not want the
5022 // move to overwrite the array's location, as we need it to emit
5023 // the read barrier.
5024 locations->SetOut(
5025 Location::RequiresRegister(),
5026 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5027 Location::kOutputOverlap :
5028 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005029 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005030 // We need a temporary register for the read barrier marking slow
5031 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5032 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5033 locations->AddTemp(Location::RequiresRegister());
5034 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005035}
5036
5037void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5038 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005039 Location obj_loc = locations->InAt(0);
5040 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005041 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005042 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005043
Calin Juravle77520bc2015-01-12 18:45:46 +00005044 Primitive::Type type = instruction->GetType();
5045 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005046 case Primitive::kPrimBoolean: {
5047 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005048 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005049 if (index.IsConstant()) {
5050 __ movzxb(out, Address(obj,
5051 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5052 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005053 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054 }
5055 break;
5056 }
5057
5058 case Primitive::kPrimByte: {
5059 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005060 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 if (index.IsConstant()) {
5062 __ movsxb(out, Address(obj,
5063 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5064 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005065 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 }
5067 break;
5068 }
5069
5070 case Primitive::kPrimShort: {
5071 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005072 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005073 if (index.IsConstant()) {
5074 __ movsxw(out, Address(obj,
5075 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5076 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005077 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 }
5079 break;
5080 }
5081
5082 case Primitive::kPrimChar: {
5083 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005084 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005085 if (index.IsConstant()) {
5086 __ movzxw(out, Address(obj,
5087 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5088 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005089 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 }
5091 break;
5092 }
5093
Roland Levillain7c1559a2015-12-15 10:55:36 +00005094 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005095 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005096 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097 if (index.IsConstant()) {
5098 __ movl(out, Address(obj,
5099 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5100 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005101 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005102 }
5103 break;
5104 }
5105
Roland Levillain7c1559a2015-12-15 10:55:36 +00005106 case Primitive::kPrimNot: {
5107 static_assert(
5108 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5109 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5110 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5111 // /* HeapReference<Object> */ out =
5112 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5113 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5114 Location temp = locations->GetTemp(0);
5115 // Note that a potential implicit null check is handled in this
5116 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5117 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5118 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5119 } else {
5120 Register out = out_loc.AsRegister<Register>();
5121 if (index.IsConstant()) {
5122 uint32_t offset =
5123 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5124 __ movl(out, Address(obj, offset));
5125 codegen_->MaybeRecordImplicitNullCheck(instruction);
5126 // If read barriers are enabled, emit read barriers other than
5127 // Baker's using a slow path (and also unpoison the loaded
5128 // reference, if heap poisoning is enabled).
5129 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5130 } else {
5131 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5132 codegen_->MaybeRecordImplicitNullCheck(instruction);
5133 // If read barriers are enabled, emit read barriers other than
5134 // Baker's using a slow path (and also unpoison the loaded
5135 // reference, if heap poisoning is enabled).
5136 codegen_->MaybeGenerateReadBarrierSlow(
5137 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5138 }
5139 }
5140 break;
5141 }
5142
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005143 case Primitive::kPrimLong: {
5144 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005145 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005146 if (index.IsConstant()) {
5147 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005149 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005150 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005151 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005152 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005153 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005154 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005155 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005156 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005157 }
5158 break;
5159 }
5160
Mark Mendell7c8d0092015-01-26 11:21:33 -05005161 case Primitive::kPrimFloat: {
5162 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005163 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005164 if (index.IsConstant()) {
5165 __ movss(out, Address(obj,
5166 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5167 } else {
5168 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5169 }
5170 break;
5171 }
5172
5173 case Primitive::kPrimDouble: {
5174 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005175 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005176 if (index.IsConstant()) {
5177 __ movsd(out, Address(obj,
5178 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5179 } else {
5180 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5181 }
5182 break;
5183 }
5184
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005185 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005186 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005187 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005188 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005189
Roland Levillain7c1559a2015-12-15 10:55:36 +00005190 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5191 // Potential implicit null checks, in the case of reference or
5192 // long arrays, are handled in the previous switch statement.
5193 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005194 codegen_->MaybeRecordImplicitNullCheck(instruction);
5195 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005196}
5197
5198void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005199 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005200
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005201 bool needs_write_barrier =
5202 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005203 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5204 bool object_array_set_with_read_barrier =
5205 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005206
Nicolas Geoffray39468442014-09-02 15:17:15 +01005207 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5208 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005209 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5210 LocationSummary::kCallOnSlowPath :
5211 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005212
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005213 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5214 || (value_type == Primitive::kPrimByte);
5215 // We need the inputs to be different than the output in case of long operation.
5216 // In case of a byte operation, the register allocator does not support multiple
5217 // inputs that die at entry with one in a specific register.
5218 locations->SetInAt(0, Location::RequiresRegister());
5219 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5220 if (is_byte_type) {
5221 // Ensure the value is in a byte register.
5222 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5223 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005224 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005226 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5227 }
5228 if (needs_write_barrier) {
5229 // Temporary registers for the write barrier.
5230 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5231 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005232 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005233 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005234}
5235
5236void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5237 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005238 Location array_loc = locations->InAt(0);
5239 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005241 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005242 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005243 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5244 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5245 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005246 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005247 bool needs_write_barrier =
5248 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005249
5250 switch (value_type) {
5251 case Primitive::kPrimBoolean:
5252 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5254 Address address = index.IsConstant()
5255 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5256 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5257 if (value.IsRegister()) {
5258 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005260 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005261 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005262 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005263 break;
5264 }
5265
5266 case Primitive::kPrimShort:
5267 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005268 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5269 Address address = index.IsConstant()
5270 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5271 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5272 if (value.IsRegister()) {
5273 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005277 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005278 break;
5279 }
5280
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005281 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005282 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5283 Address address = index.IsConstant()
5284 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5285 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005286
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005287 if (!value.IsRegister()) {
5288 // Just setting null.
5289 DCHECK(instruction->InputAt(2)->IsNullConstant());
5290 DCHECK(value.IsConstant()) << value;
5291 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005292 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005294 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297
5298 DCHECK(needs_write_barrier);
5299 Register register_value = value.AsRegister<Register>();
5300 NearLabel done, not_null, do_put;
5301 SlowPathCode* slow_path = nullptr;
5302 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005303 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005304 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5305 codegen_->AddSlowPath(slow_path);
5306 if (instruction->GetValueCanBeNull()) {
5307 __ testl(register_value, register_value);
5308 __ j(kNotEqual, &not_null);
5309 __ movl(address, Immediate(0));
5310 codegen_->MaybeRecordImplicitNullCheck(instruction);
5311 __ jmp(&done);
5312 __ Bind(&not_null);
5313 }
5314
Roland Levillain0d5a2812015-11-13 10:07:31 +00005315 if (kEmitCompilerReadBarrier) {
5316 // When read barriers are enabled, the type checking
5317 // instrumentation requires two read barriers:
5318 //
5319 // __ movl(temp2, temp);
5320 // // /* HeapReference<Class> */ temp = temp->component_type_
5321 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005322 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005323 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5324 //
5325 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5326 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005327 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005328 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5329 //
5330 // __ cmpl(temp, temp2);
5331 //
5332 // However, the second read barrier may trash `temp`, as it
5333 // is a temporary register, and as such would not be saved
5334 // along with live registers before calling the runtime (nor
5335 // restored afterwards). So in this case, we bail out and
5336 // delegate the work to the array set slow path.
5337 //
5338 // TODO: Extend the register allocator to support a new
5339 // "(locally) live temp" location so as to avoid always
5340 // going into the slow path when read barriers are enabled.
5341 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005342 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343 // /* HeapReference<Class> */ temp = array->klass_
5344 __ movl(temp, Address(array, class_offset));
5345 codegen_->MaybeRecordImplicitNullCheck(instruction);
5346 __ MaybeUnpoisonHeapReference(temp);
5347
5348 // /* HeapReference<Class> */ temp = temp->component_type_
5349 __ movl(temp, Address(temp, component_offset));
5350 // If heap poisoning is enabled, no need to unpoison `temp`
5351 // nor the object reference in `register_value->klass`, as
5352 // we are comparing two poisoned references.
5353 __ cmpl(temp, Address(register_value, class_offset));
5354
5355 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5356 __ j(kEqual, &do_put);
5357 // If heap poisoning is enabled, the `temp` reference has
5358 // not been unpoisoned yet; unpoison it now.
5359 __ MaybeUnpoisonHeapReference(temp);
5360
5361 // /* HeapReference<Class> */ temp = temp->super_class_
5362 __ movl(temp, Address(temp, super_offset));
5363 // If heap poisoning is enabled, no need to unpoison
5364 // `temp`, as we are comparing against null below.
5365 __ testl(temp, temp);
5366 __ j(kNotEqual, slow_path->GetEntryLabel());
5367 __ Bind(&do_put);
5368 } else {
5369 __ j(kNotEqual, slow_path->GetEntryLabel());
5370 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005371 }
5372 }
5373
5374 if (kPoisonHeapReferences) {
5375 __ movl(temp, register_value);
5376 __ PoisonHeapReference(temp);
5377 __ movl(address, temp);
5378 } else {
5379 __ movl(address, register_value);
5380 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005381 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005382 codegen_->MaybeRecordImplicitNullCheck(instruction);
5383 }
5384
5385 Register card = locations->GetTemp(1).AsRegister<Register>();
5386 codegen_->MarkGCCard(
5387 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5388 __ Bind(&done);
5389
5390 if (slow_path != nullptr) {
5391 __ Bind(slow_path->GetExitLabel());
5392 }
5393
5394 break;
5395 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005396
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 case Primitive::kPrimInt: {
5398 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5399 Address address = index.IsConstant()
5400 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5401 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5402 if (value.IsRegister()) {
5403 __ movl(address, value.AsRegister<Register>());
5404 } else {
5405 DCHECK(value.IsConstant()) << value;
5406 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5407 __ movl(address, Immediate(v));
5408 }
5409 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005410 break;
5411 }
5412
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005413 case Primitive::kPrimLong: {
5414 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 if (index.IsConstant()) {
5416 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005417 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005418 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005419 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005421 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005422 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005423 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005424 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005425 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005427 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005428 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005429 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005431 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005432 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005433 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005434 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005435 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005436 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005437 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005438 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005439 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005440 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005441 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005442 Immediate(High32Bits(val)));
5443 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005444 }
5445 break;
5446 }
5447
Mark Mendell7c8d0092015-01-26 11:21:33 -05005448 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005449 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5450 Address address = index.IsConstant()
5451 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5452 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005453 if (value.IsFpuRegister()) {
5454 __ movss(address, value.AsFpuRegister<XmmRegister>());
5455 } else {
5456 DCHECK(value.IsConstant());
5457 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5458 __ movl(address, Immediate(v));
5459 }
5460 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005461 break;
5462 }
5463
5464 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005465 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5466 Address address = index.IsConstant()
5467 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5468 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005469 if (value.IsFpuRegister()) {
5470 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5471 } else {
5472 DCHECK(value.IsConstant());
5473 Address address_hi = index.IsConstant() ?
5474 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5475 offset + kX86WordSize) :
5476 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5477 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5478 __ movl(address, Immediate(Low32Bits(v)));
5479 codegen_->MaybeRecordImplicitNullCheck(instruction);
5480 __ movl(address_hi, Immediate(High32Bits(v)));
5481 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005482 break;
5483 }
5484
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005485 case Primitive::kPrimVoid:
5486 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005487 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005488 }
5489}
5490
5491void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5492 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005493 locations->SetInAt(0, Location::RequiresRegister());
5494 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005495}
5496
5497void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5498 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005499 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005500 Register obj = locations->InAt(0).AsRegister<Register>();
5501 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005502 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005503 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005504}
5505
5506void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005507 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5508 ? LocationSummary::kCallOnSlowPath
5509 : LocationSummary::kNoCall;
5510 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005511 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005512 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005513 if (instruction->HasUses()) {
5514 locations->SetOut(Location::SameAsFirstInput());
5515 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005516}
5517
5518void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5519 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005520 Location index_loc = locations->InAt(0);
5521 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005522 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005523 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524
Mark Mendell99dbd682015-04-22 16:18:52 -04005525 if (length_loc.IsConstant()) {
5526 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5527 if (index_loc.IsConstant()) {
5528 // BCE will remove the bounds check if we are guarenteed to pass.
5529 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5530 if (index < 0 || index >= length) {
5531 codegen_->AddSlowPath(slow_path);
5532 __ jmp(slow_path->GetEntryLabel());
5533 } else {
5534 // Some optimization after BCE may have generated this, and we should not
5535 // generate a bounds check if it is a valid range.
5536 }
5537 return;
5538 }
5539
5540 // We have to reverse the jump condition because the length is the constant.
5541 Register index_reg = index_loc.AsRegister<Register>();
5542 __ cmpl(index_reg, Immediate(length));
5543 codegen_->AddSlowPath(slow_path);
5544 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005545 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005546 Register length = length_loc.AsRegister<Register>();
5547 if (index_loc.IsConstant()) {
5548 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5549 __ cmpl(length, Immediate(value));
5550 } else {
5551 __ cmpl(length, index_loc.AsRegister<Register>());
5552 }
5553 codegen_->AddSlowPath(slow_path);
5554 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005555 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005556}
5557
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005558void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005559 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005560}
5561
5562void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005563 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5564}
5565
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005566void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5568}
5569
5570void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005571 HBasicBlock* block = instruction->GetBlock();
5572 if (block->GetLoopInformation() != nullptr) {
5573 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5574 // The back edge will generate the suspend check.
5575 return;
5576 }
5577 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5578 // The goto will generate the suspend check.
5579 return;
5580 }
5581 GenerateSuspendCheck(instruction, nullptr);
5582}
5583
5584void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5585 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005586 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005587 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5588 if (slow_path == nullptr) {
5589 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5590 instruction->SetSlowPath(slow_path);
5591 codegen_->AddSlowPath(slow_path);
5592 if (successor != nullptr) {
5593 DCHECK(successor->IsLoopHeader());
5594 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5595 }
5596 } else {
5597 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5598 }
5599
Roland Levillain7c1559a2015-12-15 10:55:36 +00005600 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5601 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005602 if (successor == nullptr) {
5603 __ j(kNotEqual, slow_path->GetEntryLabel());
5604 __ Bind(slow_path->GetReturnLabel());
5605 } else {
5606 __ j(kEqual, codegen_->GetLabelOf(successor));
5607 __ jmp(slow_path->GetEntryLabel());
5608 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005609}
5610
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005611X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5612 return codegen_->GetAssembler();
5613}
5614
Mark Mendell7c8d0092015-01-26 11:21:33 -05005615void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005616 ScratchRegisterScope ensure_scratch(
5617 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5618 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5619 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5620 __ movl(temp_reg, Address(ESP, src + stack_offset));
5621 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005622}
5623
5624void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005625 ScratchRegisterScope ensure_scratch(
5626 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5627 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5628 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5629 __ movl(temp_reg, Address(ESP, src + stack_offset));
5630 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5631 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5632 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005633}
5634
5635void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005636 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005637 Location source = move->GetSource();
5638 Location destination = move->GetDestination();
5639
5640 if (source.IsRegister()) {
5641 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005642 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005643 } else if (destination.IsFpuRegister()) {
5644 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005645 } else {
5646 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005647 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005648 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005649 } else if (source.IsRegisterPair()) {
5650 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5651 // Create stack space for 2 elements.
5652 __ subl(ESP, Immediate(2 * elem_size));
5653 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5654 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5655 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5656 // And remove the temporary stack space we allocated.
5657 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005658 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005659 if (destination.IsRegister()) {
5660 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5661 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005662 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005663 } else if (destination.IsRegisterPair()) {
5664 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5665 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5666 __ psrlq(src_reg, Immediate(32));
5667 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005668 } else if (destination.IsStackSlot()) {
5669 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5670 } else {
5671 DCHECK(destination.IsDoubleStackSlot());
5672 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5673 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005674 } else if (source.IsStackSlot()) {
5675 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005676 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005677 } else if (destination.IsFpuRegister()) {
5678 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005679 } else {
5680 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005681 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5682 }
5683 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005684 if (destination.IsRegisterPair()) {
5685 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5686 __ movl(destination.AsRegisterPairHigh<Register>(),
5687 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5688 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005689 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5690 } else {
5691 DCHECK(destination.IsDoubleStackSlot()) << destination;
5692 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005693 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005694 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005695 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005696 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005697 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005698 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005699 if (value == 0) {
5700 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5701 } else {
5702 __ movl(destination.AsRegister<Register>(), Immediate(value));
5703 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005704 } else {
5705 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005706 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005707 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005708 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005709 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005710 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005711 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005712 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005713 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5714 if (value == 0) {
5715 // Easy handling of 0.0.
5716 __ xorps(dest, dest);
5717 } else {
5718 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005719 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5720 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5721 __ movl(temp, Immediate(value));
5722 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005723 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005724 } else {
5725 DCHECK(destination.IsStackSlot()) << destination;
5726 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5727 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005728 } else if (constant->IsLongConstant()) {
5729 int64_t value = constant->AsLongConstant()->GetValue();
5730 int32_t low_value = Low32Bits(value);
5731 int32_t high_value = High32Bits(value);
5732 Immediate low(low_value);
5733 Immediate high(high_value);
5734 if (destination.IsDoubleStackSlot()) {
5735 __ movl(Address(ESP, destination.GetStackIndex()), low);
5736 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5737 } else {
5738 __ movl(destination.AsRegisterPairLow<Register>(), low);
5739 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5740 }
5741 } else {
5742 DCHECK(constant->IsDoubleConstant());
5743 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005744 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005745 int32_t low_value = Low32Bits(value);
5746 int32_t high_value = High32Bits(value);
5747 Immediate low(low_value);
5748 Immediate high(high_value);
5749 if (destination.IsFpuRegister()) {
5750 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5751 if (value == 0) {
5752 // Easy handling of 0.0.
5753 __ xorpd(dest, dest);
5754 } else {
5755 __ pushl(high);
5756 __ pushl(low);
5757 __ movsd(dest, Address(ESP, 0));
5758 __ addl(ESP, Immediate(8));
5759 }
5760 } else {
5761 DCHECK(destination.IsDoubleStackSlot()) << destination;
5762 __ movl(Address(ESP, destination.GetStackIndex()), low);
5763 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5764 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005765 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005766 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005767 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005768 }
5769}
5770
Mark Mendella5c19ce2015-04-01 12:51:05 -04005771void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005772 Register suggested_scratch = reg == EAX ? EBX : EAX;
5773 ScratchRegisterScope ensure_scratch(
5774 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5775
5776 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5777 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5778 __ movl(Address(ESP, mem + stack_offset), reg);
5779 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005780}
5781
Mark Mendell7c8d0092015-01-26 11:21:33 -05005782void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005783 ScratchRegisterScope ensure_scratch(
5784 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5785
5786 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5787 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5788 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5789 __ movss(Address(ESP, mem + stack_offset), reg);
5790 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005791}
5792
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005793void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005794 ScratchRegisterScope ensure_scratch1(
5795 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005796
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005797 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5798 ScratchRegisterScope ensure_scratch2(
5799 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005800
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005801 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5802 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5803 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5804 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5805 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5806 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005807}
5808
5809void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005810 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005811 Location source = move->GetSource();
5812 Location destination = move->GetDestination();
5813
5814 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005815 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5816 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5817 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5818 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5819 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005820 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005821 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005822 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005823 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005824 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5825 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005826 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5827 // Use XOR Swap algorithm to avoid a temporary.
5828 DCHECK_NE(source.reg(), destination.reg());
5829 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5830 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5831 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5832 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5833 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5834 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5835 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005836 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5837 // Take advantage of the 16 bytes in the XMM register.
5838 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5839 Address stack(ESP, destination.GetStackIndex());
5840 // Load the double into the high doubleword.
5841 __ movhpd(reg, stack);
5842
5843 // Store the low double into the destination.
5844 __ movsd(stack, reg);
5845
5846 // Move the high double to the low double.
5847 __ psrldq(reg, Immediate(8));
5848 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5849 // Take advantage of the 16 bytes in the XMM register.
5850 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5851 Address stack(ESP, source.GetStackIndex());
5852 // Load the double into the high doubleword.
5853 __ movhpd(reg, stack);
5854
5855 // Store the low double into the destination.
5856 __ movsd(stack, reg);
5857
5858 // Move the high double to the low double.
5859 __ psrldq(reg, Immediate(8));
5860 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5861 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5862 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005863 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005864 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005865 }
5866}
5867
5868void ParallelMoveResolverX86::SpillScratch(int reg) {
5869 __ pushl(static_cast<Register>(reg));
5870}
5871
5872void ParallelMoveResolverX86::RestoreScratch(int reg) {
5873 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005874}
5875
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005876void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005877 InvokeRuntimeCallingConvention calling_convention;
5878 CodeGenerator::CreateLoadClassLocationSummary(
5879 cls,
5880 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005881 Location::RegisterLocation(EAX),
5882 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005883}
5884
5885void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005886 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005887 if (cls->NeedsAccessCheck()) {
5888 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5889 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5890 cls,
5891 cls->GetDexPc(),
5892 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005893 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005894 return;
5895 }
5896
Roland Levillain0d5a2812015-11-13 10:07:31 +00005897 Location out_loc = locations->Out();
5898 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005899 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005900
Calin Juravle580b6092015-10-06 17:35:58 +01005901 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005902 DCHECK(!cls->CanCallRuntime());
5903 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005904 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5905 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005906 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005907 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005908 // /* GcRoot<mirror::Class>[] */ out =
5909 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5910 __ movl(out, Address(current_method,
5911 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005912 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005913 GenerateGcRootFieldLoad(
5914 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005915
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005916 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5917 DCHECK(cls->CanCallRuntime());
5918 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5919 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5920 codegen_->AddSlowPath(slow_path);
5921
5922 if (!cls->IsInDexCache()) {
5923 __ testl(out, out);
5924 __ j(kEqual, slow_path->GetEntryLabel());
5925 }
5926
5927 if (cls->MustGenerateClinitCheck()) {
5928 GenerateClassInitializationCheck(slow_path, out);
5929 } else {
5930 __ Bind(slow_path->GetExitLabel());
5931 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005932 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005933 }
5934}
5935
5936void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5937 LocationSummary* locations =
5938 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5939 locations->SetInAt(0, Location::RequiresRegister());
5940 if (check->HasUses()) {
5941 locations->SetOut(Location::SameAsFirstInput());
5942 }
5943}
5944
5945void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005946 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005947 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005948 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005950 GenerateClassInitializationCheck(slow_path,
5951 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005952}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005953
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005954void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005955 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005956 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5957 Immediate(mirror::Class::kStatusInitialized));
5958 __ j(kLess, slow_path->GetEntryLabel());
5959 __ Bind(slow_path->GetExitLabel());
5960 // No need for memory fence, thanks to the X86 memory model.
5961}
5962
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005963HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5964 HLoadString::LoadKind desired_string_load_kind) {
5965 if (kEmitCompilerReadBarrier) {
5966 switch (desired_string_load_kind) {
5967 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5968 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5969 case HLoadString::LoadKind::kBootImageAddress:
5970 // TODO: Implement for read barrier.
5971 return HLoadString::LoadKind::kDexCacheViaMethod;
5972 default:
5973 break;
5974 }
5975 }
5976 switch (desired_string_load_kind) {
5977 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5978 DCHECK(!GetCompilerOptions().GetCompilePic());
5979 break;
5980 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5981 DCHECK(GetCompilerOptions().GetCompilePic());
5982 FALLTHROUGH_INTENDED;
5983 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005984 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005985 // We disable pc-relative load when there is an irreducible loop, as the optimization
5986 // is incompatible with it.
5987 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5988 // with irreducible loops.
5989 if (GetGraph()->HasIrreducibleLoops()) {
5990 return HLoadString::LoadKind::kDexCacheViaMethod;
5991 }
5992 break;
5993 case HLoadString::LoadKind::kBootImageAddress:
5994 break;
5995 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005996 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005997 break;
5998 case HLoadString::LoadKind::kDexCacheViaMethod:
5999 break;
6000 }
6001 return desired_string_load_kind;
6002}
6003
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006004void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006005 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006006 ? LocationSummary::kCallOnSlowPath
6007 : LocationSummary::kNoCall;
6008 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006009 HLoadString::LoadKind load_kind = load->GetLoadKind();
6010 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6011 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6012 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6013 locations->SetInAt(0, Location::RequiresRegister());
6014 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006015 locations->SetOut(Location::RequiresRegister());
6016}
6017
6018void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006019 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006020 Location out_loc = locations->Out();
6021 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006022
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006023 switch (load->GetLoadKind()) {
6024 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6025 DCHECK(!kEmitCompilerReadBarrier);
6026 __ movl(out, Immediate(/* placeholder */ 0));
6027 codegen_->RecordStringPatch(load);
6028 return; // No dex cache slow path.
6029 }
6030 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6031 DCHECK(!kEmitCompilerReadBarrier);
6032 Register method_address = locations->InAt(0).AsRegister<Register>();
6033 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6034 codegen_->RecordStringPatch(load);
6035 return; // No dex cache slow path.
6036 }
6037 case HLoadString::LoadKind::kBootImageAddress: {
6038 DCHECK(!kEmitCompilerReadBarrier);
6039 DCHECK_NE(load->GetAddress(), 0u);
6040 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6041 __ movl(out, Immediate(address));
6042 codegen_->RecordSimplePatch();
6043 return; // No dex cache slow path.
6044 }
6045 case HLoadString::LoadKind::kDexCacheAddress: {
6046 DCHECK_NE(load->GetAddress(), 0u);
6047 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6048 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6049 break;
6050 }
6051 case HLoadString::LoadKind::kDexCachePcRelative: {
6052 Register base_reg = locations->InAt(0).AsRegister<Register>();
6053 uint32_t offset = load->GetDexCacheElementOffset();
6054 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
6055 GenerateGcRootFieldLoad(
6056 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6057 break;
6058 }
6059 case HLoadString::LoadKind::kDexCacheViaMethod: {
6060 Register current_method = locations->InAt(0).AsRegister<Register>();
6061
6062 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6063 GenerateGcRootFieldLoad(
6064 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6065
6066 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6067 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6068 // /* GcRoot<mirror::String> */ out = out[string_index]
6069 GenerateGcRootFieldLoad(
6070 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6071 break;
6072 }
6073 default:
6074 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6075 UNREACHABLE();
6076 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006077
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006078 if (!load->IsInDexCache()) {
6079 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6080 codegen_->AddSlowPath(slow_path);
6081 __ testl(out, out);
6082 __ j(kEqual, slow_path->GetEntryLabel());
6083 __ Bind(slow_path->GetExitLabel());
6084 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006085}
6086
David Brazdilcb1c0552015-08-04 16:22:25 +01006087static Address GetExceptionTlsAddress() {
6088 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6089}
6090
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006091void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6092 LocationSummary* locations =
6093 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6094 locations->SetOut(Location::RequiresRegister());
6095}
6096
6097void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006098 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6099}
6100
6101void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6102 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6103}
6104
6105void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6106 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006107}
6108
6109void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6110 LocationSummary* locations =
6111 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6112 InvokeRuntimeCallingConvention calling_convention;
6113 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6114}
6115
6116void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006117 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6118 instruction,
6119 instruction->GetDexPc(),
6120 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006121 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006122}
6123
Roland Levillain7c1559a2015-12-15 10:55:36 +00006124static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6125 return kEmitCompilerReadBarrier &&
6126 (kUseBakerReadBarrier ||
6127 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6128 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6129 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6130}
6131
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006132void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006133 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006134 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6135 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006136 case TypeCheckKind::kExactCheck:
6137 case TypeCheckKind::kAbstractClassCheck:
6138 case TypeCheckKind::kClassHierarchyCheck:
6139 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140 call_kind =
6141 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 break;
6143 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144 case TypeCheckKind::kUnresolvedCheck:
6145 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006146 call_kind = LocationSummary::kCallOnSlowPath;
6147 break;
6148 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006149
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006150 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006151 locations->SetInAt(0, Location::RequiresRegister());
6152 locations->SetInAt(1, Location::Any());
6153 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6154 locations->SetOut(Location::RequiresRegister());
6155 // When read barriers are enabled, we need a temporary register for
6156 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006157 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006158 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006159 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006160}
6161
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006162void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006163 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006164 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006165 Location obj_loc = locations->InAt(0);
6166 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006167 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 Location out_loc = locations->Out();
6169 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006170 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006171 locations->GetTemp(0) :
6172 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006173 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6175 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6176 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006177 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006178 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006179
6180 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006181 // Avoid null check if we know obj is not null.
6182 if (instruction->MustDoNullCheck()) {
6183 __ testl(obj, obj);
6184 __ j(kEqual, &zero);
6185 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006186
Roland Levillain0d5a2812015-11-13 10:07:31 +00006187 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006188 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006189
Roland Levillain7c1559a2015-12-15 10:55:36 +00006190 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006191 case TypeCheckKind::kExactCheck: {
6192 if (cls.IsRegister()) {
6193 __ cmpl(out, cls.AsRegister<Register>());
6194 } else {
6195 DCHECK(cls.IsStackSlot()) << cls;
6196 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6197 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006198
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199 // Classes must be equal for the instanceof to succeed.
6200 __ j(kNotEqual, &zero);
6201 __ movl(out, Immediate(1));
6202 __ jmp(&done);
6203 break;
6204 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 case TypeCheckKind::kAbstractClassCheck: {
6207 // If the class is abstract, we eagerly fetch the super class of the
6208 // object to avoid doing a comparison we know will fail.
6209 NearLabel loop;
6210 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006211 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006212 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006213 __ testl(out, out);
6214 // If `out` is null, we use it for the result, and jump to `done`.
6215 __ j(kEqual, &done);
6216 if (cls.IsRegister()) {
6217 __ cmpl(out, cls.AsRegister<Register>());
6218 } else {
6219 DCHECK(cls.IsStackSlot()) << cls;
6220 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6221 }
6222 __ j(kNotEqual, &loop);
6223 __ movl(out, Immediate(1));
6224 if (zero.IsLinked()) {
6225 __ jmp(&done);
6226 }
6227 break;
6228 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006229
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006230 case TypeCheckKind::kClassHierarchyCheck: {
6231 // Walk over the class hierarchy to find a match.
6232 NearLabel loop, success;
6233 __ Bind(&loop);
6234 if (cls.IsRegister()) {
6235 __ cmpl(out, cls.AsRegister<Register>());
6236 } else {
6237 DCHECK(cls.IsStackSlot()) << cls;
6238 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6239 }
6240 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006241 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006242 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006243 __ testl(out, out);
6244 __ j(kNotEqual, &loop);
6245 // If `out` is null, we use it for the result, and jump to `done`.
6246 __ jmp(&done);
6247 __ Bind(&success);
6248 __ movl(out, Immediate(1));
6249 if (zero.IsLinked()) {
6250 __ jmp(&done);
6251 }
6252 break;
6253 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006254
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006255 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006256 // Do an exact check.
6257 NearLabel exact_check;
6258 if (cls.IsRegister()) {
6259 __ cmpl(out, cls.AsRegister<Register>());
6260 } else {
6261 DCHECK(cls.IsStackSlot()) << cls;
6262 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6263 }
6264 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006265 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006266 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006267 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006268 __ testl(out, out);
6269 // If `out` is null, we use it for the result, and jump to `done`.
6270 __ j(kEqual, &done);
6271 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6272 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006273 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006274 __ movl(out, Immediate(1));
6275 __ jmp(&done);
6276 break;
6277 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006278
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006279 case TypeCheckKind::kArrayCheck: {
6280 if (cls.IsRegister()) {
6281 __ cmpl(out, cls.AsRegister<Register>());
6282 } else {
6283 DCHECK(cls.IsStackSlot()) << cls;
6284 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6285 }
6286 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006287 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6288 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006289 codegen_->AddSlowPath(slow_path);
6290 __ j(kNotEqual, slow_path->GetEntryLabel());
6291 __ movl(out, Immediate(1));
6292 if (zero.IsLinked()) {
6293 __ jmp(&done);
6294 }
6295 break;
6296 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297
Calin Juravle98893e12015-10-02 21:05:03 +01006298 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006299 case TypeCheckKind::kInterfaceCheck: {
6300 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006301 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006302 // cases.
6303 //
6304 // We cannot directly call the InstanceofNonTrivial runtime
6305 // entry point without resorting to a type checking slow path
6306 // here (i.e. by calling InvokeRuntime directly), as it would
6307 // require to assign fixed registers for the inputs of this
6308 // HInstanceOf instruction (following the runtime calling
6309 // convention), which might be cluttered by the potential first
6310 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006311 //
6312 // TODO: Introduce a new runtime entry point taking the object
6313 // to test (instead of its class) as argument, and let it deal
6314 // with the read barrier issues. This will let us refactor this
6315 // case of the `switch` code as it was previously (with a direct
6316 // call to the runtime not using a type checking slow path).
6317 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006318 DCHECK(locations->OnlyCallsOnSlowPath());
6319 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6320 /* is_fatal */ false);
6321 codegen_->AddSlowPath(slow_path);
6322 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 if (zero.IsLinked()) {
6324 __ jmp(&done);
6325 }
6326 break;
6327 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006328 }
6329
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006330 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006331 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006332 __ xorl(out, out);
6333 }
6334
6335 if (done.IsLinked()) {
6336 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006337 }
6338
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006339 if (slow_path != nullptr) {
6340 __ Bind(slow_path->GetExitLabel());
6341 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006342}
6343
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006344void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006345 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6346 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006347 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6348 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 case TypeCheckKind::kExactCheck:
6350 case TypeCheckKind::kAbstractClassCheck:
6351 case TypeCheckKind::kClassHierarchyCheck:
6352 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006353 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6354 LocationSummary::kCallOnSlowPath :
6355 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006356 break;
6357 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006358 case TypeCheckKind::kUnresolvedCheck:
6359 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006360 call_kind = LocationSummary::kCallOnSlowPath;
6361 break;
6362 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6364 locations->SetInAt(0, Location::RequiresRegister());
6365 locations->SetInAt(1, Location::Any());
6366 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6367 locations->AddTemp(Location::RequiresRegister());
6368 // When read barriers are enabled, we need an additional temporary
6369 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006370 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006371 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006372 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006373}
6374
6375void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006376 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006377 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006378 Location obj_loc = locations->InAt(0);
6379 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006380 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006381 Location temp_loc = locations->GetTemp(0);
6382 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006383 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006384 locations->GetTemp(1) :
6385 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006386 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6387 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6388 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6389 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006390
Roland Levillain0d5a2812015-11-13 10:07:31 +00006391 bool is_type_check_slow_path_fatal =
6392 (type_check_kind == TypeCheckKind::kExactCheck ||
6393 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6394 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6395 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6396 !instruction->CanThrowIntoCatchBlock();
6397 SlowPathCode* type_check_slow_path =
6398 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6399 is_type_check_slow_path_fatal);
6400 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006403 // Avoid null check if we know obj is not null.
6404 if (instruction->MustDoNullCheck()) {
6405 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006407 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006408
Roland Levillain0d5a2812015-11-13 10:07:31 +00006409 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006410 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 case TypeCheckKind::kExactCheck:
6414 case TypeCheckKind::kArrayCheck: {
6415 if (cls.IsRegister()) {
6416 __ cmpl(temp, cls.AsRegister<Register>());
6417 } else {
6418 DCHECK(cls.IsStackSlot()) << cls;
6419 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6420 }
6421 // Jump to slow path for throwing the exception or doing a
6422 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424 break;
6425 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 case TypeCheckKind::kAbstractClassCheck: {
6428 // If the class is abstract, we eagerly fetch the super class of the
6429 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006433 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006434
6435 // If the class reference currently in `temp` is not null, jump
6436 // to the `compare_classes` label to compare it with the checked
6437 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 __ j(kNotEqual, &compare_classes);
6440 // Otherwise, jump to the slow path to throw the exception.
6441 //
6442 // But before, move back the object's class into `temp` before
6443 // going into the slow path, as it has been overwritten in the
6444 // meantime.
6445 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006446 GenerateReferenceLoadTwoRegisters(
6447 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006448 __ jmp(type_check_slow_path->GetEntryLabel());
6449
6450 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 if (cls.IsRegister()) {
6452 __ cmpl(temp, cls.AsRegister<Register>());
6453 } else {
6454 DCHECK(cls.IsStackSlot()) << cls;
6455 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6456 }
6457 __ j(kNotEqual, &loop);
6458 break;
6459 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006460
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 case TypeCheckKind::kClassHierarchyCheck: {
6462 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006463 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006464 __ Bind(&loop);
6465 if (cls.IsRegister()) {
6466 __ cmpl(temp, cls.AsRegister<Register>());
6467 } else {
6468 DCHECK(cls.IsStackSlot()) << cls;
6469 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6470 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006471 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006474 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475
6476 // If the class reference currently in `temp` is not null, jump
6477 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006478 __ testl(temp, temp);
6479 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006480 // Otherwise, jump to the slow path to throw the exception.
6481 //
6482 // But before, move back the object's class into `temp` before
6483 // going into the slow path, as it has been overwritten in the
6484 // meantime.
6485 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006486 GenerateReferenceLoadTwoRegisters(
6487 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006488 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006489 break;
6490 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006492 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006493 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006494 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006495 if (cls.IsRegister()) {
6496 __ cmpl(temp, cls.AsRegister<Register>());
6497 } else {
6498 DCHECK(cls.IsStackSlot()) << cls;
6499 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6500 }
6501 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006502
6503 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006504 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006505 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006506
6507 // If the component type is not null (i.e. the object is indeed
6508 // an array), jump to label `check_non_primitive_component_type`
6509 // to further check that this component type is not a primitive
6510 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006511 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006512 __ j(kNotEqual, &check_non_primitive_component_type);
6513 // Otherwise, jump to the slow path to throw the exception.
6514 //
6515 // But before, move back the object's class into `temp` before
6516 // going into the slow path, as it has been overwritten in the
6517 // meantime.
6518 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006519 GenerateReferenceLoadTwoRegisters(
6520 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006521 __ jmp(type_check_slow_path->GetEntryLabel());
6522
6523 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006525 __ j(kEqual, &done);
6526 // Same comment as above regarding `temp` and the slow path.
6527 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006528 GenerateReferenceLoadTwoRegisters(
6529 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006530 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 break;
6532 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006533
Calin Juravle98893e12015-10-02 21:05:03 +01006534 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006535 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006536 // We always go into the type check slow path for the unresolved
6537 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006538 //
6539 // We cannot directly call the CheckCast runtime entry point
6540 // without resorting to a type checking slow path here (i.e. by
6541 // calling InvokeRuntime directly), as it would require to
6542 // assign fixed registers for the inputs of this HInstanceOf
6543 // instruction (following the runtime calling convention), which
6544 // might be cluttered by the potential first read barrier
6545 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006546 //
6547 // TODO: Introduce a new runtime entry point taking the object
6548 // to test (instead of its class) as argument, and let it deal
6549 // with the read barrier issues. This will let us refactor this
6550 // case of the `switch` code as it was previously (with a direct
6551 // call to the runtime not using a type checking slow path).
6552 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006553 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006554 break;
6555 }
6556 __ Bind(&done);
6557
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006559}
6560
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006561void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6562 LocationSummary* locations =
6563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6564 InvokeRuntimeCallingConvention calling_convention;
6565 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6566}
6567
6568void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006569 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6570 : QUICK_ENTRY_POINT(pUnlockObject),
6571 instruction,
6572 instruction->GetDexPc(),
6573 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006574 if (instruction->IsEnter()) {
6575 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6576 } else {
6577 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6578 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006579}
6580
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006581void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6582void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6583void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6584
6585void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6586 LocationSummary* locations =
6587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6588 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6589 || instruction->GetResultType() == Primitive::kPrimLong);
6590 locations->SetInAt(0, Location::RequiresRegister());
6591 locations->SetInAt(1, Location::Any());
6592 locations->SetOut(Location::SameAsFirstInput());
6593}
6594
6595void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6596 HandleBitwiseOperation(instruction);
6597}
6598
6599void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6600 HandleBitwiseOperation(instruction);
6601}
6602
6603void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6604 HandleBitwiseOperation(instruction);
6605}
6606
6607void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6608 LocationSummary* locations = instruction->GetLocations();
6609 Location first = locations->InAt(0);
6610 Location second = locations->InAt(1);
6611 DCHECK(first.Equals(locations->Out()));
6612
6613 if (instruction->GetResultType() == Primitive::kPrimInt) {
6614 if (second.IsRegister()) {
6615 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006616 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006617 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006618 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006619 } else {
6620 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006621 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006622 }
6623 } else if (second.IsConstant()) {
6624 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006625 __ andl(first.AsRegister<Register>(),
6626 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006627 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006628 __ orl(first.AsRegister<Register>(),
6629 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006630 } else {
6631 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006632 __ xorl(first.AsRegister<Register>(),
6633 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006634 }
6635 } else {
6636 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006637 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006638 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006639 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006640 } else {
6641 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006642 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006643 }
6644 }
6645 } else {
6646 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6647 if (second.IsRegisterPair()) {
6648 if (instruction->IsAnd()) {
6649 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6650 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6651 } else if (instruction->IsOr()) {
6652 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6653 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6654 } else {
6655 DCHECK(instruction->IsXor());
6656 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6657 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6658 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006659 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006660 if (instruction->IsAnd()) {
6661 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6662 __ andl(first.AsRegisterPairHigh<Register>(),
6663 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6664 } else if (instruction->IsOr()) {
6665 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6666 __ orl(first.AsRegisterPairHigh<Register>(),
6667 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6668 } else {
6669 DCHECK(instruction->IsXor());
6670 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6671 __ xorl(first.AsRegisterPairHigh<Register>(),
6672 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6673 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006674 } else {
6675 DCHECK(second.IsConstant()) << second;
6676 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006677 int32_t low_value = Low32Bits(value);
6678 int32_t high_value = High32Bits(value);
6679 Immediate low(low_value);
6680 Immediate high(high_value);
6681 Register first_low = first.AsRegisterPairLow<Register>();
6682 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006683 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006684 if (low_value == 0) {
6685 __ xorl(first_low, first_low);
6686 } else if (low_value != -1) {
6687 __ andl(first_low, low);
6688 }
6689 if (high_value == 0) {
6690 __ xorl(first_high, first_high);
6691 } else if (high_value != -1) {
6692 __ andl(first_high, high);
6693 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006694 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006695 if (low_value != 0) {
6696 __ orl(first_low, low);
6697 }
6698 if (high_value != 0) {
6699 __ orl(first_high, high);
6700 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006701 } else {
6702 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006703 if (low_value != 0) {
6704 __ xorl(first_low, low);
6705 }
6706 if (high_value != 0) {
6707 __ xorl(first_high, high);
6708 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006709 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006710 }
6711 }
6712}
6713
Roland Levillain7c1559a2015-12-15 10:55:36 +00006714void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6715 Location out,
6716 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006717 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006718 Register out_reg = out.AsRegister<Register>();
6719 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006720 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006721 if (kUseBakerReadBarrier) {
6722 // Load with fast path based Baker's read barrier.
6723 // /* HeapReference<Object> */ out = *(out + offset)
6724 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006725 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006726 } else {
6727 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006728 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006729 // in the following move operation, as we will need it for the
6730 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006731 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006732 // /* HeapReference<Object> */ out = *(out + offset)
6733 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006734 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006735 }
6736 } else {
6737 // Plain load with no read barrier.
6738 // /* HeapReference<Object> */ out = *(out + offset)
6739 __ movl(out_reg, Address(out_reg, offset));
6740 __ MaybeUnpoisonHeapReference(out_reg);
6741 }
6742}
6743
6744void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6745 Location out,
6746 Location obj,
6747 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006748 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006749 Register out_reg = out.AsRegister<Register>();
6750 Register obj_reg = obj.AsRegister<Register>();
6751 if (kEmitCompilerReadBarrier) {
6752 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006753 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006754 // Load with fast path based Baker's read barrier.
6755 // /* HeapReference<Object> */ out = *(obj + offset)
6756 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006757 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006758 } else {
6759 // Load with slow path based read barrier.
6760 // /* HeapReference<Object> */ out = *(obj + offset)
6761 __ movl(out_reg, Address(obj_reg, offset));
6762 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6763 }
6764 } else {
6765 // Plain load with no read barrier.
6766 // /* HeapReference<Object> */ out = *(obj + offset)
6767 __ movl(out_reg, Address(obj_reg, offset));
6768 __ MaybeUnpoisonHeapReference(out_reg);
6769 }
6770}
6771
6772void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6773 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006774 const Address& address,
6775 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006776 Register root_reg = root.AsRegister<Register>();
6777 if (kEmitCompilerReadBarrier) {
6778 if (kUseBakerReadBarrier) {
6779 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6780 // Baker's read barrier are used:
6781 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006782 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006783 // if (Thread::Current()->GetIsGcMarking()) {
6784 // root = ReadBarrier::Mark(root)
6785 // }
6786
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006787 // /* GcRoot<mirror::Object> */ root = *address
6788 __ movl(root_reg, address);
6789 if (fixup_label != nullptr) {
6790 __ Bind(fixup_label);
6791 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006792 static_assert(
6793 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6794 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6795 "have different sizes.");
6796 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6797 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6798 "have different sizes.");
6799
6800 // Slow path used to mark the GC root `root`.
6801 SlowPathCode* slow_path =
6802 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6803 codegen_->AddSlowPath(slow_path);
6804
6805 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6806 Immediate(0));
6807 __ j(kNotEqual, slow_path->GetEntryLabel());
6808 __ Bind(slow_path->GetExitLabel());
6809 } else {
6810 // GC root loaded through a slow path for read barriers other
6811 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006812 // /* GcRoot<mirror::Object>* */ root = address
6813 __ leal(root_reg, address);
6814 if (fixup_label != nullptr) {
6815 __ Bind(fixup_label);
6816 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006817 // /* mirror::Object* */ root = root->Read()
6818 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6819 }
6820 } else {
6821 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006822 // /* GcRoot<mirror::Object> */ root = *address
6823 __ movl(root_reg, address);
6824 if (fixup_label != nullptr) {
6825 __ Bind(fixup_label);
6826 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006827 // Note that GC roots are not affected by heap poisoning, thus we
6828 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006829 }
6830}
6831
6832void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6833 Location ref,
6834 Register obj,
6835 uint32_t offset,
6836 Location temp,
6837 bool needs_null_check) {
6838 DCHECK(kEmitCompilerReadBarrier);
6839 DCHECK(kUseBakerReadBarrier);
6840
6841 // /* HeapReference<Object> */ ref = *(obj + offset)
6842 Address src(obj, offset);
6843 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6844}
6845
6846void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6847 Location ref,
6848 Register obj,
6849 uint32_t data_offset,
6850 Location index,
6851 Location temp,
6852 bool needs_null_check) {
6853 DCHECK(kEmitCompilerReadBarrier);
6854 DCHECK(kUseBakerReadBarrier);
6855
6856 // /* HeapReference<Object> */ ref =
6857 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6858 Address src = index.IsConstant() ?
6859 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6860 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6861 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6862}
6863
6864void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6865 Location ref,
6866 Register obj,
6867 const Address& src,
6868 Location temp,
6869 bool needs_null_check) {
6870 DCHECK(kEmitCompilerReadBarrier);
6871 DCHECK(kUseBakerReadBarrier);
6872
6873 // In slow path based read barriers, the read barrier call is
6874 // inserted after the original load. However, in fast path based
6875 // Baker's read barriers, we need to perform the load of
6876 // mirror::Object::monitor_ *before* the original reference load.
6877 // This load-load ordering is required by the read barrier.
6878 // The fast path/slow path (for Baker's algorithm) should look like:
6879 //
6880 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6881 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6882 // HeapReference<Object> ref = *src; // Original reference load.
6883 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6884 // if (is_gray) {
6885 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6886 // }
6887 //
6888 // Note: the original implementation in ReadBarrier::Barrier is
6889 // slightly more complex as:
6890 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006891 // the high-bits of rb_state, which are expected to be all zeroes
6892 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6893 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006894 // - it performs additional checks that we do not do here for
6895 // performance reasons.
6896
6897 Register ref_reg = ref.AsRegister<Register>();
6898 Register temp_reg = temp.AsRegister<Register>();
6899 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6900
6901 // /* int32_t */ monitor = obj->monitor_
6902 __ movl(temp_reg, Address(obj, monitor_offset));
6903 if (needs_null_check) {
6904 MaybeRecordImplicitNullCheck(instruction);
6905 }
6906 // /* LockWord */ lock_word = LockWord(monitor)
6907 static_assert(sizeof(LockWord) == sizeof(int32_t),
6908 "art::LockWord and int32_t have different sizes.");
6909 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6910 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6911 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6912 static_assert(
6913 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6914 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6915
6916 // Load fence to prevent load-load reordering.
6917 // Note that this is a no-op, thanks to the x86 memory model.
6918 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6919
6920 // The actual reference load.
6921 // /* HeapReference<Object> */ ref = *src
6922 __ movl(ref_reg, src);
6923
6924 // Object* ref = ref_addr->AsMirrorPtr()
6925 __ MaybeUnpoisonHeapReference(ref_reg);
6926
6927 // Slow path used to mark the object `ref` when it is gray.
6928 SlowPathCode* slow_path =
6929 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6930 AddSlowPath(slow_path);
6931
6932 // if (rb_state == ReadBarrier::gray_ptr_)
6933 // ref = ReadBarrier::Mark(ref);
6934 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6935 __ j(kEqual, slow_path->GetEntryLabel());
6936 __ Bind(slow_path->GetExitLabel());
6937}
6938
6939void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6940 Location out,
6941 Location ref,
6942 Location obj,
6943 uint32_t offset,
6944 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006945 DCHECK(kEmitCompilerReadBarrier);
6946
Roland Levillain7c1559a2015-12-15 10:55:36 +00006947 // Insert a slow path based read barrier *after* the reference load.
6948 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006949 // If heap poisoning is enabled, the unpoisoning of the loaded
6950 // reference will be carried out by the runtime within the slow
6951 // path.
6952 //
6953 // Note that `ref` currently does not get unpoisoned (when heap
6954 // poisoning is enabled), which is alright as the `ref` argument is
6955 // not used by the artReadBarrierSlow entry point.
6956 //
6957 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6958 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6959 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6960 AddSlowPath(slow_path);
6961
Roland Levillain0d5a2812015-11-13 10:07:31 +00006962 __ jmp(slow_path->GetEntryLabel());
6963 __ Bind(slow_path->GetExitLabel());
6964}
6965
Roland Levillain7c1559a2015-12-15 10:55:36 +00006966void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6967 Location out,
6968 Location ref,
6969 Location obj,
6970 uint32_t offset,
6971 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006972 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006973 // Baker's read barriers shall be handled by the fast path
6974 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6975 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006976 // If heap poisoning is enabled, unpoisoning will be taken care of
6977 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006978 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006979 } else if (kPoisonHeapReferences) {
6980 __ UnpoisonHeapReference(out.AsRegister<Register>());
6981 }
6982}
6983
Roland Levillain7c1559a2015-12-15 10:55:36 +00006984void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6985 Location out,
6986 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006987 DCHECK(kEmitCompilerReadBarrier);
6988
Roland Levillain7c1559a2015-12-15 10:55:36 +00006989 // Insert a slow path based read barrier *after* the GC root load.
6990 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006991 // Note that GC roots are not affected by heap poisoning, so we do
6992 // not need to do anything special for this here.
6993 SlowPathCode* slow_path =
6994 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6995 AddSlowPath(slow_path);
6996
Roland Levillain0d5a2812015-11-13 10:07:31 +00006997 __ jmp(slow_path->GetEntryLabel());
6998 __ Bind(slow_path->GetExitLabel());
6999}
7000
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007001void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007002 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007003 LOG(FATAL) << "Unreachable";
7004}
7005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007006void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007007 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007008 LOG(FATAL) << "Unreachable";
7009}
7010
Mark Mendellfe57faa2015-09-18 09:26:15 -04007011// Simple implementation of packed switch - generate cascaded compare/jumps.
7012void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7013 LocationSummary* locations =
7014 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7015 locations->SetInAt(0, Location::RequiresRegister());
7016}
7017
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007018void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7019 int32_t lower_bound,
7020 uint32_t num_entries,
7021 HBasicBlock* switch_block,
7022 HBasicBlock* default_block) {
7023 // Figure out the correct compare values and jump conditions.
7024 // Handle the first compare/branch as a special case because it might
7025 // jump to the default case.
7026 DCHECK_GT(num_entries, 2u);
7027 Condition first_condition;
7028 uint32_t index;
7029 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7030 if (lower_bound != 0) {
7031 first_condition = kLess;
7032 __ cmpl(value_reg, Immediate(lower_bound));
7033 __ j(first_condition, codegen_->GetLabelOf(default_block));
7034 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007035
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007036 index = 1;
7037 } else {
7038 // Handle all the compare/jumps below.
7039 first_condition = kBelow;
7040 index = 0;
7041 }
7042
7043 // Handle the rest of the compare/jumps.
7044 for (; index + 1 < num_entries; index += 2) {
7045 int32_t compare_to_value = lower_bound + index + 1;
7046 __ cmpl(value_reg, Immediate(compare_to_value));
7047 // Jump to successors[index] if value < case_value[index].
7048 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7049 // Jump to successors[index + 1] if value == case_value[index + 1].
7050 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7051 }
7052
7053 if (index != num_entries) {
7054 // There are an odd number of entries. Handle the last one.
7055 DCHECK_EQ(index + 1, num_entries);
7056 __ cmpl(value_reg, Immediate(lower_bound + index));
7057 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007058 }
7059
7060 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007061 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7062 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007063 }
7064}
7065
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007066void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7067 int32_t lower_bound = switch_instr->GetStartValue();
7068 uint32_t num_entries = switch_instr->GetNumEntries();
7069 LocationSummary* locations = switch_instr->GetLocations();
7070 Register value_reg = locations->InAt(0).AsRegister<Register>();
7071
7072 GenPackedSwitchWithCompares(value_reg,
7073 lower_bound,
7074 num_entries,
7075 switch_instr->GetBlock(),
7076 switch_instr->GetDefaultBlock());
7077}
7078
Mark Mendell805b3b52015-09-18 14:10:29 -04007079void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7080 LocationSummary* locations =
7081 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7082 locations->SetInAt(0, Location::RequiresRegister());
7083
7084 // Constant area pointer.
7085 locations->SetInAt(1, Location::RequiresRegister());
7086
7087 // And the temporary we need.
7088 locations->AddTemp(Location::RequiresRegister());
7089}
7090
7091void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7092 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007093 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007094 LocationSummary* locations = switch_instr->GetLocations();
7095 Register value_reg = locations->InAt(0).AsRegister<Register>();
7096 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7097
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007098 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7099 GenPackedSwitchWithCompares(value_reg,
7100 lower_bound,
7101 num_entries,
7102 switch_instr->GetBlock(),
7103 default_block);
7104 return;
7105 }
7106
Mark Mendell805b3b52015-09-18 14:10:29 -04007107 // Optimizing has a jump area.
7108 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7109 Register constant_area = locations->InAt(1).AsRegister<Register>();
7110
7111 // Remove the bias, if needed.
7112 if (lower_bound != 0) {
7113 __ leal(temp_reg, Address(value_reg, -lower_bound));
7114 value_reg = temp_reg;
7115 }
7116
7117 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007118 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007119 __ cmpl(value_reg, Immediate(num_entries - 1));
7120 __ j(kAbove, codegen_->GetLabelOf(default_block));
7121
7122 // We are in the range of the table.
7123 // Load (target-constant_area) from the jump table, indexing by the value.
7124 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7125
7126 // Compute the actual target address by adding in constant_area.
7127 __ addl(temp_reg, constant_area);
7128
7129 // And jump.
7130 __ jmp(temp_reg);
7131}
7132
Mark Mendell0616ae02015-04-17 12:49:27 -04007133void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7134 HX86ComputeBaseMethodAddress* insn) {
7135 LocationSummary* locations =
7136 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7137 locations->SetOut(Location::RequiresRegister());
7138}
7139
7140void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7141 HX86ComputeBaseMethodAddress* insn) {
7142 LocationSummary* locations = insn->GetLocations();
7143 Register reg = locations->Out().AsRegister<Register>();
7144
7145 // Generate call to next instruction.
7146 Label next_instruction;
7147 __ call(&next_instruction);
7148 __ Bind(&next_instruction);
7149
7150 // Remember this offset for later use with constant area.
7151 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7152
7153 // Grab the return address off the stack.
7154 __ popl(reg);
7155}
7156
7157void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7158 HX86LoadFromConstantTable* insn) {
7159 LocationSummary* locations =
7160 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7161
7162 locations->SetInAt(0, Location::RequiresRegister());
7163 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7164
7165 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007166 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007167 return;
7168 }
7169
7170 switch (insn->GetType()) {
7171 case Primitive::kPrimFloat:
7172 case Primitive::kPrimDouble:
7173 locations->SetOut(Location::RequiresFpuRegister());
7174 break;
7175
7176 case Primitive::kPrimInt:
7177 locations->SetOut(Location::RequiresRegister());
7178 break;
7179
7180 default:
7181 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7182 }
7183}
7184
7185void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007186 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007187 return;
7188 }
7189
7190 LocationSummary* locations = insn->GetLocations();
7191 Location out = locations->Out();
7192 Register const_area = locations->InAt(0).AsRegister<Register>();
7193 HConstant *value = insn->GetConstant();
7194
7195 switch (insn->GetType()) {
7196 case Primitive::kPrimFloat:
7197 __ movss(out.AsFpuRegister<XmmRegister>(),
7198 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7199 break;
7200
7201 case Primitive::kPrimDouble:
7202 __ movsd(out.AsFpuRegister<XmmRegister>(),
7203 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7204 break;
7205
7206 case Primitive::kPrimInt:
7207 __ movl(out.AsRegister<Register>(),
7208 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7209 break;
7210
7211 default:
7212 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7213 }
7214}
7215
Mark Mendell0616ae02015-04-17 12:49:27 -04007216/**
7217 * Class to handle late fixup of offsets into constant area.
7218 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007219class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007220 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007221 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7222 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7223
7224 protected:
7225 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7226
7227 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007228
7229 private:
7230 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7231 // Patch the correct offset for the instruction. The place to patch is the
7232 // last 4 bytes of the instruction.
7233 // The value to patch is the distance from the offset in the constant area
7234 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007235 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7236 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007237
7238 // Patch in the right value.
7239 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7240 }
7241
Mark Mendell0616ae02015-04-17 12:49:27 -04007242 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007243 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007244};
7245
Mark Mendell805b3b52015-09-18 14:10:29 -04007246/**
7247 * Class to handle late fixup of offsets to a jump table that will be created in the
7248 * constant area.
7249 */
7250class JumpTableRIPFixup : public RIPFixup {
7251 public:
7252 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7253 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7254
7255 void CreateJumpTable() {
7256 X86Assembler* assembler = codegen_->GetAssembler();
7257
7258 // Ensure that the reference to the jump table has the correct offset.
7259 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7260 SetOffset(offset_in_constant_table);
7261
7262 // The label values in the jump table are computed relative to the
7263 // instruction addressing the constant area.
7264 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7265
7266 // Populate the jump table with the correct values for the jump table.
7267 int32_t num_entries = switch_instr_->GetNumEntries();
7268 HBasicBlock* block = switch_instr_->GetBlock();
7269 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7270 // The value that we want is the target offset - the position of the table.
7271 for (int32_t i = 0; i < num_entries; i++) {
7272 HBasicBlock* b = successors[i];
7273 Label* l = codegen_->GetLabelOf(b);
7274 DCHECK(l->IsBound());
7275 int32_t offset_to_block = l->Position() - relative_offset;
7276 assembler->AppendInt32(offset_to_block);
7277 }
7278 }
7279
7280 private:
7281 const HX86PackedSwitch* switch_instr_;
7282};
7283
7284void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7285 // Generate the constant area if needed.
7286 X86Assembler* assembler = GetAssembler();
7287 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7288 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7289 // byte values.
7290 assembler->Align(4, 0);
7291 constant_area_start_ = assembler->CodeSize();
7292
7293 // Populate any jump tables.
7294 for (auto jump_table : fixups_to_jump_tables_) {
7295 jump_table->CreateJumpTable();
7296 }
7297
7298 // And now add the constant area to the generated code.
7299 assembler->AddConstantArea();
7300 }
7301
7302 // And finish up.
7303 CodeGenerator::Finalize(allocator);
7304}
7305
Mark Mendell0616ae02015-04-17 12:49:27 -04007306Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7307 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7308 return Address(reg, kDummy32BitOffset, fixup);
7309}
7310
7311Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7312 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7313 return Address(reg, kDummy32BitOffset, fixup);
7314}
7315
7316Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7317 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7318 return Address(reg, kDummy32BitOffset, fixup);
7319}
7320
7321Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7322 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7323 return Address(reg, kDummy32BitOffset, fixup);
7324}
7325
Aart Bika19616e2016-02-01 18:57:58 -08007326void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7327 if (value == 0) {
7328 __ xorl(dest, dest);
7329 } else {
7330 __ movl(dest, Immediate(value));
7331 }
7332}
7333
7334void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7335 if (value == 0) {
7336 __ testl(dest, dest);
7337 } else {
7338 __ cmpl(dest, Immediate(value));
7339 }
7340}
7341
Mark Mendell805b3b52015-09-18 14:10:29 -04007342Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7343 Register reg,
7344 Register value) {
7345 // Create a fixup to be used to create and address the jump table.
7346 JumpTableRIPFixup* table_fixup =
7347 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7348
7349 // We have to populate the jump tables.
7350 fixups_to_jump_tables_.push_back(table_fixup);
7351
7352 // We want a scaled address, as we are extracting the correct offset from the table.
7353 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7354}
7355
Andreas Gampe85b62f22015-09-09 13:15:38 -07007356// TODO: target as memory.
7357void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7358 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007359 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007360 return;
7361 }
7362
7363 DCHECK_NE(type, Primitive::kPrimVoid);
7364
7365 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7366 if (target.Equals(return_loc)) {
7367 return;
7368 }
7369
7370 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7371 // with the else branch.
7372 if (type == Primitive::kPrimLong) {
7373 HParallelMove parallel_move(GetGraph()->GetArena());
7374 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7375 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7376 GetMoveResolver()->EmitNativeCode(&parallel_move);
7377 } else {
7378 // Let the parallel move resolver take care of all of this.
7379 HParallelMove parallel_move(GetGraph()->GetArena());
7380 parallel_move.AddMove(return_loc, target, type, nullptr);
7381 GetMoveResolver()->EmitNativeCode(&parallel_move);
7382 }
7383}
7384
Roland Levillain4d027112015-07-01 15:41:14 +01007385#undef __
7386
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007387} // namespace x86
7388} // namespace art