blob: 8c643a05c850d4ad85b13dfdb0108dfc92db34f6 [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>();
Mathieu Chartiere401d142015-04-22 13:56:20 -07002015 uint32_t method_offset = mirror::Class::EmbeddedImTableEntryOffset(
2016 invoke->GetImtIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002017 Location receiver = locations->InAt(0);
2018 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2019
Roland Levillain0d5a2812015-11-13 10:07:31 +00002020 // Set the hidden argument. This is safe to do this here, as XMM7
2021 // won't be modified thereafter, before the `call` instruction.
2022 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002023 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002024 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002026 if (receiver.IsStackSlot()) {
2027 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002028 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002029 __ movl(temp, Address(temp, class_offset));
2030 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002031 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002032 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002033 }
Roland Levillain4d027112015-07-01 15:41:14 +01002034 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002035 // Instead of simply (possibly) unpoisoning `temp` here, we should
2036 // emit a read barrier for the previous class reference load.
2037 // However this is not required in practice, as this is an
2038 // intermediate/temporary reference and because the current
2039 // concurrent copying collector keeps the from-space memory
2040 // intact/accessible until the end of the marking phase (the
2041 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002042 __ MaybeUnpoisonHeapReference(temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002043 // temp = temp->GetImtEntryAt(method_offset);
2044 __ movl(temp, Address(temp, method_offset));
2045 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002046 __ call(Address(temp,
2047 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002048
2049 DCHECK(!codegen_->IsLeafMethod());
2050 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2051}
2052
Roland Levillain88cb1752014-10-20 16:36:47 +01002053void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2054 LocationSummary* locations =
2055 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2056 switch (neg->GetResultType()) {
2057 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002058 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002059 locations->SetInAt(0, Location::RequiresRegister());
2060 locations->SetOut(Location::SameAsFirstInput());
2061 break;
2062
Roland Levillain88cb1752014-10-20 16:36:47 +01002063 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002064 locations->SetInAt(0, Location::RequiresFpuRegister());
2065 locations->SetOut(Location::SameAsFirstInput());
2066 locations->AddTemp(Location::RequiresRegister());
2067 locations->AddTemp(Location::RequiresFpuRegister());
2068 break;
2069
Roland Levillain88cb1752014-10-20 16:36:47 +01002070 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002071 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002072 locations->SetOut(Location::SameAsFirstInput());
2073 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002074 break;
2075
2076 default:
2077 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2078 }
2079}
2080
2081void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2082 LocationSummary* locations = neg->GetLocations();
2083 Location out = locations->Out();
2084 Location in = locations->InAt(0);
2085 switch (neg->GetResultType()) {
2086 case Primitive::kPrimInt:
2087 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002088 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002089 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002090 break;
2091
2092 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002093 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002094 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002095 __ negl(out.AsRegisterPairLow<Register>());
2096 // Negation is similar to subtraction from zero. The least
2097 // significant byte triggers a borrow when it is different from
2098 // zero; to take it into account, add 1 to the most significant
2099 // byte if the carry flag (CF) is set to 1 after the first NEGL
2100 // operation.
2101 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2102 __ negl(out.AsRegisterPairHigh<Register>());
2103 break;
2104
Roland Levillain5368c212014-11-27 15:03:41 +00002105 case Primitive::kPrimFloat: {
2106 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002107 Register constant = locations->GetTemp(0).AsRegister<Register>();
2108 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002109 // Implement float negation with an exclusive or with value
2110 // 0x80000000 (mask for bit 31, representing the sign of a
2111 // single-precision floating-point number).
2112 __ movl(constant, Immediate(INT32_C(0x80000000)));
2113 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002115 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002116 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002117
Roland Levillain5368c212014-11-27 15:03:41 +00002118 case Primitive::kPrimDouble: {
2119 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002121 // Implement double negation with an exclusive or with value
2122 // 0x8000000000000000 (mask for bit 63, representing the sign of
2123 // a double-precision floating-point number).
2124 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002125 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002126 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002127 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002128
2129 default:
2130 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2131 }
2132}
2133
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002134void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2135 LocationSummary* locations =
2136 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2137 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2138 locations->SetInAt(0, Location::RequiresFpuRegister());
2139 locations->SetInAt(1, Location::RequiresRegister());
2140 locations->SetOut(Location::SameAsFirstInput());
2141 locations->AddTemp(Location::RequiresFpuRegister());
2142}
2143
2144void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2145 LocationSummary* locations = neg->GetLocations();
2146 Location out = locations->Out();
2147 DCHECK(locations->InAt(0).Equals(out));
2148
2149 Register constant_area = locations->InAt(1).AsRegister<Register>();
2150 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2151 if (neg->GetType() == Primitive::kPrimFloat) {
2152 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2153 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2154 } else {
2155 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2156 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2157 }
2158}
2159
Roland Levillaindff1f282014-11-05 14:15:05 +00002160void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002161 Primitive::Type result_type = conversion->GetResultType();
2162 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002163 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002164
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002165 // The float-to-long and double-to-long type conversions rely on a
2166 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002167 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002168 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2169 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002170 ? LocationSummary::kCall
2171 : LocationSummary::kNoCall;
2172 LocationSummary* locations =
2173 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2174
David Brazdilb2bd1c52015-03-25 11:17:37 +00002175 // The Java language does not allow treating boolean as an integral type but
2176 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002177
Roland Levillaindff1f282014-11-05 14:15:05 +00002178 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002179 case Primitive::kPrimByte:
2180 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002181 case Primitive::kPrimLong: {
2182 // Type conversion from long to byte is a result of code transformations.
2183 HInstruction* input = conversion->InputAt(0);
2184 Location input_location = input->IsConstant()
2185 ? Location::ConstantLocation(input->AsConstant())
2186 : Location::RegisterPairLocation(EAX, EDX);
2187 locations->SetInAt(0, input_location);
2188 // Make the output overlap to please the register allocator. This greatly simplifies
2189 // the validation of the linear scan implementation
2190 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2191 break;
2192 }
David Brazdil46e2a392015-03-16 17:31:52 +00002193 case Primitive::kPrimBoolean:
2194 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002195 case Primitive::kPrimShort:
2196 case Primitive::kPrimInt:
2197 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002198 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002199 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2200 // Make the output overlap to please the register allocator. This greatly simplifies
2201 // the validation of the linear scan implementation
2202 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002203 break;
2204
2205 default:
2206 LOG(FATAL) << "Unexpected type conversion from " << input_type
2207 << " to " << result_type;
2208 }
2209 break;
2210
Roland Levillain01a8d712014-11-14 16:27:39 +00002211 case Primitive::kPrimShort:
2212 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002213 case Primitive::kPrimLong:
2214 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002215 case Primitive::kPrimBoolean:
2216 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002217 case Primitive::kPrimByte:
2218 case Primitive::kPrimInt:
2219 case Primitive::kPrimChar:
2220 // Processing a Dex `int-to-short' instruction.
2221 locations->SetInAt(0, Location::Any());
2222 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2223 break;
2224
2225 default:
2226 LOG(FATAL) << "Unexpected type conversion from " << input_type
2227 << " to " << result_type;
2228 }
2229 break;
2230
Roland Levillain946e1432014-11-11 17:35:19 +00002231 case Primitive::kPrimInt:
2232 switch (input_type) {
2233 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002234 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002235 locations->SetInAt(0, Location::Any());
2236 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2237 break;
2238
2239 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002240 // Processing a Dex `float-to-int' instruction.
2241 locations->SetInAt(0, Location::RequiresFpuRegister());
2242 locations->SetOut(Location::RequiresRegister());
2243 locations->AddTemp(Location::RequiresFpuRegister());
2244 break;
2245
Roland Levillain946e1432014-11-11 17:35:19 +00002246 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002247 // Processing a Dex `double-to-int' instruction.
2248 locations->SetInAt(0, Location::RequiresFpuRegister());
2249 locations->SetOut(Location::RequiresRegister());
2250 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002251 break;
2252
2253 default:
2254 LOG(FATAL) << "Unexpected type conversion from " << input_type
2255 << " to " << result_type;
2256 }
2257 break;
2258
Roland Levillaindff1f282014-11-05 14:15:05 +00002259 case Primitive::kPrimLong:
2260 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002261 case Primitive::kPrimBoolean:
2262 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002263 case Primitive::kPrimByte:
2264 case Primitive::kPrimShort:
2265 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002266 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002267 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002268 locations->SetInAt(0, Location::RegisterLocation(EAX));
2269 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2270 break;
2271
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002272 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002273 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002274 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002275 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002276 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2277 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2278
Vladimir Marko949c91f2015-01-27 10:48:44 +00002279 // The runtime helper puts the result in EAX, EDX.
2280 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002281 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002282 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002283
2284 default:
2285 LOG(FATAL) << "Unexpected type conversion from " << input_type
2286 << " to " << result_type;
2287 }
2288 break;
2289
Roland Levillain981e4542014-11-14 11:47:14 +00002290 case Primitive::kPrimChar:
2291 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002292 case Primitive::kPrimLong:
2293 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002294 case Primitive::kPrimBoolean:
2295 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002296 case Primitive::kPrimByte:
2297 case Primitive::kPrimShort:
2298 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002299 // Processing a Dex `int-to-char' instruction.
2300 locations->SetInAt(0, Location::Any());
2301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2302 break;
2303
2304 default:
2305 LOG(FATAL) << "Unexpected type conversion from " << input_type
2306 << " to " << result_type;
2307 }
2308 break;
2309
Roland Levillaindff1f282014-11-05 14:15:05 +00002310 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002311 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002312 case Primitive::kPrimBoolean:
2313 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002314 case Primitive::kPrimByte:
2315 case Primitive::kPrimShort:
2316 case Primitive::kPrimInt:
2317 case Primitive::kPrimChar:
2318 // Processing a Dex `int-to-float' instruction.
2319 locations->SetInAt(0, Location::RequiresRegister());
2320 locations->SetOut(Location::RequiresFpuRegister());
2321 break;
2322
2323 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002324 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002325 locations->SetInAt(0, Location::Any());
2326 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002327 break;
2328
Roland Levillaincff13742014-11-17 14:32:17 +00002329 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002330 // Processing a Dex `double-to-float' instruction.
2331 locations->SetInAt(0, Location::RequiresFpuRegister());
2332 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002333 break;
2334
2335 default:
2336 LOG(FATAL) << "Unexpected type conversion from " << input_type
2337 << " to " << result_type;
2338 };
2339 break;
2340
Roland Levillaindff1f282014-11-05 14:15:05 +00002341 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002342 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002343 case Primitive::kPrimBoolean:
2344 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002345 case Primitive::kPrimByte:
2346 case Primitive::kPrimShort:
2347 case Primitive::kPrimInt:
2348 case Primitive::kPrimChar:
2349 // Processing a Dex `int-to-double' instruction.
2350 locations->SetInAt(0, Location::RequiresRegister());
2351 locations->SetOut(Location::RequiresFpuRegister());
2352 break;
2353
2354 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002355 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002356 locations->SetInAt(0, Location::Any());
2357 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002358 break;
2359
Roland Levillaincff13742014-11-17 14:32:17 +00002360 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002361 // Processing a Dex `float-to-double' instruction.
2362 locations->SetInAt(0, Location::RequiresFpuRegister());
2363 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002364 break;
2365
2366 default:
2367 LOG(FATAL) << "Unexpected type conversion from " << input_type
2368 << " to " << result_type;
2369 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002370 break;
2371
2372 default:
2373 LOG(FATAL) << "Unexpected type conversion from " << input_type
2374 << " to " << result_type;
2375 }
2376}
2377
2378void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2379 LocationSummary* locations = conversion->GetLocations();
2380 Location out = locations->Out();
2381 Location in = locations->InAt(0);
2382 Primitive::Type result_type = conversion->GetResultType();
2383 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002384 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002385 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002386 case Primitive::kPrimByte:
2387 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002388 case Primitive::kPrimLong:
2389 // Type conversion from long to byte is a result of code transformations.
2390 if (in.IsRegisterPair()) {
2391 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2392 } else {
2393 DCHECK(in.GetConstant()->IsLongConstant());
2394 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2395 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2396 }
2397 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002398 case Primitive::kPrimBoolean:
2399 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002400 case Primitive::kPrimShort:
2401 case Primitive::kPrimInt:
2402 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002403 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002404 if (in.IsRegister()) {
2405 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002406 } else {
2407 DCHECK(in.GetConstant()->IsIntConstant());
2408 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2409 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2410 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002411 break;
2412
2413 default:
2414 LOG(FATAL) << "Unexpected type conversion from " << input_type
2415 << " to " << result_type;
2416 }
2417 break;
2418
Roland Levillain01a8d712014-11-14 16:27:39 +00002419 case Primitive::kPrimShort:
2420 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002421 case Primitive::kPrimLong:
2422 // Type conversion from long to short is a result of code transformations.
2423 if (in.IsRegisterPair()) {
2424 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2425 } else if (in.IsDoubleStackSlot()) {
2426 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2427 } else {
2428 DCHECK(in.GetConstant()->IsLongConstant());
2429 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2430 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2431 }
2432 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002433 case Primitive::kPrimBoolean:
2434 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002435 case Primitive::kPrimByte:
2436 case Primitive::kPrimInt:
2437 case Primitive::kPrimChar:
2438 // Processing a Dex `int-to-short' instruction.
2439 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002440 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002441 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002442 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002443 } else {
2444 DCHECK(in.GetConstant()->IsIntConstant());
2445 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002447 }
2448 break;
2449
2450 default:
2451 LOG(FATAL) << "Unexpected type conversion from " << input_type
2452 << " to " << result_type;
2453 }
2454 break;
2455
Roland Levillain946e1432014-11-11 17:35:19 +00002456 case Primitive::kPrimInt:
2457 switch (input_type) {
2458 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002459 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002460 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002461 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002462 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002463 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002464 } else {
2465 DCHECK(in.IsConstant());
2466 DCHECK(in.GetConstant()->IsLongConstant());
2467 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002469 }
2470 break;
2471
Roland Levillain3f8f9362014-12-02 17:45:01 +00002472 case Primitive::kPrimFloat: {
2473 // Processing a Dex `float-to-int' instruction.
2474 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2475 Register output = out.AsRegister<Register>();
2476 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002477 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002478
2479 __ movl(output, Immediate(kPrimIntMax));
2480 // temp = int-to-float(output)
2481 __ cvtsi2ss(temp, output);
2482 // if input >= temp goto done
2483 __ comiss(input, temp);
2484 __ j(kAboveEqual, &done);
2485 // if input == NaN goto nan
2486 __ j(kUnordered, &nan);
2487 // output = float-to-int-truncate(input)
2488 __ cvttss2si(output, input);
2489 __ jmp(&done);
2490 __ Bind(&nan);
2491 // output = 0
2492 __ xorl(output, output);
2493 __ Bind(&done);
2494 break;
2495 }
2496
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002497 case Primitive::kPrimDouble: {
2498 // Processing a Dex `double-to-int' instruction.
2499 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2500 Register output = out.AsRegister<Register>();
2501 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002502 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002503
2504 __ movl(output, Immediate(kPrimIntMax));
2505 // temp = int-to-double(output)
2506 __ cvtsi2sd(temp, output);
2507 // if input >= temp goto done
2508 __ comisd(input, temp);
2509 __ j(kAboveEqual, &done);
2510 // if input == NaN goto nan
2511 __ j(kUnordered, &nan);
2512 // output = double-to-int-truncate(input)
2513 __ cvttsd2si(output, input);
2514 __ jmp(&done);
2515 __ Bind(&nan);
2516 // output = 0
2517 __ xorl(output, output);
2518 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002519 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002520 }
Roland Levillain946e1432014-11-11 17:35:19 +00002521
2522 default:
2523 LOG(FATAL) << "Unexpected type conversion from " << input_type
2524 << " to " << result_type;
2525 }
2526 break;
2527
Roland Levillaindff1f282014-11-05 14:15:05 +00002528 case Primitive::kPrimLong:
2529 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002530 case Primitive::kPrimBoolean:
2531 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002532 case Primitive::kPrimByte:
2533 case Primitive::kPrimShort:
2534 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002535 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002536 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2538 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002539 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002540 __ cdq();
2541 break;
2542
2543 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002544 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002545 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2546 conversion,
2547 conversion->GetDexPc(),
2548 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002549 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002550 break;
2551
Roland Levillaindff1f282014-11-05 14:15:05 +00002552 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002553 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002554 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2555 conversion,
2556 conversion->GetDexPc(),
2557 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002558 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002559 break;
2560
2561 default:
2562 LOG(FATAL) << "Unexpected type conversion from " << input_type
2563 << " to " << result_type;
2564 }
2565 break;
2566
Roland Levillain981e4542014-11-14 11:47:14 +00002567 case Primitive::kPrimChar:
2568 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002569 case Primitive::kPrimLong:
2570 // Type conversion from long to short is a result of code transformations.
2571 if (in.IsRegisterPair()) {
2572 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2573 } else if (in.IsDoubleStackSlot()) {
2574 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2575 } else {
2576 DCHECK(in.GetConstant()->IsLongConstant());
2577 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2578 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2579 }
2580 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002581 case Primitive::kPrimBoolean:
2582 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002583 case Primitive::kPrimByte:
2584 case Primitive::kPrimShort:
2585 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002586 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2587 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002588 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002589 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002590 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002591 } else {
2592 DCHECK(in.GetConstant()->IsIntConstant());
2593 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002595 }
2596 break;
2597
2598 default:
2599 LOG(FATAL) << "Unexpected type conversion from " << input_type
2600 << " to " << result_type;
2601 }
2602 break;
2603
Roland Levillaindff1f282014-11-05 14:15:05 +00002604 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002605 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002606 case Primitive::kPrimBoolean:
2607 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002608 case Primitive::kPrimByte:
2609 case Primitive::kPrimShort:
2610 case Primitive::kPrimInt:
2611 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002612 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002613 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002614 break;
2615
Roland Levillain6d0e4832014-11-27 18:31:21 +00002616 case Primitive::kPrimLong: {
2617 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002618 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002619
Roland Levillain232ade02015-04-20 15:14:36 +01002620 // Create stack space for the call to
2621 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2622 // TODO: enhance register allocator to ask for stack temporaries.
2623 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2624 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2625 __ subl(ESP, Immediate(adjustment));
2626 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002627
Roland Levillain232ade02015-04-20 15:14:36 +01002628 // Load the value to the FP stack, using temporaries if needed.
2629 PushOntoFPStack(in, 0, adjustment, false, true);
2630
2631 if (out.IsStackSlot()) {
2632 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2633 } else {
2634 __ fstps(Address(ESP, 0));
2635 Location stack_temp = Location::StackSlot(0);
2636 codegen_->Move32(out, stack_temp);
2637 }
2638
2639 // Remove the temporary stack space we allocated.
2640 if (adjustment != 0) {
2641 __ addl(ESP, Immediate(adjustment));
2642 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002643 break;
2644 }
2645
Roland Levillaincff13742014-11-17 14:32:17 +00002646 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002647 // Processing a Dex `double-to-float' instruction.
2648 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002649 break;
2650
2651 default:
2652 LOG(FATAL) << "Unexpected type conversion from " << input_type
2653 << " to " << result_type;
2654 };
2655 break;
2656
Roland Levillaindff1f282014-11-05 14:15:05 +00002657 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002658 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002659 case Primitive::kPrimBoolean:
2660 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002661 case Primitive::kPrimByte:
2662 case Primitive::kPrimShort:
2663 case Primitive::kPrimInt:
2664 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002665 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002666 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002667 break;
2668
Roland Levillain647b9ed2014-11-27 12:06:00 +00002669 case Primitive::kPrimLong: {
2670 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002671 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002672
Roland Levillain232ade02015-04-20 15:14:36 +01002673 // Create stack space for the call to
2674 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2675 // TODO: enhance register allocator to ask for stack temporaries.
2676 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2677 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2678 __ subl(ESP, Immediate(adjustment));
2679 }
2680
2681 // Load the value to the FP stack, using temporaries if needed.
2682 PushOntoFPStack(in, 0, adjustment, false, true);
2683
2684 if (out.IsDoubleStackSlot()) {
2685 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2686 } else {
2687 __ fstpl(Address(ESP, 0));
2688 Location stack_temp = Location::DoubleStackSlot(0);
2689 codegen_->Move64(out, stack_temp);
2690 }
2691
2692 // Remove the temporary stack space we allocated.
2693 if (adjustment != 0) {
2694 __ addl(ESP, Immediate(adjustment));
2695 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002696 break;
2697 }
2698
Roland Levillaincff13742014-11-17 14:32:17 +00002699 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002700 // Processing a Dex `float-to-double' instruction.
2701 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002702 break;
2703
2704 default:
2705 LOG(FATAL) << "Unexpected type conversion from " << input_type
2706 << " to " << result_type;
2707 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002708 break;
2709
2710 default:
2711 LOG(FATAL) << "Unexpected type conversion from " << input_type
2712 << " to " << result_type;
2713 }
2714}
2715
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002716void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002717 LocationSummary* locations =
2718 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002719 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002720 case Primitive::kPrimInt: {
2721 locations->SetInAt(0, Location::RequiresRegister());
2722 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2724 break;
2725 }
2726
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002727 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002728 locations->SetInAt(0, Location::RequiresRegister());
2729 locations->SetInAt(1, Location::Any());
2730 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731 break;
2732 }
2733
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002734 case Primitive::kPrimFloat:
2735 case Primitive::kPrimDouble: {
2736 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002737 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2738 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002739 } else if (add->InputAt(1)->IsConstant()) {
2740 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002741 } else {
2742 locations->SetInAt(1, Location::Any());
2743 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002744 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002745 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002746 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002747
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002748 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002749 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2750 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002751 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002752}
2753
2754void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2755 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002756 Location first = locations->InAt(0);
2757 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002758 Location out = locations->Out();
2759
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002760 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002761 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002762 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002763 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2764 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002765 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2766 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002767 } else {
2768 __ leal(out.AsRegister<Register>(), Address(
2769 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2770 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002771 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002772 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2773 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2774 __ addl(out.AsRegister<Register>(), Immediate(value));
2775 } else {
2776 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2777 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002778 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002779 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002780 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002781 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002782 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002783 }
2784
2785 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002786 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002787 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2788 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002789 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002790 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2791 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002792 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002793 } else {
2794 DCHECK(second.IsConstant()) << second;
2795 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2796 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2797 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002798 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002799 break;
2800 }
2801
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002802 case Primitive::kPrimFloat: {
2803 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002804 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002805 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2806 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002807 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002808 __ addss(first.AsFpuRegister<XmmRegister>(),
2809 codegen_->LiteralFloatAddress(
2810 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2811 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2812 } else {
2813 DCHECK(second.IsStackSlot());
2814 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002815 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002816 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002817 }
2818
2819 case Primitive::kPrimDouble: {
2820 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002821 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002822 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2823 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002824 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002825 __ addsd(first.AsFpuRegister<XmmRegister>(),
2826 codegen_->LiteralDoubleAddress(
2827 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2828 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2829 } else {
2830 DCHECK(second.IsDoubleStackSlot());
2831 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002832 }
2833 break;
2834 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002835
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002836 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002837 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002838 }
2839}
2840
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002841void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002842 LocationSummary* locations =
2843 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002844 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002845 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002846 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002847 locations->SetInAt(0, Location::RequiresRegister());
2848 locations->SetInAt(1, Location::Any());
2849 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002850 break;
2851 }
Calin Juravle11351682014-10-23 15:38:15 +01002852 case Primitive::kPrimFloat:
2853 case Primitive::kPrimDouble: {
2854 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002855 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2856 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002857 } else if (sub->InputAt(1)->IsConstant()) {
2858 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002859 } else {
2860 locations->SetInAt(1, Location::Any());
2861 }
Calin Juravle11351682014-10-23 15:38:15 +01002862 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002863 break;
Calin Juravle11351682014-10-23 15:38:15 +01002864 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002865
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002866 default:
Calin Juravle11351682014-10-23 15:38:15 +01002867 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002868 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002869}
2870
2871void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2872 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002873 Location first = locations->InAt(0);
2874 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002875 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002876 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002877 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002878 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002879 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002880 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002881 __ subl(first.AsRegister<Register>(),
2882 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002883 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002884 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002885 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002886 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002887 }
2888
2889 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002890 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002891 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2892 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002893 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002894 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002895 __ sbbl(first.AsRegisterPairHigh<Register>(),
2896 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002897 } else {
2898 DCHECK(second.IsConstant()) << second;
2899 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2900 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2901 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002902 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002903 break;
2904 }
2905
Calin Juravle11351682014-10-23 15:38:15 +01002906 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002907 if (second.IsFpuRegister()) {
2908 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2909 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2910 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002911 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002912 __ subss(first.AsFpuRegister<XmmRegister>(),
2913 codegen_->LiteralFloatAddress(
2914 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2915 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2916 } else {
2917 DCHECK(second.IsStackSlot());
2918 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2919 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002920 break;
Calin Juravle11351682014-10-23 15:38:15 +01002921 }
2922
2923 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002924 if (second.IsFpuRegister()) {
2925 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2926 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2927 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002928 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002929 __ subsd(first.AsFpuRegister<XmmRegister>(),
2930 codegen_->LiteralDoubleAddress(
2931 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2932 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2933 } else {
2934 DCHECK(second.IsDoubleStackSlot());
2935 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2936 }
Calin Juravle11351682014-10-23 15:38:15 +01002937 break;
2938 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002939
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002940 default:
Calin Juravle11351682014-10-23 15:38:15 +01002941 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002942 }
2943}
2944
Calin Juravle34bacdf2014-10-07 20:23:36 +01002945void LocationsBuilderX86::VisitMul(HMul* mul) {
2946 LocationSummary* locations =
2947 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2948 switch (mul->GetResultType()) {
2949 case Primitive::kPrimInt:
2950 locations->SetInAt(0, Location::RequiresRegister());
2951 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002952 if (mul->InputAt(1)->IsIntConstant()) {
2953 // Can use 3 operand multiply.
2954 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2955 } else {
2956 locations->SetOut(Location::SameAsFirstInput());
2957 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002958 break;
2959 case Primitive::kPrimLong: {
2960 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002961 locations->SetInAt(1, Location::Any());
2962 locations->SetOut(Location::SameAsFirstInput());
2963 // Needed for imul on 32bits with 64bits output.
2964 locations->AddTemp(Location::RegisterLocation(EAX));
2965 locations->AddTemp(Location::RegisterLocation(EDX));
2966 break;
2967 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002968 case Primitive::kPrimFloat:
2969 case Primitive::kPrimDouble: {
2970 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002971 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2972 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002973 } else if (mul->InputAt(1)->IsConstant()) {
2974 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002975 } else {
2976 locations->SetInAt(1, Location::Any());
2977 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002978 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002979 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002980 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002981
2982 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002983 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002984 }
2985}
2986
2987void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2988 LocationSummary* locations = mul->GetLocations();
2989 Location first = locations->InAt(0);
2990 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002991 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002992
2993 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002994 case Primitive::kPrimInt:
2995 // The constant may have ended up in a register, so test explicitly to avoid
2996 // problems where the output may not be the same as the first operand.
2997 if (mul->InputAt(1)->IsIntConstant()) {
2998 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
2999 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3000 } else if (second.IsRegister()) {
3001 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003002 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003003 } else {
3004 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003005 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007 }
3008 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003009
3010 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003011 Register in1_hi = first.AsRegisterPairHigh<Register>();
3012 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003013 Register eax = locations->GetTemp(0).AsRegister<Register>();
3014 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003015
3016 DCHECK_EQ(EAX, eax);
3017 DCHECK_EQ(EDX, edx);
3018
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003019 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003020 // output: in1
3021 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3022 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3023 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003024 if (second.IsConstant()) {
3025 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003026
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003027 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3028 int32_t low_value = Low32Bits(value);
3029 int32_t high_value = High32Bits(value);
3030 Immediate low(low_value);
3031 Immediate high(high_value);
3032
3033 __ movl(eax, high);
3034 // eax <- in1.lo * in2.hi
3035 __ imull(eax, in1_lo);
3036 // in1.hi <- in1.hi * in2.lo
3037 __ imull(in1_hi, low);
3038 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3039 __ addl(in1_hi, eax);
3040 // move in2_lo to eax to prepare for double precision
3041 __ movl(eax, low);
3042 // edx:eax <- in1.lo * in2.lo
3043 __ mull(in1_lo);
3044 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3045 __ addl(in1_hi, edx);
3046 // in1.lo <- (in1.lo * in2.lo)[31:0];
3047 __ movl(in1_lo, eax);
3048 } else if (second.IsRegisterPair()) {
3049 Register in2_hi = second.AsRegisterPairHigh<Register>();
3050 Register in2_lo = second.AsRegisterPairLow<Register>();
3051
3052 __ movl(eax, in2_hi);
3053 // eax <- in1.lo * in2.hi
3054 __ imull(eax, in1_lo);
3055 // in1.hi <- in1.hi * in2.lo
3056 __ imull(in1_hi, in2_lo);
3057 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3058 __ addl(in1_hi, eax);
3059 // move in1_lo to eax to prepare for double precision
3060 __ movl(eax, in1_lo);
3061 // edx:eax <- in1.lo * in2.lo
3062 __ mull(in2_lo);
3063 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3064 __ addl(in1_hi, edx);
3065 // in1.lo <- (in1.lo * in2.lo)[31:0];
3066 __ movl(in1_lo, eax);
3067 } else {
3068 DCHECK(second.IsDoubleStackSlot()) << second;
3069 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3070 Address in2_lo(ESP, second.GetStackIndex());
3071
3072 __ movl(eax, in2_hi);
3073 // eax <- in1.lo * in2.hi
3074 __ imull(eax, in1_lo);
3075 // in1.hi <- in1.hi * in2.lo
3076 __ imull(in1_hi, in2_lo);
3077 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3078 __ addl(in1_hi, eax);
3079 // move in1_lo to eax to prepare for double precision
3080 __ movl(eax, in1_lo);
3081 // edx:eax <- in1.lo * in2.lo
3082 __ mull(in2_lo);
3083 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3084 __ addl(in1_hi, edx);
3085 // in1.lo <- (in1.lo * in2.lo)[31:0];
3086 __ movl(in1_lo, eax);
3087 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003088
3089 break;
3090 }
3091
Calin Juravleb5bfa962014-10-21 18:02:24 +01003092 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003093 DCHECK(first.Equals(locations->Out()));
3094 if (second.IsFpuRegister()) {
3095 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3096 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3097 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003098 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003099 __ mulss(first.AsFpuRegister<XmmRegister>(),
3100 codegen_->LiteralFloatAddress(
3101 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3102 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3103 } else {
3104 DCHECK(second.IsStackSlot());
3105 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3106 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003107 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003108 }
3109
3110 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003111 DCHECK(first.Equals(locations->Out()));
3112 if (second.IsFpuRegister()) {
3113 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3114 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3115 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003116 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003117 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3118 codegen_->LiteralDoubleAddress(
3119 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3120 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3121 } else {
3122 DCHECK(second.IsDoubleStackSlot());
3123 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3124 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003125 break;
3126 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003127
3128 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003129 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003130 }
3131}
3132
Roland Levillain232ade02015-04-20 15:14:36 +01003133void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3134 uint32_t temp_offset,
3135 uint32_t stack_adjustment,
3136 bool is_fp,
3137 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003138 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003139 DCHECK(!is_wide);
3140 if (is_fp) {
3141 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3142 } else {
3143 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3144 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003145 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003146 DCHECK(is_wide);
3147 if (is_fp) {
3148 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3149 } else {
3150 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3151 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003152 } else {
3153 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003154 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003155 Location stack_temp = Location::StackSlot(temp_offset);
3156 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003157 if (is_fp) {
3158 __ flds(Address(ESP, temp_offset));
3159 } else {
3160 __ filds(Address(ESP, temp_offset));
3161 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003162 } else {
3163 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3164 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003165 if (is_fp) {
3166 __ fldl(Address(ESP, temp_offset));
3167 } else {
3168 __ fildl(Address(ESP, temp_offset));
3169 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003170 }
3171 }
3172}
3173
3174void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3175 Primitive::Type type = rem->GetResultType();
3176 bool is_float = type == Primitive::kPrimFloat;
3177 size_t elem_size = Primitive::ComponentSize(type);
3178 LocationSummary* locations = rem->GetLocations();
3179 Location first = locations->InAt(0);
3180 Location second = locations->InAt(1);
3181 Location out = locations->Out();
3182
3183 // Create stack space for 2 elements.
3184 // TODO: enhance register allocator to ask for stack temporaries.
3185 __ subl(ESP, Immediate(2 * elem_size));
3186
3187 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003188 const bool is_wide = !is_float;
3189 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3190 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003191
3192 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003193 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003194 __ Bind(&retry);
3195 __ fprem();
3196
3197 // Move FP status to AX.
3198 __ fstsw();
3199
3200 // And see if the argument reduction is complete. This is signaled by the
3201 // C2 FPU flag bit set to 0.
3202 __ andl(EAX, Immediate(kC2ConditionMask));
3203 __ j(kNotEqual, &retry);
3204
3205 // We have settled on the final value. Retrieve it into an XMM register.
3206 // Store FP top of stack to real stack.
3207 if (is_float) {
3208 __ fsts(Address(ESP, 0));
3209 } else {
3210 __ fstl(Address(ESP, 0));
3211 }
3212
3213 // Pop the 2 items from the FP stack.
3214 __ fucompp();
3215
3216 // Load the value from the stack into an XMM register.
3217 DCHECK(out.IsFpuRegister()) << out;
3218 if (is_float) {
3219 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3220 } else {
3221 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3222 }
3223
3224 // And remove the temporary stack space we allocated.
3225 __ addl(ESP, Immediate(2 * elem_size));
3226}
3227
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003228
3229void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3230 DCHECK(instruction->IsDiv() || instruction->IsRem());
3231
3232 LocationSummary* locations = instruction->GetLocations();
3233 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003234 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003235
3236 Register out_register = locations->Out().AsRegister<Register>();
3237 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003238 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003239
3240 DCHECK(imm == 1 || imm == -1);
3241
3242 if (instruction->IsRem()) {
3243 __ xorl(out_register, out_register);
3244 } else {
3245 __ movl(out_register, input_register);
3246 if (imm == -1) {
3247 __ negl(out_register);
3248 }
3249 }
3250}
3251
3252
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003253void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003254 LocationSummary* locations = instruction->GetLocations();
3255
3256 Register out_register = locations->Out().AsRegister<Register>();
3257 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003258 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003259 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3260 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003261
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003262 Register num = locations->GetTemp(0).AsRegister<Register>();
3263
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003264 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003265 __ testl(input_register, input_register);
3266 __ cmovl(kGreaterEqual, num, input_register);
3267 int shift = CTZ(imm);
3268 __ sarl(num, Immediate(shift));
3269
3270 if (imm < 0) {
3271 __ negl(num);
3272 }
3273
3274 __ movl(out_register, num);
3275}
3276
3277void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3278 DCHECK(instruction->IsDiv() || instruction->IsRem());
3279
3280 LocationSummary* locations = instruction->GetLocations();
3281 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3282
3283 Register eax = locations->InAt(0).AsRegister<Register>();
3284 Register out = locations->Out().AsRegister<Register>();
3285 Register num;
3286 Register edx;
3287
3288 if (instruction->IsDiv()) {
3289 edx = locations->GetTemp(0).AsRegister<Register>();
3290 num = locations->GetTemp(1).AsRegister<Register>();
3291 } else {
3292 edx = locations->Out().AsRegister<Register>();
3293 num = locations->GetTemp(0).AsRegister<Register>();
3294 }
3295
3296 DCHECK_EQ(EAX, eax);
3297 DCHECK_EQ(EDX, edx);
3298 if (instruction->IsDiv()) {
3299 DCHECK_EQ(EAX, out);
3300 } else {
3301 DCHECK_EQ(EDX, out);
3302 }
3303
3304 int64_t magic;
3305 int shift;
3306 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3307
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003308 // Save the numerator.
3309 __ movl(num, eax);
3310
3311 // EAX = magic
3312 __ movl(eax, Immediate(magic));
3313
3314 // EDX:EAX = magic * numerator
3315 __ imull(num);
3316
3317 if (imm > 0 && magic < 0) {
3318 // EDX += num
3319 __ addl(edx, num);
3320 } else if (imm < 0 && magic > 0) {
3321 __ subl(edx, num);
3322 }
3323
3324 // Shift if needed.
3325 if (shift != 0) {
3326 __ sarl(edx, Immediate(shift));
3327 }
3328
3329 // EDX += 1 if EDX < 0
3330 __ movl(eax, edx);
3331 __ shrl(edx, Immediate(31));
3332 __ addl(edx, eax);
3333
3334 if (instruction->IsRem()) {
3335 __ movl(eax, num);
3336 __ imull(edx, Immediate(imm));
3337 __ subl(eax, edx);
3338 __ movl(edx, eax);
3339 } else {
3340 __ movl(eax, edx);
3341 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003342}
3343
Calin Juravlebacfec32014-11-14 15:54:36 +00003344void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3345 DCHECK(instruction->IsDiv() || instruction->IsRem());
3346
3347 LocationSummary* locations = instruction->GetLocations();
3348 Location out = locations->Out();
3349 Location first = locations->InAt(0);
3350 Location second = locations->InAt(1);
3351 bool is_div = instruction->IsDiv();
3352
3353 switch (instruction->GetResultType()) {
3354 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003355 DCHECK_EQ(EAX, first.AsRegister<Register>());
3356 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003357
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003358 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003359 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003360
3361 if (imm == 0) {
3362 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3363 } else if (imm == 1 || imm == -1) {
3364 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003365 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003366 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003367 } else {
3368 DCHECK(imm <= -2 || imm >= 2);
3369 GenerateDivRemWithAnyConstant(instruction);
3370 }
3371 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003372 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3373 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003374 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003375
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003376 Register second_reg = second.AsRegister<Register>();
3377 // 0x80000000/-1 triggers an arithmetic exception!
3378 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3379 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003380
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003381 __ cmpl(second_reg, Immediate(-1));
3382 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003383
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003384 // edx:eax <- sign-extended of eax
3385 __ cdq();
3386 // eax = quotient, edx = remainder
3387 __ idivl(second_reg);
3388 __ Bind(slow_path->GetExitLabel());
3389 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003390 break;
3391 }
3392
3393 case Primitive::kPrimLong: {
3394 InvokeRuntimeCallingConvention calling_convention;
3395 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3396 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3397 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3398 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3399 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3400 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3401
3402 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003403 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3404 instruction,
3405 instruction->GetDexPc(),
3406 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003407 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003408 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003409 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3410 instruction,
3411 instruction->GetDexPc(),
3412 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003413 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003414 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003415 break;
3416 }
3417
3418 default:
3419 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3420 }
3421}
3422
Calin Juravle7c4954d2014-10-28 16:57:40 +00003423void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003424 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003425 ? LocationSummary::kCall
3426 : LocationSummary::kNoCall;
3427 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3428
Calin Juravle7c4954d2014-10-28 16:57:40 +00003429 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003430 case Primitive::kPrimInt: {
3431 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003432 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003433 locations->SetOut(Location::SameAsFirstInput());
3434 // Intel uses edx:eax as the dividend.
3435 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003436 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3437 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3438 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003439 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440 locations->AddTemp(Location::RequiresRegister());
3441 }
Calin Juravled0d48522014-11-04 16:40:20 +00003442 break;
3443 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003444 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003445 InvokeRuntimeCallingConvention calling_convention;
3446 locations->SetInAt(0, Location::RegisterPairLocation(
3447 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3448 locations->SetInAt(1, Location::RegisterPairLocation(
3449 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3450 // Runtime helper puts the result in EAX, EDX.
3451 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003452 break;
3453 }
3454 case Primitive::kPrimFloat:
3455 case Primitive::kPrimDouble: {
3456 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003457 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3458 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003459 } else if (div->InputAt(1)->IsConstant()) {
3460 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003461 } else {
3462 locations->SetInAt(1, Location::Any());
3463 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003464 locations->SetOut(Location::SameAsFirstInput());
3465 break;
3466 }
3467
3468 default:
3469 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3470 }
3471}
3472
3473void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3474 LocationSummary* locations = div->GetLocations();
3475 Location first = locations->InAt(0);
3476 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477
3478 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003479 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003480 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003481 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003482 break;
3483 }
3484
3485 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003486 if (second.IsFpuRegister()) {
3487 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3488 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3489 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003490 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003491 __ divss(first.AsFpuRegister<XmmRegister>(),
3492 codegen_->LiteralFloatAddress(
3493 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3494 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3495 } else {
3496 DCHECK(second.IsStackSlot());
3497 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3498 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003499 break;
3500 }
3501
3502 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003503 if (second.IsFpuRegister()) {
3504 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3505 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3506 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003507 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003508 __ divsd(first.AsFpuRegister<XmmRegister>(),
3509 codegen_->LiteralDoubleAddress(
3510 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3511 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3512 } else {
3513 DCHECK(second.IsDoubleStackSlot());
3514 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3515 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003516 break;
3517 }
3518
3519 default:
3520 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3521 }
3522}
3523
Calin Juravlebacfec32014-11-14 15:54:36 +00003524void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003525 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003526
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003527 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3528 ? LocationSummary::kCall
3529 : LocationSummary::kNoCall;
3530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003531
Calin Juravled2ec87d2014-12-08 14:24:46 +00003532 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003533 case Primitive::kPrimInt: {
3534 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003535 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003536 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003537 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3538 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3539 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003540 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 locations->AddTemp(Location::RequiresRegister());
3542 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 break;
3544 }
3545 case Primitive::kPrimLong: {
3546 InvokeRuntimeCallingConvention calling_convention;
3547 locations->SetInAt(0, Location::RegisterPairLocation(
3548 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3549 locations->SetInAt(1, Location::RegisterPairLocation(
3550 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3551 // Runtime helper puts the result in EAX, EDX.
3552 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3553 break;
3554 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003555 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003556 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003557 locations->SetInAt(0, Location::Any());
3558 locations->SetInAt(1, Location::Any());
3559 locations->SetOut(Location::RequiresFpuRegister());
3560 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003561 break;
3562 }
3563
3564 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003565 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003566 }
3567}
3568
3569void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3570 Primitive::Type type = rem->GetResultType();
3571 switch (type) {
3572 case Primitive::kPrimInt:
3573 case Primitive::kPrimLong: {
3574 GenerateDivRemIntegral(rem);
3575 break;
3576 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003577 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003578 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003579 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003580 break;
3581 }
3582 default:
3583 LOG(FATAL) << "Unexpected rem type " << type;
3584 }
3585}
3586
Calin Juravled0d48522014-11-04 16:40:20 +00003587void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003588 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3589 ? LocationSummary::kCallOnSlowPath
3590 : LocationSummary::kNoCall;
3591 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003592 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003593 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003594 case Primitive::kPrimByte:
3595 case Primitive::kPrimChar:
3596 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003597 case Primitive::kPrimInt: {
3598 locations->SetInAt(0, Location::Any());
3599 break;
3600 }
3601 case Primitive::kPrimLong: {
3602 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3603 if (!instruction->IsConstant()) {
3604 locations->AddTemp(Location::RequiresRegister());
3605 }
3606 break;
3607 }
3608 default:
3609 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3610 }
Calin Juravled0d48522014-11-04 16:40:20 +00003611 if (instruction->HasUses()) {
3612 locations->SetOut(Location::SameAsFirstInput());
3613 }
3614}
3615
3616void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003617 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003618 codegen_->AddSlowPath(slow_path);
3619
3620 LocationSummary* locations = instruction->GetLocations();
3621 Location value = locations->InAt(0);
3622
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003623 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003624 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003625 case Primitive::kPrimByte:
3626 case Primitive::kPrimChar:
3627 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003628 case Primitive::kPrimInt: {
3629 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003630 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003631 __ j(kEqual, slow_path->GetEntryLabel());
3632 } else if (value.IsStackSlot()) {
3633 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3634 __ j(kEqual, slow_path->GetEntryLabel());
3635 } else {
3636 DCHECK(value.IsConstant()) << value;
3637 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3638 __ jmp(slow_path->GetEntryLabel());
3639 }
3640 }
3641 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003642 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003643 case Primitive::kPrimLong: {
3644 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003645 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003646 __ movl(temp, value.AsRegisterPairLow<Register>());
3647 __ orl(temp, value.AsRegisterPairHigh<Register>());
3648 __ j(kEqual, slow_path->GetEntryLabel());
3649 } else {
3650 DCHECK(value.IsConstant()) << value;
3651 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3652 __ jmp(slow_path->GetEntryLabel());
3653 }
3654 }
3655 break;
3656 }
3657 default:
3658 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003659 }
Calin Juravled0d48522014-11-04 16:40:20 +00003660}
3661
Calin Juravle9aec02f2014-11-18 23:06:35 +00003662void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3663 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3664
3665 LocationSummary* locations =
3666 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3667
3668 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003669 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003670 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003671 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003672 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003673 // The shift count needs to be in CL or a constant.
3674 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003675 locations->SetOut(Location::SameAsFirstInput());
3676 break;
3677 }
3678 default:
3679 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3680 }
3681}
3682
3683void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3684 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3685
3686 LocationSummary* locations = op->GetLocations();
3687 Location first = locations->InAt(0);
3688 Location second = locations->InAt(1);
3689 DCHECK(first.Equals(locations->Out()));
3690
3691 switch (op->GetResultType()) {
3692 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003693 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003694 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003695 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003696 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003697 DCHECK_EQ(ECX, second_reg);
3698 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003699 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003700 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003701 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003702 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003703 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003704 }
3705 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003706 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003707 if (shift == 0) {
3708 return;
3709 }
3710 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003711 if (op->IsShl()) {
3712 __ shll(first_reg, imm);
3713 } else if (op->IsShr()) {
3714 __ sarl(first_reg, imm);
3715 } else {
3716 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717 }
3718 }
3719 break;
3720 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003721 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003722 if (second.IsRegister()) {
3723 Register second_reg = second.AsRegister<Register>();
3724 DCHECK_EQ(ECX, second_reg);
3725 if (op->IsShl()) {
3726 GenerateShlLong(first, second_reg);
3727 } else if (op->IsShr()) {
3728 GenerateShrLong(first, second_reg);
3729 } else {
3730 GenerateUShrLong(first, second_reg);
3731 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003732 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003733 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003734 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003735 // Nothing to do if the shift is 0, as the input is already the output.
3736 if (shift != 0) {
3737 if (op->IsShl()) {
3738 GenerateShlLong(first, shift);
3739 } else if (op->IsShr()) {
3740 GenerateShrLong(first, shift);
3741 } else {
3742 GenerateUShrLong(first, shift);
3743 }
3744 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003745 }
3746 break;
3747 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003748 default:
3749 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3750 }
3751}
3752
Mark P Mendell73945692015-04-29 14:56:17 +00003753void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3754 Register low = loc.AsRegisterPairLow<Register>();
3755 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003756 if (shift == 1) {
3757 // This is just an addition.
3758 __ addl(low, low);
3759 __ adcl(high, high);
3760 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003761 // Shift by 32 is easy. High gets low, and low gets 0.
3762 codegen_->EmitParallelMoves(
3763 loc.ToLow(),
3764 loc.ToHigh(),
3765 Primitive::kPrimInt,
3766 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3767 loc.ToLow(),
3768 Primitive::kPrimInt);
3769 } else if (shift > 32) {
3770 // Low part becomes 0. High part is low part << (shift-32).
3771 __ movl(high, low);
3772 __ shll(high, Immediate(shift - 32));
3773 __ xorl(low, low);
3774 } else {
3775 // Between 1 and 31.
3776 __ shld(high, low, Immediate(shift));
3777 __ shll(low, Immediate(shift));
3778 }
3779}
3780
Calin Juravle9aec02f2014-11-18 23:06:35 +00003781void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003782 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003783 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3784 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3785 __ testl(shifter, Immediate(32));
3786 __ j(kEqual, &done);
3787 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3788 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3789 __ Bind(&done);
3790}
3791
Mark P Mendell73945692015-04-29 14:56:17 +00003792void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3793 Register low = loc.AsRegisterPairLow<Register>();
3794 Register high = loc.AsRegisterPairHigh<Register>();
3795 if (shift == 32) {
3796 // Need to copy the sign.
3797 DCHECK_NE(low, high);
3798 __ movl(low, high);
3799 __ sarl(high, Immediate(31));
3800 } else if (shift > 32) {
3801 DCHECK_NE(low, high);
3802 // High part becomes sign. Low part is shifted by shift - 32.
3803 __ movl(low, high);
3804 __ sarl(high, Immediate(31));
3805 __ sarl(low, Immediate(shift - 32));
3806 } else {
3807 // Between 1 and 31.
3808 __ shrd(low, high, Immediate(shift));
3809 __ sarl(high, Immediate(shift));
3810 }
3811}
3812
Calin Juravle9aec02f2014-11-18 23:06:35 +00003813void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003814 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003815 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3816 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3817 __ testl(shifter, Immediate(32));
3818 __ j(kEqual, &done);
3819 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3820 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3821 __ Bind(&done);
3822}
3823
Mark P Mendell73945692015-04-29 14:56:17 +00003824void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3825 Register low = loc.AsRegisterPairLow<Register>();
3826 Register high = loc.AsRegisterPairHigh<Register>();
3827 if (shift == 32) {
3828 // Shift by 32 is easy. Low gets high, and high gets 0.
3829 codegen_->EmitParallelMoves(
3830 loc.ToHigh(),
3831 loc.ToLow(),
3832 Primitive::kPrimInt,
3833 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3834 loc.ToHigh(),
3835 Primitive::kPrimInt);
3836 } else if (shift > 32) {
3837 // Low part is high >> (shift - 32). High part becomes 0.
3838 __ movl(low, high);
3839 __ shrl(low, Immediate(shift - 32));
3840 __ xorl(high, high);
3841 } else {
3842 // Between 1 and 31.
3843 __ shrd(low, high, Immediate(shift));
3844 __ shrl(high, Immediate(shift));
3845 }
3846}
3847
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003849 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003850 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3851 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3852 __ testl(shifter, Immediate(32));
3853 __ j(kEqual, &done);
3854 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3855 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3856 __ Bind(&done);
3857}
3858
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003859void LocationsBuilderX86::VisitRor(HRor* ror) {
3860 LocationSummary* locations =
3861 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3862
3863 switch (ror->GetResultType()) {
3864 case Primitive::kPrimLong:
3865 // Add the temporary needed.
3866 locations->AddTemp(Location::RequiresRegister());
3867 FALLTHROUGH_INTENDED;
3868 case Primitive::kPrimInt:
3869 locations->SetInAt(0, Location::RequiresRegister());
3870 // The shift count needs to be in CL (unless it is a constant).
3871 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3872 locations->SetOut(Location::SameAsFirstInput());
3873 break;
3874 default:
3875 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3876 UNREACHABLE();
3877 }
3878}
3879
3880void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3881 LocationSummary* locations = ror->GetLocations();
3882 Location first = locations->InAt(0);
3883 Location second = locations->InAt(1);
3884
3885 if (ror->GetResultType() == Primitive::kPrimInt) {
3886 Register first_reg = first.AsRegister<Register>();
3887 if (second.IsRegister()) {
3888 Register second_reg = second.AsRegister<Register>();
3889 __ rorl(first_reg, second_reg);
3890 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003891 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003892 __ rorl(first_reg, imm);
3893 }
3894 return;
3895 }
3896
3897 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3898 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3899 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3900 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3901 if (second.IsRegister()) {
3902 Register second_reg = second.AsRegister<Register>();
3903 DCHECK_EQ(second_reg, ECX);
3904 __ movl(temp_reg, first_reg_hi);
3905 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3906 __ shrd(first_reg_lo, temp_reg, second_reg);
3907 __ movl(temp_reg, first_reg_hi);
3908 __ testl(second_reg, Immediate(32));
3909 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3910 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3911 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003912 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003913 if (shift_amt == 0) {
3914 // Already fine.
3915 return;
3916 }
3917 if (shift_amt == 32) {
3918 // Just swap.
3919 __ movl(temp_reg, first_reg_lo);
3920 __ movl(first_reg_lo, first_reg_hi);
3921 __ movl(first_reg_hi, temp_reg);
3922 return;
3923 }
3924
3925 Immediate imm(shift_amt);
3926 // Save the constents of the low value.
3927 __ movl(temp_reg, first_reg_lo);
3928
3929 // Shift right into low, feeding bits from high.
3930 __ shrd(first_reg_lo, first_reg_hi, imm);
3931
3932 // Shift right into high, feeding bits from the original low.
3933 __ shrd(first_reg_hi, temp_reg, imm);
3934
3935 // Swap if needed.
3936 if (shift_amt > 32) {
3937 __ movl(temp_reg, first_reg_lo);
3938 __ movl(first_reg_lo, first_reg_hi);
3939 __ movl(first_reg_hi, temp_reg);
3940 }
3941 }
3942}
3943
Calin Juravle9aec02f2014-11-18 23:06:35 +00003944void LocationsBuilderX86::VisitShl(HShl* shl) {
3945 HandleShift(shl);
3946}
3947
3948void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3949 HandleShift(shl);
3950}
3951
3952void LocationsBuilderX86::VisitShr(HShr* shr) {
3953 HandleShift(shr);
3954}
3955
3956void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3957 HandleShift(shr);
3958}
3959
3960void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3961 HandleShift(ushr);
3962}
3963
3964void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3965 HandleShift(ushr);
3966}
3967
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003968void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003969 LocationSummary* locations =
3970 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003971 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003972 if (instruction->IsStringAlloc()) {
3973 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3974 } else {
3975 InvokeRuntimeCallingConvention calling_convention;
3976 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3977 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3978 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003979}
3980
3981void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003982 // Note: if heap poisoning is enabled, the entry point takes cares
3983 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003984 if (instruction->IsStringAlloc()) {
3985 // String is allocated through StringFactory. Call NewEmptyString entry point.
3986 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3987 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3988 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3989 __ call(Address(temp, code_offset.Int32Value()));
3990 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3991 } else {
3992 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3993 instruction,
3994 instruction->GetDexPc(),
3995 nullptr);
3996 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
3997 DCHECK(!codegen_->IsLeafMethod());
3998 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003999}
4000
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004001void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4002 LocationSummary* locations =
4003 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4004 locations->SetOut(Location::RegisterLocation(EAX));
4005 InvokeRuntimeCallingConvention calling_convention;
4006 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004007 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004008 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004009}
4010
4011void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4012 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004013 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004014 // Note: if heap poisoning is enabled, the entry point takes cares
4015 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004016 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4017 instruction,
4018 instruction->GetDexPc(),
4019 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004020 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004021 DCHECK(!codegen_->IsLeafMethod());
4022}
4023
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004024void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004025 LocationSummary* locations =
4026 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004027 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4028 if (location.IsStackSlot()) {
4029 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4030 } else if (location.IsDoubleStackSlot()) {
4031 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004032 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004033 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004034}
4035
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004036void InstructionCodeGeneratorX86::VisitParameterValue(
4037 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4038}
4039
4040void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4041 LocationSummary* locations =
4042 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4043 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4044}
4045
4046void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004047}
4048
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004049void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4050 LocationSummary* locations =
4051 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4052 locations->SetInAt(0, Location::RequiresRegister());
4053 locations->SetOut(Location::RequiresRegister());
4054}
4055
4056void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4057 LocationSummary* locations = instruction->GetLocations();
4058 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004059 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004060 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4061 instruction->GetIndex(), kX86PointerSize).SizeValue();
4062 } else {
4063 method_offset = mirror::Class::EmbeddedImTableEntryOffset(
4064 instruction->GetIndex() % mirror::Class::kImtSize, kX86PointerSize).Uint32Value();
4065 }
4066 __ movl(locations->Out().AsRegister<Register>(),
4067 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4068}
4069
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004070void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004071 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004072 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004073 locations->SetInAt(0, Location::RequiresRegister());
4074 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004075}
4076
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004077void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4078 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004079 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004080 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004081 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004082 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004083 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004084 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004085 break;
4086
4087 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004088 __ notl(out.AsRegisterPairLow<Register>());
4089 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004090 break;
4091
4092 default:
4093 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4094 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004095}
4096
David Brazdil66d126e2015-04-03 16:02:44 +01004097void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4098 LocationSummary* locations =
4099 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4100 locations->SetInAt(0, Location::RequiresRegister());
4101 locations->SetOut(Location::SameAsFirstInput());
4102}
4103
4104void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004105 LocationSummary* locations = bool_not->GetLocations();
4106 Location in = locations->InAt(0);
4107 Location out = locations->Out();
4108 DCHECK(in.Equals(out));
4109 __ xorl(out.AsRegister<Register>(), Immediate(1));
4110}
4111
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004112void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004113 LocationSummary* locations =
4114 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004115 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004116 case Primitive::kPrimBoolean:
4117 case Primitive::kPrimByte:
4118 case Primitive::kPrimShort:
4119 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004120 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004121 case Primitive::kPrimLong: {
4122 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004123 locations->SetInAt(1, Location::Any());
4124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4125 break;
4126 }
4127 case Primitive::kPrimFloat:
4128 case Primitive::kPrimDouble: {
4129 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004130 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4131 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4132 } else if (compare->InputAt(1)->IsConstant()) {
4133 locations->SetInAt(1, Location::RequiresFpuRegister());
4134 } else {
4135 locations->SetInAt(1, Location::Any());
4136 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004137 locations->SetOut(Location::RequiresRegister());
4138 break;
4139 }
4140 default:
4141 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4142 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004143}
4144
4145void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004146 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004147 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004148 Location left = locations->InAt(0);
4149 Location right = locations->InAt(1);
4150
Mark Mendell0c9497d2015-08-21 09:30:05 -04004151 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004152 Condition less_cond = kLess;
4153
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004154 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004155 case Primitive::kPrimBoolean:
4156 case Primitive::kPrimByte:
4157 case Primitive::kPrimShort:
4158 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004159 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004160 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004161 break;
4162 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004163 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004164 Register left_low = left.AsRegisterPairLow<Register>();
4165 Register left_high = left.AsRegisterPairHigh<Register>();
4166 int32_t val_low = 0;
4167 int32_t val_high = 0;
4168 bool right_is_const = false;
4169
4170 if (right.IsConstant()) {
4171 DCHECK(right.GetConstant()->IsLongConstant());
4172 right_is_const = true;
4173 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4174 val_low = Low32Bits(val);
4175 val_high = High32Bits(val);
4176 }
4177
Calin Juravleddb7df22014-11-25 20:56:51 +00004178 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004179 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004180 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004181 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004182 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004183 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004184 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004185 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004186 __ j(kLess, &less); // Signed compare.
4187 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004188 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004189 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004190 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004191 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004192 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004193 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004194 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004195 }
Aart Bika19616e2016-02-01 18:57:58 -08004196 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004197 break;
4198 }
4199 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004200 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004201 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004202 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004203 break;
4204 }
4205 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004206 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004207 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004208 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004209 break;
4210 }
4211 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004212 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004213 }
Aart Bika19616e2016-02-01 18:57:58 -08004214
Calin Juravleddb7df22014-11-25 20:56:51 +00004215 __ movl(out, Immediate(0));
4216 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004217 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004218
4219 __ Bind(&greater);
4220 __ movl(out, Immediate(1));
4221 __ jmp(&done);
4222
4223 __ Bind(&less);
4224 __ movl(out, Immediate(-1));
4225
4226 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004227}
4228
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004229void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004230 LocationSummary* locations =
4231 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004232 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004233 locations->SetInAt(i, Location::Any());
4234 }
4235 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004236}
4237
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004238void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004239 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004240}
4241
Roland Levillain7c1559a2015-12-15 10:55:36 +00004242void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004243 /*
4244 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4245 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4246 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4247 */
4248 switch (kind) {
4249 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004250 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004251 break;
4252 }
4253 case MemBarrierKind::kAnyStore:
4254 case MemBarrierKind::kLoadAny:
4255 case MemBarrierKind::kStoreStore: {
4256 // nop
4257 break;
4258 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004259 case MemBarrierKind::kNTStoreStore:
4260 // Non-Temporal Store/Store needs an explicit fence.
4261 MemoryFence(/* non-temporal */ true);
4262 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004263 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004264}
4265
Vladimir Markodc151b22015-10-15 18:02:30 +01004266HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4267 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4268 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004269 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4270
4271 // We disable pc-relative load when there is an irreducible loop, as the optimization
4272 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004273 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4274 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004275 if (GetGraph()->HasIrreducibleLoops() &&
4276 (dispatch_info.method_load_kind ==
4277 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4278 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4279 }
4280 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004281 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4282 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4283 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4284 // (Though the direct CALL ptr16:32 is available for consideration).
4285 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004286 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004287 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004288 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004289 0u
4290 };
4291 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004292 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004293 }
4294}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004295
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004296Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4297 Register temp) {
4298 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004299 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004300 if (!invoke->GetLocations()->Intrinsified()) {
4301 return location.AsRegister<Register>();
4302 }
4303 // For intrinsics we allow any location, so it may be on the stack.
4304 if (!location.IsRegister()) {
4305 __ movl(temp, Address(ESP, location.GetStackIndex()));
4306 return temp;
4307 }
4308 // For register locations, check if the register was saved. If so, get it from the stack.
4309 // Note: There is a chance that the register was saved but not overwritten, so we could
4310 // save one load. However, since this is just an intrinsic slow path we prefer this
4311 // simple and more robust approach rather that trying to determine if that's the case.
4312 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004313 if (slow_path != nullptr) {
4314 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4315 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4316 __ movl(temp, Address(ESP, stack_offset));
4317 return temp;
4318 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004319 }
4320 return location.AsRegister<Register>();
4321}
4322
Serguei Katkov288c7a82016-05-16 11:53:15 +06004323Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4324 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004325 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4326 switch (invoke->GetMethodLoadKind()) {
4327 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4328 // temp = thread->string_init_entrypoint
4329 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4330 break;
4331 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004332 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004333 break;
4334 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4335 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4336 break;
4337 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004338 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004339 method_patches_.emplace_back(invoke->GetTargetMethod());
4340 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4341 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004342 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4343 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4344 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004345 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004346 // Bind a new fixup label at the end of the "movl" insn.
4347 uint32_t offset = invoke->GetDexCacheArrayOffset();
4348 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004349 break;
4350 }
Vladimir Marko58155012015-08-19 12:49:41 +00004351 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004352 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004353 Register method_reg;
4354 Register reg = temp.AsRegister<Register>();
4355 if (current_method.IsRegister()) {
4356 method_reg = current_method.AsRegister<Register>();
4357 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004358 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004359 DCHECK(!current_method.IsValid());
4360 method_reg = reg;
4361 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4362 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004363 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004364 __ movl(reg, Address(method_reg,
4365 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004366 // temp = temp[index_in_cache];
4367 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4368 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004369 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4370 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004371 }
Vladimir Marko58155012015-08-19 12:49:41 +00004372 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004373 return callee_method;
4374}
4375
4376void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4377 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004378
4379 switch (invoke->GetCodePtrLocation()) {
4380 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4381 __ call(GetFrameEntryLabel());
4382 break;
4383 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4384 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4385 Label* label = &relative_call_patches_.back().label;
4386 __ call(label); // Bind to the patch label, override at link time.
4387 __ Bind(label); // Bind the label at the end of the "call" insn.
4388 break;
4389 }
4390 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4391 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004392 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4393 LOG(FATAL) << "Unsupported";
4394 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004395 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4396 // (callee_method + offset_of_quick_compiled_code)()
4397 __ call(Address(callee_method.AsRegister<Register>(),
4398 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4399 kX86WordSize).Int32Value()));
4400 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004401 }
4402
4403 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004404}
4405
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004406void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4407 Register temp = temp_in.AsRegister<Register>();
4408 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4409 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004410
4411 // Use the calling convention instead of the location of the receiver, as
4412 // intrinsics may have put the receiver in a different register. In the intrinsics
4413 // slow path, the arguments have been moved to the right place, so here we are
4414 // guaranteed that the receiver is the first register of the calling convention.
4415 InvokeDexCallingConvention calling_convention;
4416 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004417 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004418 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004419 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004420 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004421 // Instead of simply (possibly) unpoisoning `temp` here, we should
4422 // emit a read barrier for the previous class reference load.
4423 // However this is not required in practice, as this is an
4424 // intermediate/temporary reference and because the current
4425 // concurrent copying collector keeps the from-space memory
4426 // intact/accessible until the end of the marking phase (the
4427 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004428 __ MaybeUnpoisonHeapReference(temp);
4429 // temp = temp->GetMethodAt(method_offset);
4430 __ movl(temp, Address(temp, method_offset));
4431 // call temp->GetEntryPoint();
4432 __ call(Address(
4433 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4434}
4435
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004436void CodeGeneratorX86::RecordSimplePatch() {
4437 if (GetCompilerOptions().GetIncludePatchInformation()) {
4438 simple_patches_.emplace_back();
4439 __ Bind(&simple_patches_.back());
4440 }
4441}
4442
4443void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4444 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4445 __ Bind(&string_patches_.back().label);
4446}
4447
4448Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4449 uint32_t element_offset) {
4450 // Add the patch entry and bind its label at the end of the instruction.
4451 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4452 return &pc_relative_dex_cache_patches_.back().label;
4453}
4454
Vladimir Marko58155012015-08-19 12:49:41 +00004455void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4456 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004457 size_t size =
4458 method_patches_.size() +
4459 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004460 pc_relative_dex_cache_patches_.size() +
4461 simple_patches_.size() +
4462 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004463 linker_patches->reserve(size);
4464 // The label points to the end of the "movl" insn but the literal offset for method
4465 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4466 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004467 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004468 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004469 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4470 info.target_method.dex_file,
4471 info.target_method.dex_method_index));
4472 }
4473 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004474 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004475 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4476 info.target_method.dex_file,
4477 info.target_method.dex_method_index));
4478 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004479 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4480 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4481 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4482 &info.target_dex_file,
4483 GetMethodAddressOffset(),
4484 info.element_offset));
4485 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004486 for (const Label& label : simple_patches_) {
4487 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4488 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4489 }
4490 if (GetCompilerOptions().GetCompilePic()) {
4491 for (const StringPatchInfo<Label>& info : string_patches_) {
4492 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4493 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4494 &info.dex_file,
4495 GetMethodAddressOffset(),
4496 info.string_index));
4497 }
4498 } else {
4499 for (const StringPatchInfo<Label>& info : string_patches_) {
4500 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4501 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4502 &info.dex_file,
4503 info.string_index));
4504 }
4505 }
Vladimir Marko58155012015-08-19 12:49:41 +00004506}
4507
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004508void CodeGeneratorX86::MarkGCCard(Register temp,
4509 Register card,
4510 Register object,
4511 Register value,
4512 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004513 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004514 if (value_can_be_null) {
4515 __ testl(value, value);
4516 __ j(kEqual, &is_null);
4517 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004518 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4519 __ movl(temp, object);
4520 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004521 __ movb(Address(temp, card, TIMES_1, 0),
4522 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004523 if (value_can_be_null) {
4524 __ Bind(&is_null);
4525 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004526}
4527
Calin Juravle52c48962014-12-16 17:02:57 +00004528void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4529 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004530
4531 bool object_field_get_with_read_barrier =
4532 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004533 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004534 new (GetGraph()->GetArena()) LocationSummary(instruction,
4535 kEmitCompilerReadBarrier ?
4536 LocationSummary::kCallOnSlowPath :
4537 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004538 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004539
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004540 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4541 locations->SetOut(Location::RequiresFpuRegister());
4542 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004543 // The output overlaps in case of long: we don't want the low move
4544 // to overwrite the object's location. Likewise, in the case of
4545 // an object field get with read barriers enabled, we do not want
4546 // the move to overwrite the object's location, as we need it to emit
4547 // the read barrier.
4548 locations->SetOut(
4549 Location::RequiresRegister(),
4550 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4551 Location::kOutputOverlap :
4552 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004553 }
Calin Juravle52c48962014-12-16 17:02:57 +00004554
4555 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4556 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004557 // So we use an XMM register as a temp to achieve atomicity (first
4558 // load the temp into the XMM and then copy the XMM into the
4559 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004560 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004561 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4562 // We need a temporary register for the read barrier marking slow
4563 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4564 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004565 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004566}
4567
Calin Juravle52c48962014-12-16 17:02:57 +00004568void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4569 const FieldInfo& field_info) {
4570 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004571
Calin Juravle52c48962014-12-16 17:02:57 +00004572 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004573 Location base_loc = locations->InAt(0);
4574 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004575 Location out = locations->Out();
4576 bool is_volatile = field_info.IsVolatile();
4577 Primitive::Type field_type = field_info.GetFieldType();
4578 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4579
4580 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004581 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004582 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004583 break;
4584 }
4585
4586 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004587 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004588 break;
4589 }
4590
4591 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004592 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593 break;
4594 }
4595
4596 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004597 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598 break;
4599 }
4600
4601 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004602 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004603 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004604
4605 case Primitive::kPrimNot: {
4606 // /* HeapReference<Object> */ out = *(base + offset)
4607 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4608 Location temp_loc = locations->GetTemp(0);
4609 // Note that a potential implicit null check is handled in this
4610 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4611 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4612 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4613 if (is_volatile) {
4614 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4615 }
4616 } else {
4617 __ movl(out.AsRegister<Register>(), Address(base, offset));
4618 codegen_->MaybeRecordImplicitNullCheck(instruction);
4619 if (is_volatile) {
4620 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4621 }
4622 // If read barriers are enabled, emit read barriers other than
4623 // Baker's using a slow path (and also unpoison the loaded
4624 // reference, if heap poisoning is enabled).
4625 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4626 }
4627 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004628 }
4629
4630 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004631 if (is_volatile) {
4632 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4633 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004634 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004635 __ movd(out.AsRegisterPairLow<Register>(), temp);
4636 __ psrlq(temp, Immediate(32));
4637 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4638 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004639 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004640 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004641 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004642 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4643 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004644 break;
4645 }
4646
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004647 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004648 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004649 break;
4650 }
4651
4652 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004653 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004654 break;
4655 }
4656
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004657 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004658 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004659 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004660 }
Calin Juravle52c48962014-12-16 17:02:57 +00004661
Roland Levillain7c1559a2015-12-15 10:55:36 +00004662 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4663 // Potential implicit null checks, in the case of reference or
4664 // long fields, are handled in the previous switch statement.
4665 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004666 codegen_->MaybeRecordImplicitNullCheck(instruction);
4667 }
4668
Calin Juravle52c48962014-12-16 17:02:57 +00004669 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004670 if (field_type == Primitive::kPrimNot) {
4671 // Memory barriers, in the case of references, are also handled
4672 // in the previous switch statement.
4673 } else {
4674 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4675 }
Roland Levillain4d027112015-07-01 15:41:14 +01004676 }
Calin Juravle52c48962014-12-16 17:02:57 +00004677}
4678
4679void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4680 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4681
4682 LocationSummary* locations =
4683 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4684 locations->SetInAt(0, Location::RequiresRegister());
4685 bool is_volatile = field_info.IsVolatile();
4686 Primitive::Type field_type = field_info.GetFieldType();
4687 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4688 || (field_type == Primitive::kPrimByte);
4689
4690 // The register allocator does not support multiple
4691 // inputs that die at entry with one in a specific register.
4692 if (is_byte_type) {
4693 // Ensure the value is in a byte register.
4694 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004695 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004696 if (is_volatile && field_type == Primitive::kPrimDouble) {
4697 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4698 locations->SetInAt(1, Location::RequiresFpuRegister());
4699 } else {
4700 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4701 }
4702 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4703 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004704 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004705
Calin Juravle52c48962014-12-16 17:02:57 +00004706 // 64bits value can be atomically written to an address with movsd and an XMM register.
4707 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4708 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4709 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4710 // isolated cases when we need this it isn't worth adding the extra complexity.
4711 locations->AddTemp(Location::RequiresFpuRegister());
4712 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004713 } else {
4714 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4715
4716 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4717 // Temporary registers for the write barrier.
4718 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4719 // Ensure the card is in a byte register.
4720 locations->AddTemp(Location::RegisterLocation(ECX));
4721 }
Calin Juravle52c48962014-12-16 17:02:57 +00004722 }
4723}
4724
4725void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004726 const FieldInfo& field_info,
4727 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004728 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4729
4730 LocationSummary* locations = instruction->GetLocations();
4731 Register base = locations->InAt(0).AsRegister<Register>();
4732 Location value = locations->InAt(1);
4733 bool is_volatile = field_info.IsVolatile();
4734 Primitive::Type field_type = field_info.GetFieldType();
4735 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004736 bool needs_write_barrier =
4737 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004738
4739 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004740 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004741 }
4742
Mark Mendell81489372015-11-04 11:30:41 -05004743 bool maybe_record_implicit_null_check_done = false;
4744
Calin Juravle52c48962014-12-16 17:02:57 +00004745 switch (field_type) {
4746 case Primitive::kPrimBoolean:
4747 case Primitive::kPrimByte: {
4748 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4749 break;
4750 }
4751
4752 case Primitive::kPrimShort:
4753 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004754 if (value.IsConstant()) {
4755 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4756 __ movw(Address(base, offset), Immediate(v));
4757 } else {
4758 __ movw(Address(base, offset), value.AsRegister<Register>());
4759 }
Calin Juravle52c48962014-12-16 17:02:57 +00004760 break;
4761 }
4762
4763 case Primitive::kPrimInt:
4764 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004765 if (kPoisonHeapReferences && needs_write_barrier) {
4766 // Note that in the case where `value` is a null reference,
4767 // we do not enter this block, as the reference does not
4768 // need poisoning.
4769 DCHECK_EQ(field_type, Primitive::kPrimNot);
4770 Register temp = locations->GetTemp(0).AsRegister<Register>();
4771 __ movl(temp, value.AsRegister<Register>());
4772 __ PoisonHeapReference(temp);
4773 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004774 } else if (value.IsConstant()) {
4775 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4776 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004777 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004778 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004779 __ movl(Address(base, offset), value.AsRegister<Register>());
4780 }
Calin Juravle52c48962014-12-16 17:02:57 +00004781 break;
4782 }
4783
4784 case Primitive::kPrimLong: {
4785 if (is_volatile) {
4786 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4787 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4788 __ movd(temp1, value.AsRegisterPairLow<Register>());
4789 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4790 __ punpckldq(temp1, temp2);
4791 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004792 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004793 } else if (value.IsConstant()) {
4794 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4795 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4796 codegen_->MaybeRecordImplicitNullCheck(instruction);
4797 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004798 } else {
4799 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004800 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004801 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4802 }
Mark Mendell81489372015-11-04 11:30:41 -05004803 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004804 break;
4805 }
4806
4807 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004808 if (value.IsConstant()) {
4809 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4810 __ movl(Address(base, offset), Immediate(v));
4811 } else {
4812 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4813 }
Calin Juravle52c48962014-12-16 17:02:57 +00004814 break;
4815 }
4816
4817 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004818 if (value.IsConstant()) {
4819 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4820 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4821 codegen_->MaybeRecordImplicitNullCheck(instruction);
4822 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4823 maybe_record_implicit_null_check_done = true;
4824 } else {
4825 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4826 }
Calin Juravle52c48962014-12-16 17:02:57 +00004827 break;
4828 }
4829
4830 case Primitive::kPrimVoid:
4831 LOG(FATAL) << "Unreachable type " << field_type;
4832 UNREACHABLE();
4833 }
4834
Mark Mendell81489372015-11-04 11:30:41 -05004835 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004836 codegen_->MaybeRecordImplicitNullCheck(instruction);
4837 }
4838
Roland Levillain4d027112015-07-01 15:41:14 +01004839 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004840 Register temp = locations->GetTemp(0).AsRegister<Register>();
4841 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004842 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004843 }
4844
Calin Juravle52c48962014-12-16 17:02:57 +00004845 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004846 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004847 }
4848}
4849
4850void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4851 HandleFieldGet(instruction, instruction->GetFieldInfo());
4852}
4853
4854void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4855 HandleFieldGet(instruction, instruction->GetFieldInfo());
4856}
4857
4858void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4859 HandleFieldSet(instruction, instruction->GetFieldInfo());
4860}
4861
4862void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004863 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004864}
4865
4866void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4867 HandleFieldSet(instruction, instruction->GetFieldInfo());
4868}
4869
4870void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004871 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004872}
4873
4874void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4875 HandleFieldGet(instruction, instruction->GetFieldInfo());
4876}
4877
4878void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4879 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004880}
4881
Calin Juravlee460d1d2015-09-29 04:52:17 +01004882void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4883 HUnresolvedInstanceFieldGet* instruction) {
4884 FieldAccessCallingConventionX86 calling_convention;
4885 codegen_->CreateUnresolvedFieldLocationSummary(
4886 instruction, instruction->GetFieldType(), calling_convention);
4887}
4888
4889void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4890 HUnresolvedInstanceFieldGet* instruction) {
4891 FieldAccessCallingConventionX86 calling_convention;
4892 codegen_->GenerateUnresolvedFieldAccess(instruction,
4893 instruction->GetFieldType(),
4894 instruction->GetFieldIndex(),
4895 instruction->GetDexPc(),
4896 calling_convention);
4897}
4898
4899void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4900 HUnresolvedInstanceFieldSet* instruction) {
4901 FieldAccessCallingConventionX86 calling_convention;
4902 codegen_->CreateUnresolvedFieldLocationSummary(
4903 instruction, instruction->GetFieldType(), calling_convention);
4904}
4905
4906void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4907 HUnresolvedInstanceFieldSet* instruction) {
4908 FieldAccessCallingConventionX86 calling_convention;
4909 codegen_->GenerateUnresolvedFieldAccess(instruction,
4910 instruction->GetFieldType(),
4911 instruction->GetFieldIndex(),
4912 instruction->GetDexPc(),
4913 calling_convention);
4914}
4915
4916void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4917 HUnresolvedStaticFieldGet* instruction) {
4918 FieldAccessCallingConventionX86 calling_convention;
4919 codegen_->CreateUnresolvedFieldLocationSummary(
4920 instruction, instruction->GetFieldType(), calling_convention);
4921}
4922
4923void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4924 HUnresolvedStaticFieldGet* instruction) {
4925 FieldAccessCallingConventionX86 calling_convention;
4926 codegen_->GenerateUnresolvedFieldAccess(instruction,
4927 instruction->GetFieldType(),
4928 instruction->GetFieldIndex(),
4929 instruction->GetDexPc(),
4930 calling_convention);
4931}
4932
4933void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4934 HUnresolvedStaticFieldSet* instruction) {
4935 FieldAccessCallingConventionX86 calling_convention;
4936 codegen_->CreateUnresolvedFieldLocationSummary(
4937 instruction, instruction->GetFieldType(), calling_convention);
4938}
4939
4940void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4941 HUnresolvedStaticFieldSet* instruction) {
4942 FieldAccessCallingConventionX86 calling_convention;
4943 codegen_->GenerateUnresolvedFieldAccess(instruction,
4944 instruction->GetFieldType(),
4945 instruction->GetFieldIndex(),
4946 instruction->GetDexPc(),
4947 calling_convention);
4948}
4949
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004950void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004951 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4952 ? LocationSummary::kCallOnSlowPath
4953 : LocationSummary::kNoCall;
4954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4955 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004956 ? Location::RequiresRegister()
4957 : Location::Any();
4958 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004959 if (instruction->HasUses()) {
4960 locations->SetOut(Location::SameAsFirstInput());
4961 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004962}
4963
Calin Juravle2ae48182016-03-16 14:05:09 +00004964void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4965 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004966 return;
4967 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004968 LocationSummary* locations = instruction->GetLocations();
4969 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004970
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004971 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004972 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004973}
4974
Calin Juravle2ae48182016-03-16 14:05:09 +00004975void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004976 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004977 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004978
4979 LocationSummary* locations = instruction->GetLocations();
4980 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004981
4982 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004983 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004984 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004985 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004986 } else {
4987 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004988 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004989 __ jmp(slow_path->GetEntryLabel());
4990 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004991 }
4992 __ j(kEqual, slow_path->GetEntryLabel());
4993}
4994
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004995void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00004996 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004997}
4998
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004999void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005000 bool object_array_get_with_read_barrier =
5001 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005002 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005003 new (GetGraph()->GetArena()) LocationSummary(instruction,
5004 object_array_get_with_read_barrier ?
5005 LocationSummary::kCallOnSlowPath :
5006 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005007 locations->SetInAt(0, Location::RequiresRegister());
5008 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005009 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5010 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5011 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005012 // The output overlaps in case of long: we don't want the low move
5013 // to overwrite the array's location. Likewise, in the case of an
5014 // object array get with read barriers enabled, we do not want the
5015 // move to overwrite the array's location, as we need it to emit
5016 // the read barrier.
5017 locations->SetOut(
5018 Location::RequiresRegister(),
5019 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5020 Location::kOutputOverlap :
5021 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005022 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005023 // We need a temporary register for the read barrier marking slow
5024 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5025 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5026 locations->AddTemp(Location::RequiresRegister());
5027 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005028}
5029
5030void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5031 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005032 Location obj_loc = locations->InAt(0);
5033 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005034 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005035 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005036
Calin Juravle77520bc2015-01-12 18:45:46 +00005037 Primitive::Type type = instruction->GetType();
5038 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039 case Primitive::kPrimBoolean: {
5040 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005041 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005042 if (index.IsConstant()) {
5043 __ movzxb(out, Address(obj,
5044 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5045 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005046 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005047 }
5048 break;
5049 }
5050
5051 case Primitive::kPrimByte: {
5052 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005053 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005054 if (index.IsConstant()) {
5055 __ movsxb(out, Address(obj,
5056 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5057 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005058 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005059 }
5060 break;
5061 }
5062
5063 case Primitive::kPrimShort: {
5064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005065 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 if (index.IsConstant()) {
5067 __ movsxw(out, Address(obj,
5068 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5069 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005070 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 }
5072 break;
5073 }
5074
5075 case Primitive::kPrimChar: {
5076 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005077 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005078 if (index.IsConstant()) {
5079 __ movzxw(out, Address(obj,
5080 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5081 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005082 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 }
5084 break;
5085 }
5086
Roland Levillain7c1559a2015-12-15 10:55:36 +00005087 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005089 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005090 if (index.IsConstant()) {
5091 __ movl(out, Address(obj,
5092 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5093 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005094 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005095 }
5096 break;
5097 }
5098
Roland Levillain7c1559a2015-12-15 10:55:36 +00005099 case Primitive::kPrimNot: {
5100 static_assert(
5101 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5102 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5104 // /* HeapReference<Object> */ out =
5105 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5106 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5107 Location temp = locations->GetTemp(0);
5108 // Note that a potential implicit null check is handled in this
5109 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5110 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5111 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5112 } else {
5113 Register out = out_loc.AsRegister<Register>();
5114 if (index.IsConstant()) {
5115 uint32_t offset =
5116 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5117 __ movl(out, Address(obj, offset));
5118 codegen_->MaybeRecordImplicitNullCheck(instruction);
5119 // If read barriers are enabled, emit read barriers other than
5120 // Baker's using a slow path (and also unpoison the loaded
5121 // reference, if heap poisoning is enabled).
5122 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5123 } else {
5124 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_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(
5130 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5131 }
5132 }
5133 break;
5134 }
5135
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005136 case Primitive::kPrimLong: {
5137 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005138 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005139 if (index.IsConstant()) {
5140 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005141 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005142 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005143 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005145 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005146 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005147 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005149 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005150 }
5151 break;
5152 }
5153
Mark Mendell7c8d0092015-01-26 11:21:33 -05005154 case Primitive::kPrimFloat: {
5155 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005156 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005157 if (index.IsConstant()) {
5158 __ movss(out, Address(obj,
5159 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5160 } else {
5161 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5162 }
5163 break;
5164 }
5165
5166 case Primitive::kPrimDouble: {
5167 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005168 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005169 if (index.IsConstant()) {
5170 __ movsd(out, Address(obj,
5171 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5172 } else {
5173 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5174 }
5175 break;
5176 }
5177
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005178 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005179 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005180 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005181 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005182
Roland Levillain7c1559a2015-12-15 10:55:36 +00005183 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5184 // Potential implicit null checks, in the case of reference or
5185 // long arrays, are handled in the previous switch statement.
5186 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005187 codegen_->MaybeRecordImplicitNullCheck(instruction);
5188 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005189}
5190
5191void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005192 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005193
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005194 bool needs_write_barrier =
5195 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005196 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5197 bool object_array_set_with_read_barrier =
5198 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005199
Nicolas Geoffray39468442014-09-02 15:17:15 +01005200 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5201 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005202 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5203 LocationSummary::kCallOnSlowPath :
5204 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005205
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005206 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5207 || (value_type == Primitive::kPrimByte);
5208 // We need the inputs to be different than the output in case of long operation.
5209 // In case of a byte operation, the register allocator does not support multiple
5210 // inputs that die at entry with one in a specific register.
5211 locations->SetInAt(0, Location::RequiresRegister());
5212 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5213 if (is_byte_type) {
5214 // Ensure the value is in a byte register.
5215 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5216 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005217 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005218 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005219 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5220 }
5221 if (needs_write_barrier) {
5222 // Temporary registers for the write barrier.
5223 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5224 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005225 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005227}
5228
5229void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5230 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005231 Location array_loc = locations->InAt(0);
5232 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005233 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005234 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005235 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5237 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5238 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005239 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005240 bool needs_write_barrier =
5241 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005242
5243 switch (value_type) {
5244 case Primitive::kPrimBoolean:
5245 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005246 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5247 Address address = index.IsConstant()
5248 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5249 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5250 if (value.IsRegister()) {
5251 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005252 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005255 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005256 break;
5257 }
5258
5259 case Primitive::kPrimShort:
5260 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005261 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5262 Address address = index.IsConstant()
5263 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5264 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5265 if (value.IsRegister()) {
5266 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005267 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005268 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005270 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271 break;
5272 }
5273
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005274 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005275 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5276 Address address = index.IsConstant()
5277 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5278 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005279
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 if (!value.IsRegister()) {
5281 // Just setting null.
5282 DCHECK(instruction->InputAt(2)->IsNullConstant());
5283 DCHECK(value.IsConstant()) << value;
5284 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005285 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005286 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005287 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005289 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005290
5291 DCHECK(needs_write_barrier);
5292 Register register_value = value.AsRegister<Register>();
5293 NearLabel done, not_null, do_put;
5294 SlowPathCode* slow_path = nullptr;
5295 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005296 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5298 codegen_->AddSlowPath(slow_path);
5299 if (instruction->GetValueCanBeNull()) {
5300 __ testl(register_value, register_value);
5301 __ j(kNotEqual, &not_null);
5302 __ movl(address, Immediate(0));
5303 codegen_->MaybeRecordImplicitNullCheck(instruction);
5304 __ jmp(&done);
5305 __ Bind(&not_null);
5306 }
5307
Roland Levillain0d5a2812015-11-13 10:07:31 +00005308 if (kEmitCompilerReadBarrier) {
5309 // When read barriers are enabled, the type checking
5310 // instrumentation requires two read barriers:
5311 //
5312 // __ movl(temp2, temp);
5313 // // /* HeapReference<Class> */ temp = temp->component_type_
5314 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005315 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005316 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5317 //
5318 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5319 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005320 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005321 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5322 //
5323 // __ cmpl(temp, temp2);
5324 //
5325 // However, the second read barrier may trash `temp`, as it
5326 // is a temporary register, and as such would not be saved
5327 // along with live registers before calling the runtime (nor
5328 // restored afterwards). So in this case, we bail out and
5329 // delegate the work to the array set slow path.
5330 //
5331 // TODO: Extend the register allocator to support a new
5332 // "(locally) live temp" location so as to avoid always
5333 // going into the slow path when read barriers are enabled.
5334 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005335 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005336 // /* HeapReference<Class> */ temp = array->klass_
5337 __ movl(temp, Address(array, class_offset));
5338 codegen_->MaybeRecordImplicitNullCheck(instruction);
5339 __ MaybeUnpoisonHeapReference(temp);
5340
5341 // /* HeapReference<Class> */ temp = temp->component_type_
5342 __ movl(temp, Address(temp, component_offset));
5343 // If heap poisoning is enabled, no need to unpoison `temp`
5344 // nor the object reference in `register_value->klass`, as
5345 // we are comparing two poisoned references.
5346 __ cmpl(temp, Address(register_value, class_offset));
5347
5348 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5349 __ j(kEqual, &do_put);
5350 // If heap poisoning is enabled, the `temp` reference has
5351 // not been unpoisoned yet; unpoison it now.
5352 __ MaybeUnpoisonHeapReference(temp);
5353
5354 // /* HeapReference<Class> */ temp = temp->super_class_
5355 __ movl(temp, Address(temp, super_offset));
5356 // If heap poisoning is enabled, no need to unpoison
5357 // `temp`, as we are comparing against null below.
5358 __ testl(temp, temp);
5359 __ j(kNotEqual, slow_path->GetEntryLabel());
5360 __ Bind(&do_put);
5361 } else {
5362 __ j(kNotEqual, slow_path->GetEntryLabel());
5363 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005364 }
5365 }
5366
5367 if (kPoisonHeapReferences) {
5368 __ movl(temp, register_value);
5369 __ PoisonHeapReference(temp);
5370 __ movl(address, temp);
5371 } else {
5372 __ movl(address, register_value);
5373 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005374 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005375 codegen_->MaybeRecordImplicitNullCheck(instruction);
5376 }
5377
5378 Register card = locations->GetTemp(1).AsRegister<Register>();
5379 codegen_->MarkGCCard(
5380 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5381 __ Bind(&done);
5382
5383 if (slow_path != nullptr) {
5384 __ Bind(slow_path->GetExitLabel());
5385 }
5386
5387 break;
5388 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005389
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005390 case Primitive::kPrimInt: {
5391 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5392 Address address = index.IsConstant()
5393 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5394 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5395 if (value.IsRegister()) {
5396 __ movl(address, value.AsRegister<Register>());
5397 } else {
5398 DCHECK(value.IsConstant()) << value;
5399 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5400 __ movl(address, Immediate(v));
5401 }
5402 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 break;
5404 }
5405
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 case Primitive::kPrimLong: {
5407 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005408 if (index.IsConstant()) {
5409 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005410 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005411 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005412 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005414 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005415 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005416 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005419 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005420 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005421 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005422 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005424 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005425 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005427 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005428 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005429 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005430 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005431 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005432 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005433 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005435 Immediate(High32Bits(val)));
5436 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005437 }
5438 break;
5439 }
5440
Mark Mendell7c8d0092015-01-26 11:21:33 -05005441 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005442 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5443 Address address = index.IsConstant()
5444 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5445 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005446 if (value.IsFpuRegister()) {
5447 __ movss(address, value.AsFpuRegister<XmmRegister>());
5448 } else {
5449 DCHECK(value.IsConstant());
5450 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5451 __ movl(address, Immediate(v));
5452 }
5453 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005454 break;
5455 }
5456
5457 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005458 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5459 Address address = index.IsConstant()
5460 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5461 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005462 if (value.IsFpuRegister()) {
5463 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5464 } else {
5465 DCHECK(value.IsConstant());
5466 Address address_hi = index.IsConstant() ?
5467 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5468 offset + kX86WordSize) :
5469 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5470 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5471 __ movl(address, Immediate(Low32Bits(v)));
5472 codegen_->MaybeRecordImplicitNullCheck(instruction);
5473 __ movl(address_hi, Immediate(High32Bits(v)));
5474 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005475 break;
5476 }
5477
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005478 case Primitive::kPrimVoid:
5479 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005480 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005481 }
5482}
5483
5484void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5485 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005486 locations->SetInAt(0, Location::RequiresRegister());
5487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005488}
5489
5490void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5491 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005492 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005493 Register obj = locations->InAt(0).AsRegister<Register>();
5494 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005495 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005496 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005497}
5498
5499void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005500 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5501 ? LocationSummary::kCallOnSlowPath
5502 : LocationSummary::kNoCall;
5503 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005504 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005505 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005506 if (instruction->HasUses()) {
5507 locations->SetOut(Location::SameAsFirstInput());
5508 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005509}
5510
5511void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5512 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005513 Location index_loc = locations->InAt(0);
5514 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005515 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005516 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005517
Mark Mendell99dbd682015-04-22 16:18:52 -04005518 if (length_loc.IsConstant()) {
5519 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5520 if (index_loc.IsConstant()) {
5521 // BCE will remove the bounds check if we are guarenteed to pass.
5522 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5523 if (index < 0 || index >= length) {
5524 codegen_->AddSlowPath(slow_path);
5525 __ jmp(slow_path->GetEntryLabel());
5526 } else {
5527 // Some optimization after BCE may have generated this, and we should not
5528 // generate a bounds check if it is a valid range.
5529 }
5530 return;
5531 }
5532
5533 // We have to reverse the jump condition because the length is the constant.
5534 Register index_reg = index_loc.AsRegister<Register>();
5535 __ cmpl(index_reg, Immediate(length));
5536 codegen_->AddSlowPath(slow_path);
5537 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005538 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005539 Register length = length_loc.AsRegister<Register>();
5540 if (index_loc.IsConstant()) {
5541 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5542 __ cmpl(length, Immediate(value));
5543 } else {
5544 __ cmpl(length, index_loc.AsRegister<Register>());
5545 }
5546 codegen_->AddSlowPath(slow_path);
5547 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005548 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005549}
5550
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005551void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005552 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005553}
5554
5555void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005556 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5557}
5558
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005559void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5560 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5561}
5562
5563void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005564 HBasicBlock* block = instruction->GetBlock();
5565 if (block->GetLoopInformation() != nullptr) {
5566 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5567 // The back edge will generate the suspend check.
5568 return;
5569 }
5570 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5571 // The goto will generate the suspend check.
5572 return;
5573 }
5574 GenerateSuspendCheck(instruction, nullptr);
5575}
5576
5577void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5578 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005579 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005580 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5581 if (slow_path == nullptr) {
5582 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5583 instruction->SetSlowPath(slow_path);
5584 codegen_->AddSlowPath(slow_path);
5585 if (successor != nullptr) {
5586 DCHECK(successor->IsLoopHeader());
5587 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5588 }
5589 } else {
5590 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5591 }
5592
Roland Levillain7c1559a2015-12-15 10:55:36 +00005593 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5594 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005595 if (successor == nullptr) {
5596 __ j(kNotEqual, slow_path->GetEntryLabel());
5597 __ Bind(slow_path->GetReturnLabel());
5598 } else {
5599 __ j(kEqual, codegen_->GetLabelOf(successor));
5600 __ jmp(slow_path->GetEntryLabel());
5601 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005602}
5603
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005604X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5605 return codegen_->GetAssembler();
5606}
5607
Mark Mendell7c8d0092015-01-26 11:21:33 -05005608void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005609 ScratchRegisterScope ensure_scratch(
5610 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5611 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5612 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5613 __ movl(temp_reg, Address(ESP, src + stack_offset));
5614 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005615}
5616
5617void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005618 ScratchRegisterScope ensure_scratch(
5619 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5620 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5621 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5622 __ movl(temp_reg, Address(ESP, src + stack_offset));
5623 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5624 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5625 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005626}
5627
5628void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005629 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005630 Location source = move->GetSource();
5631 Location destination = move->GetDestination();
5632
5633 if (source.IsRegister()) {
5634 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005635 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005636 } else if (destination.IsFpuRegister()) {
5637 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005638 } else {
5639 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005640 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005641 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005642 } else if (source.IsRegisterPair()) {
5643 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5644 // Create stack space for 2 elements.
5645 __ subl(ESP, Immediate(2 * elem_size));
5646 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5647 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5648 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5649 // And remove the temporary stack space we allocated.
5650 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005651 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005652 if (destination.IsRegister()) {
5653 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5654 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005655 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005656 } else if (destination.IsRegisterPair()) {
5657 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5658 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5659 __ psrlq(src_reg, Immediate(32));
5660 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005661 } else if (destination.IsStackSlot()) {
5662 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5663 } else {
5664 DCHECK(destination.IsDoubleStackSlot());
5665 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5666 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005667 } else if (source.IsStackSlot()) {
5668 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005669 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005670 } else if (destination.IsFpuRegister()) {
5671 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005672 } else {
5673 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005674 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5675 }
5676 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005677 if (destination.IsRegisterPair()) {
5678 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5679 __ movl(destination.AsRegisterPairHigh<Register>(),
5680 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5681 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005682 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5683 } else {
5684 DCHECK(destination.IsDoubleStackSlot()) << destination;
5685 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005686 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005687 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005688 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005689 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005690 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005691 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005692 if (value == 0) {
5693 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5694 } else {
5695 __ movl(destination.AsRegister<Register>(), Immediate(value));
5696 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697 } else {
5698 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005699 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005700 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005701 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005702 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005703 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005704 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005705 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005706 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5707 if (value == 0) {
5708 // Easy handling of 0.0.
5709 __ xorps(dest, dest);
5710 } else {
5711 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005712 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5713 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5714 __ movl(temp, Immediate(value));
5715 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005716 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005717 } else {
5718 DCHECK(destination.IsStackSlot()) << destination;
5719 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5720 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005721 } else if (constant->IsLongConstant()) {
5722 int64_t value = constant->AsLongConstant()->GetValue();
5723 int32_t low_value = Low32Bits(value);
5724 int32_t high_value = High32Bits(value);
5725 Immediate low(low_value);
5726 Immediate high(high_value);
5727 if (destination.IsDoubleStackSlot()) {
5728 __ movl(Address(ESP, destination.GetStackIndex()), low);
5729 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5730 } else {
5731 __ movl(destination.AsRegisterPairLow<Register>(), low);
5732 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5733 }
5734 } else {
5735 DCHECK(constant->IsDoubleConstant());
5736 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005737 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005738 int32_t low_value = Low32Bits(value);
5739 int32_t high_value = High32Bits(value);
5740 Immediate low(low_value);
5741 Immediate high(high_value);
5742 if (destination.IsFpuRegister()) {
5743 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5744 if (value == 0) {
5745 // Easy handling of 0.0.
5746 __ xorpd(dest, dest);
5747 } else {
5748 __ pushl(high);
5749 __ pushl(low);
5750 __ movsd(dest, Address(ESP, 0));
5751 __ addl(ESP, Immediate(8));
5752 }
5753 } else {
5754 DCHECK(destination.IsDoubleStackSlot()) << destination;
5755 __ movl(Address(ESP, destination.GetStackIndex()), low);
5756 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5757 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005758 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005759 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005760 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005761 }
5762}
5763
Mark Mendella5c19ce2015-04-01 12:51:05 -04005764void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005765 Register suggested_scratch = reg == EAX ? EBX : EAX;
5766 ScratchRegisterScope ensure_scratch(
5767 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5768
5769 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5770 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5771 __ movl(Address(ESP, mem + stack_offset), reg);
5772 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005773}
5774
Mark Mendell7c8d0092015-01-26 11:21:33 -05005775void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005776 ScratchRegisterScope ensure_scratch(
5777 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5778
5779 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5780 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5781 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5782 __ movss(Address(ESP, mem + stack_offset), reg);
5783 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005784}
5785
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005786void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005787 ScratchRegisterScope ensure_scratch1(
5788 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005789
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005790 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5791 ScratchRegisterScope ensure_scratch2(
5792 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005793
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005794 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5795 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5796 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5797 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5798 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5799 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005800}
5801
5802void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005803 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005804 Location source = move->GetSource();
5805 Location destination = move->GetDestination();
5806
5807 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005808 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5809 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5810 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5811 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5812 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005813 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005814 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005815 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005816 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005817 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5818 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5820 // Use XOR Swap algorithm to avoid a temporary.
5821 DCHECK_NE(source.reg(), destination.reg());
5822 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5823 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5824 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5825 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5826 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5827 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5828 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005829 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5830 // Take advantage of the 16 bytes in the XMM register.
5831 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5832 Address stack(ESP, destination.GetStackIndex());
5833 // Load the double into the high doubleword.
5834 __ movhpd(reg, stack);
5835
5836 // Store the low double into the destination.
5837 __ movsd(stack, reg);
5838
5839 // Move the high double to the low double.
5840 __ psrldq(reg, Immediate(8));
5841 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5842 // Take advantage of the 16 bytes in the XMM register.
5843 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5844 Address stack(ESP, source.GetStackIndex());
5845 // Load the double into the high doubleword.
5846 __ movhpd(reg, stack);
5847
5848 // Store the low double into the destination.
5849 __ movsd(stack, reg);
5850
5851 // Move the high double to the low double.
5852 __ psrldq(reg, Immediate(8));
5853 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5854 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5855 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005856 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005857 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005858 }
5859}
5860
5861void ParallelMoveResolverX86::SpillScratch(int reg) {
5862 __ pushl(static_cast<Register>(reg));
5863}
5864
5865void ParallelMoveResolverX86::RestoreScratch(int reg) {
5866 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005867}
5868
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005869void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005870 InvokeRuntimeCallingConvention calling_convention;
5871 CodeGenerator::CreateLoadClassLocationSummary(
5872 cls,
5873 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005874 Location::RegisterLocation(EAX),
5875 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005876}
5877
5878void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005879 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005880 if (cls->NeedsAccessCheck()) {
5881 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5882 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5883 cls,
5884 cls->GetDexPc(),
5885 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005886 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005887 return;
5888 }
5889
Roland Levillain0d5a2812015-11-13 10:07:31 +00005890 Location out_loc = locations->Out();
5891 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005892 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893
Calin Juravle580b6092015-10-06 17:35:58 +01005894 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005895 DCHECK(!cls->CanCallRuntime());
5896 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005897 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5898 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005899 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005900 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005901 // /* GcRoot<mirror::Class>[] */ out =
5902 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5903 __ movl(out, Address(current_method,
5904 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005905 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005906 GenerateGcRootFieldLoad(
5907 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005908
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005909 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5910 DCHECK(cls->CanCallRuntime());
5911 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5912 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5913 codegen_->AddSlowPath(slow_path);
5914
5915 if (!cls->IsInDexCache()) {
5916 __ testl(out, out);
5917 __ j(kEqual, slow_path->GetEntryLabel());
5918 }
5919
5920 if (cls->MustGenerateClinitCheck()) {
5921 GenerateClassInitializationCheck(slow_path, out);
5922 } else {
5923 __ Bind(slow_path->GetExitLabel());
5924 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005925 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005926 }
5927}
5928
5929void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5930 LocationSummary* locations =
5931 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5932 locations->SetInAt(0, Location::RequiresRegister());
5933 if (check->HasUses()) {
5934 locations->SetOut(Location::SameAsFirstInput());
5935 }
5936}
5937
5938void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005939 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005940 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005941 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005942 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005943 GenerateClassInitializationCheck(slow_path,
5944 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005945}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005946
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005947void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005948 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5950 Immediate(mirror::Class::kStatusInitialized));
5951 __ j(kLess, slow_path->GetEntryLabel());
5952 __ Bind(slow_path->GetExitLabel());
5953 // No need for memory fence, thanks to the X86 memory model.
5954}
5955
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005956HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5957 HLoadString::LoadKind desired_string_load_kind) {
5958 if (kEmitCompilerReadBarrier) {
5959 switch (desired_string_load_kind) {
5960 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5961 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5962 case HLoadString::LoadKind::kBootImageAddress:
5963 // TODO: Implement for read barrier.
5964 return HLoadString::LoadKind::kDexCacheViaMethod;
5965 default:
5966 break;
5967 }
5968 }
5969 switch (desired_string_load_kind) {
5970 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5971 DCHECK(!GetCompilerOptions().GetCompilePic());
5972 break;
5973 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5974 DCHECK(GetCompilerOptions().GetCompilePic());
5975 FALLTHROUGH_INTENDED;
5976 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005977 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005978 // We disable pc-relative load when there is an irreducible loop, as the optimization
5979 // is incompatible with it.
5980 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5981 // with irreducible loops.
5982 if (GetGraph()->HasIrreducibleLoops()) {
5983 return HLoadString::LoadKind::kDexCacheViaMethod;
5984 }
5985 break;
5986 case HLoadString::LoadKind::kBootImageAddress:
5987 break;
5988 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005989 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005990 break;
5991 case HLoadString::LoadKind::kDexCacheViaMethod:
5992 break;
5993 }
5994 return desired_string_load_kind;
5995}
5996
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00005997void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005998 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00005999 ? LocationSummary::kCallOnSlowPath
6000 : LocationSummary::kNoCall;
6001 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006002 HLoadString::LoadKind load_kind = load->GetLoadKind();
6003 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6004 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6005 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6006 locations->SetInAt(0, Location::RequiresRegister());
6007 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006008 locations->SetOut(Location::RequiresRegister());
6009}
6010
6011void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006012 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006013 Location out_loc = locations->Out();
6014 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006015
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006016 switch (load->GetLoadKind()) {
6017 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6018 DCHECK(!kEmitCompilerReadBarrier);
6019 __ movl(out, Immediate(/* placeholder */ 0));
6020 codegen_->RecordStringPatch(load);
6021 return; // No dex cache slow path.
6022 }
6023 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6024 DCHECK(!kEmitCompilerReadBarrier);
6025 Register method_address = locations->InAt(0).AsRegister<Register>();
6026 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6027 codegen_->RecordStringPatch(load);
6028 return; // No dex cache slow path.
6029 }
6030 case HLoadString::LoadKind::kBootImageAddress: {
6031 DCHECK(!kEmitCompilerReadBarrier);
6032 DCHECK_NE(load->GetAddress(), 0u);
6033 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6034 __ movl(out, Immediate(address));
6035 codegen_->RecordSimplePatch();
6036 return; // No dex cache slow path.
6037 }
6038 case HLoadString::LoadKind::kDexCacheAddress: {
6039 DCHECK_NE(load->GetAddress(), 0u);
6040 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6041 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6042 break;
6043 }
6044 case HLoadString::LoadKind::kDexCachePcRelative: {
6045 Register base_reg = locations->InAt(0).AsRegister<Register>();
6046 uint32_t offset = load->GetDexCacheElementOffset();
6047 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
6048 GenerateGcRootFieldLoad(
6049 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6050 break;
6051 }
6052 case HLoadString::LoadKind::kDexCacheViaMethod: {
6053 Register current_method = locations->InAt(0).AsRegister<Register>();
6054
6055 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6056 GenerateGcRootFieldLoad(
6057 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6058
6059 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6060 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6061 // /* GcRoot<mirror::String> */ out = out[string_index]
6062 GenerateGcRootFieldLoad(
6063 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6064 break;
6065 }
6066 default:
6067 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6068 UNREACHABLE();
6069 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006070
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006071 if (!load->IsInDexCache()) {
6072 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6073 codegen_->AddSlowPath(slow_path);
6074 __ testl(out, out);
6075 __ j(kEqual, slow_path->GetEntryLabel());
6076 __ Bind(slow_path->GetExitLabel());
6077 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006078}
6079
David Brazdilcb1c0552015-08-04 16:22:25 +01006080static Address GetExceptionTlsAddress() {
6081 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6082}
6083
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006084void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6085 LocationSummary* locations =
6086 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6087 locations->SetOut(Location::RequiresRegister());
6088}
6089
6090void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006091 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6092}
6093
6094void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6095 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6096}
6097
6098void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6099 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006100}
6101
6102void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6103 LocationSummary* locations =
6104 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6105 InvokeRuntimeCallingConvention calling_convention;
6106 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6107}
6108
6109void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006110 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6111 instruction,
6112 instruction->GetDexPc(),
6113 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006114 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006115}
6116
Roland Levillain7c1559a2015-12-15 10:55:36 +00006117static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6118 return kEmitCompilerReadBarrier &&
6119 (kUseBakerReadBarrier ||
6120 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6121 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6122 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6123}
6124
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006125void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006126 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006127 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6128 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 case TypeCheckKind::kExactCheck:
6130 case TypeCheckKind::kAbstractClassCheck:
6131 case TypeCheckKind::kClassHierarchyCheck:
6132 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006133 call_kind =
6134 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006135 break;
6136 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006137 case TypeCheckKind::kUnresolvedCheck:
6138 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006139 call_kind = LocationSummary::kCallOnSlowPath;
6140 break;
6141 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006142
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006143 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144 locations->SetInAt(0, Location::RequiresRegister());
6145 locations->SetInAt(1, Location::Any());
6146 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6147 locations->SetOut(Location::RequiresRegister());
6148 // When read barriers are enabled, we need a temporary register for
6149 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006150 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006151 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006153}
6154
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006155void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006156 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006157 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006158 Location obj_loc = locations->InAt(0);
6159 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006160 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006161 Location out_loc = locations->Out();
6162 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006163 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006164 locations->GetTemp(0) :
6165 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006166 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006167 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6168 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6169 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006170 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006171 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006172
6173 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006174 // Avoid null check if we know obj is not null.
6175 if (instruction->MustDoNullCheck()) {
6176 __ testl(obj, obj);
6177 __ j(kEqual, &zero);
6178 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006179
Roland Levillain0d5a2812015-11-13 10:07:31 +00006180 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006181 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182
Roland Levillain7c1559a2015-12-15 10:55:36 +00006183 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 case TypeCheckKind::kExactCheck: {
6185 if (cls.IsRegister()) {
6186 __ cmpl(out, cls.AsRegister<Register>());
6187 } else {
6188 DCHECK(cls.IsStackSlot()) << cls;
6189 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6190 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006191
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006192 // Classes must be equal for the instanceof to succeed.
6193 __ j(kNotEqual, &zero);
6194 __ movl(out, Immediate(1));
6195 __ jmp(&done);
6196 break;
6197 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006198
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199 case TypeCheckKind::kAbstractClassCheck: {
6200 // If the class is abstract, we eagerly fetch the super class of the
6201 // object to avoid doing a comparison we know will fail.
6202 NearLabel loop;
6203 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006204 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006205 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006206 __ testl(out, out);
6207 // If `out` is null, we use it for the result, and jump to `done`.
6208 __ j(kEqual, &done);
6209 if (cls.IsRegister()) {
6210 __ cmpl(out, cls.AsRegister<Register>());
6211 } else {
6212 DCHECK(cls.IsStackSlot()) << cls;
6213 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6214 }
6215 __ j(kNotEqual, &loop);
6216 __ movl(out, Immediate(1));
6217 if (zero.IsLinked()) {
6218 __ jmp(&done);
6219 }
6220 break;
6221 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006222
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223 case TypeCheckKind::kClassHierarchyCheck: {
6224 // Walk over the class hierarchy to find a match.
6225 NearLabel loop, success;
6226 __ Bind(&loop);
6227 if (cls.IsRegister()) {
6228 __ cmpl(out, cls.AsRegister<Register>());
6229 } else {
6230 DCHECK(cls.IsStackSlot()) << cls;
6231 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6232 }
6233 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006234 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006235 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006236 __ testl(out, out);
6237 __ j(kNotEqual, &loop);
6238 // If `out` is null, we use it for the result, and jump to `done`.
6239 __ jmp(&done);
6240 __ Bind(&success);
6241 __ movl(out, Immediate(1));
6242 if (zero.IsLinked()) {
6243 __ jmp(&done);
6244 }
6245 break;
6246 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006247
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006248 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006249 // Do an exact check.
6250 NearLabel exact_check;
6251 if (cls.IsRegister()) {
6252 __ cmpl(out, cls.AsRegister<Register>());
6253 } else {
6254 DCHECK(cls.IsStackSlot()) << cls;
6255 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6256 }
6257 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006258 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006259 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006260 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006261 __ testl(out, out);
6262 // If `out` is null, we use it for the result, and jump to `done`.
6263 __ j(kEqual, &done);
6264 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6265 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006266 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006267 __ movl(out, Immediate(1));
6268 __ jmp(&done);
6269 break;
6270 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006271
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006272 case TypeCheckKind::kArrayCheck: {
6273 if (cls.IsRegister()) {
6274 __ cmpl(out, cls.AsRegister<Register>());
6275 } else {
6276 DCHECK(cls.IsStackSlot()) << cls;
6277 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6278 }
6279 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006280 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6281 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006282 codegen_->AddSlowPath(slow_path);
6283 __ j(kNotEqual, slow_path->GetEntryLabel());
6284 __ movl(out, Immediate(1));
6285 if (zero.IsLinked()) {
6286 __ jmp(&done);
6287 }
6288 break;
6289 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290
Calin Juravle98893e12015-10-02 21:05:03 +01006291 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292 case TypeCheckKind::kInterfaceCheck: {
6293 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006294 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295 // cases.
6296 //
6297 // We cannot directly call the InstanceofNonTrivial runtime
6298 // entry point without resorting to a type checking slow path
6299 // here (i.e. by calling InvokeRuntime directly), as it would
6300 // require to assign fixed registers for the inputs of this
6301 // HInstanceOf instruction (following the runtime calling
6302 // convention), which might be cluttered by the potential first
6303 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006304 //
6305 // TODO: Introduce a new runtime entry point taking the object
6306 // to test (instead of its class) as argument, and let it deal
6307 // with the read barrier issues. This will let us refactor this
6308 // case of the `switch` code as it was previously (with a direct
6309 // call to the runtime not using a type checking slow path).
6310 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006311 DCHECK(locations->OnlyCallsOnSlowPath());
6312 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6313 /* is_fatal */ false);
6314 codegen_->AddSlowPath(slow_path);
6315 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006316 if (zero.IsLinked()) {
6317 __ jmp(&done);
6318 }
6319 break;
6320 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006321 }
6322
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006323 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006324 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006325 __ xorl(out, out);
6326 }
6327
6328 if (done.IsLinked()) {
6329 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006330 }
6331
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006332 if (slow_path != nullptr) {
6333 __ Bind(slow_path->GetExitLabel());
6334 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006335}
6336
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006337void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006338 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6339 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006340 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6341 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 case TypeCheckKind::kExactCheck:
6343 case TypeCheckKind::kAbstractClassCheck:
6344 case TypeCheckKind::kClassHierarchyCheck:
6345 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006346 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6347 LocationSummary::kCallOnSlowPath :
6348 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006349 break;
6350 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006351 case TypeCheckKind::kUnresolvedCheck:
6352 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006353 call_kind = LocationSummary::kCallOnSlowPath;
6354 break;
6355 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6357 locations->SetInAt(0, Location::RequiresRegister());
6358 locations->SetInAt(1, Location::Any());
6359 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6360 locations->AddTemp(Location::RequiresRegister());
6361 // When read barriers are enabled, we need an additional temporary
6362 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006363 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006364 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006365 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006366}
6367
6368void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006369 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006370 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006371 Location obj_loc = locations->InAt(0);
6372 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006373 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006374 Location temp_loc = locations->GetTemp(0);
6375 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006376 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006377 locations->GetTemp(1) :
6378 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006379 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6380 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6381 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6382 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006383
Roland Levillain0d5a2812015-11-13 10:07:31 +00006384 bool is_type_check_slow_path_fatal =
6385 (type_check_kind == TypeCheckKind::kExactCheck ||
6386 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6387 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6388 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6389 !instruction->CanThrowIntoCatchBlock();
6390 SlowPathCode* type_check_slow_path =
6391 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6392 is_type_check_slow_path_fatal);
6393 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006394
Roland Levillain0d5a2812015-11-13 10:07:31 +00006395 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006396 // Avoid null check if we know obj is not null.
6397 if (instruction->MustDoNullCheck()) {
6398 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006399 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006400 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006401
Roland Levillain0d5a2812015-11-13 10:07:31 +00006402 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006403 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 case TypeCheckKind::kExactCheck:
6407 case TypeCheckKind::kArrayCheck: {
6408 if (cls.IsRegister()) {
6409 __ cmpl(temp, cls.AsRegister<Register>());
6410 } else {
6411 DCHECK(cls.IsStackSlot()) << cls;
6412 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6413 }
6414 // Jump to slow path for throwing the exception or doing a
6415 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006416 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 break;
6418 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006420 case TypeCheckKind::kAbstractClassCheck: {
6421 // If the class is abstract, we eagerly fetch the super class of the
6422 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006423 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006424 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006426 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427
6428 // If the class reference currently in `temp` is not null, jump
6429 // to the `compare_classes` label to compare it with the checked
6430 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006431 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 __ j(kNotEqual, &compare_classes);
6433 // Otherwise, jump to the slow path to throw the exception.
6434 //
6435 // But before, move back the object's class into `temp` before
6436 // going into the slow path, as it has been overwritten in the
6437 // meantime.
6438 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006439 GenerateReferenceLoadTwoRegisters(
6440 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006441 __ jmp(type_check_slow_path->GetEntryLabel());
6442
6443 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006444 if (cls.IsRegister()) {
6445 __ cmpl(temp, cls.AsRegister<Register>());
6446 } else {
6447 DCHECK(cls.IsStackSlot()) << cls;
6448 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6449 }
6450 __ j(kNotEqual, &loop);
6451 break;
6452 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006453
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006454 case TypeCheckKind::kClassHierarchyCheck: {
6455 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006456 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 __ Bind(&loop);
6458 if (cls.IsRegister()) {
6459 __ cmpl(temp, cls.AsRegister<Register>());
6460 } else {
6461 DCHECK(cls.IsStackSlot()) << cls;
6462 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6463 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006464 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006465
Roland Levillain0d5a2812015-11-13 10:07:31 +00006466 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006467 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468
6469 // If the class reference currently in `temp` is not null, jump
6470 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006471 __ testl(temp, temp);
6472 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006473 // Otherwise, jump to the slow path to throw the exception.
6474 //
6475 // But before, move back the object's class into `temp` before
6476 // going into the slow path, as it has been overwritten in the
6477 // meantime.
6478 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006479 GenerateReferenceLoadTwoRegisters(
6480 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006482 break;
6483 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006486 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006488 if (cls.IsRegister()) {
6489 __ cmpl(temp, cls.AsRegister<Register>());
6490 } else {
6491 DCHECK(cls.IsStackSlot()) << cls;
6492 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6493 }
6494 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006495
6496 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006497 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006498 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006499
6500 // If the component type is not null (i.e. the object is indeed
6501 // an array), jump to label `check_non_primitive_component_type`
6502 // to further check that this component type is not a primitive
6503 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006504 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006505 __ j(kNotEqual, &check_non_primitive_component_type);
6506 // Otherwise, jump to the slow path to throw the exception.
6507 //
6508 // But before, move back the object's class into `temp` before
6509 // going into the slow path, as it has been overwritten in the
6510 // meantime.
6511 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006512 GenerateReferenceLoadTwoRegisters(
6513 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006514 __ jmp(type_check_slow_path->GetEntryLabel());
6515
6516 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006517 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006518 __ j(kEqual, &done);
6519 // Same comment as above regarding `temp` and the slow path.
6520 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006521 GenerateReferenceLoadTwoRegisters(
6522 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006523 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006524 break;
6525 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526
Calin Juravle98893e12015-10-02 21:05:03 +01006527 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006528 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006529 // We always go into the type check slow path for the unresolved
6530 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531 //
6532 // We cannot directly call the CheckCast runtime entry point
6533 // without resorting to a type checking slow path here (i.e. by
6534 // calling InvokeRuntime directly), as it would require to
6535 // assign fixed registers for the inputs of this HInstanceOf
6536 // instruction (following the runtime calling convention), which
6537 // might be cluttered by the potential first read barrier
6538 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006539 //
6540 // TODO: Introduce a new runtime entry point taking the object
6541 // to test (instead of its class) as argument, and let it deal
6542 // with the read barrier issues. This will let us refactor this
6543 // case of the `switch` code as it was previously (with a direct
6544 // call to the runtime not using a type checking slow path).
6545 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006546 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006547 break;
6548 }
6549 __ Bind(&done);
6550
Roland Levillain0d5a2812015-11-13 10:07:31 +00006551 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006552}
6553
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006554void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6555 LocationSummary* locations =
6556 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6557 InvokeRuntimeCallingConvention calling_convention;
6558 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6559}
6560
6561void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006562 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6563 : QUICK_ENTRY_POINT(pUnlockObject),
6564 instruction,
6565 instruction->GetDexPc(),
6566 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006567 if (instruction->IsEnter()) {
6568 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6569 } else {
6570 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6571 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006572}
6573
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006574void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6575void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6576void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6577
6578void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6579 LocationSummary* locations =
6580 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6581 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6582 || instruction->GetResultType() == Primitive::kPrimLong);
6583 locations->SetInAt(0, Location::RequiresRegister());
6584 locations->SetInAt(1, Location::Any());
6585 locations->SetOut(Location::SameAsFirstInput());
6586}
6587
6588void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6589 HandleBitwiseOperation(instruction);
6590}
6591
6592void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6593 HandleBitwiseOperation(instruction);
6594}
6595
6596void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6597 HandleBitwiseOperation(instruction);
6598}
6599
6600void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6601 LocationSummary* locations = instruction->GetLocations();
6602 Location first = locations->InAt(0);
6603 Location second = locations->InAt(1);
6604 DCHECK(first.Equals(locations->Out()));
6605
6606 if (instruction->GetResultType() == Primitive::kPrimInt) {
6607 if (second.IsRegister()) {
6608 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006609 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006610 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006611 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006612 } else {
6613 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006614 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006615 }
6616 } else if (second.IsConstant()) {
6617 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006618 __ andl(first.AsRegister<Register>(),
6619 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006620 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006621 __ orl(first.AsRegister<Register>(),
6622 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006623 } else {
6624 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006625 __ xorl(first.AsRegister<Register>(),
6626 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006627 }
6628 } else {
6629 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006630 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006631 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006632 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006633 } else {
6634 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006635 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006636 }
6637 }
6638 } else {
6639 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6640 if (second.IsRegisterPair()) {
6641 if (instruction->IsAnd()) {
6642 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6643 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6644 } else if (instruction->IsOr()) {
6645 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6646 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6647 } else {
6648 DCHECK(instruction->IsXor());
6649 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6650 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6651 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006652 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006653 if (instruction->IsAnd()) {
6654 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6655 __ andl(first.AsRegisterPairHigh<Register>(),
6656 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6657 } else if (instruction->IsOr()) {
6658 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6659 __ orl(first.AsRegisterPairHigh<Register>(),
6660 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6661 } else {
6662 DCHECK(instruction->IsXor());
6663 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6664 __ xorl(first.AsRegisterPairHigh<Register>(),
6665 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6666 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006667 } else {
6668 DCHECK(second.IsConstant()) << second;
6669 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006670 int32_t low_value = Low32Bits(value);
6671 int32_t high_value = High32Bits(value);
6672 Immediate low(low_value);
6673 Immediate high(high_value);
6674 Register first_low = first.AsRegisterPairLow<Register>();
6675 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006676 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006677 if (low_value == 0) {
6678 __ xorl(first_low, first_low);
6679 } else if (low_value != -1) {
6680 __ andl(first_low, low);
6681 }
6682 if (high_value == 0) {
6683 __ xorl(first_high, first_high);
6684 } else if (high_value != -1) {
6685 __ andl(first_high, high);
6686 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006687 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006688 if (low_value != 0) {
6689 __ orl(first_low, low);
6690 }
6691 if (high_value != 0) {
6692 __ orl(first_high, high);
6693 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006694 } else {
6695 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006696 if (low_value != 0) {
6697 __ xorl(first_low, low);
6698 }
6699 if (high_value != 0) {
6700 __ xorl(first_high, high);
6701 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006702 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006703 }
6704 }
6705}
6706
Roland Levillain7c1559a2015-12-15 10:55:36 +00006707void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6708 Location out,
6709 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006710 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006711 Register out_reg = out.AsRegister<Register>();
6712 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006713 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006714 if (kUseBakerReadBarrier) {
6715 // Load with fast path based Baker's read barrier.
6716 // /* HeapReference<Object> */ out = *(out + offset)
6717 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006718 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006719 } else {
6720 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006721 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006722 // in the following move operation, as we will need it for the
6723 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006724 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006725 // /* HeapReference<Object> */ out = *(out + offset)
6726 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006727 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006728 }
6729 } else {
6730 // Plain load with no read barrier.
6731 // /* HeapReference<Object> */ out = *(out + offset)
6732 __ movl(out_reg, Address(out_reg, offset));
6733 __ MaybeUnpoisonHeapReference(out_reg);
6734 }
6735}
6736
6737void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6738 Location out,
6739 Location obj,
6740 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006741 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006742 Register out_reg = out.AsRegister<Register>();
6743 Register obj_reg = obj.AsRegister<Register>();
6744 if (kEmitCompilerReadBarrier) {
6745 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006746 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006747 // Load with fast path based Baker's read barrier.
6748 // /* HeapReference<Object> */ out = *(obj + offset)
6749 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006750 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006751 } else {
6752 // Load with slow path based read barrier.
6753 // /* HeapReference<Object> */ out = *(obj + offset)
6754 __ movl(out_reg, Address(obj_reg, offset));
6755 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6756 }
6757 } else {
6758 // Plain load with no read barrier.
6759 // /* HeapReference<Object> */ out = *(obj + offset)
6760 __ movl(out_reg, Address(obj_reg, offset));
6761 __ MaybeUnpoisonHeapReference(out_reg);
6762 }
6763}
6764
6765void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6766 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006767 const Address& address,
6768 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006769 Register root_reg = root.AsRegister<Register>();
6770 if (kEmitCompilerReadBarrier) {
6771 if (kUseBakerReadBarrier) {
6772 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6773 // Baker's read barrier are used:
6774 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006775 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006776 // if (Thread::Current()->GetIsGcMarking()) {
6777 // root = ReadBarrier::Mark(root)
6778 // }
6779
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006780 // /* GcRoot<mirror::Object> */ root = *address
6781 __ movl(root_reg, address);
6782 if (fixup_label != nullptr) {
6783 __ Bind(fixup_label);
6784 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006785 static_assert(
6786 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6787 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6788 "have different sizes.");
6789 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6790 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6791 "have different sizes.");
6792
6793 // Slow path used to mark the GC root `root`.
6794 SlowPathCode* slow_path =
6795 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6796 codegen_->AddSlowPath(slow_path);
6797
6798 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6799 Immediate(0));
6800 __ j(kNotEqual, slow_path->GetEntryLabel());
6801 __ Bind(slow_path->GetExitLabel());
6802 } else {
6803 // GC root loaded through a slow path for read barriers other
6804 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006805 // /* GcRoot<mirror::Object>* */ root = address
6806 __ leal(root_reg, address);
6807 if (fixup_label != nullptr) {
6808 __ Bind(fixup_label);
6809 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006810 // /* mirror::Object* */ root = root->Read()
6811 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6812 }
6813 } else {
6814 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006815 // /* GcRoot<mirror::Object> */ root = *address
6816 __ movl(root_reg, address);
6817 if (fixup_label != nullptr) {
6818 __ Bind(fixup_label);
6819 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006820 // Note that GC roots are not affected by heap poisoning, thus we
6821 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006822 }
6823}
6824
6825void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6826 Location ref,
6827 Register obj,
6828 uint32_t offset,
6829 Location temp,
6830 bool needs_null_check) {
6831 DCHECK(kEmitCompilerReadBarrier);
6832 DCHECK(kUseBakerReadBarrier);
6833
6834 // /* HeapReference<Object> */ ref = *(obj + offset)
6835 Address src(obj, offset);
6836 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6837}
6838
6839void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6840 Location ref,
6841 Register obj,
6842 uint32_t data_offset,
6843 Location index,
6844 Location temp,
6845 bool needs_null_check) {
6846 DCHECK(kEmitCompilerReadBarrier);
6847 DCHECK(kUseBakerReadBarrier);
6848
6849 // /* HeapReference<Object> */ ref =
6850 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6851 Address src = index.IsConstant() ?
6852 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6853 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6854 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6855}
6856
6857void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6858 Location ref,
6859 Register obj,
6860 const Address& src,
6861 Location temp,
6862 bool needs_null_check) {
6863 DCHECK(kEmitCompilerReadBarrier);
6864 DCHECK(kUseBakerReadBarrier);
6865
6866 // In slow path based read barriers, the read barrier call is
6867 // inserted after the original load. However, in fast path based
6868 // Baker's read barriers, we need to perform the load of
6869 // mirror::Object::monitor_ *before* the original reference load.
6870 // This load-load ordering is required by the read barrier.
6871 // The fast path/slow path (for Baker's algorithm) should look like:
6872 //
6873 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6874 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6875 // HeapReference<Object> ref = *src; // Original reference load.
6876 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6877 // if (is_gray) {
6878 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6879 // }
6880 //
6881 // Note: the original implementation in ReadBarrier::Barrier is
6882 // slightly more complex as:
6883 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006884 // the high-bits of rb_state, which are expected to be all zeroes
6885 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6886 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006887 // - it performs additional checks that we do not do here for
6888 // performance reasons.
6889
6890 Register ref_reg = ref.AsRegister<Register>();
6891 Register temp_reg = temp.AsRegister<Register>();
6892 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6893
6894 // /* int32_t */ monitor = obj->monitor_
6895 __ movl(temp_reg, Address(obj, monitor_offset));
6896 if (needs_null_check) {
6897 MaybeRecordImplicitNullCheck(instruction);
6898 }
6899 // /* LockWord */ lock_word = LockWord(monitor)
6900 static_assert(sizeof(LockWord) == sizeof(int32_t),
6901 "art::LockWord and int32_t have different sizes.");
6902 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6903 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6904 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6905 static_assert(
6906 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6907 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6908
6909 // Load fence to prevent load-load reordering.
6910 // Note that this is a no-op, thanks to the x86 memory model.
6911 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6912
6913 // The actual reference load.
6914 // /* HeapReference<Object> */ ref = *src
6915 __ movl(ref_reg, src);
6916
6917 // Object* ref = ref_addr->AsMirrorPtr()
6918 __ MaybeUnpoisonHeapReference(ref_reg);
6919
6920 // Slow path used to mark the object `ref` when it is gray.
6921 SlowPathCode* slow_path =
6922 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6923 AddSlowPath(slow_path);
6924
6925 // if (rb_state == ReadBarrier::gray_ptr_)
6926 // ref = ReadBarrier::Mark(ref);
6927 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6928 __ j(kEqual, slow_path->GetEntryLabel());
6929 __ Bind(slow_path->GetExitLabel());
6930}
6931
6932void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6933 Location out,
6934 Location ref,
6935 Location obj,
6936 uint32_t offset,
6937 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006938 DCHECK(kEmitCompilerReadBarrier);
6939
Roland Levillain7c1559a2015-12-15 10:55:36 +00006940 // Insert a slow path based read barrier *after* the reference load.
6941 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006942 // If heap poisoning is enabled, the unpoisoning of the loaded
6943 // reference will be carried out by the runtime within the slow
6944 // path.
6945 //
6946 // Note that `ref` currently does not get unpoisoned (when heap
6947 // poisoning is enabled), which is alright as the `ref` argument is
6948 // not used by the artReadBarrierSlow entry point.
6949 //
6950 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6951 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6952 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6953 AddSlowPath(slow_path);
6954
Roland Levillain0d5a2812015-11-13 10:07:31 +00006955 __ jmp(slow_path->GetEntryLabel());
6956 __ Bind(slow_path->GetExitLabel());
6957}
6958
Roland Levillain7c1559a2015-12-15 10:55:36 +00006959void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6960 Location out,
6961 Location ref,
6962 Location obj,
6963 uint32_t offset,
6964 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006965 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006966 // Baker's read barriers shall be handled by the fast path
6967 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6968 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006969 // If heap poisoning is enabled, unpoisoning will be taken care of
6970 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006971 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006972 } else if (kPoisonHeapReferences) {
6973 __ UnpoisonHeapReference(out.AsRegister<Register>());
6974 }
6975}
6976
Roland Levillain7c1559a2015-12-15 10:55:36 +00006977void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6978 Location out,
6979 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006980 DCHECK(kEmitCompilerReadBarrier);
6981
Roland Levillain7c1559a2015-12-15 10:55:36 +00006982 // Insert a slow path based read barrier *after* the GC root load.
6983 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006984 // Note that GC roots are not affected by heap poisoning, so we do
6985 // not need to do anything special for this here.
6986 SlowPathCode* slow_path =
6987 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6988 AddSlowPath(slow_path);
6989
Roland Levillain0d5a2812015-11-13 10:07:31 +00006990 __ jmp(slow_path->GetEntryLabel());
6991 __ Bind(slow_path->GetExitLabel());
6992}
6993
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006994void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006995 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006996 LOG(FATAL) << "Unreachable";
6997}
6998
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006999void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007000 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007001 LOG(FATAL) << "Unreachable";
7002}
7003
Mark Mendellfe57faa2015-09-18 09:26:15 -04007004// Simple implementation of packed switch - generate cascaded compare/jumps.
7005void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7006 LocationSummary* locations =
7007 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7008 locations->SetInAt(0, Location::RequiresRegister());
7009}
7010
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007011void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7012 int32_t lower_bound,
7013 uint32_t num_entries,
7014 HBasicBlock* switch_block,
7015 HBasicBlock* default_block) {
7016 // Figure out the correct compare values and jump conditions.
7017 // Handle the first compare/branch as a special case because it might
7018 // jump to the default case.
7019 DCHECK_GT(num_entries, 2u);
7020 Condition first_condition;
7021 uint32_t index;
7022 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7023 if (lower_bound != 0) {
7024 first_condition = kLess;
7025 __ cmpl(value_reg, Immediate(lower_bound));
7026 __ j(first_condition, codegen_->GetLabelOf(default_block));
7027 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007028
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007029 index = 1;
7030 } else {
7031 // Handle all the compare/jumps below.
7032 first_condition = kBelow;
7033 index = 0;
7034 }
7035
7036 // Handle the rest of the compare/jumps.
7037 for (; index + 1 < num_entries; index += 2) {
7038 int32_t compare_to_value = lower_bound + index + 1;
7039 __ cmpl(value_reg, Immediate(compare_to_value));
7040 // Jump to successors[index] if value < case_value[index].
7041 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7042 // Jump to successors[index + 1] if value == case_value[index + 1].
7043 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7044 }
7045
7046 if (index != num_entries) {
7047 // There are an odd number of entries. Handle the last one.
7048 DCHECK_EQ(index + 1, num_entries);
7049 __ cmpl(value_reg, Immediate(lower_bound + index));
7050 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007051 }
7052
7053 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007054 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7055 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007056 }
7057}
7058
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007059void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7060 int32_t lower_bound = switch_instr->GetStartValue();
7061 uint32_t num_entries = switch_instr->GetNumEntries();
7062 LocationSummary* locations = switch_instr->GetLocations();
7063 Register value_reg = locations->InAt(0).AsRegister<Register>();
7064
7065 GenPackedSwitchWithCompares(value_reg,
7066 lower_bound,
7067 num_entries,
7068 switch_instr->GetBlock(),
7069 switch_instr->GetDefaultBlock());
7070}
7071
Mark Mendell805b3b52015-09-18 14:10:29 -04007072void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7073 LocationSummary* locations =
7074 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7075 locations->SetInAt(0, Location::RequiresRegister());
7076
7077 // Constant area pointer.
7078 locations->SetInAt(1, Location::RequiresRegister());
7079
7080 // And the temporary we need.
7081 locations->AddTemp(Location::RequiresRegister());
7082}
7083
7084void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7085 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007086 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007087 LocationSummary* locations = switch_instr->GetLocations();
7088 Register value_reg = locations->InAt(0).AsRegister<Register>();
7089 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7090
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007091 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7092 GenPackedSwitchWithCompares(value_reg,
7093 lower_bound,
7094 num_entries,
7095 switch_instr->GetBlock(),
7096 default_block);
7097 return;
7098 }
7099
Mark Mendell805b3b52015-09-18 14:10:29 -04007100 // Optimizing has a jump area.
7101 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7102 Register constant_area = locations->InAt(1).AsRegister<Register>();
7103
7104 // Remove the bias, if needed.
7105 if (lower_bound != 0) {
7106 __ leal(temp_reg, Address(value_reg, -lower_bound));
7107 value_reg = temp_reg;
7108 }
7109
7110 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007111 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007112 __ cmpl(value_reg, Immediate(num_entries - 1));
7113 __ j(kAbove, codegen_->GetLabelOf(default_block));
7114
7115 // We are in the range of the table.
7116 // Load (target-constant_area) from the jump table, indexing by the value.
7117 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7118
7119 // Compute the actual target address by adding in constant_area.
7120 __ addl(temp_reg, constant_area);
7121
7122 // And jump.
7123 __ jmp(temp_reg);
7124}
7125
Mark Mendell0616ae02015-04-17 12:49:27 -04007126void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7127 HX86ComputeBaseMethodAddress* insn) {
7128 LocationSummary* locations =
7129 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7130 locations->SetOut(Location::RequiresRegister());
7131}
7132
7133void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7134 HX86ComputeBaseMethodAddress* insn) {
7135 LocationSummary* locations = insn->GetLocations();
7136 Register reg = locations->Out().AsRegister<Register>();
7137
7138 // Generate call to next instruction.
7139 Label next_instruction;
7140 __ call(&next_instruction);
7141 __ Bind(&next_instruction);
7142
7143 // Remember this offset for later use with constant area.
7144 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7145
7146 // Grab the return address off the stack.
7147 __ popl(reg);
7148}
7149
7150void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7151 HX86LoadFromConstantTable* insn) {
7152 LocationSummary* locations =
7153 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7154
7155 locations->SetInAt(0, Location::RequiresRegister());
7156 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7157
7158 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007159 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007160 return;
7161 }
7162
7163 switch (insn->GetType()) {
7164 case Primitive::kPrimFloat:
7165 case Primitive::kPrimDouble:
7166 locations->SetOut(Location::RequiresFpuRegister());
7167 break;
7168
7169 case Primitive::kPrimInt:
7170 locations->SetOut(Location::RequiresRegister());
7171 break;
7172
7173 default:
7174 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7175 }
7176}
7177
7178void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007179 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007180 return;
7181 }
7182
7183 LocationSummary* locations = insn->GetLocations();
7184 Location out = locations->Out();
7185 Register const_area = locations->InAt(0).AsRegister<Register>();
7186 HConstant *value = insn->GetConstant();
7187
7188 switch (insn->GetType()) {
7189 case Primitive::kPrimFloat:
7190 __ movss(out.AsFpuRegister<XmmRegister>(),
7191 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7192 break;
7193
7194 case Primitive::kPrimDouble:
7195 __ movsd(out.AsFpuRegister<XmmRegister>(),
7196 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7197 break;
7198
7199 case Primitive::kPrimInt:
7200 __ movl(out.AsRegister<Register>(),
7201 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7202 break;
7203
7204 default:
7205 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7206 }
7207}
7208
Mark Mendell0616ae02015-04-17 12:49:27 -04007209/**
7210 * Class to handle late fixup of offsets into constant area.
7211 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007212class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007213 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007214 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7215 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7216
7217 protected:
7218 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7219
7220 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007221
7222 private:
7223 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7224 // Patch the correct offset for the instruction. The place to patch is the
7225 // last 4 bytes of the instruction.
7226 // The value to patch is the distance from the offset in the constant area
7227 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007228 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7229 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007230
7231 // Patch in the right value.
7232 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7233 }
7234
Mark Mendell0616ae02015-04-17 12:49:27 -04007235 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007236 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007237};
7238
Mark Mendell805b3b52015-09-18 14:10:29 -04007239/**
7240 * Class to handle late fixup of offsets to a jump table that will be created in the
7241 * constant area.
7242 */
7243class JumpTableRIPFixup : public RIPFixup {
7244 public:
7245 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7246 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7247
7248 void CreateJumpTable() {
7249 X86Assembler* assembler = codegen_->GetAssembler();
7250
7251 // Ensure that the reference to the jump table has the correct offset.
7252 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7253 SetOffset(offset_in_constant_table);
7254
7255 // The label values in the jump table are computed relative to the
7256 // instruction addressing the constant area.
7257 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7258
7259 // Populate the jump table with the correct values for the jump table.
7260 int32_t num_entries = switch_instr_->GetNumEntries();
7261 HBasicBlock* block = switch_instr_->GetBlock();
7262 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7263 // The value that we want is the target offset - the position of the table.
7264 for (int32_t i = 0; i < num_entries; i++) {
7265 HBasicBlock* b = successors[i];
7266 Label* l = codegen_->GetLabelOf(b);
7267 DCHECK(l->IsBound());
7268 int32_t offset_to_block = l->Position() - relative_offset;
7269 assembler->AppendInt32(offset_to_block);
7270 }
7271 }
7272
7273 private:
7274 const HX86PackedSwitch* switch_instr_;
7275};
7276
7277void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7278 // Generate the constant area if needed.
7279 X86Assembler* assembler = GetAssembler();
7280 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7281 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7282 // byte values.
7283 assembler->Align(4, 0);
7284 constant_area_start_ = assembler->CodeSize();
7285
7286 // Populate any jump tables.
7287 for (auto jump_table : fixups_to_jump_tables_) {
7288 jump_table->CreateJumpTable();
7289 }
7290
7291 // And now add the constant area to the generated code.
7292 assembler->AddConstantArea();
7293 }
7294
7295 // And finish up.
7296 CodeGenerator::Finalize(allocator);
7297}
7298
Mark Mendell0616ae02015-04-17 12:49:27 -04007299Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7300 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7301 return Address(reg, kDummy32BitOffset, fixup);
7302}
7303
7304Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7305 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7306 return Address(reg, kDummy32BitOffset, fixup);
7307}
7308
7309Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7310 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7311 return Address(reg, kDummy32BitOffset, fixup);
7312}
7313
7314Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7315 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7316 return Address(reg, kDummy32BitOffset, fixup);
7317}
7318
Aart Bika19616e2016-02-01 18:57:58 -08007319void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7320 if (value == 0) {
7321 __ xorl(dest, dest);
7322 } else {
7323 __ movl(dest, Immediate(value));
7324 }
7325}
7326
7327void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7328 if (value == 0) {
7329 __ testl(dest, dest);
7330 } else {
7331 __ cmpl(dest, Immediate(value));
7332 }
7333}
7334
Mark Mendell805b3b52015-09-18 14:10:29 -04007335Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7336 Register reg,
7337 Register value) {
7338 // Create a fixup to be used to create and address the jump table.
7339 JumpTableRIPFixup* table_fixup =
7340 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7341
7342 // We have to populate the jump tables.
7343 fixups_to_jump_tables_.push_back(table_fixup);
7344
7345 // We want a scaled address, as we are extracting the correct offset from the table.
7346 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7347}
7348
Andreas Gampe85b62f22015-09-09 13:15:38 -07007349// TODO: target as memory.
7350void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7351 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007352 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007353 return;
7354 }
7355
7356 DCHECK_NE(type, Primitive::kPrimVoid);
7357
7358 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7359 if (target.Equals(return_loc)) {
7360 return;
7361 }
7362
7363 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7364 // with the else branch.
7365 if (type == Primitive::kPrimLong) {
7366 HParallelMove parallel_move(GetGraph()->GetArena());
7367 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7368 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7369 GetMoveResolver()->EmitNativeCode(&parallel_move);
7370 } else {
7371 // Let the parallel move resolver take care of all of this.
7372 HParallelMove parallel_move(GetGraph()->GetArena());
7373 parallel_move.AddMove(return_loc, target, type, nullptr);
7374 GetMoveResolver()->EmitNativeCode(&parallel_move);
7375 }
7376}
7377
Roland Levillain4d027112015-07-01 15:41:14 +01007378#undef __
7379
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007380} // namespace x86
7381} // namespace art