blob: 54ca9a42d7251811e353b6635342cf5590b71690 [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() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100447 instruction_->IsCheckCast() ||
448 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
449 instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000450 << "Unexpected instruction in read barrier marking slow path: "
451 << instruction_->DebugName();
452
453 __ Bind(GetEntryLabel());
454 SaveLiveRegisters(codegen, locations);
455
456 InvokeRuntimeCallingConvention calling_convention;
457 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
458 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
459 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
460 instruction_,
461 instruction_->GetDexPc(),
462 this);
463 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
464 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
465
466 RestoreLiveRegisters(codegen, locations);
467 __ jmp(GetExitLabel());
468 }
469
470 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000471 const Location out_;
472 const Location obj_;
473
474 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
475};
476
Roland Levillain0d5a2812015-11-13 10:07:31 +0000477// Slow path generating a read barrier for a heap reference.
478class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
479 public:
480 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
481 Location out,
482 Location ref,
483 Location obj,
484 uint32_t offset,
485 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000486 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000487 out_(out),
488 ref_(ref),
489 obj_(obj),
490 offset_(offset),
491 index_(index) {
492 DCHECK(kEmitCompilerReadBarrier);
493 // If `obj` is equal to `out` or `ref`, it means the initial object
494 // has been overwritten by (or after) the heap object reference load
495 // to be instrumented, e.g.:
496 //
497 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000498 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000499 //
500 // In that case, we have lost the information about the original
501 // object, and the emitted read barrier cannot work properly.
502 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
503 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
504 }
505
506 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
507 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
508 LocationSummary* locations = instruction_->GetLocations();
509 Register reg_out = out_.AsRegister<Register>();
510 DCHECK(locations->CanCall());
511 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100512 DCHECK(instruction_->IsInstanceFieldGet() ||
513 instruction_->IsStaticFieldGet() ||
514 instruction_->IsArrayGet() ||
515 instruction_->IsInstanceOf() ||
516 instruction_->IsCheckCast() ||
517 ((instruction_->IsInvokeStaticOrDirect() || instruction_->IsInvokeVirtual()) &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000518 instruction_->GetLocations()->Intrinsified()))
519 << "Unexpected instruction in read barrier for heap reference slow path: "
520 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000521
522 __ Bind(GetEntryLabel());
523 SaveLiveRegisters(codegen, locations);
524
525 // We may have to change the index's value, but as `index_` is a
526 // constant member (like other "inputs" of this slow path),
527 // introduce a copy of it, `index`.
528 Location index = index_;
529 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100530 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000531 if (instruction_->IsArrayGet()) {
532 // Compute the actual memory offset and store it in `index`.
533 Register index_reg = index_.AsRegister<Register>();
534 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
535 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
536 // We are about to change the value of `index_reg` (see the
537 // calls to art::x86::X86Assembler::shll and
538 // art::x86::X86Assembler::AddImmediate below), but it has
539 // not been saved by the previous call to
540 // art::SlowPathCode::SaveLiveRegisters, as it is a
541 // callee-save register --
542 // art::SlowPathCode::SaveLiveRegisters does not consider
543 // callee-save registers, as it has been designed with the
544 // assumption that callee-save registers are supposed to be
545 // handled by the called function. So, as a callee-save
546 // register, `index_reg` _would_ eventually be saved onto
547 // the stack, but it would be too late: we would have
548 // changed its value earlier. Therefore, we manually save
549 // it here into another freely available register,
550 // `free_reg`, chosen of course among the caller-save
551 // registers (as a callee-save `free_reg` register would
552 // exhibit the same problem).
553 //
554 // Note we could have requested a temporary register from
555 // the register allocator instead; but we prefer not to, as
556 // this is a slow path, and we know we can find a
557 // caller-save register that is available.
558 Register free_reg = FindAvailableCallerSaveRegister(codegen);
559 __ movl(free_reg, index_reg);
560 index_reg = free_reg;
561 index = Location::RegisterLocation(index_reg);
562 } else {
563 // The initial register stored in `index_` has already been
564 // saved in the call to art::SlowPathCode::SaveLiveRegisters
565 // (as it is not a callee-save register), so we can freely
566 // use it.
567 }
568 // Shifting the index value contained in `index_reg` by the scale
569 // factor (2) cannot overflow in practice, as the runtime is
570 // unable to allocate object arrays with a size larger than
571 // 2^26 - 1 (that is, 2^28 - 4 bytes).
572 __ shll(index_reg, Immediate(TIMES_4));
573 static_assert(
574 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
575 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
576 __ AddImmediate(index_reg, Immediate(offset_));
577 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100578 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
579 // intrinsics, `index_` is not shifted by a scale factor of 2
580 // (as in the case of ArrayGet), as it is actually an offset
581 // to an object field within an object.
582 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000583 DCHECK(instruction_->GetLocations()->Intrinsified());
584 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
585 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
586 << instruction_->AsInvoke()->GetIntrinsic();
587 DCHECK_EQ(offset_, 0U);
588 DCHECK(index_.IsRegisterPair());
589 // UnsafeGet's offset location is a register pair, the low
590 // part contains the correct offset.
591 index = index_.ToLow();
592 }
593 }
594
595 // We're moving two or three locations to locations that could
596 // overlap, so we need a parallel move resolver.
597 InvokeRuntimeCallingConvention calling_convention;
598 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
599 parallel_move.AddMove(ref_,
600 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
601 Primitive::kPrimNot,
602 nullptr);
603 parallel_move.AddMove(obj_,
604 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
605 Primitive::kPrimNot,
606 nullptr);
607 if (index.IsValid()) {
608 parallel_move.AddMove(index,
609 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
610 Primitive::kPrimInt,
611 nullptr);
612 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
613 } else {
614 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
615 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
616 }
617 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
618 instruction_,
619 instruction_->GetDexPc(),
620 this);
621 CheckEntrypointTypes<
622 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
623 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
624
625 RestoreLiveRegisters(codegen, locations);
626 __ jmp(GetExitLabel());
627 }
628
629 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
630
631 private:
632 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
633 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
634 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
635 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
636 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
637 return static_cast<Register>(i);
638 }
639 }
640 // We shall never fail to find a free caller-save register, as
641 // there are more than two core caller-save registers on x86
642 // (meaning it is possible to find one which is different from
643 // `ref` and `obj`).
644 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
645 LOG(FATAL) << "Could not find a free caller-save register";
646 UNREACHABLE();
647 }
648
Roland Levillain0d5a2812015-11-13 10:07:31 +0000649 const Location out_;
650 const Location ref_;
651 const Location obj_;
652 const uint32_t offset_;
653 // An additional location containing an index to an array.
654 // Only used for HArrayGet and the UnsafeGetObject &
655 // UnsafeGetObjectVolatile intrinsics.
656 const Location index_;
657
658 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
659};
660
661// Slow path generating a read barrier for a GC root.
662class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
663 public:
664 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000665 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000666 DCHECK(kEmitCompilerReadBarrier);
667 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000668
669 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
670 LocationSummary* locations = instruction_->GetLocations();
671 Register reg_out = out_.AsRegister<Register>();
672 DCHECK(locations->CanCall());
673 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000674 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
675 << "Unexpected instruction in read barrier for GC root slow path: "
676 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000677
678 __ Bind(GetEntryLabel());
679 SaveLiveRegisters(codegen, locations);
680
681 InvokeRuntimeCallingConvention calling_convention;
682 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
683 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
684 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
685 instruction_,
686 instruction_->GetDexPc(),
687 this);
688 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
689 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
690
691 RestoreLiveRegisters(codegen, locations);
692 __ jmp(GetExitLabel());
693 }
694
695 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
696
697 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000698 const Location out_;
699 const Location root_;
700
701 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
702};
703
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100704#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700705// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
706#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100707
Aart Bike9f37602015-10-09 11:15:55 -0700708inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700709 switch (cond) {
710 case kCondEQ: return kEqual;
711 case kCondNE: return kNotEqual;
712 case kCondLT: return kLess;
713 case kCondLE: return kLessEqual;
714 case kCondGT: return kGreater;
715 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700716 case kCondB: return kBelow;
717 case kCondBE: return kBelowEqual;
718 case kCondA: return kAbove;
719 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700720 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100721 LOG(FATAL) << "Unreachable";
722 UNREACHABLE();
723}
724
Aart Bike9f37602015-10-09 11:15:55 -0700725// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100726inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
727 switch (cond) {
728 case kCondEQ: return kEqual;
729 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700730 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100731 case kCondLT: return kBelow;
732 case kCondLE: return kBelowEqual;
733 case kCondGT: return kAbove;
734 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700735 // Unsigned remain unchanged.
736 case kCondB: return kBelow;
737 case kCondBE: return kBelowEqual;
738 case kCondA: return kAbove;
739 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100740 }
741 LOG(FATAL) << "Unreachable";
742 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700743}
744
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100745void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100746 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100747}
748
749void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100750 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100751}
752
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100753size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
754 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
755 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100756}
757
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100758size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
759 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
760 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100761}
762
Mark Mendell7c8d0092015-01-26 11:21:33 -0500763size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
764 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
765 return GetFloatingPointSpillSlotSize();
766}
767
768size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
769 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
770 return GetFloatingPointSpillSlotSize();
771}
772
Calin Juravle175dc732015-08-25 15:42:32 +0100773void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
774 HInstruction* instruction,
775 uint32_t dex_pc,
776 SlowPathCode* slow_path) {
777 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
778 instruction,
779 dex_pc,
780 slow_path);
781}
782
783void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100784 HInstruction* instruction,
785 uint32_t dex_pc,
786 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100787 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100788 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100789 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100790}
791
Mark Mendellfb8d2792015-03-31 22:16:59 -0400792CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000793 const X86InstructionSetFeatures& isa_features,
794 const CompilerOptions& compiler_options,
795 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500796 : CodeGenerator(graph,
797 kNumberOfCpuRegisters,
798 kNumberOfXmmRegisters,
799 kNumberOfRegisterPairs,
800 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
801 arraysize(kCoreCalleeSaves))
802 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100803 0,
804 compiler_options,
805 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100806 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100807 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100808 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400809 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100810 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000811 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100812 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400813 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000814 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000815 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
816 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100817 constant_area_start_(-1),
818 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
819 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000820 // Use a fake return address register to mimic Quick.
821 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100822}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100823
David Brazdil58282f42016-01-14 12:45:10 +0000824void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100825 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100826 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100827
828 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100829 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100830
Calin Juravle34bacdf2014-10-07 20:23:36 +0100831 UpdateBlockedPairRegisters();
832}
833
834void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
835 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
836 X86ManagedRegister current =
837 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
838 if (blocked_core_registers_[current.AsRegisterPairLow()]
839 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
840 blocked_register_pairs_[i] = true;
841 }
842 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100843}
844
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100845InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800846 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100847 assembler_(codegen->GetAssembler()),
848 codegen_(codegen) {}
849
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100850static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100851 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100852}
853
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000854void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100855 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000856 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000857 bool skip_overflow_check =
858 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000859 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000860
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000861 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100862 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100863 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100864 }
865
Mark Mendell5f874182015-03-04 15:42:45 -0500866 if (HasEmptyFrame()) {
867 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000868 }
Mark Mendell5f874182015-03-04 15:42:45 -0500869
870 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
871 Register reg = kCoreCalleeSaves[i];
872 if (allocated_registers_.ContainsCoreRegister(reg)) {
873 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100874 __ cfi().AdjustCFAOffset(kX86WordSize);
875 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500876 }
877 }
878
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100879 int adjust = GetFrameSize() - FrameEntrySpillSize();
880 __ subl(ESP, Immediate(adjust));
881 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100882 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000883}
884
885void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100886 __ cfi().RememberState();
887 if (!HasEmptyFrame()) {
888 int adjust = GetFrameSize() - FrameEntrySpillSize();
889 __ addl(ESP, Immediate(adjust));
890 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500891
David Srbeckyc34dc932015-04-12 09:27:43 +0100892 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
893 Register reg = kCoreCalleeSaves[i];
894 if (allocated_registers_.ContainsCoreRegister(reg)) {
895 __ popl(reg);
896 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
897 __ cfi().Restore(DWARFReg(reg));
898 }
Mark Mendell5f874182015-03-04 15:42:45 -0500899 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000900 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100901 __ ret();
902 __ cfi().RestoreState();
903 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000904}
905
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100906void CodeGeneratorX86::Bind(HBasicBlock* block) {
907 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000908}
909
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100910Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
911 switch (type) {
912 case Primitive::kPrimBoolean:
913 case Primitive::kPrimByte:
914 case Primitive::kPrimChar:
915 case Primitive::kPrimShort:
916 case Primitive::kPrimInt:
917 case Primitive::kPrimNot:
918 return Location::RegisterLocation(EAX);
919
920 case Primitive::kPrimLong:
921 return Location::RegisterPairLocation(EAX, EDX);
922
923 case Primitive::kPrimVoid:
924 return Location::NoLocation();
925
926 case Primitive::kPrimDouble:
927 case Primitive::kPrimFloat:
928 return Location::FpuRegisterLocation(XMM0);
929 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100930
931 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100932}
933
934Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
935 return Location::RegisterLocation(kMethodRegisterArgument);
936}
937
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100938Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100939 switch (type) {
940 case Primitive::kPrimBoolean:
941 case Primitive::kPrimByte:
942 case Primitive::kPrimChar:
943 case Primitive::kPrimShort:
944 case Primitive::kPrimInt:
945 case Primitive::kPrimNot: {
946 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000947 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100948 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100949 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100950 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000951 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100952 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100953 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100954
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000955 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100956 uint32_t index = gp_index_;
957 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000958 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100959 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100960 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
961 calling_convention.GetRegisterPairAt(index));
962 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100963 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
965 }
966 }
967
968 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100969 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000970 stack_index_++;
971 if (index < calling_convention.GetNumberOfFpuRegisters()) {
972 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
973 } else {
974 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
975 }
976 }
977
978 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100979 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000980 stack_index_ += 2;
981 if (index < calling_convention.GetNumberOfFpuRegisters()) {
982 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
983 } else {
984 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100985 }
986 }
987
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100988 case Primitive::kPrimVoid:
989 LOG(FATAL) << "Unexpected parameter type " << type;
990 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100991 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000992 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100993}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100994
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100995void CodeGeneratorX86::Move32(Location destination, Location source) {
996 if (source.Equals(destination)) {
997 return;
998 }
999 if (destination.IsRegister()) {
1000 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001001 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001002 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001003 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001004 } else {
1005 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001006 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001007 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001008 } else if (destination.IsFpuRegister()) {
1009 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001010 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001011 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001012 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001013 } else {
1014 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001015 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001016 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001017 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001018 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001019 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001020 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001021 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001022 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001023 } else if (source.IsConstant()) {
1024 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001025 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001026 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001027 } else {
1028 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001029 __ pushl(Address(ESP, source.GetStackIndex()));
1030 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001031 }
1032 }
1033}
1034
1035void CodeGeneratorX86::Move64(Location destination, Location source) {
1036 if (source.Equals(destination)) {
1037 return;
1038 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001039 if (destination.IsRegisterPair()) {
1040 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001041 EmitParallelMoves(
1042 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1043 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001044 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001045 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001046 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1047 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001048 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001049 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1050 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1051 __ psrlq(src_reg, Immediate(32));
1052 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001053 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001054 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001055 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001056 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1057 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001058 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1059 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001060 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001061 if (source.IsFpuRegister()) {
1062 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1063 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001064 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001065 } else if (source.IsRegisterPair()) {
1066 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1067 // Create stack space for 2 elements.
1068 __ subl(ESP, Immediate(2 * elem_size));
1069 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1070 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1071 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1072 // And remove the temporary stack space we allocated.
1073 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001074 } else {
1075 LOG(FATAL) << "Unimplemented";
1076 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001077 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001078 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001079 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001080 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001081 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001082 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001083 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001084 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001085 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001086 } else if (source.IsConstant()) {
1087 HConstant* constant = source.GetConstant();
1088 int64_t value;
1089 if (constant->IsLongConstant()) {
1090 value = constant->AsLongConstant()->GetValue();
1091 } else {
1092 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001093 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001094 }
1095 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1096 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001098 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001099 EmitParallelMoves(
1100 Location::StackSlot(source.GetStackIndex()),
1101 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001102 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001103 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001104 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1105 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001106 }
1107 }
1108}
1109
Calin Juravle175dc732015-08-25 15:42:32 +01001110void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1111 DCHECK(location.IsRegister());
1112 __ movl(location.AsRegister<Register>(), Immediate(value));
1113}
1114
Calin Juravlee460d1d2015-09-29 04:52:17 +01001115void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001116 HParallelMove move(GetGraph()->GetArena());
1117 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1118 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1119 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001120 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001121 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001122 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001123 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001124}
1125
1126void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1127 if (location.IsRegister()) {
1128 locations->AddTemp(location);
1129 } else if (location.IsRegisterPair()) {
1130 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1131 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1132 } else {
1133 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1134 }
1135}
1136
David Brazdilfc6a86a2015-06-26 10:33:45 +00001137void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001138 DCHECK(!successor->IsExitBlock());
1139
1140 HBasicBlock* block = got->GetBlock();
1141 HInstruction* previous = got->GetPrevious();
1142
1143 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001144 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001145 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1146 return;
1147 }
1148
1149 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1150 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1151 }
1152 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001153 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001154 }
1155}
1156
David Brazdilfc6a86a2015-06-26 10:33:45 +00001157void LocationsBuilderX86::VisitGoto(HGoto* got) {
1158 got->SetLocations(nullptr);
1159}
1160
1161void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1162 HandleGoto(got, got->GetSuccessor());
1163}
1164
1165void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1166 try_boundary->SetLocations(nullptr);
1167}
1168
1169void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1170 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1171 if (!successor->IsExitBlock()) {
1172 HandleGoto(try_boundary, successor);
1173 }
1174}
1175
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001176void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001177 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001178}
1179
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001180void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001181}
1182
Mark Mendell152408f2015-12-31 12:28:50 -05001183template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001184void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001185 LabelType* true_label,
1186 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001187 if (cond->IsFPConditionTrueIfNaN()) {
1188 __ j(kUnordered, true_label);
1189 } else if (cond->IsFPConditionFalseIfNaN()) {
1190 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001191 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001192 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001193}
1194
Mark Mendell152408f2015-12-31 12:28:50 -05001195template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001196void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001197 LabelType* true_label,
1198 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001199 LocationSummary* locations = cond->GetLocations();
1200 Location left = locations->InAt(0);
1201 Location right = locations->InAt(1);
1202 IfCondition if_cond = cond->GetCondition();
1203
Mark Mendellc4701932015-04-10 13:18:51 -04001204 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001205 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001206 IfCondition true_high_cond = if_cond;
1207 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001208 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001209
1210 // Set the conditions for the test, remembering that == needs to be
1211 // decided using the low words.
1212 switch (if_cond) {
1213 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001214 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001215 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001216 break;
1217 case kCondLT:
1218 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001219 break;
1220 case kCondLE:
1221 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001222 break;
1223 case kCondGT:
1224 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001225 break;
1226 case kCondGE:
1227 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001228 break;
Aart Bike9f37602015-10-09 11:15:55 -07001229 case kCondB:
1230 false_high_cond = kCondA;
1231 break;
1232 case kCondBE:
1233 true_high_cond = kCondB;
1234 break;
1235 case kCondA:
1236 false_high_cond = kCondB;
1237 break;
1238 case kCondAE:
1239 true_high_cond = kCondA;
1240 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001241 }
1242
1243 if (right.IsConstant()) {
1244 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001245 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001246 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001247
Aart Bika19616e2016-02-01 18:57:58 -08001248 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001249 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001250 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001251 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001252 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001253 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001254 __ j(X86Condition(true_high_cond), true_label);
1255 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001256 }
1257 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001258 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001259 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001260 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001261 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001262
1263 __ cmpl(left_high, right_high);
1264 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001265 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001266 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001267 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001268 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001269 __ j(X86Condition(true_high_cond), true_label);
1270 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001271 }
1272 // Must be equal high, so compare the lows.
1273 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001274 } else {
1275 DCHECK(right.IsDoubleStackSlot());
1276 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1277 if (if_cond == kCondNE) {
1278 __ j(X86Condition(true_high_cond), true_label);
1279 } else if (if_cond == kCondEQ) {
1280 __ j(X86Condition(false_high_cond), false_label);
1281 } else {
1282 __ j(X86Condition(true_high_cond), true_label);
1283 __ j(X86Condition(false_high_cond), false_label);
1284 }
1285 // Must be equal high, so compare the lows.
1286 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001287 }
1288 // The last comparison might be unsigned.
1289 __ j(final_condition, true_label);
1290}
1291
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001292void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1293 Location rhs,
1294 HInstruction* insn,
1295 bool is_double) {
1296 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1297 if (is_double) {
1298 if (rhs.IsFpuRegister()) {
1299 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1300 } else if (const_area != nullptr) {
1301 DCHECK(const_area->IsEmittedAtUseSite());
1302 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1303 codegen_->LiteralDoubleAddress(
1304 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1305 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1306 } else {
1307 DCHECK(rhs.IsDoubleStackSlot());
1308 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1309 }
1310 } else {
1311 if (rhs.IsFpuRegister()) {
1312 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1313 } else if (const_area != nullptr) {
1314 DCHECK(const_area->IsEmittedAtUseSite());
1315 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1316 codegen_->LiteralFloatAddress(
1317 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1318 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1319 } else {
1320 DCHECK(rhs.IsStackSlot());
1321 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1322 }
1323 }
1324}
1325
Mark Mendell152408f2015-12-31 12:28:50 -05001326template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001327void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001328 LabelType* true_target_in,
1329 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001330 // Generated branching requires both targets to be explicit. If either of the
1331 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001332 LabelType fallthrough_target;
1333 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1334 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001335
Mark Mendellc4701932015-04-10 13:18:51 -04001336 LocationSummary* locations = condition->GetLocations();
1337 Location left = locations->InAt(0);
1338 Location right = locations->InAt(1);
1339
Mark Mendellc4701932015-04-10 13:18:51 -04001340 Primitive::Type type = condition->InputAt(0)->GetType();
1341 switch (type) {
1342 case Primitive::kPrimLong:
1343 GenerateLongComparesAndJumps(condition, true_target, false_target);
1344 break;
1345 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001346 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001347 GenerateFPJumps(condition, true_target, false_target);
1348 break;
1349 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001350 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001351 GenerateFPJumps(condition, true_target, false_target);
1352 break;
1353 default:
1354 LOG(FATAL) << "Unexpected compare type " << type;
1355 }
1356
David Brazdil0debae72015-11-12 18:37:00 +00001357 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001358 __ jmp(false_target);
1359 }
David Brazdil0debae72015-11-12 18:37:00 +00001360
1361 if (fallthrough_target.IsLinked()) {
1362 __ Bind(&fallthrough_target);
1363 }
Mark Mendellc4701932015-04-10 13:18:51 -04001364}
1365
David Brazdil0debae72015-11-12 18:37:00 +00001366static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1367 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1368 // are set only strictly before `branch`. We can't use the eflags on long/FP
1369 // conditions if they are materialized due to the complex branching.
1370 return cond->IsCondition() &&
1371 cond->GetNext() == branch &&
1372 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1373 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1374}
1375
Mark Mendell152408f2015-12-31 12:28:50 -05001376template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001377void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001378 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001379 LabelType* true_target,
1380 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001381 HInstruction* cond = instruction->InputAt(condition_input_index);
1382
1383 if (true_target == nullptr && false_target == nullptr) {
1384 // Nothing to do. The code always falls through.
1385 return;
1386 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001387 // Constant condition, statically compared against "true" (integer value 1).
1388 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001389 if (true_target != nullptr) {
1390 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001391 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001392 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001393 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001394 if (false_target != nullptr) {
1395 __ jmp(false_target);
1396 }
1397 }
1398 return;
1399 }
1400
1401 // The following code generates these patterns:
1402 // (1) true_target == nullptr && false_target != nullptr
1403 // - opposite condition true => branch to false_target
1404 // (2) true_target != nullptr && false_target == nullptr
1405 // - condition true => branch to true_target
1406 // (3) true_target != nullptr && false_target != nullptr
1407 // - condition true => branch to true_target
1408 // - branch to false_target
1409 if (IsBooleanValueOrMaterializedCondition(cond)) {
1410 if (AreEflagsSetFrom(cond, instruction)) {
1411 if (true_target == nullptr) {
1412 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1413 } else {
1414 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1415 }
1416 } else {
1417 // Materialized condition, compare against 0.
1418 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1419 if (lhs.IsRegister()) {
1420 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1421 } else {
1422 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1423 }
1424 if (true_target == nullptr) {
1425 __ j(kEqual, false_target);
1426 } else {
1427 __ j(kNotEqual, true_target);
1428 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001429 }
1430 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001431 // Condition has not been materialized, use its inputs as the comparison and
1432 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001433 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001434
1435 // If this is a long or FP comparison that has been folded into
1436 // the HCondition, generate the comparison directly.
1437 Primitive::Type type = condition->InputAt(0)->GetType();
1438 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1439 GenerateCompareTestAndBranch(condition, true_target, false_target);
1440 return;
1441 }
1442
1443 Location lhs = condition->GetLocations()->InAt(0);
1444 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001445 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001446 if (rhs.IsRegister()) {
1447 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1448 } else if (rhs.IsConstant()) {
1449 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001450 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001451 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001452 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1453 }
1454 if (true_target == nullptr) {
1455 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1456 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001457 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001458 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001459 }
David Brazdil0debae72015-11-12 18:37:00 +00001460
1461 // If neither branch falls through (case 3), the conditional branch to `true_target`
1462 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1463 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001464 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001465 }
1466}
1467
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001468void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001469 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1470 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001471 locations->SetInAt(0, Location::Any());
1472 }
1473}
1474
1475void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001476 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1477 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1478 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1479 nullptr : codegen_->GetLabelOf(true_successor);
1480 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1481 nullptr : codegen_->GetLabelOf(false_successor);
1482 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001483}
1484
1485void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1486 LocationSummary* locations = new (GetGraph()->GetArena())
1487 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001488 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001489 locations->SetInAt(0, Location::Any());
1490 }
1491}
1492
1493void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001494 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001495 GenerateTestAndBranch<Label>(deoptimize,
1496 /* condition_input_index */ 0,
1497 slow_path->GetEntryLabel(),
1498 /* false_target */ nullptr);
1499}
1500
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001501static bool SelectCanUseCMOV(HSelect* select) {
1502 // There are no conditional move instructions for XMMs.
1503 if (Primitive::IsFloatingPointType(select->GetType())) {
1504 return false;
1505 }
1506
1507 // A FP condition doesn't generate the single CC that we need.
1508 // In 32 bit mode, a long condition doesn't generate a single CC either.
1509 HInstruction* condition = select->GetCondition();
1510 if (condition->IsCondition()) {
1511 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1512 if (compare_type == Primitive::kPrimLong ||
1513 Primitive::IsFloatingPointType(compare_type)) {
1514 return false;
1515 }
1516 }
1517
1518 // We can generate a CMOV for this Select.
1519 return true;
1520}
1521
David Brazdil74eb1b22015-12-14 11:44:01 +00001522void LocationsBuilderX86::VisitSelect(HSelect* select) {
1523 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001524 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001525 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001526 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001527 } else {
1528 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001529 if (SelectCanUseCMOV(select)) {
1530 if (select->InputAt(1)->IsConstant()) {
1531 // Cmov can't handle a constant value.
1532 locations->SetInAt(1, Location::RequiresRegister());
1533 } else {
1534 locations->SetInAt(1, Location::Any());
1535 }
1536 } else {
1537 locations->SetInAt(1, Location::Any());
1538 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001539 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001540 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1541 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001542 }
1543 locations->SetOut(Location::SameAsFirstInput());
1544}
1545
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001546void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1547 Register lhs_reg = lhs.AsRegister<Register>();
1548 if (rhs.IsConstant()) {
1549 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1550 codegen_->Compare32BitValue(lhs_reg, value);
1551 } else if (rhs.IsStackSlot()) {
1552 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1553 } else {
1554 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1555 }
1556}
1557
David Brazdil74eb1b22015-12-14 11:44:01 +00001558void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1559 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001560 DCHECK(locations->InAt(0).Equals(locations->Out()));
1561 if (SelectCanUseCMOV(select)) {
1562 // If both the condition and the source types are integer, we can generate
1563 // a CMOV to implement Select.
1564
1565 HInstruction* select_condition = select->GetCondition();
1566 Condition cond = kNotEqual;
1567
1568 // Figure out how to test the 'condition'.
1569 if (select_condition->IsCondition()) {
1570 HCondition* condition = select_condition->AsCondition();
1571 if (!condition->IsEmittedAtUseSite()) {
1572 // This was a previously materialized condition.
1573 // Can we use the existing condition code?
1574 if (AreEflagsSetFrom(condition, select)) {
1575 // Materialization was the previous instruction. Condition codes are right.
1576 cond = X86Condition(condition->GetCondition());
1577 } else {
1578 // No, we have to recreate the condition code.
1579 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1580 __ testl(cond_reg, cond_reg);
1581 }
1582 } else {
1583 // We can't handle FP or long here.
1584 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1585 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1586 LocationSummary* cond_locations = condition->GetLocations();
1587 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1588 cond = X86Condition(condition->GetCondition());
1589 }
1590 } else {
1591 // Must be a boolean condition, which needs to be compared to 0.
1592 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1593 __ testl(cond_reg, cond_reg);
1594 }
1595
1596 // If the condition is true, overwrite the output, which already contains false.
1597 Location false_loc = locations->InAt(0);
1598 Location true_loc = locations->InAt(1);
1599 if (select->GetType() == Primitive::kPrimLong) {
1600 // 64 bit conditional move.
1601 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1602 Register false_low = false_loc.AsRegisterPairLow<Register>();
1603 if (true_loc.IsRegisterPair()) {
1604 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1605 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1606 } else {
1607 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1608 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1609 }
1610 } else {
1611 // 32 bit conditional move.
1612 Register false_reg = false_loc.AsRegister<Register>();
1613 if (true_loc.IsRegister()) {
1614 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1615 } else {
1616 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1617 }
1618 }
1619 } else {
1620 NearLabel false_target;
1621 GenerateTestAndBranch<NearLabel>(
1622 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1623 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1624 __ Bind(&false_target);
1625 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001626}
1627
David Srbecky0cf44932015-12-09 14:09:59 +00001628void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1629 new (GetGraph()->GetArena()) LocationSummary(info);
1630}
1631
David Srbeckyd28f4a02016-03-14 17:14:24 +00001632void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1633 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001634}
1635
1636void CodeGeneratorX86::GenerateNop() {
1637 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001638}
1639
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001640void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001641 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001642 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001643 // Handle the long/FP comparisons made in instruction simplification.
1644 switch (cond->InputAt(0)->GetType()) {
1645 case Primitive::kPrimLong: {
1646 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001647 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001648 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001649 locations->SetOut(Location::RequiresRegister());
1650 }
1651 break;
1652 }
1653 case Primitive::kPrimFloat:
1654 case Primitive::kPrimDouble: {
1655 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001656 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1657 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1658 } else if (cond->InputAt(1)->IsConstant()) {
1659 locations->SetInAt(1, Location::RequiresFpuRegister());
1660 } else {
1661 locations->SetInAt(1, Location::Any());
1662 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001663 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001664 locations->SetOut(Location::RequiresRegister());
1665 }
1666 break;
1667 }
1668 default:
1669 locations->SetInAt(0, Location::RequiresRegister());
1670 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001671 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001672 // We need a byte register.
1673 locations->SetOut(Location::RegisterLocation(ECX));
1674 }
1675 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001676 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001677}
1678
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001679void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001680 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001681 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001682 }
Mark Mendellc4701932015-04-10 13:18:51 -04001683
1684 LocationSummary* locations = cond->GetLocations();
1685 Location lhs = locations->InAt(0);
1686 Location rhs = locations->InAt(1);
1687 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001688 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001689
1690 switch (cond->InputAt(0)->GetType()) {
1691 default: {
1692 // Integer case.
1693
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001694 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001695 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001696 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001697 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001698 return;
1699 }
1700 case Primitive::kPrimLong:
1701 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1702 break;
1703 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001704 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001705 GenerateFPJumps(cond, &true_label, &false_label);
1706 break;
1707 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001708 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001709 GenerateFPJumps(cond, &true_label, &false_label);
1710 break;
1711 }
1712
1713 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001714 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001715
Roland Levillain4fa13f62015-07-06 18:11:54 +01001716 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001717 __ Bind(&false_label);
1718 __ xorl(reg, reg);
1719 __ jmp(&done_label);
1720
Roland Levillain4fa13f62015-07-06 18:11:54 +01001721 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001722 __ Bind(&true_label);
1723 __ movl(reg, Immediate(1));
1724 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001725}
1726
1727void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001728 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001729}
1730
1731void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001732 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001733}
1734
1735void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001736 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001737}
1738
1739void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001740 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001741}
1742
1743void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001744 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001745}
1746
1747void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001748 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001749}
1750
1751void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001752 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001753}
1754
1755void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001756 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001757}
1758
1759void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001760 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001761}
1762
1763void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001764 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001765}
1766
1767void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001768 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001769}
1770
1771void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001772 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001773}
1774
Aart Bike9f37602015-10-09 11:15:55 -07001775void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001776 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001777}
1778
1779void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001780 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001781}
1782
1783void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001784 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001785}
1786
1787void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001788 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001789}
1790
1791void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001792 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001793}
1794
1795void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001796 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001797}
1798
1799void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001800 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001801}
1802
1803void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001804 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001805}
1806
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001807void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001808 LocationSummary* locations =
1809 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001810 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001811}
1812
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001813void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001814 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001815}
1816
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001817void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1818 LocationSummary* locations =
1819 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1820 locations->SetOut(Location::ConstantLocation(constant));
1821}
1822
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001823void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001824 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001825}
1826
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001827void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001828 LocationSummary* locations =
1829 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001830 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001831}
1832
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001833void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001834 // Will be generated at use site.
1835}
1836
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001837void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* 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::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001844 // Will be generated at use site.
1845}
1846
1847void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1848 LocationSummary* locations =
1849 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1850 locations->SetOut(Location::ConstantLocation(constant));
1851}
1852
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001853void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001854 // Will be generated at use site.
1855}
1856
Calin Juravle27df7582015-04-17 19:12:31 +01001857void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1858 memory_barrier->SetLocations(nullptr);
1859}
1860
1861void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001862 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001863}
1864
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001865void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001866 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001867}
1868
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001869void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001870 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001871}
1872
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001873void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001874 LocationSummary* locations =
1875 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001876 switch (ret->InputAt(0)->GetType()) {
1877 case Primitive::kPrimBoolean:
1878 case Primitive::kPrimByte:
1879 case Primitive::kPrimChar:
1880 case Primitive::kPrimShort:
1881 case Primitive::kPrimInt:
1882 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001883 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001884 break;
1885
1886 case Primitive::kPrimLong:
1887 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001888 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001889 break;
1890
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001891 case Primitive::kPrimFloat:
1892 case Primitive::kPrimDouble:
1893 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001894 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001895 break;
1896
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001898 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001899 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001900}
1901
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001902void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903 if (kIsDebugBuild) {
1904 switch (ret->InputAt(0)->GetType()) {
1905 case Primitive::kPrimBoolean:
1906 case Primitive::kPrimByte:
1907 case Primitive::kPrimChar:
1908 case Primitive::kPrimShort:
1909 case Primitive::kPrimInt:
1910 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001911 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001912 break;
1913
1914 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001915 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1916 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 break;
1918
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001919 case Primitive::kPrimFloat:
1920 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001921 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001922 break;
1923
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001924 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001925 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001926 }
1927 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001928 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001929}
1930
Calin Juravle175dc732015-08-25 15:42:32 +01001931void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1932 // The trampoline uses the same calling convention as dex calling conventions,
1933 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1934 // the method_idx.
1935 HandleInvoke(invoke);
1936}
1937
1938void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1939 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1940}
1941
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001942void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001943 // Explicit clinit checks triggered by static invokes must have been pruned by
1944 // art::PrepareForRegisterAllocation.
1945 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001946
Mark Mendellfb8d2792015-03-31 22:16:59 -04001947 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001948 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001949 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001950 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001951 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001952 return;
1953 }
1954
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001955 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001956
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001957 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1958 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001959 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001960 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001961}
1962
Mark Mendell09ed1a32015-03-25 08:30:06 -04001963static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1964 if (invoke->GetLocations()->Intrinsified()) {
1965 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1966 intrinsic.Dispatch(invoke);
1967 return true;
1968 }
1969 return false;
1970}
1971
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001972void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001973 // Explicit clinit checks triggered by static invokes must have been pruned by
1974 // art::PrepareForRegisterAllocation.
1975 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001976
Mark Mendell09ed1a32015-03-25 08:30:06 -04001977 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1978 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001979 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001980
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001981 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001982 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001983 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001984 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001985}
1986
1987void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001988 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1989 if (intrinsic.TryDispatch(invoke)) {
1990 return;
1991 }
1992
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001993 HandleInvoke(invoke);
1994}
1995
1996void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001997 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001998 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001999}
2000
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002001void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002002 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2003 return;
2004 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002005
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002006 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002007 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002008 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002009}
2010
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002011void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002012 // This call to HandleInvoke allocates a temporary (core) register
2013 // which is also used to transfer the hidden argument from FP to
2014 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002015 HandleInvoke(invoke);
2016 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002017 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002018}
2019
2020void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2021 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002022 LocationSummary* locations = invoke->GetLocations();
2023 Register temp = locations->GetTemp(0).AsRegister<Register>();
2024 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025 Location receiver = locations->InAt(0);
2026 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2027
Roland Levillain0d5a2812015-11-13 10:07:31 +00002028 // Set the hidden argument. This is safe to do this here, as XMM7
2029 // won't be modified thereafter, before the `call` instruction.
2030 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002031 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002032 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002033
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002034 if (receiver.IsStackSlot()) {
2035 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002036 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002037 __ movl(temp, Address(temp, class_offset));
2038 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002039 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002040 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002041 }
Roland Levillain4d027112015-07-01 15:41:14 +01002042 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002043 // Instead of simply (possibly) unpoisoning `temp` here, we should
2044 // emit a read barrier for the previous class reference load.
2045 // However this is not required in practice, as this is an
2046 // intermediate/temporary reference and because the current
2047 // concurrent copying collector keeps the from-space memory
2048 // intact/accessible until the end of the marking phase (the
2049 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002050 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03002051 // temp = temp->GetAddressOfIMT()
2052 __ movl(temp,
2053 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002054 // temp = temp->GetImtEntryAt(method_offset);
Nelli Kimbadee982016-05-13 13:08:53 +03002055 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07002056 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002057 __ movl(temp, Address(temp, method_offset));
2058 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002059 __ call(Address(temp,
2060 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002061
2062 DCHECK(!codegen_->IsLeafMethod());
2063 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2064}
2065
Roland Levillain88cb1752014-10-20 16:36:47 +01002066void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2067 LocationSummary* locations =
2068 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2069 switch (neg->GetResultType()) {
2070 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002071 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002072 locations->SetInAt(0, Location::RequiresRegister());
2073 locations->SetOut(Location::SameAsFirstInput());
2074 break;
2075
Roland Levillain88cb1752014-10-20 16:36:47 +01002076 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002077 locations->SetInAt(0, Location::RequiresFpuRegister());
2078 locations->SetOut(Location::SameAsFirstInput());
2079 locations->AddTemp(Location::RequiresRegister());
2080 locations->AddTemp(Location::RequiresFpuRegister());
2081 break;
2082
Roland Levillain88cb1752014-10-20 16:36:47 +01002083 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002084 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002085 locations->SetOut(Location::SameAsFirstInput());
2086 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002087 break;
2088
2089 default:
2090 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2091 }
2092}
2093
2094void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2095 LocationSummary* locations = neg->GetLocations();
2096 Location out = locations->Out();
2097 Location in = locations->InAt(0);
2098 switch (neg->GetResultType()) {
2099 case Primitive::kPrimInt:
2100 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002101 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002102 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002103 break;
2104
2105 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002106 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002107 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002108 __ negl(out.AsRegisterPairLow<Register>());
2109 // Negation is similar to subtraction from zero. The least
2110 // significant byte triggers a borrow when it is different from
2111 // zero; to take it into account, add 1 to the most significant
2112 // byte if the carry flag (CF) is set to 1 after the first NEGL
2113 // operation.
2114 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2115 __ negl(out.AsRegisterPairHigh<Register>());
2116 break;
2117
Roland Levillain5368c212014-11-27 15:03:41 +00002118 case Primitive::kPrimFloat: {
2119 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002120 Register constant = locations->GetTemp(0).AsRegister<Register>();
2121 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002122 // Implement float negation with an exclusive or with value
2123 // 0x80000000 (mask for bit 31, representing the sign of a
2124 // single-precision floating-point number).
2125 __ movl(constant, Immediate(INT32_C(0x80000000)));
2126 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002127 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002128 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002129 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002130
Roland Levillain5368c212014-11-27 15:03:41 +00002131 case Primitive::kPrimDouble: {
2132 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002133 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002134 // Implement double negation with an exclusive or with value
2135 // 0x8000000000000000 (mask for bit 63, representing the sign of
2136 // a double-precision floating-point number).
2137 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002138 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002139 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002140 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002141
2142 default:
2143 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2144 }
2145}
2146
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002147void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2148 LocationSummary* locations =
2149 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2150 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2151 locations->SetInAt(0, Location::RequiresFpuRegister());
2152 locations->SetInAt(1, Location::RequiresRegister());
2153 locations->SetOut(Location::SameAsFirstInput());
2154 locations->AddTemp(Location::RequiresFpuRegister());
2155}
2156
2157void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2158 LocationSummary* locations = neg->GetLocations();
2159 Location out = locations->Out();
2160 DCHECK(locations->InAt(0).Equals(out));
2161
2162 Register constant_area = locations->InAt(1).AsRegister<Register>();
2163 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2164 if (neg->GetType() == Primitive::kPrimFloat) {
2165 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2166 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2167 } else {
2168 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2169 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2170 }
2171}
2172
Roland Levillaindff1f282014-11-05 14:15:05 +00002173void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002174 Primitive::Type result_type = conversion->GetResultType();
2175 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002176 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002177
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002178 // The float-to-long and double-to-long type conversions rely on a
2179 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002180 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002181 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2182 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002183 ? LocationSummary::kCall
2184 : LocationSummary::kNoCall;
2185 LocationSummary* locations =
2186 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2187
David Brazdilb2bd1c52015-03-25 11:17:37 +00002188 // The Java language does not allow treating boolean as an integral type but
2189 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002190
Roland Levillaindff1f282014-11-05 14:15:05 +00002191 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002192 case Primitive::kPrimByte:
2193 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002194 case Primitive::kPrimLong: {
2195 // Type conversion from long to byte is a result of code transformations.
2196 HInstruction* input = conversion->InputAt(0);
2197 Location input_location = input->IsConstant()
2198 ? Location::ConstantLocation(input->AsConstant())
2199 : Location::RegisterPairLocation(EAX, EDX);
2200 locations->SetInAt(0, input_location);
2201 // Make the output overlap to please the register allocator. This greatly simplifies
2202 // the validation of the linear scan implementation
2203 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2204 break;
2205 }
David Brazdil46e2a392015-03-16 17:31:52 +00002206 case Primitive::kPrimBoolean:
2207 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002208 case Primitive::kPrimShort:
2209 case Primitive::kPrimInt:
2210 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002211 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002212 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2213 // Make the output overlap to please the register allocator. This greatly simplifies
2214 // the validation of the linear scan implementation
2215 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002216 break;
2217
2218 default:
2219 LOG(FATAL) << "Unexpected type conversion from " << input_type
2220 << " to " << result_type;
2221 }
2222 break;
2223
Roland Levillain01a8d712014-11-14 16:27:39 +00002224 case Primitive::kPrimShort:
2225 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002226 case Primitive::kPrimLong:
2227 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002228 case Primitive::kPrimBoolean:
2229 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002230 case Primitive::kPrimByte:
2231 case Primitive::kPrimInt:
2232 case Primitive::kPrimChar:
2233 // Processing a Dex `int-to-short' instruction.
2234 locations->SetInAt(0, Location::Any());
2235 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2236 break;
2237
2238 default:
2239 LOG(FATAL) << "Unexpected type conversion from " << input_type
2240 << " to " << result_type;
2241 }
2242 break;
2243
Roland Levillain946e1432014-11-11 17:35:19 +00002244 case Primitive::kPrimInt:
2245 switch (input_type) {
2246 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002247 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002248 locations->SetInAt(0, Location::Any());
2249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2250 break;
2251
2252 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002253 // Processing a Dex `float-to-int' instruction.
2254 locations->SetInAt(0, Location::RequiresFpuRegister());
2255 locations->SetOut(Location::RequiresRegister());
2256 locations->AddTemp(Location::RequiresFpuRegister());
2257 break;
2258
Roland Levillain946e1432014-11-11 17:35:19 +00002259 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002260 // Processing a Dex `double-to-int' instruction.
2261 locations->SetInAt(0, Location::RequiresFpuRegister());
2262 locations->SetOut(Location::RequiresRegister());
2263 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002264 break;
2265
2266 default:
2267 LOG(FATAL) << "Unexpected type conversion from " << input_type
2268 << " to " << result_type;
2269 }
2270 break;
2271
Roland Levillaindff1f282014-11-05 14:15:05 +00002272 case Primitive::kPrimLong:
2273 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002274 case Primitive::kPrimBoolean:
2275 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002276 case Primitive::kPrimByte:
2277 case Primitive::kPrimShort:
2278 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002279 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002280 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002281 locations->SetInAt(0, Location::RegisterLocation(EAX));
2282 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2283 break;
2284
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002285 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002286 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002287 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002288 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002289 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2290 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2291
Vladimir Marko949c91f2015-01-27 10:48:44 +00002292 // The runtime helper puts the result in EAX, EDX.
2293 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002294 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002295 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002296
2297 default:
2298 LOG(FATAL) << "Unexpected type conversion from " << input_type
2299 << " to " << result_type;
2300 }
2301 break;
2302
Roland Levillain981e4542014-11-14 11:47:14 +00002303 case Primitive::kPrimChar:
2304 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002305 case Primitive::kPrimLong:
2306 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002307 case Primitive::kPrimBoolean:
2308 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002309 case Primitive::kPrimByte:
2310 case Primitive::kPrimShort:
2311 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002312 // Processing a Dex `int-to-char' instruction.
2313 locations->SetInAt(0, Location::Any());
2314 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2315 break;
2316
2317 default:
2318 LOG(FATAL) << "Unexpected type conversion from " << input_type
2319 << " to " << result_type;
2320 }
2321 break;
2322
Roland Levillaindff1f282014-11-05 14:15:05 +00002323 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002324 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002325 case Primitive::kPrimBoolean:
2326 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002327 case Primitive::kPrimByte:
2328 case Primitive::kPrimShort:
2329 case Primitive::kPrimInt:
2330 case Primitive::kPrimChar:
2331 // Processing a Dex `int-to-float' instruction.
2332 locations->SetInAt(0, Location::RequiresRegister());
2333 locations->SetOut(Location::RequiresFpuRegister());
2334 break;
2335
2336 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002337 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002338 locations->SetInAt(0, Location::Any());
2339 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002340 break;
2341
Roland Levillaincff13742014-11-17 14:32:17 +00002342 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002343 // Processing a Dex `double-to-float' instruction.
2344 locations->SetInAt(0, Location::RequiresFpuRegister());
2345 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002346 break;
2347
2348 default:
2349 LOG(FATAL) << "Unexpected type conversion from " << input_type
2350 << " to " << result_type;
2351 };
2352 break;
2353
Roland Levillaindff1f282014-11-05 14:15:05 +00002354 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002355 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002356 case Primitive::kPrimBoolean:
2357 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002358 case Primitive::kPrimByte:
2359 case Primitive::kPrimShort:
2360 case Primitive::kPrimInt:
2361 case Primitive::kPrimChar:
2362 // Processing a Dex `int-to-double' instruction.
2363 locations->SetInAt(0, Location::RequiresRegister());
2364 locations->SetOut(Location::RequiresFpuRegister());
2365 break;
2366
2367 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002368 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002369 locations->SetInAt(0, Location::Any());
2370 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002371 break;
2372
Roland Levillaincff13742014-11-17 14:32:17 +00002373 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002374 // Processing a Dex `float-to-double' instruction.
2375 locations->SetInAt(0, Location::RequiresFpuRegister());
2376 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002383 break;
2384
2385 default:
2386 LOG(FATAL) << "Unexpected type conversion from " << input_type
2387 << " to " << result_type;
2388 }
2389}
2390
2391void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2392 LocationSummary* locations = conversion->GetLocations();
2393 Location out = locations->Out();
2394 Location in = locations->InAt(0);
2395 Primitive::Type result_type = conversion->GetResultType();
2396 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002397 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002398 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002399 case Primitive::kPrimByte:
2400 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002401 case Primitive::kPrimLong:
2402 // Type conversion from long to byte is a result of code transformations.
2403 if (in.IsRegisterPair()) {
2404 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2405 } else {
2406 DCHECK(in.GetConstant()->IsLongConstant());
2407 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2408 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2409 }
2410 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002411 case Primitive::kPrimBoolean:
2412 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002413 case Primitive::kPrimShort:
2414 case Primitive::kPrimInt:
2415 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002416 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002417 if (in.IsRegister()) {
2418 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002419 } else {
2420 DCHECK(in.GetConstant()->IsIntConstant());
2421 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2422 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2423 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002424 break;
2425
2426 default:
2427 LOG(FATAL) << "Unexpected type conversion from " << input_type
2428 << " to " << result_type;
2429 }
2430 break;
2431
Roland Levillain01a8d712014-11-14 16:27:39 +00002432 case Primitive::kPrimShort:
2433 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002434 case Primitive::kPrimLong:
2435 // Type conversion from long to short is a result of code transformations.
2436 if (in.IsRegisterPair()) {
2437 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2438 } else if (in.IsDoubleStackSlot()) {
2439 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2440 } else {
2441 DCHECK(in.GetConstant()->IsLongConstant());
2442 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2443 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2444 }
2445 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002446 case Primitive::kPrimBoolean:
2447 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002448 case Primitive::kPrimByte:
2449 case Primitive::kPrimInt:
2450 case Primitive::kPrimChar:
2451 // Processing a Dex `int-to-short' instruction.
2452 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002454 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002455 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002456 } else {
2457 DCHECK(in.GetConstant()->IsIntConstant());
2458 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002459 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002460 }
2461 break;
2462
2463 default:
2464 LOG(FATAL) << "Unexpected type conversion from " << input_type
2465 << " to " << result_type;
2466 }
2467 break;
2468
Roland Levillain946e1432014-11-11 17:35:19 +00002469 case Primitive::kPrimInt:
2470 switch (input_type) {
2471 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002472 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002473 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002474 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002475 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002476 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002477 } else {
2478 DCHECK(in.IsConstant());
2479 DCHECK(in.GetConstant()->IsLongConstant());
2480 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002481 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002482 }
2483 break;
2484
Roland Levillain3f8f9362014-12-02 17:45:01 +00002485 case Primitive::kPrimFloat: {
2486 // Processing a Dex `float-to-int' instruction.
2487 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2488 Register output = out.AsRegister<Register>();
2489 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002490 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002491
2492 __ movl(output, Immediate(kPrimIntMax));
2493 // temp = int-to-float(output)
2494 __ cvtsi2ss(temp, output);
2495 // if input >= temp goto done
2496 __ comiss(input, temp);
2497 __ j(kAboveEqual, &done);
2498 // if input == NaN goto nan
2499 __ j(kUnordered, &nan);
2500 // output = float-to-int-truncate(input)
2501 __ cvttss2si(output, input);
2502 __ jmp(&done);
2503 __ Bind(&nan);
2504 // output = 0
2505 __ xorl(output, output);
2506 __ Bind(&done);
2507 break;
2508 }
2509
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002510 case Primitive::kPrimDouble: {
2511 // Processing a Dex `double-to-int' instruction.
2512 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2513 Register output = out.AsRegister<Register>();
2514 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002515 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002516
2517 __ movl(output, Immediate(kPrimIntMax));
2518 // temp = int-to-double(output)
2519 __ cvtsi2sd(temp, output);
2520 // if input >= temp goto done
2521 __ comisd(input, temp);
2522 __ j(kAboveEqual, &done);
2523 // if input == NaN goto nan
2524 __ j(kUnordered, &nan);
2525 // output = double-to-int-truncate(input)
2526 __ cvttsd2si(output, input);
2527 __ jmp(&done);
2528 __ Bind(&nan);
2529 // output = 0
2530 __ xorl(output, output);
2531 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002532 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002533 }
Roland Levillain946e1432014-11-11 17:35:19 +00002534
2535 default:
2536 LOG(FATAL) << "Unexpected type conversion from " << input_type
2537 << " to " << result_type;
2538 }
2539 break;
2540
Roland Levillaindff1f282014-11-05 14:15:05 +00002541 case Primitive::kPrimLong:
2542 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002543 case Primitive::kPrimBoolean:
2544 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002545 case Primitive::kPrimByte:
2546 case Primitive::kPrimShort:
2547 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002548 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002549 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002550 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2551 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002552 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002553 __ cdq();
2554 break;
2555
2556 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002557 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002558 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2559 conversion,
2560 conversion->GetDexPc(),
2561 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002562 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002563 break;
2564
Roland Levillaindff1f282014-11-05 14:15:05 +00002565 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002566 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002567 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2568 conversion,
2569 conversion->GetDexPc(),
2570 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002571 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002572 break;
2573
2574 default:
2575 LOG(FATAL) << "Unexpected type conversion from " << input_type
2576 << " to " << result_type;
2577 }
2578 break;
2579
Roland Levillain981e4542014-11-14 11:47:14 +00002580 case Primitive::kPrimChar:
2581 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002582 case Primitive::kPrimLong:
2583 // Type conversion from long to short is a result of code transformations.
2584 if (in.IsRegisterPair()) {
2585 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2586 } else if (in.IsDoubleStackSlot()) {
2587 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2588 } else {
2589 DCHECK(in.GetConstant()->IsLongConstant());
2590 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2591 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2592 }
2593 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002594 case Primitive::kPrimBoolean:
2595 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002596 case Primitive::kPrimByte:
2597 case Primitive::kPrimShort:
2598 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002599 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2600 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002602 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002603 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002604 } else {
2605 DCHECK(in.GetConstant()->IsIntConstant());
2606 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002607 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002608 }
2609 break;
2610
2611 default:
2612 LOG(FATAL) << "Unexpected type conversion from " << input_type
2613 << " to " << result_type;
2614 }
2615 break;
2616
Roland Levillaindff1f282014-11-05 14:15:05 +00002617 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002618 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002619 case Primitive::kPrimBoolean:
2620 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002621 case Primitive::kPrimByte:
2622 case Primitive::kPrimShort:
2623 case Primitive::kPrimInt:
2624 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002625 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002626 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002627 break;
2628
Roland Levillain6d0e4832014-11-27 18:31:21 +00002629 case Primitive::kPrimLong: {
2630 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002631 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002632
Roland Levillain232ade02015-04-20 15:14:36 +01002633 // Create stack space for the call to
2634 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2635 // TODO: enhance register allocator to ask for stack temporaries.
2636 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2637 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2638 __ subl(ESP, Immediate(adjustment));
2639 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002640
Roland Levillain232ade02015-04-20 15:14:36 +01002641 // Load the value to the FP stack, using temporaries if needed.
2642 PushOntoFPStack(in, 0, adjustment, false, true);
2643
2644 if (out.IsStackSlot()) {
2645 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2646 } else {
2647 __ fstps(Address(ESP, 0));
2648 Location stack_temp = Location::StackSlot(0);
2649 codegen_->Move32(out, stack_temp);
2650 }
2651
2652 // Remove the temporary stack space we allocated.
2653 if (adjustment != 0) {
2654 __ addl(ESP, Immediate(adjustment));
2655 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002656 break;
2657 }
2658
Roland Levillaincff13742014-11-17 14:32:17 +00002659 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002660 // Processing a Dex `double-to-float' instruction.
2661 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002662 break;
2663
2664 default:
2665 LOG(FATAL) << "Unexpected type conversion from " << input_type
2666 << " to " << result_type;
2667 };
2668 break;
2669
Roland Levillaindff1f282014-11-05 14:15:05 +00002670 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002671 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002672 case Primitive::kPrimBoolean:
2673 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002674 case Primitive::kPrimByte:
2675 case Primitive::kPrimShort:
2676 case Primitive::kPrimInt:
2677 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002678 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002679 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002680 break;
2681
Roland Levillain647b9ed2014-11-27 12:06:00 +00002682 case Primitive::kPrimLong: {
2683 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002684 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002685
Roland Levillain232ade02015-04-20 15:14:36 +01002686 // Create stack space for the call to
2687 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2688 // TODO: enhance register allocator to ask for stack temporaries.
2689 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2690 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2691 __ subl(ESP, Immediate(adjustment));
2692 }
2693
2694 // Load the value to the FP stack, using temporaries if needed.
2695 PushOntoFPStack(in, 0, adjustment, false, true);
2696
2697 if (out.IsDoubleStackSlot()) {
2698 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2699 } else {
2700 __ fstpl(Address(ESP, 0));
2701 Location stack_temp = Location::DoubleStackSlot(0);
2702 codegen_->Move64(out, stack_temp);
2703 }
2704
2705 // Remove the temporary stack space we allocated.
2706 if (adjustment != 0) {
2707 __ addl(ESP, Immediate(adjustment));
2708 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002709 break;
2710 }
2711
Roland Levillaincff13742014-11-17 14:32:17 +00002712 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002713 // Processing a Dex `float-to-double' instruction.
2714 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002715 break;
2716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002721 break;
2722
2723 default:
2724 LOG(FATAL) << "Unexpected type conversion from " << input_type
2725 << " to " << result_type;
2726 }
2727}
2728
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002729void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002730 LocationSummary* locations =
2731 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002732 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002733 case Primitive::kPrimInt: {
2734 locations->SetInAt(0, Location::RequiresRegister());
2735 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2736 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2737 break;
2738 }
2739
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002740 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002741 locations->SetInAt(0, Location::RequiresRegister());
2742 locations->SetInAt(1, Location::Any());
2743 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002744 break;
2745 }
2746
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002747 case Primitive::kPrimFloat:
2748 case Primitive::kPrimDouble: {
2749 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002750 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2751 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002752 } else if (add->InputAt(1)->IsConstant()) {
2753 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002754 } else {
2755 locations->SetInAt(1, Location::Any());
2756 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002757 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002758 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002759 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002760
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002761 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002762 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2763 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002764 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002765}
2766
2767void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2768 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002769 Location first = locations->InAt(0);
2770 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002771 Location out = locations->Out();
2772
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002773 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002774 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002775 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002776 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2777 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002778 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2779 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002780 } else {
2781 __ leal(out.AsRegister<Register>(), Address(
2782 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2783 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002784 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002785 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2786 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2787 __ addl(out.AsRegister<Register>(), Immediate(value));
2788 } else {
2789 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2790 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002791 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002792 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002793 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002794 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002795 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002796 }
2797
2798 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002799 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002800 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2801 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002802 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002803 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2804 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002805 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002806 } else {
2807 DCHECK(second.IsConstant()) << second;
2808 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2809 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2810 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002811 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002812 break;
2813 }
2814
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002815 case Primitive::kPrimFloat: {
2816 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002817 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002818 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2819 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002820 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002821 __ addss(first.AsFpuRegister<XmmRegister>(),
2822 codegen_->LiteralFloatAddress(
2823 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2824 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2825 } else {
2826 DCHECK(second.IsStackSlot());
2827 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002828 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002829 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002830 }
2831
2832 case Primitive::kPrimDouble: {
2833 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002834 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002835 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2836 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002837 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002838 __ addsd(first.AsFpuRegister<XmmRegister>(),
2839 codegen_->LiteralDoubleAddress(
2840 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2841 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2842 } else {
2843 DCHECK(second.IsDoubleStackSlot());
2844 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002845 }
2846 break;
2847 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002848
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002849 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002850 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002851 }
2852}
2853
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002854void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002855 LocationSummary* locations =
2856 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002857 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002858 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002859 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002860 locations->SetInAt(0, Location::RequiresRegister());
2861 locations->SetInAt(1, Location::Any());
2862 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002863 break;
2864 }
Calin Juravle11351682014-10-23 15:38:15 +01002865 case Primitive::kPrimFloat:
2866 case Primitive::kPrimDouble: {
2867 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002868 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2869 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002870 } else if (sub->InputAt(1)->IsConstant()) {
2871 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002872 } else {
2873 locations->SetInAt(1, Location::Any());
2874 }
Calin Juravle11351682014-10-23 15:38:15 +01002875 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002876 break;
Calin Juravle11351682014-10-23 15:38:15 +01002877 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002878
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002879 default:
Calin Juravle11351682014-10-23 15:38:15 +01002880 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002881 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002882}
2883
2884void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2885 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002886 Location first = locations->InAt(0);
2887 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002888 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002889 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002890 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002891 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002892 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002893 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002894 __ subl(first.AsRegister<Register>(),
2895 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002896 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002897 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002898 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002899 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002900 }
2901
2902 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002903 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002904 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2905 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002906 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002907 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002908 __ sbbl(first.AsRegisterPairHigh<Register>(),
2909 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002910 } else {
2911 DCHECK(second.IsConstant()) << second;
2912 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2913 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2914 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002915 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002916 break;
2917 }
2918
Calin Juravle11351682014-10-23 15:38:15 +01002919 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002920 if (second.IsFpuRegister()) {
2921 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2922 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2923 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002924 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002925 __ subss(first.AsFpuRegister<XmmRegister>(),
2926 codegen_->LiteralFloatAddress(
2927 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2928 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2929 } else {
2930 DCHECK(second.IsStackSlot());
2931 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2932 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002933 break;
Calin Juravle11351682014-10-23 15:38:15 +01002934 }
2935
2936 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002937 if (second.IsFpuRegister()) {
2938 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2939 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2940 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002941 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002942 __ subsd(first.AsFpuRegister<XmmRegister>(),
2943 codegen_->LiteralDoubleAddress(
2944 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2945 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2946 } else {
2947 DCHECK(second.IsDoubleStackSlot());
2948 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2949 }
Calin Juravle11351682014-10-23 15:38:15 +01002950 break;
2951 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002952
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002953 default:
Calin Juravle11351682014-10-23 15:38:15 +01002954 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002955 }
2956}
2957
Calin Juravle34bacdf2014-10-07 20:23:36 +01002958void LocationsBuilderX86::VisitMul(HMul* mul) {
2959 LocationSummary* locations =
2960 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2961 switch (mul->GetResultType()) {
2962 case Primitive::kPrimInt:
2963 locations->SetInAt(0, Location::RequiresRegister());
2964 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002965 if (mul->InputAt(1)->IsIntConstant()) {
2966 // Can use 3 operand multiply.
2967 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2968 } else {
2969 locations->SetOut(Location::SameAsFirstInput());
2970 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002971 break;
2972 case Primitive::kPrimLong: {
2973 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002974 locations->SetInAt(1, Location::Any());
2975 locations->SetOut(Location::SameAsFirstInput());
2976 // Needed for imul on 32bits with 64bits output.
2977 locations->AddTemp(Location::RegisterLocation(EAX));
2978 locations->AddTemp(Location::RegisterLocation(EDX));
2979 break;
2980 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002981 case Primitive::kPrimFloat:
2982 case Primitive::kPrimDouble: {
2983 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002984 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2985 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002986 } else if (mul->InputAt(1)->IsConstant()) {
2987 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002988 } else {
2989 locations->SetInAt(1, Location::Any());
2990 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002991 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002992 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002993 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002994
2995 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002996 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002997 }
2998}
2999
3000void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3001 LocationSummary* locations = mul->GetLocations();
3002 Location first = locations->InAt(0);
3003 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003004 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003005
3006 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003007 case Primitive::kPrimInt:
3008 // The constant may have ended up in a register, so test explicitly to avoid
3009 // problems where the output may not be the same as the first operand.
3010 if (mul->InputAt(1)->IsIntConstant()) {
3011 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3012 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3013 } else if (second.IsRegister()) {
3014 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003015 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003016 } else {
3017 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003018 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003019 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003020 }
3021 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003022
3023 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003024 Register in1_hi = first.AsRegisterPairHigh<Register>();
3025 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003026 Register eax = locations->GetTemp(0).AsRegister<Register>();
3027 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003028
3029 DCHECK_EQ(EAX, eax);
3030 DCHECK_EQ(EDX, edx);
3031
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003032 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033 // output: in1
3034 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3035 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3036 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003037 if (second.IsConstant()) {
3038 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003039
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003040 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3041 int32_t low_value = Low32Bits(value);
3042 int32_t high_value = High32Bits(value);
3043 Immediate low(low_value);
3044 Immediate high(high_value);
3045
3046 __ movl(eax, high);
3047 // eax <- in1.lo * in2.hi
3048 __ imull(eax, in1_lo);
3049 // in1.hi <- in1.hi * in2.lo
3050 __ imull(in1_hi, low);
3051 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3052 __ addl(in1_hi, eax);
3053 // move in2_lo to eax to prepare for double precision
3054 __ movl(eax, low);
3055 // edx:eax <- in1.lo * in2.lo
3056 __ mull(in1_lo);
3057 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3058 __ addl(in1_hi, edx);
3059 // in1.lo <- (in1.lo * in2.lo)[31:0];
3060 __ movl(in1_lo, eax);
3061 } else if (second.IsRegisterPair()) {
3062 Register in2_hi = second.AsRegisterPairHigh<Register>();
3063 Register in2_lo = second.AsRegisterPairLow<Register>();
3064
3065 __ movl(eax, in2_hi);
3066 // eax <- in1.lo * in2.hi
3067 __ imull(eax, in1_lo);
3068 // in1.hi <- in1.hi * in2.lo
3069 __ imull(in1_hi, in2_lo);
3070 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3071 __ addl(in1_hi, eax);
3072 // move in1_lo to eax to prepare for double precision
3073 __ movl(eax, in1_lo);
3074 // edx:eax <- in1.lo * in2.lo
3075 __ mull(in2_lo);
3076 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3077 __ addl(in1_hi, edx);
3078 // in1.lo <- (in1.lo * in2.lo)[31:0];
3079 __ movl(in1_lo, eax);
3080 } else {
3081 DCHECK(second.IsDoubleStackSlot()) << second;
3082 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3083 Address in2_lo(ESP, second.GetStackIndex());
3084
3085 __ movl(eax, in2_hi);
3086 // eax <- in1.lo * in2.hi
3087 __ imull(eax, in1_lo);
3088 // in1.hi <- in1.hi * in2.lo
3089 __ imull(in1_hi, in2_lo);
3090 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3091 __ addl(in1_hi, eax);
3092 // move in1_lo to eax to prepare for double precision
3093 __ movl(eax, in1_lo);
3094 // edx:eax <- in1.lo * in2.lo
3095 __ mull(in2_lo);
3096 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3097 __ addl(in1_hi, edx);
3098 // in1.lo <- (in1.lo * in2.lo)[31:0];
3099 __ movl(in1_lo, eax);
3100 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003101
3102 break;
3103 }
3104
Calin Juravleb5bfa962014-10-21 18:02:24 +01003105 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003106 DCHECK(first.Equals(locations->Out()));
3107 if (second.IsFpuRegister()) {
3108 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3109 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3110 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003111 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003112 __ mulss(first.AsFpuRegister<XmmRegister>(),
3113 codegen_->LiteralFloatAddress(
3114 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3115 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3116 } else {
3117 DCHECK(second.IsStackSlot());
3118 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3119 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003120 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003121 }
3122
3123 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003124 DCHECK(first.Equals(locations->Out()));
3125 if (second.IsFpuRegister()) {
3126 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3127 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3128 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003129 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003130 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3131 codegen_->LiteralDoubleAddress(
3132 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3133 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3134 } else {
3135 DCHECK(second.IsDoubleStackSlot());
3136 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3137 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003138 break;
3139 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003140
3141 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003142 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003143 }
3144}
3145
Roland Levillain232ade02015-04-20 15:14:36 +01003146void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3147 uint32_t temp_offset,
3148 uint32_t stack_adjustment,
3149 bool is_fp,
3150 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003151 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003152 DCHECK(!is_wide);
3153 if (is_fp) {
3154 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3155 } else {
3156 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3157 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003158 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003159 DCHECK(is_wide);
3160 if (is_fp) {
3161 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3162 } else {
3163 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3164 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003165 } else {
3166 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003167 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003168 Location stack_temp = Location::StackSlot(temp_offset);
3169 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003170 if (is_fp) {
3171 __ flds(Address(ESP, temp_offset));
3172 } else {
3173 __ filds(Address(ESP, temp_offset));
3174 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003175 } else {
3176 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3177 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003178 if (is_fp) {
3179 __ fldl(Address(ESP, temp_offset));
3180 } else {
3181 __ fildl(Address(ESP, temp_offset));
3182 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003183 }
3184 }
3185}
3186
3187void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3188 Primitive::Type type = rem->GetResultType();
3189 bool is_float = type == Primitive::kPrimFloat;
3190 size_t elem_size = Primitive::ComponentSize(type);
3191 LocationSummary* locations = rem->GetLocations();
3192 Location first = locations->InAt(0);
3193 Location second = locations->InAt(1);
3194 Location out = locations->Out();
3195
3196 // Create stack space for 2 elements.
3197 // TODO: enhance register allocator to ask for stack temporaries.
3198 __ subl(ESP, Immediate(2 * elem_size));
3199
3200 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003201 const bool is_wide = !is_float;
3202 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3203 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003204
3205 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003206 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003207 __ Bind(&retry);
3208 __ fprem();
3209
3210 // Move FP status to AX.
3211 __ fstsw();
3212
3213 // And see if the argument reduction is complete. This is signaled by the
3214 // C2 FPU flag bit set to 0.
3215 __ andl(EAX, Immediate(kC2ConditionMask));
3216 __ j(kNotEqual, &retry);
3217
3218 // We have settled on the final value. Retrieve it into an XMM register.
3219 // Store FP top of stack to real stack.
3220 if (is_float) {
3221 __ fsts(Address(ESP, 0));
3222 } else {
3223 __ fstl(Address(ESP, 0));
3224 }
3225
3226 // Pop the 2 items from the FP stack.
3227 __ fucompp();
3228
3229 // Load the value from the stack into an XMM register.
3230 DCHECK(out.IsFpuRegister()) << out;
3231 if (is_float) {
3232 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3233 } else {
3234 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3235 }
3236
3237 // And remove the temporary stack space we allocated.
3238 __ addl(ESP, Immediate(2 * elem_size));
3239}
3240
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003241
3242void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3243 DCHECK(instruction->IsDiv() || instruction->IsRem());
3244
3245 LocationSummary* locations = instruction->GetLocations();
3246 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003247 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003248
3249 Register out_register = locations->Out().AsRegister<Register>();
3250 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003251 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003252
3253 DCHECK(imm == 1 || imm == -1);
3254
3255 if (instruction->IsRem()) {
3256 __ xorl(out_register, out_register);
3257 } else {
3258 __ movl(out_register, input_register);
3259 if (imm == -1) {
3260 __ negl(out_register);
3261 }
3262 }
3263}
3264
3265
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003266void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003267 LocationSummary* locations = instruction->GetLocations();
3268
3269 Register out_register = locations->Out().AsRegister<Register>();
3270 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003271 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003272 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3273 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003274
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003275 Register num = locations->GetTemp(0).AsRegister<Register>();
3276
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003277 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003278 __ testl(input_register, input_register);
3279 __ cmovl(kGreaterEqual, num, input_register);
3280 int shift = CTZ(imm);
3281 __ sarl(num, Immediate(shift));
3282
3283 if (imm < 0) {
3284 __ negl(num);
3285 }
3286
3287 __ movl(out_register, num);
3288}
3289
3290void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3291 DCHECK(instruction->IsDiv() || instruction->IsRem());
3292
3293 LocationSummary* locations = instruction->GetLocations();
3294 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3295
3296 Register eax = locations->InAt(0).AsRegister<Register>();
3297 Register out = locations->Out().AsRegister<Register>();
3298 Register num;
3299 Register edx;
3300
3301 if (instruction->IsDiv()) {
3302 edx = locations->GetTemp(0).AsRegister<Register>();
3303 num = locations->GetTemp(1).AsRegister<Register>();
3304 } else {
3305 edx = locations->Out().AsRegister<Register>();
3306 num = locations->GetTemp(0).AsRegister<Register>();
3307 }
3308
3309 DCHECK_EQ(EAX, eax);
3310 DCHECK_EQ(EDX, edx);
3311 if (instruction->IsDiv()) {
3312 DCHECK_EQ(EAX, out);
3313 } else {
3314 DCHECK_EQ(EDX, out);
3315 }
3316
3317 int64_t magic;
3318 int shift;
3319 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3320
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003321 // Save the numerator.
3322 __ movl(num, eax);
3323
3324 // EAX = magic
3325 __ movl(eax, Immediate(magic));
3326
3327 // EDX:EAX = magic * numerator
3328 __ imull(num);
3329
3330 if (imm > 0 && magic < 0) {
3331 // EDX += num
3332 __ addl(edx, num);
3333 } else if (imm < 0 && magic > 0) {
3334 __ subl(edx, num);
3335 }
3336
3337 // Shift if needed.
3338 if (shift != 0) {
3339 __ sarl(edx, Immediate(shift));
3340 }
3341
3342 // EDX += 1 if EDX < 0
3343 __ movl(eax, edx);
3344 __ shrl(edx, Immediate(31));
3345 __ addl(edx, eax);
3346
3347 if (instruction->IsRem()) {
3348 __ movl(eax, num);
3349 __ imull(edx, Immediate(imm));
3350 __ subl(eax, edx);
3351 __ movl(edx, eax);
3352 } else {
3353 __ movl(eax, edx);
3354 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003355}
3356
Calin Juravlebacfec32014-11-14 15:54:36 +00003357void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3358 DCHECK(instruction->IsDiv() || instruction->IsRem());
3359
3360 LocationSummary* locations = instruction->GetLocations();
3361 Location out = locations->Out();
3362 Location first = locations->InAt(0);
3363 Location second = locations->InAt(1);
3364 bool is_div = instruction->IsDiv();
3365
3366 switch (instruction->GetResultType()) {
3367 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003368 DCHECK_EQ(EAX, first.AsRegister<Register>());
3369 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003370
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003371 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003372 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003373
3374 if (imm == 0) {
3375 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3376 } else if (imm == 1 || imm == -1) {
3377 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003378 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003379 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003380 } else {
3381 DCHECK(imm <= -2 || imm >= 2);
3382 GenerateDivRemWithAnyConstant(instruction);
3383 }
3384 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003385 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3386 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003387 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003388
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003389 Register second_reg = second.AsRegister<Register>();
3390 // 0x80000000/-1 triggers an arithmetic exception!
3391 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3392 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003393
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003394 __ cmpl(second_reg, Immediate(-1));
3395 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003396
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003397 // edx:eax <- sign-extended of eax
3398 __ cdq();
3399 // eax = quotient, edx = remainder
3400 __ idivl(second_reg);
3401 __ Bind(slow_path->GetExitLabel());
3402 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003403 break;
3404 }
3405
3406 case Primitive::kPrimLong: {
3407 InvokeRuntimeCallingConvention calling_convention;
3408 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3409 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3410 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3411 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3412 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3413 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3414
3415 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003416 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3417 instruction,
3418 instruction->GetDexPc(),
3419 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003420 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003421 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003422 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3423 instruction,
3424 instruction->GetDexPc(),
3425 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003426 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003427 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003428 break;
3429 }
3430
3431 default:
3432 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3433 }
3434}
3435
Calin Juravle7c4954d2014-10-28 16:57:40 +00003436void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003437 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003438 ? LocationSummary::kCall
3439 : LocationSummary::kNoCall;
3440 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3441
Calin Juravle7c4954d2014-10-28 16:57:40 +00003442 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003443 case Primitive::kPrimInt: {
3444 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003445 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003446 locations->SetOut(Location::SameAsFirstInput());
3447 // Intel uses edx:eax as the dividend.
3448 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003449 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3450 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3451 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003452 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003453 locations->AddTemp(Location::RequiresRegister());
3454 }
Calin Juravled0d48522014-11-04 16:40:20 +00003455 break;
3456 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003457 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003458 InvokeRuntimeCallingConvention calling_convention;
3459 locations->SetInAt(0, Location::RegisterPairLocation(
3460 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3461 locations->SetInAt(1, Location::RegisterPairLocation(
3462 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3463 // Runtime helper puts the result in EAX, EDX.
3464 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003465 break;
3466 }
3467 case Primitive::kPrimFloat:
3468 case Primitive::kPrimDouble: {
3469 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003470 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3471 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003472 } else if (div->InputAt(1)->IsConstant()) {
3473 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003474 } else {
3475 locations->SetInAt(1, Location::Any());
3476 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003477 locations->SetOut(Location::SameAsFirstInput());
3478 break;
3479 }
3480
3481 default:
3482 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3483 }
3484}
3485
3486void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3487 LocationSummary* locations = div->GetLocations();
3488 Location first = locations->InAt(0);
3489 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003490
3491 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003492 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003493 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003494 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003495 break;
3496 }
3497
3498 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003499 if (second.IsFpuRegister()) {
3500 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3501 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3502 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003503 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003504 __ divss(first.AsFpuRegister<XmmRegister>(),
3505 codegen_->LiteralFloatAddress(
3506 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3507 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3508 } else {
3509 DCHECK(second.IsStackSlot());
3510 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3511 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003512 break;
3513 }
3514
3515 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003516 if (second.IsFpuRegister()) {
3517 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3518 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3519 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003520 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003521 __ divsd(first.AsFpuRegister<XmmRegister>(),
3522 codegen_->LiteralDoubleAddress(
3523 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3524 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3525 } else {
3526 DCHECK(second.IsDoubleStackSlot());
3527 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3528 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003529 break;
3530 }
3531
3532 default:
3533 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3534 }
3535}
3536
Calin Juravlebacfec32014-11-14 15:54:36 +00003537void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003538 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003540 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3541 ? LocationSummary::kCall
3542 : LocationSummary::kNoCall;
3543 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003544
Calin Juravled2ec87d2014-12-08 14:24:46 +00003545 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003546 case Primitive::kPrimInt: {
3547 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003549 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003550 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3551 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3552 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003553 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003554 locations->AddTemp(Location::RequiresRegister());
3555 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003556 break;
3557 }
3558 case Primitive::kPrimLong: {
3559 InvokeRuntimeCallingConvention calling_convention;
3560 locations->SetInAt(0, Location::RegisterPairLocation(
3561 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3562 locations->SetInAt(1, Location::RegisterPairLocation(
3563 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3564 // Runtime helper puts the result in EAX, EDX.
3565 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3566 break;
3567 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003568 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003569 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003570 locations->SetInAt(0, Location::Any());
3571 locations->SetInAt(1, Location::Any());
3572 locations->SetOut(Location::RequiresFpuRegister());
3573 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003574 break;
3575 }
3576
3577 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003578 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003579 }
3580}
3581
3582void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3583 Primitive::Type type = rem->GetResultType();
3584 switch (type) {
3585 case Primitive::kPrimInt:
3586 case Primitive::kPrimLong: {
3587 GenerateDivRemIntegral(rem);
3588 break;
3589 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003590 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003591 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003592 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003593 break;
3594 }
3595 default:
3596 LOG(FATAL) << "Unexpected rem type " << type;
3597 }
3598}
3599
Calin Juravled0d48522014-11-04 16:40:20 +00003600void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003601 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3602 ? LocationSummary::kCallOnSlowPath
3603 : LocationSummary::kNoCall;
3604 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003605 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003606 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003607 case Primitive::kPrimByte:
3608 case Primitive::kPrimChar:
3609 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003610 case Primitive::kPrimInt: {
3611 locations->SetInAt(0, Location::Any());
3612 break;
3613 }
3614 case Primitive::kPrimLong: {
3615 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3616 if (!instruction->IsConstant()) {
3617 locations->AddTemp(Location::RequiresRegister());
3618 }
3619 break;
3620 }
3621 default:
3622 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3623 }
Calin Juravled0d48522014-11-04 16:40:20 +00003624 if (instruction->HasUses()) {
3625 locations->SetOut(Location::SameAsFirstInput());
3626 }
3627}
3628
3629void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003630 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003631 codegen_->AddSlowPath(slow_path);
3632
3633 LocationSummary* locations = instruction->GetLocations();
3634 Location value = locations->InAt(0);
3635
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003636 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003637 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003638 case Primitive::kPrimByte:
3639 case Primitive::kPrimChar:
3640 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003641 case Primitive::kPrimInt: {
3642 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003643 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003644 __ j(kEqual, slow_path->GetEntryLabel());
3645 } else if (value.IsStackSlot()) {
3646 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3647 __ j(kEqual, slow_path->GetEntryLabel());
3648 } else {
3649 DCHECK(value.IsConstant()) << value;
3650 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3651 __ jmp(slow_path->GetEntryLabel());
3652 }
3653 }
3654 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003655 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003656 case Primitive::kPrimLong: {
3657 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003658 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003659 __ movl(temp, value.AsRegisterPairLow<Register>());
3660 __ orl(temp, value.AsRegisterPairHigh<Register>());
3661 __ j(kEqual, slow_path->GetEntryLabel());
3662 } else {
3663 DCHECK(value.IsConstant()) << value;
3664 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3665 __ jmp(slow_path->GetEntryLabel());
3666 }
3667 }
3668 break;
3669 }
3670 default:
3671 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003672 }
Calin Juravled0d48522014-11-04 16:40:20 +00003673}
3674
Calin Juravle9aec02f2014-11-18 23:06:35 +00003675void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3676 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3677
3678 LocationSummary* locations =
3679 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3680
3681 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003682 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003683 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003684 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003685 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003686 // The shift count needs to be in CL or a constant.
3687 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003688 locations->SetOut(Location::SameAsFirstInput());
3689 break;
3690 }
3691 default:
3692 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3693 }
3694}
3695
3696void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3697 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3698
3699 LocationSummary* locations = op->GetLocations();
3700 Location first = locations->InAt(0);
3701 Location second = locations->InAt(1);
3702 DCHECK(first.Equals(locations->Out()));
3703
3704 switch (op->GetResultType()) {
3705 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003706 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003707 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003709 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003710 DCHECK_EQ(ECX, second_reg);
3711 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003712 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003713 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003714 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003715 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003716 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003717 }
3718 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003719 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003720 if (shift == 0) {
3721 return;
3722 }
3723 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003724 if (op->IsShl()) {
3725 __ shll(first_reg, imm);
3726 } else if (op->IsShr()) {
3727 __ sarl(first_reg, imm);
3728 } else {
3729 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003730 }
3731 }
3732 break;
3733 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003734 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003735 if (second.IsRegister()) {
3736 Register second_reg = second.AsRegister<Register>();
3737 DCHECK_EQ(ECX, second_reg);
3738 if (op->IsShl()) {
3739 GenerateShlLong(first, second_reg);
3740 } else if (op->IsShr()) {
3741 GenerateShrLong(first, second_reg);
3742 } else {
3743 GenerateUShrLong(first, second_reg);
3744 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003745 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003746 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003747 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003748 // Nothing to do if the shift is 0, as the input is already the output.
3749 if (shift != 0) {
3750 if (op->IsShl()) {
3751 GenerateShlLong(first, shift);
3752 } else if (op->IsShr()) {
3753 GenerateShrLong(first, shift);
3754 } else {
3755 GenerateUShrLong(first, shift);
3756 }
3757 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003758 }
3759 break;
3760 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003761 default:
3762 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3763 }
3764}
3765
Mark P Mendell73945692015-04-29 14:56:17 +00003766void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3767 Register low = loc.AsRegisterPairLow<Register>();
3768 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003769 if (shift == 1) {
3770 // This is just an addition.
3771 __ addl(low, low);
3772 __ adcl(high, high);
3773 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003774 // Shift by 32 is easy. High gets low, and low gets 0.
3775 codegen_->EmitParallelMoves(
3776 loc.ToLow(),
3777 loc.ToHigh(),
3778 Primitive::kPrimInt,
3779 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3780 loc.ToLow(),
3781 Primitive::kPrimInt);
3782 } else if (shift > 32) {
3783 // Low part becomes 0. High part is low part << (shift-32).
3784 __ movl(high, low);
3785 __ shll(high, Immediate(shift - 32));
3786 __ xorl(low, low);
3787 } else {
3788 // Between 1 and 31.
3789 __ shld(high, low, Immediate(shift));
3790 __ shll(low, Immediate(shift));
3791 }
3792}
3793
Calin Juravle9aec02f2014-11-18 23:06:35 +00003794void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003795 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003796 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3797 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3798 __ testl(shifter, Immediate(32));
3799 __ j(kEqual, &done);
3800 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3801 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3802 __ Bind(&done);
3803}
3804
Mark P Mendell73945692015-04-29 14:56:17 +00003805void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3806 Register low = loc.AsRegisterPairLow<Register>();
3807 Register high = loc.AsRegisterPairHigh<Register>();
3808 if (shift == 32) {
3809 // Need to copy the sign.
3810 DCHECK_NE(low, high);
3811 __ movl(low, high);
3812 __ sarl(high, Immediate(31));
3813 } else if (shift > 32) {
3814 DCHECK_NE(low, high);
3815 // High part becomes sign. Low part is shifted by shift - 32.
3816 __ movl(low, high);
3817 __ sarl(high, Immediate(31));
3818 __ sarl(low, Immediate(shift - 32));
3819 } else {
3820 // Between 1 and 31.
3821 __ shrd(low, high, Immediate(shift));
3822 __ sarl(high, Immediate(shift));
3823 }
3824}
3825
Calin Juravle9aec02f2014-11-18 23:06:35 +00003826void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003827 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003828 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3829 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3830 __ testl(shifter, Immediate(32));
3831 __ j(kEqual, &done);
3832 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3833 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3834 __ Bind(&done);
3835}
3836
Mark P Mendell73945692015-04-29 14:56:17 +00003837void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3838 Register low = loc.AsRegisterPairLow<Register>();
3839 Register high = loc.AsRegisterPairHigh<Register>();
3840 if (shift == 32) {
3841 // Shift by 32 is easy. Low gets high, and high gets 0.
3842 codegen_->EmitParallelMoves(
3843 loc.ToHigh(),
3844 loc.ToLow(),
3845 Primitive::kPrimInt,
3846 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3847 loc.ToHigh(),
3848 Primitive::kPrimInt);
3849 } else if (shift > 32) {
3850 // Low part is high >> (shift - 32). High part becomes 0.
3851 __ movl(low, high);
3852 __ shrl(low, Immediate(shift - 32));
3853 __ xorl(high, high);
3854 } else {
3855 // Between 1 and 31.
3856 __ shrd(low, high, Immediate(shift));
3857 __ shrl(high, Immediate(shift));
3858 }
3859}
3860
Calin Juravle9aec02f2014-11-18 23:06:35 +00003861void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003862 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003863 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3864 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3865 __ testl(shifter, Immediate(32));
3866 __ j(kEqual, &done);
3867 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3868 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3869 __ Bind(&done);
3870}
3871
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003872void LocationsBuilderX86::VisitRor(HRor* ror) {
3873 LocationSummary* locations =
3874 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3875
3876 switch (ror->GetResultType()) {
3877 case Primitive::kPrimLong:
3878 // Add the temporary needed.
3879 locations->AddTemp(Location::RequiresRegister());
3880 FALLTHROUGH_INTENDED;
3881 case Primitive::kPrimInt:
3882 locations->SetInAt(0, Location::RequiresRegister());
3883 // The shift count needs to be in CL (unless it is a constant).
3884 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3885 locations->SetOut(Location::SameAsFirstInput());
3886 break;
3887 default:
3888 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3889 UNREACHABLE();
3890 }
3891}
3892
3893void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3894 LocationSummary* locations = ror->GetLocations();
3895 Location first = locations->InAt(0);
3896 Location second = locations->InAt(1);
3897
3898 if (ror->GetResultType() == Primitive::kPrimInt) {
3899 Register first_reg = first.AsRegister<Register>();
3900 if (second.IsRegister()) {
3901 Register second_reg = second.AsRegister<Register>();
3902 __ rorl(first_reg, second_reg);
3903 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003904 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003905 __ rorl(first_reg, imm);
3906 }
3907 return;
3908 }
3909
3910 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3911 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3912 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3913 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3914 if (second.IsRegister()) {
3915 Register second_reg = second.AsRegister<Register>();
3916 DCHECK_EQ(second_reg, ECX);
3917 __ movl(temp_reg, first_reg_hi);
3918 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3919 __ shrd(first_reg_lo, temp_reg, second_reg);
3920 __ movl(temp_reg, first_reg_hi);
3921 __ testl(second_reg, Immediate(32));
3922 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3923 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3924 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003925 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003926 if (shift_amt == 0) {
3927 // Already fine.
3928 return;
3929 }
3930 if (shift_amt == 32) {
3931 // Just swap.
3932 __ movl(temp_reg, first_reg_lo);
3933 __ movl(first_reg_lo, first_reg_hi);
3934 __ movl(first_reg_hi, temp_reg);
3935 return;
3936 }
3937
3938 Immediate imm(shift_amt);
3939 // Save the constents of the low value.
3940 __ movl(temp_reg, first_reg_lo);
3941
3942 // Shift right into low, feeding bits from high.
3943 __ shrd(first_reg_lo, first_reg_hi, imm);
3944
3945 // Shift right into high, feeding bits from the original low.
3946 __ shrd(first_reg_hi, temp_reg, imm);
3947
3948 // Swap if needed.
3949 if (shift_amt > 32) {
3950 __ movl(temp_reg, first_reg_lo);
3951 __ movl(first_reg_lo, first_reg_hi);
3952 __ movl(first_reg_hi, temp_reg);
3953 }
3954 }
3955}
3956
Calin Juravle9aec02f2014-11-18 23:06:35 +00003957void LocationsBuilderX86::VisitShl(HShl* shl) {
3958 HandleShift(shl);
3959}
3960
3961void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3962 HandleShift(shl);
3963}
3964
3965void LocationsBuilderX86::VisitShr(HShr* shr) {
3966 HandleShift(shr);
3967}
3968
3969void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3970 HandleShift(shr);
3971}
3972
3973void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3974 HandleShift(ushr);
3975}
3976
3977void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3978 HandleShift(ushr);
3979}
3980
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003981void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003982 LocationSummary* locations =
3983 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003984 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003985 if (instruction->IsStringAlloc()) {
3986 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3987 } else {
3988 InvokeRuntimeCallingConvention calling_convention;
3989 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3990 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3991 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003992}
3993
3994void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003995 // Note: if heap poisoning is enabled, the entry point takes cares
3996 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003997 if (instruction->IsStringAlloc()) {
3998 // String is allocated through StringFactory. Call NewEmptyString entry point.
3999 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
4000 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
4001 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4002 __ call(Address(temp, code_offset.Int32Value()));
4003 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4004 } else {
4005 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4006 instruction,
4007 instruction->GetDexPc(),
4008 nullptr);
4009 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4010 DCHECK(!codegen_->IsLeafMethod());
4011 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004012}
4013
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004014void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4015 LocationSummary* locations =
4016 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4017 locations->SetOut(Location::RegisterLocation(EAX));
4018 InvokeRuntimeCallingConvention calling_convention;
4019 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004020 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004021 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004022}
4023
4024void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4025 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004026 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004027 // Note: if heap poisoning is enabled, the entry point takes cares
4028 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004029 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4030 instruction,
4031 instruction->GetDexPc(),
4032 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004033 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004034 DCHECK(!codegen_->IsLeafMethod());
4035}
4036
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004037void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004038 LocationSummary* locations =
4039 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004040 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4041 if (location.IsStackSlot()) {
4042 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4043 } else if (location.IsDoubleStackSlot()) {
4044 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004045 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004046 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004047}
4048
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004049void InstructionCodeGeneratorX86::VisitParameterValue(
4050 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4051}
4052
4053void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4054 LocationSummary* locations =
4055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4056 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4057}
4058
4059void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004060}
4061
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004062void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4063 LocationSummary* locations =
4064 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4065 locations->SetInAt(0, Location::RequiresRegister());
4066 locations->SetOut(Location::RequiresRegister());
4067}
4068
4069void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4070 LocationSummary* locations = instruction->GetLocations();
4071 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004072 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004073 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4074 instruction->GetIndex(), kX86PointerSize).SizeValue();
4075 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03004076 __ movl(locations->InAt(0).AsRegister<Register>(),
4077 Address(locations->InAt(0).AsRegister<Register>(),
4078 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4079 // temp = temp->GetImtEntryAt(method_offset);
4080 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07004081 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004082 }
4083 __ movl(locations->Out().AsRegister<Register>(),
4084 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4085}
4086
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004087void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004088 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004089 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004090 locations->SetInAt(0, Location::RequiresRegister());
4091 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004092}
4093
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004094void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4095 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004096 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004097 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004098 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004099 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004100 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004101 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004102 break;
4103
4104 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004105 __ notl(out.AsRegisterPairLow<Register>());
4106 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004107 break;
4108
4109 default:
4110 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4111 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004112}
4113
David Brazdil66d126e2015-04-03 16:02:44 +01004114void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4115 LocationSummary* locations =
4116 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4117 locations->SetInAt(0, Location::RequiresRegister());
4118 locations->SetOut(Location::SameAsFirstInput());
4119}
4120
4121void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004122 LocationSummary* locations = bool_not->GetLocations();
4123 Location in = locations->InAt(0);
4124 Location out = locations->Out();
4125 DCHECK(in.Equals(out));
4126 __ xorl(out.AsRegister<Register>(), Immediate(1));
4127}
4128
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004129void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004130 LocationSummary* locations =
4131 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004132 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004133 case Primitive::kPrimBoolean:
4134 case Primitive::kPrimByte:
4135 case Primitive::kPrimShort:
4136 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004137 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004138 case Primitive::kPrimLong: {
4139 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004140 locations->SetInAt(1, Location::Any());
4141 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4142 break;
4143 }
4144 case Primitive::kPrimFloat:
4145 case Primitive::kPrimDouble: {
4146 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004147 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4148 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4149 } else if (compare->InputAt(1)->IsConstant()) {
4150 locations->SetInAt(1, Location::RequiresFpuRegister());
4151 } else {
4152 locations->SetInAt(1, Location::Any());
4153 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004154 locations->SetOut(Location::RequiresRegister());
4155 break;
4156 }
4157 default:
4158 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4159 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004160}
4161
4162void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004163 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004164 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004165 Location left = locations->InAt(0);
4166 Location right = locations->InAt(1);
4167
Mark Mendell0c9497d2015-08-21 09:30:05 -04004168 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004169 Condition less_cond = kLess;
4170
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004171 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004172 case Primitive::kPrimBoolean:
4173 case Primitive::kPrimByte:
4174 case Primitive::kPrimShort:
4175 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004176 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004177 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004178 break;
4179 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004180 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004181 Register left_low = left.AsRegisterPairLow<Register>();
4182 Register left_high = left.AsRegisterPairHigh<Register>();
4183 int32_t val_low = 0;
4184 int32_t val_high = 0;
4185 bool right_is_const = false;
4186
4187 if (right.IsConstant()) {
4188 DCHECK(right.GetConstant()->IsLongConstant());
4189 right_is_const = true;
4190 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4191 val_low = Low32Bits(val);
4192 val_high = High32Bits(val);
4193 }
4194
Calin Juravleddb7df22014-11-25 20:56:51 +00004195 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004196 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004197 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004198 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004199 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004200 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004201 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004202 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004203 __ j(kLess, &less); // Signed compare.
4204 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004205 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004206 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004207 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004208 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004209 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004210 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004211 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004212 }
Aart Bika19616e2016-02-01 18:57:58 -08004213 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004214 break;
4215 }
4216 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004217 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004218 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004219 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004220 break;
4221 }
4222 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004223 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004224 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004225 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004226 break;
4227 }
4228 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004229 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004230 }
Aart Bika19616e2016-02-01 18:57:58 -08004231
Calin Juravleddb7df22014-11-25 20:56:51 +00004232 __ movl(out, Immediate(0));
4233 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004234 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004235
4236 __ Bind(&greater);
4237 __ movl(out, Immediate(1));
4238 __ jmp(&done);
4239
4240 __ Bind(&less);
4241 __ movl(out, Immediate(-1));
4242
4243 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004244}
4245
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004246void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004247 LocationSummary* locations =
4248 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004249 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004250 locations->SetInAt(i, Location::Any());
4251 }
4252 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004253}
4254
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004255void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004256 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004257}
4258
Roland Levillain7c1559a2015-12-15 10:55:36 +00004259void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004260 /*
4261 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4262 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4263 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4264 */
4265 switch (kind) {
4266 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004267 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004268 break;
4269 }
4270 case MemBarrierKind::kAnyStore:
4271 case MemBarrierKind::kLoadAny:
4272 case MemBarrierKind::kStoreStore: {
4273 // nop
4274 break;
4275 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004276 case MemBarrierKind::kNTStoreStore:
4277 // Non-Temporal Store/Store needs an explicit fence.
4278 MemoryFence(/* non-temporal */ true);
4279 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004280 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004281}
4282
Vladimir Markodc151b22015-10-15 18:02:30 +01004283HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4284 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4285 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004286 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4287
4288 // We disable pc-relative load when there is an irreducible loop, as the optimization
4289 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004290 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4291 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004292 if (GetGraph()->HasIrreducibleLoops() &&
4293 (dispatch_info.method_load_kind ==
4294 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4295 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4296 }
4297 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004298 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4299 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4300 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4301 // (Though the direct CALL ptr16:32 is available for consideration).
4302 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004303 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004304 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004305 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004306 0u
4307 };
4308 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004309 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004310 }
4311}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004312
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004313Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4314 Register temp) {
4315 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004316 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004317 if (!invoke->GetLocations()->Intrinsified()) {
4318 return location.AsRegister<Register>();
4319 }
4320 // For intrinsics we allow any location, so it may be on the stack.
4321 if (!location.IsRegister()) {
4322 __ movl(temp, Address(ESP, location.GetStackIndex()));
4323 return temp;
4324 }
4325 // For register locations, check if the register was saved. If so, get it from the stack.
4326 // Note: There is a chance that the register was saved but not overwritten, so we could
4327 // save one load. However, since this is just an intrinsic slow path we prefer this
4328 // simple and more robust approach rather that trying to determine if that's the case.
4329 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004330 if (slow_path != nullptr) {
4331 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4332 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4333 __ movl(temp, Address(ESP, stack_offset));
4334 return temp;
4335 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004336 }
4337 return location.AsRegister<Register>();
4338}
4339
Serguei Katkov288c7a82016-05-16 11:53:15 +06004340Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4341 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004342 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4343 switch (invoke->GetMethodLoadKind()) {
4344 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4345 // temp = thread->string_init_entrypoint
4346 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4347 break;
4348 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004349 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004350 break;
4351 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4352 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4353 break;
4354 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004355 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004356 method_patches_.emplace_back(invoke->GetTargetMethod());
4357 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4358 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004359 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4360 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4361 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004362 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004363 // Bind a new fixup label at the end of the "movl" insn.
4364 uint32_t offset = invoke->GetDexCacheArrayOffset();
4365 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004366 break;
4367 }
Vladimir Marko58155012015-08-19 12:49:41 +00004368 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004369 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004370 Register method_reg;
4371 Register reg = temp.AsRegister<Register>();
4372 if (current_method.IsRegister()) {
4373 method_reg = current_method.AsRegister<Register>();
4374 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004375 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004376 DCHECK(!current_method.IsValid());
4377 method_reg = reg;
4378 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4379 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004380 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004381 __ movl(reg, Address(method_reg,
4382 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004383 // temp = temp[index_in_cache];
4384 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4385 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004386 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4387 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004388 }
Vladimir Marko58155012015-08-19 12:49:41 +00004389 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004390 return callee_method;
4391}
4392
4393void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4394 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004395
4396 switch (invoke->GetCodePtrLocation()) {
4397 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4398 __ call(GetFrameEntryLabel());
4399 break;
4400 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4401 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4402 Label* label = &relative_call_patches_.back().label;
4403 __ call(label); // Bind to the patch label, override at link time.
4404 __ Bind(label); // Bind the label at the end of the "call" insn.
4405 break;
4406 }
4407 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4408 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004409 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4410 LOG(FATAL) << "Unsupported";
4411 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004412 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4413 // (callee_method + offset_of_quick_compiled_code)()
4414 __ call(Address(callee_method.AsRegister<Register>(),
4415 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4416 kX86WordSize).Int32Value()));
4417 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004418 }
4419
4420 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004421}
4422
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004423void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4424 Register temp = temp_in.AsRegister<Register>();
4425 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4426 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004427
4428 // Use the calling convention instead of the location of the receiver, as
4429 // intrinsics may have put the receiver in a different register. In the intrinsics
4430 // slow path, the arguments have been moved to the right place, so here we are
4431 // guaranteed that the receiver is the first register of the calling convention.
4432 InvokeDexCallingConvention calling_convention;
4433 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004434 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004435 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004436 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004437 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004438 // Instead of simply (possibly) unpoisoning `temp` here, we should
4439 // emit a read barrier for the previous class reference load.
4440 // However this is not required in practice, as this is an
4441 // intermediate/temporary reference and because the current
4442 // concurrent copying collector keeps the from-space memory
4443 // intact/accessible until the end of the marking phase (the
4444 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004445 __ MaybeUnpoisonHeapReference(temp);
4446 // temp = temp->GetMethodAt(method_offset);
4447 __ movl(temp, Address(temp, method_offset));
4448 // call temp->GetEntryPoint();
4449 __ call(Address(
4450 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4451}
4452
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004453void CodeGeneratorX86::RecordSimplePatch() {
4454 if (GetCompilerOptions().GetIncludePatchInformation()) {
4455 simple_patches_.emplace_back();
4456 __ Bind(&simple_patches_.back());
4457 }
4458}
4459
4460void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4461 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4462 __ Bind(&string_patches_.back().label);
4463}
4464
4465Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4466 uint32_t element_offset) {
4467 // Add the patch entry and bind its label at the end of the instruction.
4468 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4469 return &pc_relative_dex_cache_patches_.back().label;
4470}
4471
Vladimir Marko58155012015-08-19 12:49:41 +00004472void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4473 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004474 size_t size =
4475 method_patches_.size() +
4476 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004477 pc_relative_dex_cache_patches_.size() +
4478 simple_patches_.size() +
4479 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004480 linker_patches->reserve(size);
4481 // The label points to the end of the "movl" insn but the literal offset for method
4482 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4483 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004484 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004485 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004486 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4487 info.target_method.dex_file,
4488 info.target_method.dex_method_index));
4489 }
4490 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004491 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004492 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4493 info.target_method.dex_file,
4494 info.target_method.dex_method_index));
4495 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004496 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4497 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4498 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4499 &info.target_dex_file,
4500 GetMethodAddressOffset(),
4501 info.element_offset));
4502 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004503 for (const Label& label : simple_patches_) {
4504 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4505 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4506 }
4507 if (GetCompilerOptions().GetCompilePic()) {
4508 for (const StringPatchInfo<Label>& info : string_patches_) {
4509 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4510 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4511 &info.dex_file,
4512 GetMethodAddressOffset(),
4513 info.string_index));
4514 }
4515 } else {
4516 for (const StringPatchInfo<Label>& info : string_patches_) {
4517 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4518 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4519 &info.dex_file,
4520 info.string_index));
4521 }
4522 }
Vladimir Marko58155012015-08-19 12:49:41 +00004523}
4524
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004525void CodeGeneratorX86::MarkGCCard(Register temp,
4526 Register card,
4527 Register object,
4528 Register value,
4529 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004530 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004531 if (value_can_be_null) {
4532 __ testl(value, value);
4533 __ j(kEqual, &is_null);
4534 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004535 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4536 __ movl(temp, object);
4537 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004538 __ movb(Address(temp, card, TIMES_1, 0),
4539 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004540 if (value_can_be_null) {
4541 __ Bind(&is_null);
4542 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004543}
4544
Calin Juravle52c48962014-12-16 17:02:57 +00004545void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4546 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004547
4548 bool object_field_get_with_read_barrier =
4549 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004550 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004551 new (GetGraph()->GetArena()) LocationSummary(instruction,
4552 kEmitCompilerReadBarrier ?
4553 LocationSummary::kCallOnSlowPath :
4554 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004555 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004556
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004557 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4558 locations->SetOut(Location::RequiresFpuRegister());
4559 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004560 // The output overlaps in case of long: we don't want the low move
4561 // to overwrite the object's location. Likewise, in the case of
4562 // an object field get with read barriers enabled, we do not want
4563 // the move to overwrite the object's location, as we need it to emit
4564 // the read barrier.
4565 locations->SetOut(
4566 Location::RequiresRegister(),
4567 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4568 Location::kOutputOverlap :
4569 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004570 }
Calin Juravle52c48962014-12-16 17:02:57 +00004571
4572 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4573 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004574 // So we use an XMM register as a temp to achieve atomicity (first
4575 // load the temp into the XMM and then copy the XMM into the
4576 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004577 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004578 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4579 // We need a temporary register for the read barrier marking slow
4580 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4581 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004582 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004583}
4584
Calin Juravle52c48962014-12-16 17:02:57 +00004585void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4586 const FieldInfo& field_info) {
4587 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004588
Calin Juravle52c48962014-12-16 17:02:57 +00004589 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004590 Location base_loc = locations->InAt(0);
4591 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004592 Location out = locations->Out();
4593 bool is_volatile = field_info.IsVolatile();
4594 Primitive::Type field_type = field_info.GetFieldType();
4595 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4596
4597 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004599 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004600 break;
4601 }
4602
4603 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004604 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004605 break;
4606 }
4607
4608 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004609 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004610 break;
4611 }
4612
4613 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004614 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004615 break;
4616 }
4617
4618 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004619 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004620 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004621
4622 case Primitive::kPrimNot: {
4623 // /* HeapReference<Object> */ out = *(base + offset)
4624 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4625 Location temp_loc = locations->GetTemp(0);
4626 // Note that a potential implicit null check is handled in this
4627 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4628 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4629 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4630 if (is_volatile) {
4631 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4632 }
4633 } else {
4634 __ movl(out.AsRegister<Register>(), Address(base, offset));
4635 codegen_->MaybeRecordImplicitNullCheck(instruction);
4636 if (is_volatile) {
4637 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4638 }
4639 // If read barriers are enabled, emit read barriers other than
4640 // Baker's using a slow path (and also unpoison the loaded
4641 // reference, if heap poisoning is enabled).
4642 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4643 }
4644 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004645 }
4646
4647 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004648 if (is_volatile) {
4649 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4650 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004651 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004652 __ movd(out.AsRegisterPairLow<Register>(), temp);
4653 __ psrlq(temp, Immediate(32));
4654 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4655 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004656 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004657 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004658 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4660 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004661 break;
4662 }
4663
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004664 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004665 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004666 break;
4667 }
4668
4669 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004670 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004671 break;
4672 }
4673
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004674 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004675 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004676 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004677 }
Calin Juravle52c48962014-12-16 17:02:57 +00004678
Roland Levillain7c1559a2015-12-15 10:55:36 +00004679 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4680 // Potential implicit null checks, in the case of reference or
4681 // long fields, are handled in the previous switch statement.
4682 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004683 codegen_->MaybeRecordImplicitNullCheck(instruction);
4684 }
4685
Calin Juravle52c48962014-12-16 17:02:57 +00004686 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004687 if (field_type == Primitive::kPrimNot) {
4688 // Memory barriers, in the case of references, are also handled
4689 // in the previous switch statement.
4690 } else {
4691 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4692 }
Roland Levillain4d027112015-07-01 15:41:14 +01004693 }
Calin Juravle52c48962014-12-16 17:02:57 +00004694}
4695
4696void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4697 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4698
4699 LocationSummary* locations =
4700 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4701 locations->SetInAt(0, Location::RequiresRegister());
4702 bool is_volatile = field_info.IsVolatile();
4703 Primitive::Type field_type = field_info.GetFieldType();
4704 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4705 || (field_type == Primitive::kPrimByte);
4706
4707 // The register allocator does not support multiple
4708 // inputs that die at entry with one in a specific register.
4709 if (is_byte_type) {
4710 // Ensure the value is in a byte register.
4711 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004712 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004713 if (is_volatile && field_type == Primitive::kPrimDouble) {
4714 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4715 locations->SetInAt(1, Location::RequiresFpuRegister());
4716 } else {
4717 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4718 }
4719 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4720 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004721 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004722
Calin Juravle52c48962014-12-16 17:02:57 +00004723 // 64bits value can be atomically written to an address with movsd and an XMM register.
4724 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4725 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4726 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4727 // isolated cases when we need this it isn't worth adding the extra complexity.
4728 locations->AddTemp(Location::RequiresFpuRegister());
4729 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004730 } else {
4731 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4732
4733 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4734 // Temporary registers for the write barrier.
4735 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4736 // Ensure the card is in a byte register.
4737 locations->AddTemp(Location::RegisterLocation(ECX));
4738 }
Calin Juravle52c48962014-12-16 17:02:57 +00004739 }
4740}
4741
4742void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004743 const FieldInfo& field_info,
4744 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004745 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4746
4747 LocationSummary* locations = instruction->GetLocations();
4748 Register base = locations->InAt(0).AsRegister<Register>();
4749 Location value = locations->InAt(1);
4750 bool is_volatile = field_info.IsVolatile();
4751 Primitive::Type field_type = field_info.GetFieldType();
4752 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004753 bool needs_write_barrier =
4754 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004755
4756 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004757 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004758 }
4759
Mark Mendell81489372015-11-04 11:30:41 -05004760 bool maybe_record_implicit_null_check_done = false;
4761
Calin Juravle52c48962014-12-16 17:02:57 +00004762 switch (field_type) {
4763 case Primitive::kPrimBoolean:
4764 case Primitive::kPrimByte: {
4765 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4766 break;
4767 }
4768
4769 case Primitive::kPrimShort:
4770 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004771 if (value.IsConstant()) {
4772 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4773 __ movw(Address(base, offset), Immediate(v));
4774 } else {
4775 __ movw(Address(base, offset), value.AsRegister<Register>());
4776 }
Calin Juravle52c48962014-12-16 17:02:57 +00004777 break;
4778 }
4779
4780 case Primitive::kPrimInt:
4781 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004782 if (kPoisonHeapReferences && needs_write_barrier) {
4783 // Note that in the case where `value` is a null reference,
4784 // we do not enter this block, as the reference does not
4785 // need poisoning.
4786 DCHECK_EQ(field_type, Primitive::kPrimNot);
4787 Register temp = locations->GetTemp(0).AsRegister<Register>();
4788 __ movl(temp, value.AsRegister<Register>());
4789 __ PoisonHeapReference(temp);
4790 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004791 } else if (value.IsConstant()) {
4792 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4793 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004794 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004795 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004796 __ movl(Address(base, offset), value.AsRegister<Register>());
4797 }
Calin Juravle52c48962014-12-16 17:02:57 +00004798 break;
4799 }
4800
4801 case Primitive::kPrimLong: {
4802 if (is_volatile) {
4803 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4804 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4805 __ movd(temp1, value.AsRegisterPairLow<Register>());
4806 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4807 __ punpckldq(temp1, temp2);
4808 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004809 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004810 } else if (value.IsConstant()) {
4811 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4812 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4813 codegen_->MaybeRecordImplicitNullCheck(instruction);
4814 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004815 } else {
4816 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004817 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004818 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4819 }
Mark Mendell81489372015-11-04 11:30:41 -05004820 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004821 break;
4822 }
4823
4824 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004825 if (value.IsConstant()) {
4826 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4827 __ movl(Address(base, offset), Immediate(v));
4828 } else {
4829 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4830 }
Calin Juravle52c48962014-12-16 17:02:57 +00004831 break;
4832 }
4833
4834 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004835 if (value.IsConstant()) {
4836 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4837 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4838 codegen_->MaybeRecordImplicitNullCheck(instruction);
4839 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4840 maybe_record_implicit_null_check_done = true;
4841 } else {
4842 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4843 }
Calin Juravle52c48962014-12-16 17:02:57 +00004844 break;
4845 }
4846
4847 case Primitive::kPrimVoid:
4848 LOG(FATAL) << "Unreachable type " << field_type;
4849 UNREACHABLE();
4850 }
4851
Mark Mendell81489372015-11-04 11:30:41 -05004852 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004853 codegen_->MaybeRecordImplicitNullCheck(instruction);
4854 }
4855
Roland Levillain4d027112015-07-01 15:41:14 +01004856 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004857 Register temp = locations->GetTemp(0).AsRegister<Register>();
4858 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004859 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004860 }
4861
Calin Juravle52c48962014-12-16 17:02:57 +00004862 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004863 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004864 }
4865}
4866
4867void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4868 HandleFieldGet(instruction, instruction->GetFieldInfo());
4869}
4870
4871void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4872 HandleFieldGet(instruction, instruction->GetFieldInfo());
4873}
4874
4875void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4876 HandleFieldSet(instruction, instruction->GetFieldInfo());
4877}
4878
4879void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004880 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004881}
4882
4883void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4884 HandleFieldSet(instruction, instruction->GetFieldInfo());
4885}
4886
4887void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004888 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004889}
4890
4891void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4892 HandleFieldGet(instruction, instruction->GetFieldInfo());
4893}
4894
4895void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4896 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004897}
4898
Calin Juravlee460d1d2015-09-29 04:52:17 +01004899void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4900 HUnresolvedInstanceFieldGet* instruction) {
4901 FieldAccessCallingConventionX86 calling_convention;
4902 codegen_->CreateUnresolvedFieldLocationSummary(
4903 instruction, instruction->GetFieldType(), calling_convention);
4904}
4905
4906void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4907 HUnresolvedInstanceFieldGet* 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::VisitUnresolvedInstanceFieldSet(
4917 HUnresolvedInstanceFieldSet* instruction) {
4918 FieldAccessCallingConventionX86 calling_convention;
4919 codegen_->CreateUnresolvedFieldLocationSummary(
4920 instruction, instruction->GetFieldType(), calling_convention);
4921}
4922
4923void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4924 HUnresolvedInstanceFieldSet* 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::VisitUnresolvedStaticFieldGet(
4934 HUnresolvedStaticFieldGet* instruction) {
4935 FieldAccessCallingConventionX86 calling_convention;
4936 codegen_->CreateUnresolvedFieldLocationSummary(
4937 instruction, instruction->GetFieldType(), calling_convention);
4938}
4939
4940void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4941 HUnresolvedStaticFieldGet* instruction) {
4942 FieldAccessCallingConventionX86 calling_convention;
4943 codegen_->GenerateUnresolvedFieldAccess(instruction,
4944 instruction->GetFieldType(),
4945 instruction->GetFieldIndex(),
4946 instruction->GetDexPc(),
4947 calling_convention);
4948}
4949
4950void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4951 HUnresolvedStaticFieldSet* instruction) {
4952 FieldAccessCallingConventionX86 calling_convention;
4953 codegen_->CreateUnresolvedFieldLocationSummary(
4954 instruction, instruction->GetFieldType(), calling_convention);
4955}
4956
4957void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4958 HUnresolvedStaticFieldSet* instruction) {
4959 FieldAccessCallingConventionX86 calling_convention;
4960 codegen_->GenerateUnresolvedFieldAccess(instruction,
4961 instruction->GetFieldType(),
4962 instruction->GetFieldIndex(),
4963 instruction->GetDexPc(),
4964 calling_convention);
4965}
4966
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004967void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004968 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4969 ? LocationSummary::kCallOnSlowPath
4970 : LocationSummary::kNoCall;
4971 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4972 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004973 ? Location::RequiresRegister()
4974 : Location::Any();
4975 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004976 if (instruction->HasUses()) {
4977 locations->SetOut(Location::SameAsFirstInput());
4978 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004979}
4980
Calin Juravle2ae48182016-03-16 14:05:09 +00004981void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4982 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004983 return;
4984 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004985 LocationSummary* locations = instruction->GetLocations();
4986 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004987
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004988 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004989 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004990}
4991
Calin Juravle2ae48182016-03-16 14:05:09 +00004992void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004993 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004994 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004995
4996 LocationSummary* locations = instruction->GetLocations();
4997 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004998
4999 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005000 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005001 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005002 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005003 } else {
5004 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005005 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005006 __ jmp(slow_path->GetEntryLabel());
5007 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008 }
5009 __ j(kEqual, slow_path->GetEntryLabel());
5010}
5011
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005012void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005013 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005014}
5015
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005016void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005017 bool object_array_get_with_read_barrier =
5018 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005019 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005020 new (GetGraph()->GetArena()) LocationSummary(instruction,
5021 object_array_get_with_read_barrier ?
5022 LocationSummary::kCallOnSlowPath :
5023 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005024 locations->SetInAt(0, Location::RequiresRegister());
5025 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005026 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5027 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5028 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005029 // The output overlaps in case of long: we don't want the low move
5030 // to overwrite the array's location. Likewise, in the case of an
5031 // object array get with read barriers enabled, we do not want the
5032 // move to overwrite the array's location, as we need it to emit
5033 // the read barrier.
5034 locations->SetOut(
5035 Location::RequiresRegister(),
5036 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5037 Location::kOutputOverlap :
5038 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005039 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005040 // We need a temporary register for the read barrier marking slow
5041 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5042 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5043 locations->AddTemp(Location::RequiresRegister());
5044 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005045}
5046
5047void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5048 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005049 Location obj_loc = locations->InAt(0);
5050 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005052 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005053
Calin Juravle77520bc2015-01-12 18:45:46 +00005054 Primitive::Type type = instruction->GetType();
5055 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005056 case Primitive::kPrimBoolean: {
5057 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005058 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005059 if (index.IsConstant()) {
5060 __ movzxb(out, Address(obj,
5061 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5062 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005063 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 }
5065 break;
5066 }
5067
5068 case Primitive::kPrimByte: {
5069 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005070 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005071 if (index.IsConstant()) {
5072 __ movsxb(out, Address(obj,
5073 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5074 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005075 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005076 }
5077 break;
5078 }
5079
5080 case Primitive::kPrimShort: {
5081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005082 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005083 if (index.IsConstant()) {
5084 __ movsxw(out, Address(obj,
5085 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5086 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005087 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005088 }
5089 break;
5090 }
5091
5092 case Primitive::kPrimChar: {
5093 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005094 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005095 if (index.IsConstant()) {
5096 __ movzxw(out, Address(obj,
5097 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5098 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005099 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005100 }
5101 break;
5102 }
5103
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005106 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005107 if (index.IsConstant()) {
5108 __ movl(out, Address(obj,
5109 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5110 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005111 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005112 }
5113 break;
5114 }
5115
Roland Levillain7c1559a2015-12-15 10:55:36 +00005116 case Primitive::kPrimNot: {
5117 static_assert(
5118 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5119 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5120 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5121 // /* HeapReference<Object> */ out =
5122 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5123 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5124 Location temp = locations->GetTemp(0);
5125 // Note that a potential implicit null check is handled in this
5126 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5127 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5128 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5129 } else {
5130 Register out = out_loc.AsRegister<Register>();
5131 if (index.IsConstant()) {
5132 uint32_t offset =
5133 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5134 __ movl(out, Address(obj, offset));
5135 codegen_->MaybeRecordImplicitNullCheck(instruction);
5136 // If read barriers are enabled, emit read barriers other than
5137 // Baker's using a slow path (and also unpoison the loaded
5138 // reference, if heap poisoning is enabled).
5139 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5140 } else {
5141 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5142 codegen_->MaybeRecordImplicitNullCheck(instruction);
5143 // If read barriers are enabled, emit read barriers other than
5144 // Baker's using a slow path (and also unpoison the loaded
5145 // reference, if heap poisoning is enabled).
5146 codegen_->MaybeGenerateReadBarrierSlow(
5147 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5148 }
5149 }
5150 break;
5151 }
5152
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005153 case Primitive::kPrimLong: {
5154 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005155 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005156 if (index.IsConstant()) {
5157 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005158 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005159 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005160 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005161 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005162 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005163 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005164 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005165 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005166 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005167 }
5168 break;
5169 }
5170
Mark Mendell7c8d0092015-01-26 11:21:33 -05005171 case Primitive::kPrimFloat: {
5172 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005173 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005174 if (index.IsConstant()) {
5175 __ movss(out, Address(obj,
5176 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5177 } else {
5178 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5179 }
5180 break;
5181 }
5182
5183 case Primitive::kPrimDouble: {
5184 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005185 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005186 if (index.IsConstant()) {
5187 __ movsd(out, Address(obj,
5188 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5189 } else {
5190 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5191 }
5192 break;
5193 }
5194
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005195 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005196 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005197 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005198 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005199
Roland Levillain7c1559a2015-12-15 10:55:36 +00005200 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5201 // Potential implicit null checks, in the case of reference or
5202 // long arrays, are handled in the previous switch statement.
5203 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005204 codegen_->MaybeRecordImplicitNullCheck(instruction);
5205 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005206}
5207
5208void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005209 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005210
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005211 bool needs_write_barrier =
5212 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005213 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5214 bool object_array_set_with_read_barrier =
5215 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005216
Nicolas Geoffray39468442014-09-02 15:17:15 +01005217 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5218 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005219 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5220 LocationSummary::kCallOnSlowPath :
5221 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005222
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005223 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5224 || (value_type == Primitive::kPrimByte);
5225 // We need the inputs to be different than the output in case of long operation.
5226 // In case of a byte operation, the register allocator does not support multiple
5227 // inputs that die at entry with one in a specific register.
5228 locations->SetInAt(0, Location::RequiresRegister());
5229 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5230 if (is_byte_type) {
5231 // Ensure the value is in a byte register.
5232 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5233 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005234 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005235 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005236 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5237 }
5238 if (needs_write_barrier) {
5239 // Temporary registers for the write barrier.
5240 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5241 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005242 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005243 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005244}
5245
5246void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5247 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005248 Location array_loc = locations->InAt(0);
5249 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005250 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005251 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005252 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005253 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5254 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5255 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005256 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005257 bool needs_write_barrier =
5258 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259
5260 switch (value_type) {
5261 case Primitive::kPrimBoolean:
5262 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5264 Address address = index.IsConstant()
5265 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5266 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5267 if (value.IsRegister()) {
5268 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005270 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005272 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005273 break;
5274 }
5275
5276 case Primitive::kPrimShort:
5277 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005278 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5279 Address address = index.IsConstant()
5280 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5281 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5282 if (value.IsRegister()) {
5283 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005284 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005285 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005286 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005287 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005288 break;
5289 }
5290
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005291 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005292 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5293 Address address = index.IsConstant()
5294 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5295 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005296
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005297 if (!value.IsRegister()) {
5298 // Just setting null.
5299 DCHECK(instruction->InputAt(2)->IsNullConstant());
5300 DCHECK(value.IsConstant()) << value;
5301 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005302 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005303 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005304 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005305 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005306 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307
5308 DCHECK(needs_write_barrier);
5309 Register register_value = value.AsRegister<Register>();
5310 NearLabel done, not_null, do_put;
5311 SlowPathCode* slow_path = nullptr;
5312 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005313 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005314 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5315 codegen_->AddSlowPath(slow_path);
5316 if (instruction->GetValueCanBeNull()) {
5317 __ testl(register_value, register_value);
5318 __ j(kNotEqual, &not_null);
5319 __ movl(address, Immediate(0));
5320 codegen_->MaybeRecordImplicitNullCheck(instruction);
5321 __ jmp(&done);
5322 __ Bind(&not_null);
5323 }
5324
Roland Levillain0d5a2812015-11-13 10:07:31 +00005325 if (kEmitCompilerReadBarrier) {
5326 // When read barriers are enabled, the type checking
5327 // instrumentation requires two read barriers:
5328 //
5329 // __ movl(temp2, temp);
5330 // // /* HeapReference<Class> */ temp = temp->component_type_
5331 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005332 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005333 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5334 //
5335 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5336 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005337 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005338 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5339 //
5340 // __ cmpl(temp, temp2);
5341 //
5342 // However, the second read barrier may trash `temp`, as it
5343 // is a temporary register, and as such would not be saved
5344 // along with live registers before calling the runtime (nor
5345 // restored afterwards). So in this case, we bail out and
5346 // delegate the work to the array set slow path.
5347 //
5348 // TODO: Extend the register allocator to support a new
5349 // "(locally) live temp" location so as to avoid always
5350 // going into the slow path when read barriers are enabled.
5351 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005352 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005353 // /* HeapReference<Class> */ temp = array->klass_
5354 __ movl(temp, Address(array, class_offset));
5355 codegen_->MaybeRecordImplicitNullCheck(instruction);
5356 __ MaybeUnpoisonHeapReference(temp);
5357
5358 // /* HeapReference<Class> */ temp = temp->component_type_
5359 __ movl(temp, Address(temp, component_offset));
5360 // If heap poisoning is enabled, no need to unpoison `temp`
5361 // nor the object reference in `register_value->klass`, as
5362 // we are comparing two poisoned references.
5363 __ cmpl(temp, Address(register_value, class_offset));
5364
5365 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5366 __ j(kEqual, &do_put);
5367 // If heap poisoning is enabled, the `temp` reference has
5368 // not been unpoisoned yet; unpoison it now.
5369 __ MaybeUnpoisonHeapReference(temp);
5370
5371 // /* HeapReference<Class> */ temp = temp->super_class_
5372 __ movl(temp, Address(temp, super_offset));
5373 // If heap poisoning is enabled, no need to unpoison
5374 // `temp`, as we are comparing against null below.
5375 __ testl(temp, temp);
5376 __ j(kNotEqual, slow_path->GetEntryLabel());
5377 __ Bind(&do_put);
5378 } else {
5379 __ j(kNotEqual, slow_path->GetEntryLabel());
5380 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005381 }
5382 }
5383
5384 if (kPoisonHeapReferences) {
5385 __ movl(temp, register_value);
5386 __ PoisonHeapReference(temp);
5387 __ movl(address, temp);
5388 } else {
5389 __ movl(address, register_value);
5390 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005391 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005392 codegen_->MaybeRecordImplicitNullCheck(instruction);
5393 }
5394
5395 Register card = locations->GetTemp(1).AsRegister<Register>();
5396 codegen_->MarkGCCard(
5397 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5398 __ Bind(&done);
5399
5400 if (slow_path != nullptr) {
5401 __ Bind(slow_path->GetExitLabel());
5402 }
5403
5404 break;
5405 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005406
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005407 case Primitive::kPrimInt: {
5408 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5409 Address address = index.IsConstant()
5410 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5411 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5412 if (value.IsRegister()) {
5413 __ movl(address, value.AsRegister<Register>());
5414 } else {
5415 DCHECK(value.IsConstant()) << value;
5416 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5417 __ movl(address, Immediate(v));
5418 }
5419 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005420 break;
5421 }
5422
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005423 case Primitive::kPrimLong: {
5424 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005425 if (index.IsConstant()) {
5426 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005427 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005428 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005429 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005430 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005431 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005432 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005433 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005435 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005437 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005438 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005439 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005441 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005442 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005443 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005444 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005445 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005446 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005447 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005448 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005449 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005450 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005451 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005452 Immediate(High32Bits(val)));
5453 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005454 }
5455 break;
5456 }
5457
Mark Mendell7c8d0092015-01-26 11:21:33 -05005458 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005459 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5460 Address address = index.IsConstant()
5461 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5462 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005463 if (value.IsFpuRegister()) {
5464 __ movss(address, value.AsFpuRegister<XmmRegister>());
5465 } else {
5466 DCHECK(value.IsConstant());
5467 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5468 __ movl(address, Immediate(v));
5469 }
5470 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005471 break;
5472 }
5473
5474 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005475 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5476 Address address = index.IsConstant()
5477 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5478 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005479 if (value.IsFpuRegister()) {
5480 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5481 } else {
5482 DCHECK(value.IsConstant());
5483 Address address_hi = index.IsConstant() ?
5484 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5485 offset + kX86WordSize) :
5486 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5487 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5488 __ movl(address, Immediate(Low32Bits(v)));
5489 codegen_->MaybeRecordImplicitNullCheck(instruction);
5490 __ movl(address_hi, Immediate(High32Bits(v)));
5491 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005492 break;
5493 }
5494
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005495 case Primitive::kPrimVoid:
5496 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005497 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005498 }
5499}
5500
5501void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5502 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005503 locations->SetInAt(0, Location::RequiresRegister());
5504 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005505}
5506
5507void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5508 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005509 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005510 Register obj = locations->InAt(0).AsRegister<Register>();
5511 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005512 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005513 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005514}
5515
5516void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005517 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5518 ? LocationSummary::kCallOnSlowPath
5519 : LocationSummary::kNoCall;
5520 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005521 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005522 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005523 if (instruction->HasUses()) {
5524 locations->SetOut(Location::SameAsFirstInput());
5525 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005526}
5527
5528void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5529 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005530 Location index_loc = locations->InAt(0);
5531 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005532 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005533 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005534
Mark Mendell99dbd682015-04-22 16:18:52 -04005535 if (length_loc.IsConstant()) {
5536 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5537 if (index_loc.IsConstant()) {
5538 // BCE will remove the bounds check if we are guarenteed to pass.
5539 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5540 if (index < 0 || index >= length) {
5541 codegen_->AddSlowPath(slow_path);
5542 __ jmp(slow_path->GetEntryLabel());
5543 } else {
5544 // Some optimization after BCE may have generated this, and we should not
5545 // generate a bounds check if it is a valid range.
5546 }
5547 return;
5548 }
5549
5550 // We have to reverse the jump condition because the length is the constant.
5551 Register index_reg = index_loc.AsRegister<Register>();
5552 __ cmpl(index_reg, Immediate(length));
5553 codegen_->AddSlowPath(slow_path);
5554 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005555 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005556 Register length = length_loc.AsRegister<Register>();
5557 if (index_loc.IsConstant()) {
5558 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5559 __ cmpl(length, Immediate(value));
5560 } else {
5561 __ cmpl(length, index_loc.AsRegister<Register>());
5562 }
5563 codegen_->AddSlowPath(slow_path);
5564 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005565 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005566}
5567
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005568void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005569 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005570}
5571
5572void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005573 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5574}
5575
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005576void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5577 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5578}
5579
5580void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005581 HBasicBlock* block = instruction->GetBlock();
5582 if (block->GetLoopInformation() != nullptr) {
5583 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5584 // The back edge will generate the suspend check.
5585 return;
5586 }
5587 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5588 // The goto will generate the suspend check.
5589 return;
5590 }
5591 GenerateSuspendCheck(instruction, nullptr);
5592}
5593
5594void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5595 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005596 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005597 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5598 if (slow_path == nullptr) {
5599 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5600 instruction->SetSlowPath(slow_path);
5601 codegen_->AddSlowPath(slow_path);
5602 if (successor != nullptr) {
5603 DCHECK(successor->IsLoopHeader());
5604 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5605 }
5606 } else {
5607 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5608 }
5609
Roland Levillain7c1559a2015-12-15 10:55:36 +00005610 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5611 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005612 if (successor == nullptr) {
5613 __ j(kNotEqual, slow_path->GetEntryLabel());
5614 __ Bind(slow_path->GetReturnLabel());
5615 } else {
5616 __ j(kEqual, codegen_->GetLabelOf(successor));
5617 __ jmp(slow_path->GetEntryLabel());
5618 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005619}
5620
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005621X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5622 return codegen_->GetAssembler();
5623}
5624
Mark Mendell7c8d0092015-01-26 11:21:33 -05005625void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005626 ScratchRegisterScope ensure_scratch(
5627 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5628 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5629 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5630 __ movl(temp_reg, Address(ESP, src + stack_offset));
5631 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005632}
5633
5634void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005635 ScratchRegisterScope ensure_scratch(
5636 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5637 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5638 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5639 __ movl(temp_reg, Address(ESP, src + stack_offset));
5640 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5641 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5642 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005643}
5644
5645void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005646 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005647 Location source = move->GetSource();
5648 Location destination = move->GetDestination();
5649
5650 if (source.IsRegister()) {
5651 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005652 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005653 } else if (destination.IsFpuRegister()) {
5654 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005655 } else {
5656 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005657 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005658 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005659 } else if (source.IsRegisterPair()) {
5660 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5661 // Create stack space for 2 elements.
5662 __ subl(ESP, Immediate(2 * elem_size));
5663 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5664 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5665 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5666 // And remove the temporary stack space we allocated.
5667 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005668 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005669 if (destination.IsRegister()) {
5670 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5671 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005672 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005673 } else if (destination.IsRegisterPair()) {
5674 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5675 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5676 __ psrlq(src_reg, Immediate(32));
5677 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005678 } else if (destination.IsStackSlot()) {
5679 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5680 } else {
5681 DCHECK(destination.IsDoubleStackSlot());
5682 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5683 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005684 } else if (source.IsStackSlot()) {
5685 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005686 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005687 } else if (destination.IsFpuRegister()) {
5688 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005689 } else {
5690 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005691 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5692 }
5693 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005694 if (destination.IsRegisterPair()) {
5695 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5696 __ movl(destination.AsRegisterPairHigh<Register>(),
5697 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5698 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005699 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5700 } else {
5701 DCHECK(destination.IsDoubleStackSlot()) << destination;
5702 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005703 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005704 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005705 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005706 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005707 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005708 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005709 if (value == 0) {
5710 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5711 } else {
5712 __ movl(destination.AsRegister<Register>(), Immediate(value));
5713 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005714 } else {
5715 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005716 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005717 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005718 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005719 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005720 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005721 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005722 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005723 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5724 if (value == 0) {
5725 // Easy handling of 0.0.
5726 __ xorps(dest, dest);
5727 } else {
5728 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005729 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5730 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5731 __ movl(temp, Immediate(value));
5732 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005733 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005734 } else {
5735 DCHECK(destination.IsStackSlot()) << destination;
5736 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5737 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005738 } else if (constant->IsLongConstant()) {
5739 int64_t value = constant->AsLongConstant()->GetValue();
5740 int32_t low_value = Low32Bits(value);
5741 int32_t high_value = High32Bits(value);
5742 Immediate low(low_value);
5743 Immediate high(high_value);
5744 if (destination.IsDoubleStackSlot()) {
5745 __ movl(Address(ESP, destination.GetStackIndex()), low);
5746 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5747 } else {
5748 __ movl(destination.AsRegisterPairLow<Register>(), low);
5749 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5750 }
5751 } else {
5752 DCHECK(constant->IsDoubleConstant());
5753 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005754 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005755 int32_t low_value = Low32Bits(value);
5756 int32_t high_value = High32Bits(value);
5757 Immediate low(low_value);
5758 Immediate high(high_value);
5759 if (destination.IsFpuRegister()) {
5760 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5761 if (value == 0) {
5762 // Easy handling of 0.0.
5763 __ xorpd(dest, dest);
5764 } else {
5765 __ pushl(high);
5766 __ pushl(low);
5767 __ movsd(dest, Address(ESP, 0));
5768 __ addl(ESP, Immediate(8));
5769 }
5770 } else {
5771 DCHECK(destination.IsDoubleStackSlot()) << destination;
5772 __ movl(Address(ESP, destination.GetStackIndex()), low);
5773 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5774 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005775 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005776 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005777 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005778 }
5779}
5780
Mark Mendella5c19ce2015-04-01 12:51:05 -04005781void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005782 Register suggested_scratch = reg == EAX ? EBX : EAX;
5783 ScratchRegisterScope ensure_scratch(
5784 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5785
5786 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5787 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5788 __ movl(Address(ESP, mem + stack_offset), reg);
5789 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005790}
5791
Mark Mendell7c8d0092015-01-26 11:21:33 -05005792void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005793 ScratchRegisterScope ensure_scratch(
5794 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5795
5796 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5797 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5798 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5799 __ movss(Address(ESP, mem + stack_offset), reg);
5800 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005801}
5802
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005803void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005804 ScratchRegisterScope ensure_scratch1(
5805 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005806
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005807 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5808 ScratchRegisterScope ensure_scratch2(
5809 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005810
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005811 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5812 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5813 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5814 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5815 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5816 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005817}
5818
5819void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005820 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005821 Location source = move->GetSource();
5822 Location destination = move->GetDestination();
5823
5824 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005825 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5826 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5827 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5828 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5829 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005830 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005831 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005832 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005833 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005834 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5835 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005836 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5837 // Use XOR Swap algorithm to avoid a temporary.
5838 DCHECK_NE(source.reg(), destination.reg());
5839 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5840 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5841 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5842 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5843 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5844 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5845 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005846 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5847 // Take advantage of the 16 bytes in the XMM register.
5848 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5849 Address stack(ESP, destination.GetStackIndex());
5850 // Load the double into the high doubleword.
5851 __ movhpd(reg, stack);
5852
5853 // Store the low double into the destination.
5854 __ movsd(stack, reg);
5855
5856 // Move the high double to the low double.
5857 __ psrldq(reg, Immediate(8));
5858 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5859 // Take advantage of the 16 bytes in the XMM register.
5860 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5861 Address stack(ESP, source.GetStackIndex());
5862 // Load the double into the high doubleword.
5863 __ movhpd(reg, stack);
5864
5865 // Store the low double into the destination.
5866 __ movsd(stack, reg);
5867
5868 // Move the high double to the low double.
5869 __ psrldq(reg, Immediate(8));
5870 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5871 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5872 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005873 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005874 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005875 }
5876}
5877
5878void ParallelMoveResolverX86::SpillScratch(int reg) {
5879 __ pushl(static_cast<Register>(reg));
5880}
5881
5882void ParallelMoveResolverX86::RestoreScratch(int reg) {
5883 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005884}
5885
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005886void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005887 InvokeRuntimeCallingConvention calling_convention;
5888 CodeGenerator::CreateLoadClassLocationSummary(
5889 cls,
5890 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005891 Location::RegisterLocation(EAX),
5892 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005893}
5894
5895void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005896 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005897 if (cls->NeedsAccessCheck()) {
5898 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5899 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5900 cls,
5901 cls->GetDexPc(),
5902 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005903 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005904 return;
5905 }
5906
Roland Levillain0d5a2812015-11-13 10:07:31 +00005907 Location out_loc = locations->Out();
5908 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005909 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005910
Calin Juravle580b6092015-10-06 17:35:58 +01005911 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005912 DCHECK(!cls->CanCallRuntime());
5913 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005914 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5915 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005916 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005917 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005918 // /* GcRoot<mirror::Class>[] */ out =
5919 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5920 __ movl(out, Address(current_method,
5921 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005922 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005923 GenerateGcRootFieldLoad(
5924 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005925
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005926 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5927 DCHECK(cls->CanCallRuntime());
5928 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5929 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5930 codegen_->AddSlowPath(slow_path);
5931
5932 if (!cls->IsInDexCache()) {
5933 __ testl(out, out);
5934 __ j(kEqual, slow_path->GetEntryLabel());
5935 }
5936
5937 if (cls->MustGenerateClinitCheck()) {
5938 GenerateClassInitializationCheck(slow_path, out);
5939 } else {
5940 __ Bind(slow_path->GetExitLabel());
5941 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005942 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005943 }
5944}
5945
5946void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5947 LocationSummary* locations =
5948 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5949 locations->SetInAt(0, Location::RequiresRegister());
5950 if (check->HasUses()) {
5951 locations->SetOut(Location::SameAsFirstInput());
5952 }
5953}
5954
5955void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005956 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005957 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005958 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005959 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005960 GenerateClassInitializationCheck(slow_path,
5961 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005962}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005963
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005964void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005965 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005966 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5967 Immediate(mirror::Class::kStatusInitialized));
5968 __ j(kLess, slow_path->GetEntryLabel());
5969 __ Bind(slow_path->GetExitLabel());
5970 // No need for memory fence, thanks to the X86 memory model.
5971}
5972
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005973HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5974 HLoadString::LoadKind desired_string_load_kind) {
5975 if (kEmitCompilerReadBarrier) {
5976 switch (desired_string_load_kind) {
5977 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5978 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5979 case HLoadString::LoadKind::kBootImageAddress:
5980 // TODO: Implement for read barrier.
5981 return HLoadString::LoadKind::kDexCacheViaMethod;
5982 default:
5983 break;
5984 }
5985 }
5986 switch (desired_string_load_kind) {
5987 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5988 DCHECK(!GetCompilerOptions().GetCompilePic());
5989 break;
5990 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5991 DCHECK(GetCompilerOptions().GetCompilePic());
5992 FALLTHROUGH_INTENDED;
5993 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005994 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005995 // We disable pc-relative load when there is an irreducible loop, as the optimization
5996 // is incompatible with it.
5997 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5998 // with irreducible loops.
5999 if (GetGraph()->HasIrreducibleLoops()) {
6000 return HLoadString::LoadKind::kDexCacheViaMethod;
6001 }
6002 break;
6003 case HLoadString::LoadKind::kBootImageAddress:
6004 break;
6005 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006006 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006007 break;
6008 case HLoadString::LoadKind::kDexCacheViaMethod:
6009 break;
6010 }
6011 return desired_string_load_kind;
6012}
6013
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006014void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006015 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006016 ? LocationSummary::kCallOnSlowPath
6017 : LocationSummary::kNoCall;
6018 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006019 HLoadString::LoadKind load_kind = load->GetLoadKind();
6020 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6021 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6022 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6023 locations->SetInAt(0, Location::RequiresRegister());
6024 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006025 locations->SetOut(Location::RequiresRegister());
6026}
6027
6028void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006029 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006030 Location out_loc = locations->Out();
6031 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006032
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006033 switch (load->GetLoadKind()) {
6034 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6035 DCHECK(!kEmitCompilerReadBarrier);
6036 __ movl(out, Immediate(/* placeholder */ 0));
6037 codegen_->RecordStringPatch(load);
6038 return; // No dex cache slow path.
6039 }
6040 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6041 DCHECK(!kEmitCompilerReadBarrier);
6042 Register method_address = locations->InAt(0).AsRegister<Register>();
6043 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6044 codegen_->RecordStringPatch(load);
6045 return; // No dex cache slow path.
6046 }
6047 case HLoadString::LoadKind::kBootImageAddress: {
6048 DCHECK(!kEmitCompilerReadBarrier);
6049 DCHECK_NE(load->GetAddress(), 0u);
6050 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6051 __ movl(out, Immediate(address));
6052 codegen_->RecordSimplePatch();
6053 return; // No dex cache slow path.
6054 }
6055 case HLoadString::LoadKind::kDexCacheAddress: {
6056 DCHECK_NE(load->GetAddress(), 0u);
6057 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6058 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6059 break;
6060 }
6061 case HLoadString::LoadKind::kDexCachePcRelative: {
6062 Register base_reg = locations->InAt(0).AsRegister<Register>();
6063 uint32_t offset = load->GetDexCacheElementOffset();
6064 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
6065 GenerateGcRootFieldLoad(
6066 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6067 break;
6068 }
6069 case HLoadString::LoadKind::kDexCacheViaMethod: {
6070 Register current_method = locations->InAt(0).AsRegister<Register>();
6071
6072 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6073 GenerateGcRootFieldLoad(
6074 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6075
6076 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6077 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6078 // /* GcRoot<mirror::String> */ out = out[string_index]
6079 GenerateGcRootFieldLoad(
6080 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6081 break;
6082 }
6083 default:
6084 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6085 UNREACHABLE();
6086 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006088 if (!load->IsInDexCache()) {
6089 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6090 codegen_->AddSlowPath(slow_path);
6091 __ testl(out, out);
6092 __ j(kEqual, slow_path->GetEntryLabel());
6093 __ Bind(slow_path->GetExitLabel());
6094 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006095}
6096
David Brazdilcb1c0552015-08-04 16:22:25 +01006097static Address GetExceptionTlsAddress() {
6098 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6099}
6100
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006101void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6102 LocationSummary* locations =
6103 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6104 locations->SetOut(Location::RequiresRegister());
6105}
6106
6107void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006108 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6109}
6110
6111void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6112 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6113}
6114
6115void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6116 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006117}
6118
6119void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6120 LocationSummary* locations =
6121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6122 InvokeRuntimeCallingConvention calling_convention;
6123 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6124}
6125
6126void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006127 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6128 instruction,
6129 instruction->GetDexPc(),
6130 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006131 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006132}
6133
Roland Levillain7c1559a2015-12-15 10:55:36 +00006134static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6135 return kEmitCompilerReadBarrier &&
6136 (kUseBakerReadBarrier ||
6137 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6138 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6139 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6140}
6141
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006142void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006143 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006144 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6145 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006146 case TypeCheckKind::kExactCheck:
6147 case TypeCheckKind::kAbstractClassCheck:
6148 case TypeCheckKind::kClassHierarchyCheck:
6149 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006150 call_kind =
6151 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006152 break;
6153 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006154 case TypeCheckKind::kUnresolvedCheck:
6155 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006156 call_kind = LocationSummary::kCallOnSlowPath;
6157 break;
6158 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006159
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006160 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006161 locations->SetInAt(0, Location::RequiresRegister());
6162 locations->SetInAt(1, Location::Any());
6163 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6164 locations->SetOut(Location::RequiresRegister());
6165 // When read barriers are enabled, we need a temporary register for
6166 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006167 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006168 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006169 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006170}
6171
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006172void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006173 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006174 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006175 Location obj_loc = locations->InAt(0);
6176 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006177 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006178 Location out_loc = locations->Out();
6179 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006180 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006181 locations->GetTemp(0) :
6182 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006183 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006184 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6185 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6186 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006187 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006188 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006189
6190 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006191 // Avoid null check if we know obj is not null.
6192 if (instruction->MustDoNullCheck()) {
6193 __ testl(obj, obj);
6194 __ j(kEqual, &zero);
6195 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006196
Roland Levillain0d5a2812015-11-13 10:07:31 +00006197 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006198 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006199
Roland Levillain7c1559a2015-12-15 10:55:36 +00006200 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006201 case TypeCheckKind::kExactCheck: {
6202 if (cls.IsRegister()) {
6203 __ cmpl(out, cls.AsRegister<Register>());
6204 } else {
6205 DCHECK(cls.IsStackSlot()) << cls;
6206 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6207 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006208
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209 // Classes must be equal for the instanceof to succeed.
6210 __ j(kNotEqual, &zero);
6211 __ movl(out, Immediate(1));
6212 __ jmp(&done);
6213 break;
6214 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006215
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006216 case TypeCheckKind::kAbstractClassCheck: {
6217 // If the class is abstract, we eagerly fetch the super class of the
6218 // object to avoid doing a comparison we know will fail.
6219 NearLabel loop;
6220 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006221 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006222 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006223 __ testl(out, out);
6224 // If `out` is null, we use it for the result, and jump to `done`.
6225 __ j(kEqual, &done);
6226 if (cls.IsRegister()) {
6227 __ cmpl(out, cls.AsRegister<Register>());
6228 } else {
6229 DCHECK(cls.IsStackSlot()) << cls;
6230 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6231 }
6232 __ j(kNotEqual, &loop);
6233 __ movl(out, Immediate(1));
6234 if (zero.IsLinked()) {
6235 __ jmp(&done);
6236 }
6237 break;
6238 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006239
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006240 case TypeCheckKind::kClassHierarchyCheck: {
6241 // Walk over the class hierarchy to find a match.
6242 NearLabel loop, success;
6243 __ Bind(&loop);
6244 if (cls.IsRegister()) {
6245 __ cmpl(out, cls.AsRegister<Register>());
6246 } else {
6247 DCHECK(cls.IsStackSlot()) << cls;
6248 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6249 }
6250 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006251 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006252 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006253 __ testl(out, out);
6254 __ j(kNotEqual, &loop);
6255 // If `out` is null, we use it for the result, and jump to `done`.
6256 __ jmp(&done);
6257 __ Bind(&success);
6258 __ movl(out, Immediate(1));
6259 if (zero.IsLinked()) {
6260 __ jmp(&done);
6261 }
6262 break;
6263 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006264
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006265 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006266 // Do an exact check.
6267 NearLabel exact_check;
6268 if (cls.IsRegister()) {
6269 __ cmpl(out, cls.AsRegister<Register>());
6270 } else {
6271 DCHECK(cls.IsStackSlot()) << cls;
6272 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6273 }
6274 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006275 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006276 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006277 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006278 __ testl(out, out);
6279 // If `out` is null, we use it for the result, and jump to `done`.
6280 __ j(kEqual, &done);
6281 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6282 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006283 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006284 __ movl(out, Immediate(1));
6285 __ jmp(&done);
6286 break;
6287 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006288
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006289 case TypeCheckKind::kArrayCheck: {
6290 if (cls.IsRegister()) {
6291 __ cmpl(out, cls.AsRegister<Register>());
6292 } else {
6293 DCHECK(cls.IsStackSlot()) << cls;
6294 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6295 }
6296 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006297 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6298 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006299 codegen_->AddSlowPath(slow_path);
6300 __ j(kNotEqual, slow_path->GetEntryLabel());
6301 __ movl(out, Immediate(1));
6302 if (zero.IsLinked()) {
6303 __ jmp(&done);
6304 }
6305 break;
6306 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006307
Calin Juravle98893e12015-10-02 21:05:03 +01006308 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006309 case TypeCheckKind::kInterfaceCheck: {
6310 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006311 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006312 // cases.
6313 //
6314 // We cannot directly call the InstanceofNonTrivial runtime
6315 // entry point without resorting to a type checking slow path
6316 // here (i.e. by calling InvokeRuntime directly), as it would
6317 // require to assign fixed registers for the inputs of this
6318 // HInstanceOf instruction (following the runtime calling
6319 // convention), which might be cluttered by the potential first
6320 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006321 //
6322 // TODO: Introduce a new runtime entry point taking the object
6323 // to test (instead of its class) as argument, and let it deal
6324 // with the read barrier issues. This will let us refactor this
6325 // case of the `switch` code as it was previously (with a direct
6326 // call to the runtime not using a type checking slow path).
6327 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006328 DCHECK(locations->OnlyCallsOnSlowPath());
6329 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6330 /* is_fatal */ false);
6331 codegen_->AddSlowPath(slow_path);
6332 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006333 if (zero.IsLinked()) {
6334 __ jmp(&done);
6335 }
6336 break;
6337 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006338 }
6339
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006340 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006341 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006342 __ xorl(out, out);
6343 }
6344
6345 if (done.IsLinked()) {
6346 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006347 }
6348
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006349 if (slow_path != nullptr) {
6350 __ Bind(slow_path->GetExitLabel());
6351 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006352}
6353
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006354void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006355 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6356 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006357 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6358 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006359 case TypeCheckKind::kExactCheck:
6360 case TypeCheckKind::kAbstractClassCheck:
6361 case TypeCheckKind::kClassHierarchyCheck:
6362 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006363 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6364 LocationSummary::kCallOnSlowPath :
6365 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006366 break;
6367 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006368 case TypeCheckKind::kUnresolvedCheck:
6369 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006370 call_kind = LocationSummary::kCallOnSlowPath;
6371 break;
6372 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006373 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6374 locations->SetInAt(0, Location::RequiresRegister());
6375 locations->SetInAt(1, Location::Any());
6376 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6377 locations->AddTemp(Location::RequiresRegister());
6378 // When read barriers are enabled, we need an additional temporary
6379 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006380 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006381 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006383}
6384
6385void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006386 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006387 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006388 Location obj_loc = locations->InAt(0);
6389 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006390 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006391 Location temp_loc = locations->GetTemp(0);
6392 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006393 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006394 locations->GetTemp(1) :
6395 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6397 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6398 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6399 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006400
Roland Levillain0d5a2812015-11-13 10:07:31 +00006401 bool is_type_check_slow_path_fatal =
6402 (type_check_kind == TypeCheckKind::kExactCheck ||
6403 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6404 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6405 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6406 !instruction->CanThrowIntoCatchBlock();
6407 SlowPathCode* type_check_slow_path =
6408 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6409 is_type_check_slow_path_fatal);
6410 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006411
Roland Levillain0d5a2812015-11-13 10:07:31 +00006412 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006413 // Avoid null check if we know obj is not null.
6414 if (instruction->MustDoNullCheck()) {
6415 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006416 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006417 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006418
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006420 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006421
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 case TypeCheckKind::kExactCheck:
6424 case TypeCheckKind::kArrayCheck: {
6425 if (cls.IsRegister()) {
6426 __ cmpl(temp, cls.AsRegister<Register>());
6427 } else {
6428 DCHECK(cls.IsStackSlot()) << cls;
6429 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6430 }
6431 // Jump to slow path for throwing the exception or doing a
6432 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006433 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006434 break;
6435 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006437 case TypeCheckKind::kAbstractClassCheck: {
6438 // If the class is abstract, we eagerly fetch the super class of the
6439 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006440 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006441 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006442 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006443 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444
6445 // If the class reference currently in `temp` is not null, jump
6446 // to the `compare_classes` label to compare it with the checked
6447 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006448 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006449 __ j(kNotEqual, &compare_classes);
6450 // Otherwise, jump to the slow path to throw the exception.
6451 //
6452 // But before, move back the object's class into `temp` before
6453 // going into the slow path, as it has been overwritten in the
6454 // meantime.
6455 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006456 GenerateReferenceLoadTwoRegisters(
6457 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006458 __ jmp(type_check_slow_path->GetEntryLabel());
6459
6460 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006461 if (cls.IsRegister()) {
6462 __ cmpl(temp, cls.AsRegister<Register>());
6463 } else {
6464 DCHECK(cls.IsStackSlot()) << cls;
6465 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6466 }
6467 __ j(kNotEqual, &loop);
6468 break;
6469 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006470
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006471 case TypeCheckKind::kClassHierarchyCheck: {
6472 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006473 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 __ Bind(&loop);
6475 if (cls.IsRegister()) {
6476 __ cmpl(temp, cls.AsRegister<Register>());
6477 } else {
6478 DCHECK(cls.IsStackSlot()) << cls;
6479 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6480 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006481 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006482
Roland Levillain0d5a2812015-11-13 10:07:31 +00006483 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006484 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006485
6486 // If the class reference currently in `temp` is not null, jump
6487 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 __ testl(temp, temp);
6489 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006490 // Otherwise, jump to the slow path to throw the exception.
6491 //
6492 // But before, move back the object's class into `temp` before
6493 // going into the slow path, as it has been overwritten in the
6494 // meantime.
6495 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006496 GenerateReferenceLoadTwoRegisters(
6497 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 break;
6500 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006501
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006502 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006503 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006504 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006505 if (cls.IsRegister()) {
6506 __ cmpl(temp, cls.AsRegister<Register>());
6507 } else {
6508 DCHECK(cls.IsStackSlot()) << cls;
6509 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6510 }
6511 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006512
6513 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006514 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006515 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006516
6517 // If the component type is not null (i.e. the object is indeed
6518 // an array), jump to label `check_non_primitive_component_type`
6519 // to further check that this component type is not a primitive
6520 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006522 __ j(kNotEqual, &check_non_primitive_component_type);
6523 // Otherwise, jump to the slow path to throw the exception.
6524 //
6525 // But before, move back the object's class into `temp` before
6526 // going into the slow path, as it has been overwritten in the
6527 // meantime.
6528 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006529 GenerateReferenceLoadTwoRegisters(
6530 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531 __ jmp(type_check_slow_path->GetEntryLabel());
6532
6533 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006534 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006535 __ j(kEqual, &done);
6536 // Same comment as above regarding `temp` and the slow path.
6537 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006538 GenerateReferenceLoadTwoRegisters(
6539 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006540 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006541 break;
6542 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006543
Calin Juravle98893e12015-10-02 21:05:03 +01006544 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006545 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006546 // We always go into the type check slow path for the unresolved
6547 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548 //
6549 // We cannot directly call the CheckCast runtime entry point
6550 // without resorting to a type checking slow path here (i.e. by
6551 // calling InvokeRuntime directly), as it would require to
6552 // assign fixed registers for the inputs of this HInstanceOf
6553 // instruction (following the runtime calling convention), which
6554 // might be cluttered by the potential first read barrier
6555 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006556 //
6557 // TODO: Introduce a new runtime entry point taking the object
6558 // to test (instead of its class) as argument, and let it deal
6559 // with the read barrier issues. This will let us refactor this
6560 // case of the `switch` code as it was previously (with a direct
6561 // call to the runtime not using a type checking slow path).
6562 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006563 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006564 break;
6565 }
6566 __ Bind(&done);
6567
Roland Levillain0d5a2812015-11-13 10:07:31 +00006568 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006569}
6570
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006571void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6572 LocationSummary* locations =
6573 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6574 InvokeRuntimeCallingConvention calling_convention;
6575 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6576}
6577
6578void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006579 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6580 : QUICK_ENTRY_POINT(pUnlockObject),
6581 instruction,
6582 instruction->GetDexPc(),
6583 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006584 if (instruction->IsEnter()) {
6585 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6586 } else {
6587 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6588 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006589}
6590
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006591void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6592void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6593void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6594
6595void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6596 LocationSummary* locations =
6597 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6598 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6599 || instruction->GetResultType() == Primitive::kPrimLong);
6600 locations->SetInAt(0, Location::RequiresRegister());
6601 locations->SetInAt(1, Location::Any());
6602 locations->SetOut(Location::SameAsFirstInput());
6603}
6604
6605void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6606 HandleBitwiseOperation(instruction);
6607}
6608
6609void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6610 HandleBitwiseOperation(instruction);
6611}
6612
6613void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6614 HandleBitwiseOperation(instruction);
6615}
6616
6617void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6618 LocationSummary* locations = instruction->GetLocations();
6619 Location first = locations->InAt(0);
6620 Location second = locations->InAt(1);
6621 DCHECK(first.Equals(locations->Out()));
6622
6623 if (instruction->GetResultType() == Primitive::kPrimInt) {
6624 if (second.IsRegister()) {
6625 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006626 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006627 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006628 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006629 } else {
6630 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006631 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006632 }
6633 } else if (second.IsConstant()) {
6634 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006635 __ andl(first.AsRegister<Register>(),
6636 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006637 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006638 __ orl(first.AsRegister<Register>(),
6639 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006640 } else {
6641 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006642 __ xorl(first.AsRegister<Register>(),
6643 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006644 }
6645 } else {
6646 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006647 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006648 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006649 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006650 } else {
6651 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006652 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006653 }
6654 }
6655 } else {
6656 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6657 if (second.IsRegisterPair()) {
6658 if (instruction->IsAnd()) {
6659 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6660 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6661 } else if (instruction->IsOr()) {
6662 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6663 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6664 } else {
6665 DCHECK(instruction->IsXor());
6666 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6667 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6668 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006669 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006670 if (instruction->IsAnd()) {
6671 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6672 __ andl(first.AsRegisterPairHigh<Register>(),
6673 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6674 } else if (instruction->IsOr()) {
6675 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6676 __ orl(first.AsRegisterPairHigh<Register>(),
6677 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6678 } else {
6679 DCHECK(instruction->IsXor());
6680 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6681 __ xorl(first.AsRegisterPairHigh<Register>(),
6682 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6683 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006684 } else {
6685 DCHECK(second.IsConstant()) << second;
6686 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006687 int32_t low_value = Low32Bits(value);
6688 int32_t high_value = High32Bits(value);
6689 Immediate low(low_value);
6690 Immediate high(high_value);
6691 Register first_low = first.AsRegisterPairLow<Register>();
6692 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006693 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006694 if (low_value == 0) {
6695 __ xorl(first_low, first_low);
6696 } else if (low_value != -1) {
6697 __ andl(first_low, low);
6698 }
6699 if (high_value == 0) {
6700 __ xorl(first_high, first_high);
6701 } else if (high_value != -1) {
6702 __ andl(first_high, high);
6703 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006704 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006705 if (low_value != 0) {
6706 __ orl(first_low, low);
6707 }
6708 if (high_value != 0) {
6709 __ orl(first_high, high);
6710 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006711 } else {
6712 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006713 if (low_value != 0) {
6714 __ xorl(first_low, low);
6715 }
6716 if (high_value != 0) {
6717 __ xorl(first_high, high);
6718 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006719 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006720 }
6721 }
6722}
6723
Roland Levillain7c1559a2015-12-15 10:55:36 +00006724void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6725 Location out,
6726 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006727 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006728 Register out_reg = out.AsRegister<Register>();
6729 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006730 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006731 if (kUseBakerReadBarrier) {
6732 // Load with fast path based Baker's read barrier.
6733 // /* HeapReference<Object> */ out = *(out + offset)
6734 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006735 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006736 } else {
6737 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006738 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006739 // in the following move operation, as we will need it for the
6740 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006741 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006742 // /* HeapReference<Object> */ out = *(out + offset)
6743 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006744 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006745 }
6746 } else {
6747 // Plain load with no read barrier.
6748 // /* HeapReference<Object> */ out = *(out + offset)
6749 __ movl(out_reg, Address(out_reg, offset));
6750 __ MaybeUnpoisonHeapReference(out_reg);
6751 }
6752}
6753
6754void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6755 Location out,
6756 Location obj,
6757 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006758 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006759 Register out_reg = out.AsRegister<Register>();
6760 Register obj_reg = obj.AsRegister<Register>();
6761 if (kEmitCompilerReadBarrier) {
6762 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006763 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006764 // Load with fast path based Baker's read barrier.
6765 // /* HeapReference<Object> */ out = *(obj + offset)
6766 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006767 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006768 } else {
6769 // Load with slow path based read barrier.
6770 // /* HeapReference<Object> */ out = *(obj + offset)
6771 __ movl(out_reg, Address(obj_reg, offset));
6772 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6773 }
6774 } else {
6775 // Plain load with no read barrier.
6776 // /* HeapReference<Object> */ out = *(obj + offset)
6777 __ movl(out_reg, Address(obj_reg, offset));
6778 __ MaybeUnpoisonHeapReference(out_reg);
6779 }
6780}
6781
6782void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6783 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006784 const Address& address,
6785 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006786 Register root_reg = root.AsRegister<Register>();
6787 if (kEmitCompilerReadBarrier) {
6788 if (kUseBakerReadBarrier) {
6789 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6790 // Baker's read barrier are used:
6791 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006792 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006793 // if (Thread::Current()->GetIsGcMarking()) {
6794 // root = ReadBarrier::Mark(root)
6795 // }
6796
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006797 // /* GcRoot<mirror::Object> */ root = *address
6798 __ movl(root_reg, address);
6799 if (fixup_label != nullptr) {
6800 __ Bind(fixup_label);
6801 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006802 static_assert(
6803 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6804 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6805 "have different sizes.");
6806 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6807 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6808 "have different sizes.");
6809
6810 // Slow path used to mark the GC root `root`.
6811 SlowPathCode* slow_path =
6812 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6813 codegen_->AddSlowPath(slow_path);
6814
6815 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6816 Immediate(0));
6817 __ j(kNotEqual, slow_path->GetEntryLabel());
6818 __ Bind(slow_path->GetExitLabel());
6819 } else {
6820 // GC root loaded through a slow path for read barriers other
6821 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006822 // /* GcRoot<mirror::Object>* */ root = address
6823 __ leal(root_reg, address);
6824 if (fixup_label != nullptr) {
6825 __ Bind(fixup_label);
6826 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006827 // /* mirror::Object* */ root = root->Read()
6828 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6829 }
6830 } else {
6831 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006832 // /* GcRoot<mirror::Object> */ root = *address
6833 __ movl(root_reg, address);
6834 if (fixup_label != nullptr) {
6835 __ Bind(fixup_label);
6836 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006837 // Note that GC roots are not affected by heap poisoning, thus we
6838 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006839 }
6840}
6841
6842void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6843 Location ref,
6844 Register obj,
6845 uint32_t offset,
6846 Location temp,
6847 bool needs_null_check) {
6848 DCHECK(kEmitCompilerReadBarrier);
6849 DCHECK(kUseBakerReadBarrier);
6850
6851 // /* HeapReference<Object> */ ref = *(obj + offset)
6852 Address src(obj, offset);
6853 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6854}
6855
6856void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6857 Location ref,
6858 Register obj,
6859 uint32_t data_offset,
6860 Location index,
6861 Location temp,
6862 bool needs_null_check) {
6863 DCHECK(kEmitCompilerReadBarrier);
6864 DCHECK(kUseBakerReadBarrier);
6865
Roland Levillain3d312422016-06-23 13:53:42 +01006866 static_assert(
6867 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6868 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00006869 // /* HeapReference<Object> */ ref =
6870 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6871 Address src = index.IsConstant() ?
6872 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6873 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6874 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6875}
6876
6877void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6878 Location ref,
6879 Register obj,
6880 const Address& src,
6881 Location temp,
6882 bool needs_null_check) {
6883 DCHECK(kEmitCompilerReadBarrier);
6884 DCHECK(kUseBakerReadBarrier);
6885
6886 // In slow path based read barriers, the read barrier call is
6887 // inserted after the original load. However, in fast path based
6888 // Baker's read barriers, we need to perform the load of
6889 // mirror::Object::monitor_ *before* the original reference load.
6890 // This load-load ordering is required by the read barrier.
6891 // The fast path/slow path (for Baker's algorithm) should look like:
6892 //
6893 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6894 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6895 // HeapReference<Object> ref = *src; // Original reference load.
6896 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6897 // if (is_gray) {
6898 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6899 // }
6900 //
6901 // Note: the original implementation in ReadBarrier::Barrier is
6902 // slightly more complex as:
6903 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006904 // the high-bits of rb_state, which are expected to be all zeroes
6905 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6906 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006907 // - it performs additional checks that we do not do here for
6908 // performance reasons.
6909
6910 Register ref_reg = ref.AsRegister<Register>();
6911 Register temp_reg = temp.AsRegister<Register>();
6912 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6913
6914 // /* int32_t */ monitor = obj->monitor_
6915 __ movl(temp_reg, Address(obj, monitor_offset));
6916 if (needs_null_check) {
6917 MaybeRecordImplicitNullCheck(instruction);
6918 }
6919 // /* LockWord */ lock_word = LockWord(monitor)
6920 static_assert(sizeof(LockWord) == sizeof(int32_t),
6921 "art::LockWord and int32_t have different sizes.");
6922 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6923 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6924 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6925 static_assert(
6926 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6927 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6928
6929 // Load fence to prevent load-load reordering.
6930 // Note that this is a no-op, thanks to the x86 memory model.
6931 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6932
6933 // The actual reference load.
6934 // /* HeapReference<Object> */ ref = *src
6935 __ movl(ref_reg, src);
6936
6937 // Object* ref = ref_addr->AsMirrorPtr()
6938 __ MaybeUnpoisonHeapReference(ref_reg);
6939
6940 // Slow path used to mark the object `ref` when it is gray.
6941 SlowPathCode* slow_path =
6942 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6943 AddSlowPath(slow_path);
6944
6945 // if (rb_state == ReadBarrier::gray_ptr_)
6946 // ref = ReadBarrier::Mark(ref);
6947 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6948 __ j(kEqual, slow_path->GetEntryLabel());
6949 __ Bind(slow_path->GetExitLabel());
6950}
6951
6952void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6953 Location out,
6954 Location ref,
6955 Location obj,
6956 uint32_t offset,
6957 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006958 DCHECK(kEmitCompilerReadBarrier);
6959
Roland Levillain7c1559a2015-12-15 10:55:36 +00006960 // Insert a slow path based read barrier *after* the reference load.
6961 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006962 // If heap poisoning is enabled, the unpoisoning of the loaded
6963 // reference will be carried out by the runtime within the slow
6964 // path.
6965 //
6966 // Note that `ref` currently does not get unpoisoned (when heap
6967 // poisoning is enabled), which is alright as the `ref` argument is
6968 // not used by the artReadBarrierSlow entry point.
6969 //
6970 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6971 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6972 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6973 AddSlowPath(slow_path);
6974
Roland Levillain0d5a2812015-11-13 10:07:31 +00006975 __ jmp(slow_path->GetEntryLabel());
6976 __ Bind(slow_path->GetExitLabel());
6977}
6978
Roland Levillain7c1559a2015-12-15 10:55:36 +00006979void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6980 Location out,
6981 Location ref,
6982 Location obj,
6983 uint32_t offset,
6984 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006985 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006986 // Baker's read barriers shall be handled by the fast path
6987 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6988 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006989 // If heap poisoning is enabled, unpoisoning will be taken care of
6990 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006991 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006992 } else if (kPoisonHeapReferences) {
6993 __ UnpoisonHeapReference(out.AsRegister<Register>());
6994 }
6995}
6996
Roland Levillain7c1559a2015-12-15 10:55:36 +00006997void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6998 Location out,
6999 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007000 DCHECK(kEmitCompilerReadBarrier);
7001
Roland Levillain7c1559a2015-12-15 10:55:36 +00007002 // Insert a slow path based read barrier *after* the GC root load.
7003 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007004 // Note that GC roots are not affected by heap poisoning, so we do
7005 // not need to do anything special for this here.
7006 SlowPathCode* slow_path =
7007 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7008 AddSlowPath(slow_path);
7009
Roland Levillain0d5a2812015-11-13 10:07:31 +00007010 __ jmp(slow_path->GetEntryLabel());
7011 __ Bind(slow_path->GetExitLabel());
7012}
7013
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007014void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007015 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007016 LOG(FATAL) << "Unreachable";
7017}
7018
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007019void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007020 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007021 LOG(FATAL) << "Unreachable";
7022}
7023
Mark Mendellfe57faa2015-09-18 09:26:15 -04007024// Simple implementation of packed switch - generate cascaded compare/jumps.
7025void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7026 LocationSummary* locations =
7027 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7028 locations->SetInAt(0, Location::RequiresRegister());
7029}
7030
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007031void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7032 int32_t lower_bound,
7033 uint32_t num_entries,
7034 HBasicBlock* switch_block,
7035 HBasicBlock* default_block) {
7036 // Figure out the correct compare values and jump conditions.
7037 // Handle the first compare/branch as a special case because it might
7038 // jump to the default case.
7039 DCHECK_GT(num_entries, 2u);
7040 Condition first_condition;
7041 uint32_t index;
7042 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7043 if (lower_bound != 0) {
7044 first_condition = kLess;
7045 __ cmpl(value_reg, Immediate(lower_bound));
7046 __ j(first_condition, codegen_->GetLabelOf(default_block));
7047 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007048
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007049 index = 1;
7050 } else {
7051 // Handle all the compare/jumps below.
7052 first_condition = kBelow;
7053 index = 0;
7054 }
7055
7056 // Handle the rest of the compare/jumps.
7057 for (; index + 1 < num_entries; index += 2) {
7058 int32_t compare_to_value = lower_bound + index + 1;
7059 __ cmpl(value_reg, Immediate(compare_to_value));
7060 // Jump to successors[index] if value < case_value[index].
7061 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7062 // Jump to successors[index + 1] if value == case_value[index + 1].
7063 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7064 }
7065
7066 if (index != num_entries) {
7067 // There are an odd number of entries. Handle the last one.
7068 DCHECK_EQ(index + 1, num_entries);
7069 __ cmpl(value_reg, Immediate(lower_bound + index));
7070 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007071 }
7072
7073 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007074 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7075 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007076 }
7077}
7078
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007079void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7080 int32_t lower_bound = switch_instr->GetStartValue();
7081 uint32_t num_entries = switch_instr->GetNumEntries();
7082 LocationSummary* locations = switch_instr->GetLocations();
7083 Register value_reg = locations->InAt(0).AsRegister<Register>();
7084
7085 GenPackedSwitchWithCompares(value_reg,
7086 lower_bound,
7087 num_entries,
7088 switch_instr->GetBlock(),
7089 switch_instr->GetDefaultBlock());
7090}
7091
Mark Mendell805b3b52015-09-18 14:10:29 -04007092void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7093 LocationSummary* locations =
7094 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7095 locations->SetInAt(0, Location::RequiresRegister());
7096
7097 // Constant area pointer.
7098 locations->SetInAt(1, Location::RequiresRegister());
7099
7100 // And the temporary we need.
7101 locations->AddTemp(Location::RequiresRegister());
7102}
7103
7104void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7105 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007106 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007107 LocationSummary* locations = switch_instr->GetLocations();
7108 Register value_reg = locations->InAt(0).AsRegister<Register>();
7109 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7110
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007111 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7112 GenPackedSwitchWithCompares(value_reg,
7113 lower_bound,
7114 num_entries,
7115 switch_instr->GetBlock(),
7116 default_block);
7117 return;
7118 }
7119
Mark Mendell805b3b52015-09-18 14:10:29 -04007120 // Optimizing has a jump area.
7121 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7122 Register constant_area = locations->InAt(1).AsRegister<Register>();
7123
7124 // Remove the bias, if needed.
7125 if (lower_bound != 0) {
7126 __ leal(temp_reg, Address(value_reg, -lower_bound));
7127 value_reg = temp_reg;
7128 }
7129
7130 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007131 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007132 __ cmpl(value_reg, Immediate(num_entries - 1));
7133 __ j(kAbove, codegen_->GetLabelOf(default_block));
7134
7135 // We are in the range of the table.
7136 // Load (target-constant_area) from the jump table, indexing by the value.
7137 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7138
7139 // Compute the actual target address by adding in constant_area.
7140 __ addl(temp_reg, constant_area);
7141
7142 // And jump.
7143 __ jmp(temp_reg);
7144}
7145
Mark Mendell0616ae02015-04-17 12:49:27 -04007146void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7147 HX86ComputeBaseMethodAddress* insn) {
7148 LocationSummary* locations =
7149 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7150 locations->SetOut(Location::RequiresRegister());
7151}
7152
7153void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7154 HX86ComputeBaseMethodAddress* insn) {
7155 LocationSummary* locations = insn->GetLocations();
7156 Register reg = locations->Out().AsRegister<Register>();
7157
7158 // Generate call to next instruction.
7159 Label next_instruction;
7160 __ call(&next_instruction);
7161 __ Bind(&next_instruction);
7162
7163 // Remember this offset for later use with constant area.
7164 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7165
7166 // Grab the return address off the stack.
7167 __ popl(reg);
7168}
7169
7170void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7171 HX86LoadFromConstantTable* insn) {
7172 LocationSummary* locations =
7173 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7174
7175 locations->SetInAt(0, Location::RequiresRegister());
7176 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7177
7178 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007179 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007180 return;
7181 }
7182
7183 switch (insn->GetType()) {
7184 case Primitive::kPrimFloat:
7185 case Primitive::kPrimDouble:
7186 locations->SetOut(Location::RequiresFpuRegister());
7187 break;
7188
7189 case Primitive::kPrimInt:
7190 locations->SetOut(Location::RequiresRegister());
7191 break;
7192
7193 default:
7194 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7195 }
7196}
7197
7198void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007199 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007200 return;
7201 }
7202
7203 LocationSummary* locations = insn->GetLocations();
7204 Location out = locations->Out();
7205 Register const_area = locations->InAt(0).AsRegister<Register>();
7206 HConstant *value = insn->GetConstant();
7207
7208 switch (insn->GetType()) {
7209 case Primitive::kPrimFloat:
7210 __ movss(out.AsFpuRegister<XmmRegister>(),
7211 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7212 break;
7213
7214 case Primitive::kPrimDouble:
7215 __ movsd(out.AsFpuRegister<XmmRegister>(),
7216 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7217 break;
7218
7219 case Primitive::kPrimInt:
7220 __ movl(out.AsRegister<Register>(),
7221 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7222 break;
7223
7224 default:
7225 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7226 }
7227}
7228
Mark Mendell0616ae02015-04-17 12:49:27 -04007229/**
7230 * Class to handle late fixup of offsets into constant area.
7231 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007232class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007233 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007234 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7235 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7236
7237 protected:
7238 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7239
7240 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007241
7242 private:
7243 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7244 // Patch the correct offset for the instruction. The place to patch is the
7245 // last 4 bytes of the instruction.
7246 // The value to patch is the distance from the offset in the constant area
7247 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007248 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7249 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007250
7251 // Patch in the right value.
7252 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7253 }
7254
Mark Mendell0616ae02015-04-17 12:49:27 -04007255 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007256 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007257};
7258
Mark Mendell805b3b52015-09-18 14:10:29 -04007259/**
7260 * Class to handle late fixup of offsets to a jump table that will be created in the
7261 * constant area.
7262 */
7263class JumpTableRIPFixup : public RIPFixup {
7264 public:
7265 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7266 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7267
7268 void CreateJumpTable() {
7269 X86Assembler* assembler = codegen_->GetAssembler();
7270
7271 // Ensure that the reference to the jump table has the correct offset.
7272 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7273 SetOffset(offset_in_constant_table);
7274
7275 // The label values in the jump table are computed relative to the
7276 // instruction addressing the constant area.
7277 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7278
7279 // Populate the jump table with the correct values for the jump table.
7280 int32_t num_entries = switch_instr_->GetNumEntries();
7281 HBasicBlock* block = switch_instr_->GetBlock();
7282 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7283 // The value that we want is the target offset - the position of the table.
7284 for (int32_t i = 0; i < num_entries; i++) {
7285 HBasicBlock* b = successors[i];
7286 Label* l = codegen_->GetLabelOf(b);
7287 DCHECK(l->IsBound());
7288 int32_t offset_to_block = l->Position() - relative_offset;
7289 assembler->AppendInt32(offset_to_block);
7290 }
7291 }
7292
7293 private:
7294 const HX86PackedSwitch* switch_instr_;
7295};
7296
7297void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7298 // Generate the constant area if needed.
7299 X86Assembler* assembler = GetAssembler();
7300 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7301 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7302 // byte values.
7303 assembler->Align(4, 0);
7304 constant_area_start_ = assembler->CodeSize();
7305
7306 // Populate any jump tables.
7307 for (auto jump_table : fixups_to_jump_tables_) {
7308 jump_table->CreateJumpTable();
7309 }
7310
7311 // And now add the constant area to the generated code.
7312 assembler->AddConstantArea();
7313 }
7314
7315 // And finish up.
7316 CodeGenerator::Finalize(allocator);
7317}
7318
Mark Mendell0616ae02015-04-17 12:49:27 -04007319Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7320 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7321 return Address(reg, kDummy32BitOffset, fixup);
7322}
7323
7324Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7325 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7326 return Address(reg, kDummy32BitOffset, fixup);
7327}
7328
7329Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7330 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7331 return Address(reg, kDummy32BitOffset, fixup);
7332}
7333
7334Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7335 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7336 return Address(reg, kDummy32BitOffset, fixup);
7337}
7338
Aart Bika19616e2016-02-01 18:57:58 -08007339void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7340 if (value == 0) {
7341 __ xorl(dest, dest);
7342 } else {
7343 __ movl(dest, Immediate(value));
7344 }
7345}
7346
7347void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7348 if (value == 0) {
7349 __ testl(dest, dest);
7350 } else {
7351 __ cmpl(dest, Immediate(value));
7352 }
7353}
7354
Mark Mendell805b3b52015-09-18 14:10:29 -04007355Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7356 Register reg,
7357 Register value) {
7358 // Create a fixup to be used to create and address the jump table.
7359 JumpTableRIPFixup* table_fixup =
7360 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7361
7362 // We have to populate the jump tables.
7363 fixups_to_jump_tables_.push_back(table_fixup);
7364
7365 // We want a scaled address, as we are extracting the correct offset from the table.
7366 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7367}
7368
Andreas Gampe85b62f22015-09-09 13:15:38 -07007369// TODO: target as memory.
7370void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7371 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007372 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007373 return;
7374 }
7375
7376 DCHECK_NE(type, Primitive::kPrimVoid);
7377
7378 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7379 if (target.Equals(return_loc)) {
7380 return;
7381 }
7382
7383 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7384 // with the else branch.
7385 if (type == Primitive::kPrimLong) {
7386 HParallelMove parallel_move(GetGraph()->GetArena());
7387 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7388 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7389 GetMoveResolver()->EmitNativeCode(&parallel_move);
7390 } else {
7391 // Let the parallel move resolver take care of all of this.
7392 HParallelMove parallel_move(GetGraph()->GetArena());
7393 parallel_move.AddMove(return_loc, target, type, nullptr);
7394 GetMoveResolver()->EmitNativeCode(&parallel_move);
7395 }
7396}
7397
Roland Levillain4d027112015-07-01 15:41:14 +01007398#undef __
7399
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007400} // namespace x86
7401} // namespace art