blob: a816d5f0e87b946868d85e8ace6c03c773f27d4e [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_x86.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010018
Mathieu Chartiere401d142015-04-22 13:56:20 -070019#include "art_method.h"
Guillaume Sanchez0f88e872015-03-30 17:55:45 +010020#include "code_generator_utils.h"
Vladimir Marko58155012015-08-19 12:49:41 +000021#include "compiled_method.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010022#include "entrypoints/quick/quick_entrypoints.h"
Nicolas Geoffraycb1b00a2015-01-28 14:50:01 +000023#include "entrypoints/quick/quick_entrypoints_enum.h"
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +010024#include "gc/accounting/card_table.h"
Mark Mendell09ed1a32015-03-25 08:30:06 -040025#include "intrinsics.h"
26#include "intrinsics_x86.h"
Ian Rogers7e70b002014-10-08 11:47:24 -070027#include "mirror/array-inl.h"
Mathieu Chartiere401d142015-04-22 13:56:20 -070028#include "mirror/class-inl.h"
Nicolas Geoffrayf6e206c2014-08-07 20:25:41 +010029#include "thread.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030#include "utils/assembler.h"
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010031#include "utils/stack_checks.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000032#include "utils/x86/assembler_x86.h"
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010033#include "utils/x86/managed_register_x86.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000034
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000035namespace art {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +010036
Roland Levillain0d5a2812015-11-13 10:07:31 +000037template<class MirrorType>
38class GcRoot;
39
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000040namespace x86 {
41
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +010042static constexpr int kCurrentMethodStackOffset = 0;
Nicolas Geoffray76b1e172015-05-27 17:18:33 +010043static constexpr Register kMethodRegisterArgument = EAX;
Mark Mendell5f874182015-03-04 15:42:45 -050044static constexpr Register kCoreCalleeSaves[] = { EBP, ESI, EDI };
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +010045
Mark Mendell24f2dfa2015-01-14 19:51:45 -050046static constexpr int kC2ConditionMask = 0x400;
47
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +000048static constexpr int kFakeReturnRegister = Register(8);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +000049
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -070050// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Calin Juravle175dc732015-08-25 15:42:32 +010052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86WordSize, x).Int32Value()
Nicolas Geoffraye5038322014-07-04 09:41:32 +010053
Andreas Gampe85b62f22015-09-09 13:15:38 -070054class NullCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffraye5038322014-07-04 09:41:32 +010055 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000056 explicit NullCheckSlowPathX86(HNullCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye5038322014-07-04 09:41:32 +010057
Alexandre Rames2ed20af2015-03-06 13:55:35 +000058 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010059 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffraye5038322014-07-04 09:41:32 +010060 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000061 if (instruction_->CanThrowIntoCatchBlock()) {
62 // Live registers will be restored in the catch block if caught.
63 SaveLiveRegisters(codegen, instruction_->GetLocations());
64 }
Alexandre Rames8158f282015-08-07 10:26:17 +010065 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowNullPointer),
66 instruction_,
67 instruction_->GetDexPc(),
68 this);
Roland Levillain888d0672015-11-23 18:53:50 +000069 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
Nicolas Geoffraye5038322014-07-04 09:41:32 +010070 }
71
Alexandre Rames8158f282015-08-07 10:26:17 +010072 bool IsFatal() const OVERRIDE { return true; }
73
Alexandre Rames9931f312015-06-19 14:47:01 +010074 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathX86"; }
75
Nicolas Geoffraye5038322014-07-04 09:41:32 +010076 private:
Nicolas Geoffraye5038322014-07-04 09:41:32 +010077 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathX86);
78};
79
Andreas Gampe85b62f22015-09-09 13:15:38 -070080class DivZeroCheckSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +000081 public:
David Srbecky9cd6d372016-02-09 15:24:47 +000082 explicit DivZeroCheckSlowPathX86(HDivZeroCheck* instruction) : SlowPathCode(instruction) {}
Calin Juravled0d48522014-11-04 16:40:20 +000083
Alexandre Rames2ed20af2015-03-06 13:55:35 +000084 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +010085 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Calin Juravled0d48522014-11-04 16:40:20 +000086 __ Bind(GetEntryLabel());
David Brazdil77a48ae2015-09-15 12:34:04 +000087 if (instruction_->CanThrowIntoCatchBlock()) {
88 // Live registers will be restored in the catch block if caught.
89 SaveLiveRegisters(codegen, instruction_->GetLocations());
90 }
Alexandre Rames8158f282015-08-07 10:26:17 +010091 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowDivZero),
92 instruction_,
93 instruction_->GetDexPc(),
94 this);
Roland Levillain888d0672015-11-23 18:53:50 +000095 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000096 }
97
Alexandre Rames8158f282015-08-07 10:26:17 +010098 bool IsFatal() const OVERRIDE { return true; }
99
Alexandre Rames9931f312015-06-19 14:47:01 +0100100 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
101
Calin Juravled0d48522014-11-04 16:40:20 +0000102 private:
Calin Juravled0d48522014-11-04 16:40:20 +0000103 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
104};
105
Andreas Gampe85b62f22015-09-09 13:15:38 -0700106class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000107 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000108 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
109 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000110
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000111 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000112 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000113 if (is_div_) {
114 __ negl(reg_);
115 } else {
116 __ movl(reg_, Immediate(0));
117 }
Calin Juravled0d48522014-11-04 16:40:20 +0000118 __ jmp(GetExitLabel());
119 }
120
Alexandre Rames9931f312015-06-19 14:47:01 +0100121 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
122
Calin Juravled0d48522014-11-04 16:40:20 +0000123 private:
124 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000125 bool is_div_;
126 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000127};
128
Andreas Gampe85b62f22015-09-09 13:15:38 -0700129class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100130 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000131 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100132
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000133 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100134 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100135 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100136 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000137 // We're moving two locations to locations that could overlap, so we need a parallel
138 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000139 if (instruction_->CanThrowIntoCatchBlock()) {
140 // Live registers will be restored in the catch block if caught.
141 SaveLiveRegisters(codegen, instruction_->GetLocations());
142 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100143 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000144 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100145 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000146 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100147 Primitive::kPrimInt,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100148 locations->InAt(1),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100149 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
150 Primitive::kPrimInt);
Alexandre Rames8158f282015-08-07 10:26:17 +0100151 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pThrowArrayBounds),
152 instruction_,
153 instruction_->GetDexPc(),
154 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000155 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100156 }
157
Alexandre Rames8158f282015-08-07 10:26:17 +0100158 bool IsFatal() const OVERRIDE { return true; }
159
Alexandre Rames9931f312015-06-19 14:47:01 +0100160 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
161
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100162 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100163 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
164};
165
Andreas Gampe85b62f22015-09-09 13:15:38 -0700166class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000167 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000168 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000169 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000170
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000171 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100172 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000173 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000174 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100175 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
176 instruction_,
177 instruction_->GetDexPc(),
178 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000179 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000180 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100181 if (successor_ == nullptr) {
182 __ jmp(GetReturnLabel());
183 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100184 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000186 }
187
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100188 Label* GetReturnLabel() {
189 DCHECK(successor_ == nullptr);
190 return &return_label_;
191 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000192
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100193 HBasicBlock* GetSuccessor() const {
194 return successor_;
195 }
196
Alexandre Rames9931f312015-06-19 14:47:01 +0100197 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
198
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000199 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100200 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201 Label return_label_;
202
203 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
204};
205
Andreas Gampe85b62f22015-09-09 13:15:38 -0700206class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000207 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000208 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000209
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000210 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 LocationSummary* locations = instruction_->GetLocations();
212 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
213
214 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
215 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000216 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000217
218 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000219 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
220 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100221 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
222 instruction_,
223 instruction_->GetDexPc(),
224 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000225 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000226 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000227 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000228
229 __ jmp(GetExitLabel());
230 }
231
Alexandre Rames9931f312015-06-19 14:47:01 +0100232 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
233
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000234 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000235 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
236};
237
Andreas Gampe85b62f22015-09-09 13:15:38 -0700238class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000239 public:
240 LoadClassSlowPathX86(HLoadClass* cls,
241 HInstruction* at,
242 uint32_t dex_pc,
243 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000244 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000245 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
246 }
247
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000248 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 LocationSummary* locations = at_->GetLocations();
250 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
251 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000252 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253
254 InvokeRuntimeCallingConvention calling_convention;
255 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100256 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
257 : QUICK_ENTRY_POINT(pInitializeType),
258 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000259 if (do_clinit_) {
260 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
261 } else {
262 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
263 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000264
265 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000266 Location out = locations->Out();
267 if (out.IsValid()) {
268 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
269 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000270 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000271
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000272 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000273 __ jmp(GetExitLabel());
274 }
275
Alexandre Rames9931f312015-06-19 14:47:01 +0100276 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
277
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000278 private:
279 // The class this slow path will load.
280 HLoadClass* const cls_;
281
282 // The instruction where this slow path is happening.
283 // (Might be the load class or an initialization check).
284 HInstruction* const at_;
285
286 // The dex PC of `at_`.
287 const uint32_t dex_pc_;
288
289 // Whether to initialize the class.
290 const bool do_clinit_;
291
292 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
293};
294
Andreas Gampe85b62f22015-09-09 13:15:38 -0700295class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000296 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000297 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000298 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000299
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000300 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000301 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100302 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
303 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000304 DCHECK(instruction_->IsCheckCast()
305 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000306
307 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
308 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000309
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000310 if (!is_fatal_) {
311 SaveLiveRegisters(codegen, locations);
312 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000313
314 // We're moving two locations to locations that could overlap, so we need a parallel
315 // move resolver.
316 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000317 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100318 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000319 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100320 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100321 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100322 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
323 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000325 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100326 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
327 instruction_,
328 instruction_->GetDexPc(),
329 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000330 CheckEntrypointTypes<
331 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000332 } else {
333 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100334 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
335 instruction_,
336 instruction_->GetDexPc(),
337 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000338 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000339 }
340
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000341 if (!is_fatal_) {
342 if (instruction_->IsInstanceOf()) {
343 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
344 }
345 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000346
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000347 __ jmp(GetExitLabel());
348 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000349 }
350
Alexandre Rames9931f312015-06-19 14:47:01 +0100351 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000352 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100353
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000354 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000356
357 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
358};
359
Andreas Gampe85b62f22015-09-09 13:15:38 -0700360class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700361 public:
Aart Bik42249c32016-01-07 15:33:50 -0800362 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000363 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700364
365 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100366 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700367 __ Bind(GetEntryLabel());
368 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100369 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
370 instruction_,
371 instruction_->GetDexPc(),
372 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000373 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700374 }
375
Alexandre Rames9931f312015-06-19 14:47:01 +0100376 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
377
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700379 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
380};
381
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100382class ArraySetSlowPathX86 : public SlowPathCode {
383 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000384 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
387 LocationSummary* locations = instruction_->GetLocations();
388 __ Bind(GetEntryLabel());
389 SaveLiveRegisters(codegen, locations);
390
391 InvokeRuntimeCallingConvention calling_convention;
392 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
393 parallel_move.AddMove(
394 locations->InAt(0),
395 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
396 Primitive::kPrimNot,
397 nullptr);
398 parallel_move.AddMove(
399 locations->InAt(1),
400 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
401 Primitive::kPrimInt,
402 nullptr);
403 parallel_move.AddMove(
404 locations->InAt(2),
405 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
406 Primitive::kPrimNot,
407 nullptr);
408 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
409
410 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
411 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
412 instruction_,
413 instruction_->GetDexPc(),
414 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000415 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100416 RestoreLiveRegisters(codegen, locations);
417 __ jmp(GetExitLabel());
418 }
419
420 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
421
422 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100423 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
424};
425
Roland Levillain7c1559a2015-12-15 10:55:36 +0000426// Slow path marking an object during a read barrier.
427class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
428 public:
429 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000430 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000431 DCHECK(kEmitCompilerReadBarrier);
432 }
433
434 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
435
436 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
437 LocationSummary* locations = instruction_->GetLocations();
438 Register reg_out = out_.AsRegister<Register>();
439 DCHECK(locations->CanCall());
440 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
441 DCHECK(instruction_->IsInstanceFieldGet() ||
442 instruction_->IsStaticFieldGet() ||
443 instruction_->IsArrayGet() ||
444 instruction_->IsLoadClass() ||
445 instruction_->IsLoadString() ||
446 instruction_->IsInstanceOf() ||
447 instruction_->IsCheckCast())
448 << "Unexpected instruction in read barrier marking slow path: "
449 << instruction_->DebugName();
450
451 __ Bind(GetEntryLabel());
452 SaveLiveRegisters(codegen, locations);
453
454 InvokeRuntimeCallingConvention calling_convention;
455 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
456 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
457 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
458 instruction_,
459 instruction_->GetDexPc(),
460 this);
461 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
462 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
463
464 RestoreLiveRegisters(codegen, locations);
465 __ jmp(GetExitLabel());
466 }
467
468 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000469 const Location out_;
470 const Location obj_;
471
472 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
473};
474
Roland Levillain0d5a2812015-11-13 10:07:31 +0000475// Slow path generating a read barrier for a heap reference.
476class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
477 public:
478 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
479 Location out,
480 Location ref,
481 Location obj,
482 uint32_t offset,
483 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000484 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000485 out_(out),
486 ref_(ref),
487 obj_(obj),
488 offset_(offset),
489 index_(index) {
490 DCHECK(kEmitCompilerReadBarrier);
491 // If `obj` is equal to `out` or `ref`, it means the initial object
492 // has been overwritten by (or after) the heap object reference load
493 // to be instrumented, e.g.:
494 //
495 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000496 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000497 //
498 // In that case, we have lost the information about the original
499 // object, and the emitted read barrier cannot work properly.
500 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
501 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
502 }
503
504 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
505 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
506 LocationSummary* locations = instruction_->GetLocations();
507 Register reg_out = out_.AsRegister<Register>();
508 DCHECK(locations->CanCall());
509 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
510 DCHECK(!instruction_->IsInvoke() ||
511 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000512 instruction_->GetLocations()->Intrinsified()))
513 << "Unexpected instruction in read barrier for heap reference slow path: "
514 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000515
516 __ Bind(GetEntryLabel());
517 SaveLiveRegisters(codegen, locations);
518
519 // We may have to change the index's value, but as `index_` is a
520 // constant member (like other "inputs" of this slow path),
521 // introduce a copy of it, `index`.
522 Location index = index_;
523 if (index_.IsValid()) {
524 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
525 if (instruction_->IsArrayGet()) {
526 // Compute the actual memory offset and store it in `index`.
527 Register index_reg = index_.AsRegister<Register>();
528 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
529 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
530 // We are about to change the value of `index_reg` (see the
531 // calls to art::x86::X86Assembler::shll and
532 // art::x86::X86Assembler::AddImmediate below), but it has
533 // not been saved by the previous call to
534 // art::SlowPathCode::SaveLiveRegisters, as it is a
535 // callee-save register --
536 // art::SlowPathCode::SaveLiveRegisters does not consider
537 // callee-save registers, as it has been designed with the
538 // assumption that callee-save registers are supposed to be
539 // handled by the called function. So, as a callee-save
540 // register, `index_reg` _would_ eventually be saved onto
541 // the stack, but it would be too late: we would have
542 // changed its value earlier. Therefore, we manually save
543 // it here into another freely available register,
544 // `free_reg`, chosen of course among the caller-save
545 // registers (as a callee-save `free_reg` register would
546 // exhibit the same problem).
547 //
548 // Note we could have requested a temporary register from
549 // the register allocator instead; but we prefer not to, as
550 // this is a slow path, and we know we can find a
551 // caller-save register that is available.
552 Register free_reg = FindAvailableCallerSaveRegister(codegen);
553 __ movl(free_reg, index_reg);
554 index_reg = free_reg;
555 index = Location::RegisterLocation(index_reg);
556 } else {
557 // The initial register stored in `index_` has already been
558 // saved in the call to art::SlowPathCode::SaveLiveRegisters
559 // (as it is not a callee-save register), so we can freely
560 // use it.
561 }
562 // Shifting the index value contained in `index_reg` by the scale
563 // factor (2) cannot overflow in practice, as the runtime is
564 // unable to allocate object arrays with a size larger than
565 // 2^26 - 1 (that is, 2^28 - 4 bytes).
566 __ shll(index_reg, Immediate(TIMES_4));
567 static_assert(
568 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
569 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
570 __ AddImmediate(index_reg, Immediate(offset_));
571 } else {
572 DCHECK(instruction_->IsInvoke());
573 DCHECK(instruction_->GetLocations()->Intrinsified());
574 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
575 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
576 << instruction_->AsInvoke()->GetIntrinsic();
577 DCHECK_EQ(offset_, 0U);
578 DCHECK(index_.IsRegisterPair());
579 // UnsafeGet's offset location is a register pair, the low
580 // part contains the correct offset.
581 index = index_.ToLow();
582 }
583 }
584
585 // We're moving two or three locations to locations that could
586 // overlap, so we need a parallel move resolver.
587 InvokeRuntimeCallingConvention calling_convention;
588 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
589 parallel_move.AddMove(ref_,
590 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
591 Primitive::kPrimNot,
592 nullptr);
593 parallel_move.AddMove(obj_,
594 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
595 Primitive::kPrimNot,
596 nullptr);
597 if (index.IsValid()) {
598 parallel_move.AddMove(index,
599 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
600 Primitive::kPrimInt,
601 nullptr);
602 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
603 } else {
604 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
605 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
606 }
607 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
608 instruction_,
609 instruction_->GetDexPc(),
610 this);
611 CheckEntrypointTypes<
612 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
613 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
614
615 RestoreLiveRegisters(codegen, locations);
616 __ jmp(GetExitLabel());
617 }
618
619 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
620
621 private:
622 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
623 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
624 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
625 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
626 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
627 return static_cast<Register>(i);
628 }
629 }
630 // We shall never fail to find a free caller-save register, as
631 // there are more than two core caller-save registers on x86
632 // (meaning it is possible to find one which is different from
633 // `ref` and `obj`).
634 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
635 LOG(FATAL) << "Could not find a free caller-save register";
636 UNREACHABLE();
637 }
638
Roland Levillain0d5a2812015-11-13 10:07:31 +0000639 const Location out_;
640 const Location ref_;
641 const Location obj_;
642 const uint32_t offset_;
643 // An additional location containing an index to an array.
644 // Only used for HArrayGet and the UnsafeGetObject &
645 // UnsafeGetObjectVolatile intrinsics.
646 const Location index_;
647
648 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
649};
650
651// Slow path generating a read barrier for a GC root.
652class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
653 public:
654 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000655 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000656 DCHECK(kEmitCompilerReadBarrier);
657 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000658
659 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
660 LocationSummary* locations = instruction_->GetLocations();
661 Register reg_out = out_.AsRegister<Register>();
662 DCHECK(locations->CanCall());
663 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000664 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
665 << "Unexpected instruction in read barrier for GC root slow path: "
666 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000667
668 __ Bind(GetEntryLabel());
669 SaveLiveRegisters(codegen, locations);
670
671 InvokeRuntimeCallingConvention calling_convention;
672 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
673 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
674 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
675 instruction_,
676 instruction_->GetDexPc(),
677 this);
678 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
679 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
680
681 RestoreLiveRegisters(codegen, locations);
682 __ jmp(GetExitLabel());
683 }
684
685 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
686
687 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000688 const Location out_;
689 const Location root_;
690
691 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
692};
693
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100694#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700695// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
696#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100697
Aart Bike9f37602015-10-09 11:15:55 -0700698inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700699 switch (cond) {
700 case kCondEQ: return kEqual;
701 case kCondNE: return kNotEqual;
702 case kCondLT: return kLess;
703 case kCondLE: return kLessEqual;
704 case kCondGT: return kGreater;
705 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700706 case kCondB: return kBelow;
707 case kCondBE: return kBelowEqual;
708 case kCondA: return kAbove;
709 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700710 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100711 LOG(FATAL) << "Unreachable";
712 UNREACHABLE();
713}
714
Aart Bike9f37602015-10-09 11:15:55 -0700715// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100716inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
717 switch (cond) {
718 case kCondEQ: return kEqual;
719 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700720 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100721 case kCondLT: return kBelow;
722 case kCondLE: return kBelowEqual;
723 case kCondGT: return kAbove;
724 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700725 // Unsigned remain unchanged.
726 case kCondB: return kBelow;
727 case kCondBE: return kBelowEqual;
728 case kCondA: return kAbove;
729 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100730 }
731 LOG(FATAL) << "Unreachable";
732 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700733}
734
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100735void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100736 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100737}
738
739void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100740 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100741}
742
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100743size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
744 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
745 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100746}
747
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100748size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
749 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
750 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100751}
752
Mark Mendell7c8d0092015-01-26 11:21:33 -0500753size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
754 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
755 return GetFloatingPointSpillSlotSize();
756}
757
758size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
759 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
760 return GetFloatingPointSpillSlotSize();
761}
762
Calin Juravle175dc732015-08-25 15:42:32 +0100763void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
764 HInstruction* instruction,
765 uint32_t dex_pc,
766 SlowPathCode* slow_path) {
767 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
768 instruction,
769 dex_pc,
770 slow_path);
771}
772
773void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100774 HInstruction* instruction,
775 uint32_t dex_pc,
776 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100777 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100778 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100779 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100780}
781
Mark Mendellfb8d2792015-03-31 22:16:59 -0400782CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000783 const X86InstructionSetFeatures& isa_features,
784 const CompilerOptions& compiler_options,
785 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500786 : CodeGenerator(graph,
787 kNumberOfCpuRegisters,
788 kNumberOfXmmRegisters,
789 kNumberOfRegisterPairs,
790 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
791 arraysize(kCoreCalleeSaves))
792 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100793 0,
794 compiler_options,
795 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100796 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100797 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100798 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400799 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100800 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000801 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100802 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400803 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000804 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000805 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
806 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100807 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100808 constant_area_start_(-1),
809 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
810 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000811 // Use a fake return address register to mimic Quick.
812 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100813}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100814
David Brazdil58282f42016-01-14 12:45:10 +0000815void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100816 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100817 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100818
819 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100820 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821
Calin Juravle34bacdf2014-10-07 20:23:36 +0100822 UpdateBlockedPairRegisters();
823}
824
825void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
826 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
827 X86ManagedRegister current =
828 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
829 if (blocked_core_registers_[current.AsRegisterPairLow()]
830 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
831 blocked_register_pairs_[i] = true;
832 }
833 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100834}
835
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100836InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800837 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100838 assembler_(codegen->GetAssembler()),
839 codegen_(codegen) {}
840
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100841static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100842 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100843}
844
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000845void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100846 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000847 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000848 bool skip_overflow_check =
849 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000850 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000851
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000852 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100853 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100854 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100855 }
856
Mark Mendell5f874182015-03-04 15:42:45 -0500857 if (HasEmptyFrame()) {
858 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000859 }
Mark Mendell5f874182015-03-04 15:42:45 -0500860
861 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
862 Register reg = kCoreCalleeSaves[i];
863 if (allocated_registers_.ContainsCoreRegister(reg)) {
864 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100865 __ cfi().AdjustCFAOffset(kX86WordSize);
866 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500867 }
868 }
869
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100870 int adjust = GetFrameSize() - FrameEntrySpillSize();
871 __ subl(ESP, Immediate(adjust));
872 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100873 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000874}
875
876void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100877 __ cfi().RememberState();
878 if (!HasEmptyFrame()) {
879 int adjust = GetFrameSize() - FrameEntrySpillSize();
880 __ addl(ESP, Immediate(adjust));
881 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500882
David Srbeckyc34dc932015-04-12 09:27:43 +0100883 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
884 Register reg = kCoreCalleeSaves[i];
885 if (allocated_registers_.ContainsCoreRegister(reg)) {
886 __ popl(reg);
887 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
888 __ cfi().Restore(DWARFReg(reg));
889 }
Mark Mendell5f874182015-03-04 15:42:45 -0500890 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000891 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100892 __ ret();
893 __ cfi().RestoreState();
894 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000895}
896
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100897void CodeGeneratorX86::Bind(HBasicBlock* block) {
898 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000899}
900
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100901Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
902 switch (type) {
903 case Primitive::kPrimBoolean:
904 case Primitive::kPrimByte:
905 case Primitive::kPrimChar:
906 case Primitive::kPrimShort:
907 case Primitive::kPrimInt:
908 case Primitive::kPrimNot:
909 return Location::RegisterLocation(EAX);
910
911 case Primitive::kPrimLong:
912 return Location::RegisterPairLocation(EAX, EDX);
913
914 case Primitive::kPrimVoid:
915 return Location::NoLocation();
916
917 case Primitive::kPrimDouble:
918 case Primitive::kPrimFloat:
919 return Location::FpuRegisterLocation(XMM0);
920 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100921
922 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100923}
924
925Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
926 return Location::RegisterLocation(kMethodRegisterArgument);
927}
928
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100929Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100930 switch (type) {
931 case Primitive::kPrimBoolean:
932 case Primitive::kPrimByte:
933 case Primitive::kPrimChar:
934 case Primitive::kPrimShort:
935 case Primitive::kPrimInt:
936 case Primitive::kPrimNot: {
937 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000938 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100939 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100940 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100941 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000942 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100943 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100944 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100945
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000946 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100947 uint32_t index = gp_index_;
948 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000949 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100950 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100951 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
952 calling_convention.GetRegisterPairAt(index));
953 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100954 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000955 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
956 }
957 }
958
959 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100960 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000961 stack_index_++;
962 if (index < calling_convention.GetNumberOfFpuRegisters()) {
963 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
964 } else {
965 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
966 }
967 }
968
969 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100970 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000971 stack_index_ += 2;
972 if (index < calling_convention.GetNumberOfFpuRegisters()) {
973 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
974 } else {
975 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100976 }
977 }
978
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 case Primitive::kPrimVoid:
980 LOG(FATAL) << "Unexpected parameter type " << type;
981 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100982 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000983 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100984}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100985
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100986void CodeGeneratorX86::Move32(Location destination, Location source) {
987 if (source.Equals(destination)) {
988 return;
989 }
990 if (destination.IsRegister()) {
991 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000992 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100993 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000994 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100995 } else {
996 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +0000997 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100998 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100999 } else if (destination.IsFpuRegister()) {
1000 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001001 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001002 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001003 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001004 } else {
1005 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001006 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001007 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001008 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001009 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001010 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001011 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001012 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001013 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001014 } else if (source.IsConstant()) {
1015 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001016 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001017 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001018 } else {
1019 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001020 __ pushl(Address(ESP, source.GetStackIndex()));
1021 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001022 }
1023 }
1024}
1025
1026void CodeGeneratorX86::Move64(Location destination, Location source) {
1027 if (source.Equals(destination)) {
1028 return;
1029 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001030 if (destination.IsRegisterPair()) {
1031 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001032 EmitParallelMoves(
1033 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1034 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001035 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001036 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001037 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1038 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001039 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001040 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1041 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1042 __ psrlq(src_reg, Immediate(32));
1043 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001044 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001045 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001046 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001047 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1048 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1050 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001051 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001052 if (source.IsFpuRegister()) {
1053 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1054 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001055 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001056 } else if (source.IsRegisterPair()) {
1057 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1058 // Create stack space for 2 elements.
1059 __ subl(ESP, Immediate(2 * elem_size));
1060 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1061 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1062 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1063 // And remove the temporary stack space we allocated.
1064 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001065 } else {
1066 LOG(FATAL) << "Unimplemented";
1067 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001068 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001069 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001070 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001071 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001072 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001073 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001074 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001075 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001076 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001077 } else if (source.IsConstant()) {
1078 HConstant* constant = source.GetConstant();
1079 int64_t value;
1080 if (constant->IsLongConstant()) {
1081 value = constant->AsLongConstant()->GetValue();
1082 } else {
1083 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001084 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001085 }
1086 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1087 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001088 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001089 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001090 EmitParallelMoves(
1091 Location::StackSlot(source.GetStackIndex()),
1092 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001093 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001094 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001095 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1096 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001097 }
1098 }
1099}
1100
Calin Juravle175dc732015-08-25 15:42:32 +01001101void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1102 DCHECK(location.IsRegister());
1103 __ movl(location.AsRegister<Register>(), Immediate(value));
1104}
1105
Calin Juravlee460d1d2015-09-29 04:52:17 +01001106void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001107 HParallelMove move(GetGraph()->GetArena());
1108 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1109 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1110 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001111 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001112 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001113 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001114 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001115}
1116
1117void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1118 if (location.IsRegister()) {
1119 locations->AddTemp(location);
1120 } else if (location.IsRegisterPair()) {
1121 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1122 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1123 } else {
1124 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1125 }
1126}
1127
David Brazdilfc6a86a2015-06-26 10:33:45 +00001128void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001129 DCHECK(!successor->IsExitBlock());
1130
1131 HBasicBlock* block = got->GetBlock();
1132 HInstruction* previous = got->GetPrevious();
1133
1134 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001135 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001136 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1137 return;
1138 }
1139
1140 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1141 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1142 }
1143 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001144 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001145 }
1146}
1147
David Brazdilfc6a86a2015-06-26 10:33:45 +00001148void LocationsBuilderX86::VisitGoto(HGoto* got) {
1149 got->SetLocations(nullptr);
1150}
1151
1152void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1153 HandleGoto(got, got->GetSuccessor());
1154}
1155
1156void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1157 try_boundary->SetLocations(nullptr);
1158}
1159
1160void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1161 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1162 if (!successor->IsExitBlock()) {
1163 HandleGoto(try_boundary, successor);
1164 }
1165}
1166
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001167void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001168 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001169}
1170
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001171void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001172}
1173
Mark Mendell152408f2015-12-31 12:28:50 -05001174template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001175void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001176 LabelType* true_label,
1177 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001178 if (cond->IsFPConditionTrueIfNaN()) {
1179 __ j(kUnordered, true_label);
1180 } else if (cond->IsFPConditionFalseIfNaN()) {
1181 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001182 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001183 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001184}
1185
Mark Mendell152408f2015-12-31 12:28:50 -05001186template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001187void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001188 LabelType* true_label,
1189 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001190 LocationSummary* locations = cond->GetLocations();
1191 Location left = locations->InAt(0);
1192 Location right = locations->InAt(1);
1193 IfCondition if_cond = cond->GetCondition();
1194
Mark Mendellc4701932015-04-10 13:18:51 -04001195 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001196 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001197 IfCondition true_high_cond = if_cond;
1198 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001199 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001200
1201 // Set the conditions for the test, remembering that == needs to be
1202 // decided using the low words.
1203 switch (if_cond) {
1204 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001205 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001206 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001207 break;
1208 case kCondLT:
1209 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001210 break;
1211 case kCondLE:
1212 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001213 break;
1214 case kCondGT:
1215 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001216 break;
1217 case kCondGE:
1218 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001219 break;
Aart Bike9f37602015-10-09 11:15:55 -07001220 case kCondB:
1221 false_high_cond = kCondA;
1222 break;
1223 case kCondBE:
1224 true_high_cond = kCondB;
1225 break;
1226 case kCondA:
1227 false_high_cond = kCondB;
1228 break;
1229 case kCondAE:
1230 true_high_cond = kCondA;
1231 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001232 }
1233
1234 if (right.IsConstant()) {
1235 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001236 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001237 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001238
Aart Bika19616e2016-02-01 18:57:58 -08001239 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001240 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001241 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001242 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001243 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001244 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001245 __ j(X86Condition(true_high_cond), true_label);
1246 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001247 }
1248 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001249 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001250 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001251 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001252 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001253
1254 __ cmpl(left_high, right_high);
1255 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001256 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001257 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001258 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001259 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001260 __ j(X86Condition(true_high_cond), true_label);
1261 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001262 }
1263 // Must be equal high, so compare the lows.
1264 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001265 } else {
1266 DCHECK(right.IsDoubleStackSlot());
1267 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1268 if (if_cond == kCondNE) {
1269 __ j(X86Condition(true_high_cond), true_label);
1270 } else if (if_cond == kCondEQ) {
1271 __ j(X86Condition(false_high_cond), false_label);
1272 } else {
1273 __ j(X86Condition(true_high_cond), true_label);
1274 __ j(X86Condition(false_high_cond), false_label);
1275 }
1276 // Must be equal high, so compare the lows.
1277 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001278 }
1279 // The last comparison might be unsigned.
1280 __ j(final_condition, true_label);
1281}
1282
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001283void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1284 Location rhs,
1285 HInstruction* insn,
1286 bool is_double) {
1287 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1288 if (is_double) {
1289 if (rhs.IsFpuRegister()) {
1290 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1291 } else if (const_area != nullptr) {
1292 DCHECK(const_area->IsEmittedAtUseSite());
1293 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1294 codegen_->LiteralDoubleAddress(
1295 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1296 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1297 } else {
1298 DCHECK(rhs.IsDoubleStackSlot());
1299 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1300 }
1301 } else {
1302 if (rhs.IsFpuRegister()) {
1303 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1304 } else if (const_area != nullptr) {
1305 DCHECK(const_area->IsEmittedAtUseSite());
1306 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1307 codegen_->LiteralFloatAddress(
1308 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1309 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1310 } else {
1311 DCHECK(rhs.IsStackSlot());
1312 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1313 }
1314 }
1315}
1316
Mark Mendell152408f2015-12-31 12:28:50 -05001317template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001318void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001319 LabelType* true_target_in,
1320 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001321 // Generated branching requires both targets to be explicit. If either of the
1322 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001323 LabelType fallthrough_target;
1324 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1325 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001326
Mark Mendellc4701932015-04-10 13:18:51 -04001327 LocationSummary* locations = condition->GetLocations();
1328 Location left = locations->InAt(0);
1329 Location right = locations->InAt(1);
1330
Mark Mendellc4701932015-04-10 13:18:51 -04001331 Primitive::Type type = condition->InputAt(0)->GetType();
1332 switch (type) {
1333 case Primitive::kPrimLong:
1334 GenerateLongComparesAndJumps(condition, true_target, false_target);
1335 break;
1336 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001337 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001338 GenerateFPJumps(condition, true_target, false_target);
1339 break;
1340 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001341 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001342 GenerateFPJumps(condition, true_target, false_target);
1343 break;
1344 default:
1345 LOG(FATAL) << "Unexpected compare type " << type;
1346 }
1347
David Brazdil0debae72015-11-12 18:37:00 +00001348 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001349 __ jmp(false_target);
1350 }
David Brazdil0debae72015-11-12 18:37:00 +00001351
1352 if (fallthrough_target.IsLinked()) {
1353 __ Bind(&fallthrough_target);
1354 }
Mark Mendellc4701932015-04-10 13:18:51 -04001355}
1356
David Brazdil0debae72015-11-12 18:37:00 +00001357static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1358 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1359 // are set only strictly before `branch`. We can't use the eflags on long/FP
1360 // conditions if they are materialized due to the complex branching.
1361 return cond->IsCondition() &&
1362 cond->GetNext() == branch &&
1363 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1364 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1365}
1366
Mark Mendell152408f2015-12-31 12:28:50 -05001367template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001368void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001369 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001370 LabelType* true_target,
1371 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001372 HInstruction* cond = instruction->InputAt(condition_input_index);
1373
1374 if (true_target == nullptr && false_target == nullptr) {
1375 // Nothing to do. The code always falls through.
1376 return;
1377 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001378 // Constant condition, statically compared against "true" (integer value 1).
1379 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001380 if (true_target != nullptr) {
1381 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001382 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001383 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001384 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001385 if (false_target != nullptr) {
1386 __ jmp(false_target);
1387 }
1388 }
1389 return;
1390 }
1391
1392 // The following code generates these patterns:
1393 // (1) true_target == nullptr && false_target != nullptr
1394 // - opposite condition true => branch to false_target
1395 // (2) true_target != nullptr && false_target == nullptr
1396 // - condition true => branch to true_target
1397 // (3) true_target != nullptr && false_target != nullptr
1398 // - condition true => branch to true_target
1399 // - branch to false_target
1400 if (IsBooleanValueOrMaterializedCondition(cond)) {
1401 if (AreEflagsSetFrom(cond, instruction)) {
1402 if (true_target == nullptr) {
1403 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1404 } else {
1405 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1406 }
1407 } else {
1408 // Materialized condition, compare against 0.
1409 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1410 if (lhs.IsRegister()) {
1411 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1412 } else {
1413 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1414 }
1415 if (true_target == nullptr) {
1416 __ j(kEqual, false_target);
1417 } else {
1418 __ j(kNotEqual, true_target);
1419 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001420 }
1421 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001422 // Condition has not been materialized, use its inputs as the comparison and
1423 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001424 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001425
1426 // If this is a long or FP comparison that has been folded into
1427 // the HCondition, generate the comparison directly.
1428 Primitive::Type type = condition->InputAt(0)->GetType();
1429 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1430 GenerateCompareTestAndBranch(condition, true_target, false_target);
1431 return;
1432 }
1433
1434 Location lhs = condition->GetLocations()->InAt(0);
1435 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001436 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001437 if (rhs.IsRegister()) {
1438 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1439 } else if (rhs.IsConstant()) {
1440 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001441 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001442 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001443 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1444 }
1445 if (true_target == nullptr) {
1446 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1447 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001448 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001449 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001450 }
David Brazdil0debae72015-11-12 18:37:00 +00001451
1452 // If neither branch falls through (case 3), the conditional branch to `true_target`
1453 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1454 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001455 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001456 }
1457}
1458
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001459void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001460 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1461 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001462 locations->SetInAt(0, Location::Any());
1463 }
1464}
1465
1466void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001467 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1468 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1469 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1470 nullptr : codegen_->GetLabelOf(true_successor);
1471 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1472 nullptr : codegen_->GetLabelOf(false_successor);
1473 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001474}
1475
1476void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1477 LocationSummary* locations = new (GetGraph()->GetArena())
1478 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001479 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001480 locations->SetInAt(0, Location::Any());
1481 }
1482}
1483
1484void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001485 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001486 GenerateTestAndBranch<Label>(deoptimize,
1487 /* condition_input_index */ 0,
1488 slow_path->GetEntryLabel(),
1489 /* false_target */ nullptr);
1490}
1491
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001492static bool SelectCanUseCMOV(HSelect* select) {
1493 // There are no conditional move instructions for XMMs.
1494 if (Primitive::IsFloatingPointType(select->GetType())) {
1495 return false;
1496 }
1497
1498 // A FP condition doesn't generate the single CC that we need.
1499 // In 32 bit mode, a long condition doesn't generate a single CC either.
1500 HInstruction* condition = select->GetCondition();
1501 if (condition->IsCondition()) {
1502 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1503 if (compare_type == Primitive::kPrimLong ||
1504 Primitive::IsFloatingPointType(compare_type)) {
1505 return false;
1506 }
1507 }
1508
1509 // We can generate a CMOV for this Select.
1510 return true;
1511}
1512
David Brazdil74eb1b22015-12-14 11:44:01 +00001513void LocationsBuilderX86::VisitSelect(HSelect* select) {
1514 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001515 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001516 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001517 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001518 } else {
1519 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001520 if (SelectCanUseCMOV(select)) {
1521 if (select->InputAt(1)->IsConstant()) {
1522 // Cmov can't handle a constant value.
1523 locations->SetInAt(1, Location::RequiresRegister());
1524 } else {
1525 locations->SetInAt(1, Location::Any());
1526 }
1527 } else {
1528 locations->SetInAt(1, Location::Any());
1529 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001530 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001531 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1532 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001533 }
1534 locations->SetOut(Location::SameAsFirstInput());
1535}
1536
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001537void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1538 Register lhs_reg = lhs.AsRegister<Register>();
1539 if (rhs.IsConstant()) {
1540 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1541 codegen_->Compare32BitValue(lhs_reg, value);
1542 } else if (rhs.IsStackSlot()) {
1543 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1544 } else {
1545 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1546 }
1547}
1548
David Brazdil74eb1b22015-12-14 11:44:01 +00001549void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1550 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001551 DCHECK(locations->InAt(0).Equals(locations->Out()));
1552 if (SelectCanUseCMOV(select)) {
1553 // If both the condition and the source types are integer, we can generate
1554 // a CMOV to implement Select.
1555
1556 HInstruction* select_condition = select->GetCondition();
1557 Condition cond = kNotEqual;
1558
1559 // Figure out how to test the 'condition'.
1560 if (select_condition->IsCondition()) {
1561 HCondition* condition = select_condition->AsCondition();
1562 if (!condition->IsEmittedAtUseSite()) {
1563 // This was a previously materialized condition.
1564 // Can we use the existing condition code?
1565 if (AreEflagsSetFrom(condition, select)) {
1566 // Materialization was the previous instruction. Condition codes are right.
1567 cond = X86Condition(condition->GetCondition());
1568 } else {
1569 // No, we have to recreate the condition code.
1570 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1571 __ testl(cond_reg, cond_reg);
1572 }
1573 } else {
1574 // We can't handle FP or long here.
1575 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1576 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1577 LocationSummary* cond_locations = condition->GetLocations();
1578 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1579 cond = X86Condition(condition->GetCondition());
1580 }
1581 } else {
1582 // Must be a boolean condition, which needs to be compared to 0.
1583 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1584 __ testl(cond_reg, cond_reg);
1585 }
1586
1587 // If the condition is true, overwrite the output, which already contains false.
1588 Location false_loc = locations->InAt(0);
1589 Location true_loc = locations->InAt(1);
1590 if (select->GetType() == Primitive::kPrimLong) {
1591 // 64 bit conditional move.
1592 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1593 Register false_low = false_loc.AsRegisterPairLow<Register>();
1594 if (true_loc.IsRegisterPair()) {
1595 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1596 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1597 } else {
1598 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1599 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1600 }
1601 } else {
1602 // 32 bit conditional move.
1603 Register false_reg = false_loc.AsRegister<Register>();
1604 if (true_loc.IsRegister()) {
1605 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1606 } else {
1607 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1608 }
1609 }
1610 } else {
1611 NearLabel false_target;
1612 GenerateTestAndBranch<NearLabel>(
1613 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1614 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1615 __ Bind(&false_target);
1616 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001617}
1618
David Srbecky0cf44932015-12-09 14:09:59 +00001619void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1620 new (GetGraph()->GetArena()) LocationSummary(info);
1621}
1622
David Srbeckyd28f4a02016-03-14 17:14:24 +00001623void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1624 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001625}
1626
1627void CodeGeneratorX86::GenerateNop() {
1628 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001629}
1630
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001631void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001632 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001633 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001634 // Handle the long/FP comparisons made in instruction simplification.
1635 switch (cond->InputAt(0)->GetType()) {
1636 case Primitive::kPrimLong: {
1637 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001638 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001639 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001640 locations->SetOut(Location::RequiresRegister());
1641 }
1642 break;
1643 }
1644 case Primitive::kPrimFloat:
1645 case Primitive::kPrimDouble: {
1646 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001647 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1648 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1649 } else if (cond->InputAt(1)->IsConstant()) {
1650 locations->SetInAt(1, Location::RequiresFpuRegister());
1651 } else {
1652 locations->SetInAt(1, Location::Any());
1653 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001654 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001655 locations->SetOut(Location::RequiresRegister());
1656 }
1657 break;
1658 }
1659 default:
1660 locations->SetInAt(0, Location::RequiresRegister());
1661 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001662 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001663 // We need a byte register.
1664 locations->SetOut(Location::RegisterLocation(ECX));
1665 }
1666 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001667 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001668}
1669
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001670void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001671 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001672 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001673 }
Mark Mendellc4701932015-04-10 13:18:51 -04001674
1675 LocationSummary* locations = cond->GetLocations();
1676 Location lhs = locations->InAt(0);
1677 Location rhs = locations->InAt(1);
1678 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001679 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001680
1681 switch (cond->InputAt(0)->GetType()) {
1682 default: {
1683 // Integer case.
1684
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001685 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001686 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001687 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001688 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001689 return;
1690 }
1691 case Primitive::kPrimLong:
1692 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1693 break;
1694 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001695 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001696 GenerateFPJumps(cond, &true_label, &false_label);
1697 break;
1698 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001699 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001700 GenerateFPJumps(cond, &true_label, &false_label);
1701 break;
1702 }
1703
1704 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001705 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001706
Roland Levillain4fa13f62015-07-06 18:11:54 +01001707 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001708 __ Bind(&false_label);
1709 __ xorl(reg, reg);
1710 __ jmp(&done_label);
1711
Roland Levillain4fa13f62015-07-06 18:11:54 +01001712 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001713 __ Bind(&true_label);
1714 __ movl(reg, Immediate(1));
1715 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001716}
1717
1718void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001719 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001720}
1721
1722void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001723 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001724}
1725
1726void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001727 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001728}
1729
1730void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001731 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001732}
1733
1734void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001735 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001736}
1737
1738void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001739 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001740}
1741
1742void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001743 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001744}
1745
1746void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001747 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001748}
1749
1750void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001751 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001752}
1753
1754void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001755 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001756}
1757
1758void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001759 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001760}
1761
1762void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001763 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001764}
1765
Aart Bike9f37602015-10-09 11:15:55 -07001766void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001767 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001768}
1769
1770void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001771 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001772}
1773
1774void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001775 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001776}
1777
1778void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001779 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001780}
1781
1782void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001783 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001784}
1785
1786void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001787 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001788}
1789
1790void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001791 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001792}
1793
1794void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001795 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001796}
1797
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001798void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001799 LocationSummary* locations =
1800 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001801 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001802}
1803
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001804void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001805 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001806}
1807
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001808void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1809 LocationSummary* locations =
1810 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1811 locations->SetOut(Location::ConstantLocation(constant));
1812}
1813
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001814void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001815 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001816}
1817
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001818void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001819 LocationSummary* locations =
1820 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001821 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001822}
1823
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001824void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825 // Will be generated at use site.
1826}
1827
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001828void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1829 LocationSummary* locations =
1830 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1831 locations->SetOut(Location::ConstantLocation(constant));
1832}
1833
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001834void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001835 // Will be generated at use site.
1836}
1837
1838void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1839 LocationSummary* locations =
1840 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1841 locations->SetOut(Location::ConstantLocation(constant));
1842}
1843
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001844void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001845 // Will be generated at use site.
1846}
1847
Calin Juravle27df7582015-04-17 19:12:31 +01001848void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1849 memory_barrier->SetLocations(nullptr);
1850}
1851
1852void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001853 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001854}
1855
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001856void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001857 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001858}
1859
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001860void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001861 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001862}
1863
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001864void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001865 LocationSummary* locations =
1866 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001867 switch (ret->InputAt(0)->GetType()) {
1868 case Primitive::kPrimBoolean:
1869 case Primitive::kPrimByte:
1870 case Primitive::kPrimChar:
1871 case Primitive::kPrimShort:
1872 case Primitive::kPrimInt:
1873 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001874 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001875 break;
1876
1877 case Primitive::kPrimLong:
1878 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001879 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001880 break;
1881
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001882 case Primitive::kPrimFloat:
1883 case Primitive::kPrimDouble:
1884 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001885 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001886 break;
1887
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001888 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001890 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001891}
1892
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001893void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001894 if (kIsDebugBuild) {
1895 switch (ret->InputAt(0)->GetType()) {
1896 case Primitive::kPrimBoolean:
1897 case Primitive::kPrimByte:
1898 case Primitive::kPrimChar:
1899 case Primitive::kPrimShort:
1900 case Primitive::kPrimInt:
1901 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001902 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001903 break;
1904
1905 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001906 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1907 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001908 break;
1909
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001910 case Primitive::kPrimFloat:
1911 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001912 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001913 break;
1914
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001915 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001916 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001917 }
1918 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001919 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001920}
1921
Calin Juravle175dc732015-08-25 15:42:32 +01001922void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1923 // The trampoline uses the same calling convention as dex calling conventions,
1924 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1925 // the method_idx.
1926 HandleInvoke(invoke);
1927}
1928
1929void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1930 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1931}
1932
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001933void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001934 // Explicit clinit checks triggered by static invokes must have been pruned by
1935 // art::PrepareForRegisterAllocation.
1936 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001937
Mark Mendellfb8d2792015-03-31 22:16:59 -04001938 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001939 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001940 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001941 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001942 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001943 return;
1944 }
1945
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001946 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001947
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001948 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1949 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001950 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001951 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001952}
1953
Mark Mendell09ed1a32015-03-25 08:30:06 -04001954static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1955 if (invoke->GetLocations()->Intrinsified()) {
1956 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1957 intrinsic.Dispatch(invoke);
1958 return true;
1959 }
1960 return false;
1961}
1962
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001963void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001964 // Explicit clinit checks triggered by static invokes must have been pruned by
1965 // art::PrepareForRegisterAllocation.
1966 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001967
Mark Mendell09ed1a32015-03-25 08:30:06 -04001968 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1969 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001970 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001971
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001972 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001973 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001974 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001975 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001976}
1977
1978void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001979 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1980 if (intrinsic.TryDispatch(invoke)) {
1981 return;
1982 }
1983
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001984 HandleInvoke(invoke);
1985}
1986
1987void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001988 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001989 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001990}
1991
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001992void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001993 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1994 return;
1995 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001996
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001997 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01001998 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01001999 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002000}
2001
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002002void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002003 // This call to HandleInvoke allocates a temporary (core) register
2004 // which is also used to transfer the hidden argument from FP to
2005 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002006 HandleInvoke(invoke);
2007 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002008 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002009}
2010
2011void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2012 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002013 LocationSummary* locations = invoke->GetLocations();
2014 Register temp = locations->GetTemp(0).AsRegister<Register>();
2015 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002016 Location receiver = locations->InAt(0);
2017 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2018
Roland Levillain0d5a2812015-11-13 10:07:31 +00002019 // Set the hidden argument. This is safe to do this here, as XMM7
2020 // won't be modified thereafter, before the `call` instruction.
2021 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002022 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002023 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002024
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025 if (receiver.IsStackSlot()) {
2026 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002027 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028 __ movl(temp, Address(temp, class_offset));
2029 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002030 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002031 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002032 }
Roland Levillain4d027112015-07-01 15:41:14 +01002033 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002034 // Instead of simply (possibly) unpoisoning `temp` here, we should
2035 // emit a read barrier for the previous class reference load.
2036 // However this is not required in practice, as this is an
2037 // intermediate/temporary reference and because the current
2038 // concurrent copying collector keeps the from-space memory
2039 // intact/accessible until the end of the marking phase (the
2040 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002041 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03002042 // temp = temp->GetAddressOfIMT()
2043 __ movl(temp,
2044 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002045 // temp = temp->GetImtEntryAt(method_offset);
Nelli Kimbadee982016-05-13 13:08:53 +03002046 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07002047 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002048 __ movl(temp, Address(temp, method_offset));
2049 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002050 __ call(Address(temp,
2051 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002052
2053 DCHECK(!codegen_->IsLeafMethod());
2054 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2055}
2056
Roland Levillain88cb1752014-10-20 16:36:47 +01002057void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2058 LocationSummary* locations =
2059 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2060 switch (neg->GetResultType()) {
2061 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002062 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002063 locations->SetInAt(0, Location::RequiresRegister());
2064 locations->SetOut(Location::SameAsFirstInput());
2065 break;
2066
Roland Levillain88cb1752014-10-20 16:36:47 +01002067 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002068 locations->SetInAt(0, Location::RequiresFpuRegister());
2069 locations->SetOut(Location::SameAsFirstInput());
2070 locations->AddTemp(Location::RequiresRegister());
2071 locations->AddTemp(Location::RequiresFpuRegister());
2072 break;
2073
Roland Levillain88cb1752014-10-20 16:36:47 +01002074 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002075 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002076 locations->SetOut(Location::SameAsFirstInput());
2077 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002078 break;
2079
2080 default:
2081 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2082 }
2083}
2084
2085void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2086 LocationSummary* locations = neg->GetLocations();
2087 Location out = locations->Out();
2088 Location in = locations->InAt(0);
2089 switch (neg->GetResultType()) {
2090 case Primitive::kPrimInt:
2091 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002092 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002094 break;
2095
2096 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002097 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002098 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002099 __ negl(out.AsRegisterPairLow<Register>());
2100 // Negation is similar to subtraction from zero. The least
2101 // significant byte triggers a borrow when it is different from
2102 // zero; to take it into account, add 1 to the most significant
2103 // byte if the carry flag (CF) is set to 1 after the first NEGL
2104 // operation.
2105 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2106 __ negl(out.AsRegisterPairHigh<Register>());
2107 break;
2108
Roland Levillain5368c212014-11-27 15:03:41 +00002109 case Primitive::kPrimFloat: {
2110 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002111 Register constant = locations->GetTemp(0).AsRegister<Register>();
2112 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002113 // Implement float negation with an exclusive or with value
2114 // 0x80000000 (mask for bit 31, representing the sign of a
2115 // single-precision floating-point number).
2116 __ movl(constant, Immediate(INT32_C(0x80000000)));
2117 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002118 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002119 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002120 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002121
Roland Levillain5368c212014-11-27 15:03:41 +00002122 case Primitive::kPrimDouble: {
2123 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002124 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002125 // Implement double negation with an exclusive or with value
2126 // 0x8000000000000000 (mask for bit 63, representing the sign of
2127 // a double-precision floating-point number).
2128 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002129 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002130 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002131 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002132
2133 default:
2134 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2135 }
2136}
2137
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002138void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2139 LocationSummary* locations =
2140 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2141 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2142 locations->SetInAt(0, Location::RequiresFpuRegister());
2143 locations->SetInAt(1, Location::RequiresRegister());
2144 locations->SetOut(Location::SameAsFirstInput());
2145 locations->AddTemp(Location::RequiresFpuRegister());
2146}
2147
2148void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2149 LocationSummary* locations = neg->GetLocations();
2150 Location out = locations->Out();
2151 DCHECK(locations->InAt(0).Equals(out));
2152
2153 Register constant_area = locations->InAt(1).AsRegister<Register>();
2154 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2155 if (neg->GetType() == Primitive::kPrimFloat) {
2156 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2157 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2158 } else {
2159 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2160 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2161 }
2162}
2163
Roland Levillaindff1f282014-11-05 14:15:05 +00002164void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002165 Primitive::Type result_type = conversion->GetResultType();
2166 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002167 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002168
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002169 // The float-to-long and double-to-long type conversions rely on a
2170 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002171 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002172 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2173 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002174 ? LocationSummary::kCall
2175 : LocationSummary::kNoCall;
2176 LocationSummary* locations =
2177 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2178
David Brazdilb2bd1c52015-03-25 11:17:37 +00002179 // The Java language does not allow treating boolean as an integral type but
2180 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002181
Roland Levillaindff1f282014-11-05 14:15:05 +00002182 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002183 case Primitive::kPrimByte:
2184 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002185 case Primitive::kPrimLong: {
2186 // Type conversion from long to byte is a result of code transformations.
2187 HInstruction* input = conversion->InputAt(0);
2188 Location input_location = input->IsConstant()
2189 ? Location::ConstantLocation(input->AsConstant())
2190 : Location::RegisterPairLocation(EAX, EDX);
2191 locations->SetInAt(0, input_location);
2192 // Make the output overlap to please the register allocator. This greatly simplifies
2193 // the validation of the linear scan implementation
2194 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2195 break;
2196 }
David Brazdil46e2a392015-03-16 17:31:52 +00002197 case Primitive::kPrimBoolean:
2198 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002199 case Primitive::kPrimShort:
2200 case Primitive::kPrimInt:
2201 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002202 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002203 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2204 // Make the output overlap to please the register allocator. This greatly simplifies
2205 // the validation of the linear scan implementation
2206 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002207 break;
2208
2209 default:
2210 LOG(FATAL) << "Unexpected type conversion from " << input_type
2211 << " to " << result_type;
2212 }
2213 break;
2214
Roland Levillain01a8d712014-11-14 16:27:39 +00002215 case Primitive::kPrimShort:
2216 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002217 case Primitive::kPrimLong:
2218 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002219 case Primitive::kPrimBoolean:
2220 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002221 case Primitive::kPrimByte:
2222 case Primitive::kPrimInt:
2223 case Primitive::kPrimChar:
2224 // Processing a Dex `int-to-short' instruction.
2225 locations->SetInAt(0, Location::Any());
2226 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2227 break;
2228
2229 default:
2230 LOG(FATAL) << "Unexpected type conversion from " << input_type
2231 << " to " << result_type;
2232 }
2233 break;
2234
Roland Levillain946e1432014-11-11 17:35:19 +00002235 case Primitive::kPrimInt:
2236 switch (input_type) {
2237 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002238 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002239 locations->SetInAt(0, Location::Any());
2240 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2241 break;
2242
2243 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002244 // Processing a Dex `float-to-int' instruction.
2245 locations->SetInAt(0, Location::RequiresFpuRegister());
2246 locations->SetOut(Location::RequiresRegister());
2247 locations->AddTemp(Location::RequiresFpuRegister());
2248 break;
2249
Roland Levillain946e1432014-11-11 17:35:19 +00002250 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002251 // Processing a Dex `double-to-int' instruction.
2252 locations->SetInAt(0, Location::RequiresFpuRegister());
2253 locations->SetOut(Location::RequiresRegister());
2254 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002255 break;
2256
2257 default:
2258 LOG(FATAL) << "Unexpected type conversion from " << input_type
2259 << " to " << result_type;
2260 }
2261 break;
2262
Roland Levillaindff1f282014-11-05 14:15:05 +00002263 case Primitive::kPrimLong:
2264 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002265 case Primitive::kPrimBoolean:
2266 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002267 case Primitive::kPrimByte:
2268 case Primitive::kPrimShort:
2269 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002270 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002271 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002272 locations->SetInAt(0, Location::RegisterLocation(EAX));
2273 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2274 break;
2275
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002276 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002277 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002278 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002279 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002280 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2281 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2282
Vladimir Marko949c91f2015-01-27 10:48:44 +00002283 // The runtime helper puts the result in EAX, EDX.
2284 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002285 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002286 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002287
2288 default:
2289 LOG(FATAL) << "Unexpected type conversion from " << input_type
2290 << " to " << result_type;
2291 }
2292 break;
2293
Roland Levillain981e4542014-11-14 11:47:14 +00002294 case Primitive::kPrimChar:
2295 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002296 case Primitive::kPrimLong:
2297 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002298 case Primitive::kPrimBoolean:
2299 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002300 case Primitive::kPrimByte:
2301 case Primitive::kPrimShort:
2302 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002303 // Processing a Dex `int-to-char' instruction.
2304 locations->SetInAt(0, Location::Any());
2305 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2306 break;
2307
2308 default:
2309 LOG(FATAL) << "Unexpected type conversion from " << input_type
2310 << " to " << result_type;
2311 }
2312 break;
2313
Roland Levillaindff1f282014-11-05 14:15:05 +00002314 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002315 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002316 case Primitive::kPrimBoolean:
2317 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002318 case Primitive::kPrimByte:
2319 case Primitive::kPrimShort:
2320 case Primitive::kPrimInt:
2321 case Primitive::kPrimChar:
2322 // Processing a Dex `int-to-float' instruction.
2323 locations->SetInAt(0, Location::RequiresRegister());
2324 locations->SetOut(Location::RequiresFpuRegister());
2325 break;
2326
2327 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002328 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002329 locations->SetInAt(0, Location::Any());
2330 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002331 break;
2332
Roland Levillaincff13742014-11-17 14:32:17 +00002333 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002334 // Processing a Dex `double-to-float' instruction.
2335 locations->SetInAt(0, Location::RequiresFpuRegister());
2336 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002337 break;
2338
2339 default:
2340 LOG(FATAL) << "Unexpected type conversion from " << input_type
2341 << " to " << result_type;
2342 };
2343 break;
2344
Roland Levillaindff1f282014-11-05 14:15:05 +00002345 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002346 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002347 case Primitive::kPrimBoolean:
2348 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002349 case Primitive::kPrimByte:
2350 case Primitive::kPrimShort:
2351 case Primitive::kPrimInt:
2352 case Primitive::kPrimChar:
2353 // Processing a Dex `int-to-double' instruction.
2354 locations->SetInAt(0, Location::RequiresRegister());
2355 locations->SetOut(Location::RequiresFpuRegister());
2356 break;
2357
2358 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002359 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002360 locations->SetInAt(0, Location::Any());
2361 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002362 break;
2363
Roland Levillaincff13742014-11-17 14:32:17 +00002364 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002365 // Processing a Dex `float-to-double' instruction.
2366 locations->SetInAt(0, Location::RequiresFpuRegister());
2367 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002368 break;
2369
2370 default:
2371 LOG(FATAL) << "Unexpected type conversion from " << input_type
2372 << " to " << result_type;
2373 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002374 break;
2375
2376 default:
2377 LOG(FATAL) << "Unexpected type conversion from " << input_type
2378 << " to " << result_type;
2379 }
2380}
2381
2382void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2383 LocationSummary* locations = conversion->GetLocations();
2384 Location out = locations->Out();
2385 Location in = locations->InAt(0);
2386 Primitive::Type result_type = conversion->GetResultType();
2387 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002388 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002389 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002390 case Primitive::kPrimByte:
2391 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002392 case Primitive::kPrimLong:
2393 // Type conversion from long to byte is a result of code transformations.
2394 if (in.IsRegisterPair()) {
2395 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2396 } else {
2397 DCHECK(in.GetConstant()->IsLongConstant());
2398 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2399 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2400 }
2401 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002402 case Primitive::kPrimBoolean:
2403 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002404 case Primitive::kPrimShort:
2405 case Primitive::kPrimInt:
2406 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002407 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002408 if (in.IsRegister()) {
2409 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002410 } else {
2411 DCHECK(in.GetConstant()->IsIntConstant());
2412 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2413 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2414 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002415 break;
2416
2417 default:
2418 LOG(FATAL) << "Unexpected type conversion from " << input_type
2419 << " to " << result_type;
2420 }
2421 break;
2422
Roland Levillain01a8d712014-11-14 16:27:39 +00002423 case Primitive::kPrimShort:
2424 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002425 case Primitive::kPrimLong:
2426 // Type conversion from long to short is a result of code transformations.
2427 if (in.IsRegisterPair()) {
2428 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2429 } else if (in.IsDoubleStackSlot()) {
2430 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2431 } else {
2432 DCHECK(in.GetConstant()->IsLongConstant());
2433 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2434 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2435 }
2436 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002437 case Primitive::kPrimBoolean:
2438 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002439 case Primitive::kPrimByte:
2440 case Primitive::kPrimInt:
2441 case Primitive::kPrimChar:
2442 // Processing a Dex `int-to-short' instruction.
2443 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002444 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002445 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002446 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002447 } else {
2448 DCHECK(in.GetConstant()->IsIntConstant());
2449 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002450 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002451 }
2452 break;
2453
2454 default:
2455 LOG(FATAL) << "Unexpected type conversion from " << input_type
2456 << " to " << result_type;
2457 }
2458 break;
2459
Roland Levillain946e1432014-11-11 17:35:19 +00002460 case Primitive::kPrimInt:
2461 switch (input_type) {
2462 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002463 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002464 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002465 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002466 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002467 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002468 } else {
2469 DCHECK(in.IsConstant());
2470 DCHECK(in.GetConstant()->IsLongConstant());
2471 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002472 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002473 }
2474 break;
2475
Roland Levillain3f8f9362014-12-02 17:45:01 +00002476 case Primitive::kPrimFloat: {
2477 // Processing a Dex `float-to-int' instruction.
2478 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2479 Register output = out.AsRegister<Register>();
2480 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002481 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002482
2483 __ movl(output, Immediate(kPrimIntMax));
2484 // temp = int-to-float(output)
2485 __ cvtsi2ss(temp, output);
2486 // if input >= temp goto done
2487 __ comiss(input, temp);
2488 __ j(kAboveEqual, &done);
2489 // if input == NaN goto nan
2490 __ j(kUnordered, &nan);
2491 // output = float-to-int-truncate(input)
2492 __ cvttss2si(output, input);
2493 __ jmp(&done);
2494 __ Bind(&nan);
2495 // output = 0
2496 __ xorl(output, output);
2497 __ Bind(&done);
2498 break;
2499 }
2500
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002501 case Primitive::kPrimDouble: {
2502 // Processing a Dex `double-to-int' instruction.
2503 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2504 Register output = out.AsRegister<Register>();
2505 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002506 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002507
2508 __ movl(output, Immediate(kPrimIntMax));
2509 // temp = int-to-double(output)
2510 __ cvtsi2sd(temp, output);
2511 // if input >= temp goto done
2512 __ comisd(input, temp);
2513 __ j(kAboveEqual, &done);
2514 // if input == NaN goto nan
2515 __ j(kUnordered, &nan);
2516 // output = double-to-int-truncate(input)
2517 __ cvttsd2si(output, input);
2518 __ jmp(&done);
2519 __ Bind(&nan);
2520 // output = 0
2521 __ xorl(output, output);
2522 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002523 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002524 }
Roland Levillain946e1432014-11-11 17:35:19 +00002525
2526 default:
2527 LOG(FATAL) << "Unexpected type conversion from " << input_type
2528 << " to " << result_type;
2529 }
2530 break;
2531
Roland Levillaindff1f282014-11-05 14:15:05 +00002532 case Primitive::kPrimLong:
2533 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002534 case Primitive::kPrimBoolean:
2535 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002536 case Primitive::kPrimByte:
2537 case Primitive::kPrimShort:
2538 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002539 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002540 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002541 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2542 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002543 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 __ cdq();
2545 break;
2546
2547 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002548 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002549 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2550 conversion,
2551 conversion->GetDexPc(),
2552 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002553 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002554 break;
2555
Roland Levillaindff1f282014-11-05 14:15:05 +00002556 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002557 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002558 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2559 conversion,
2560 conversion->GetDexPc(),
2561 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002562 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002563 break;
2564
2565 default:
2566 LOG(FATAL) << "Unexpected type conversion from " << input_type
2567 << " to " << result_type;
2568 }
2569 break;
2570
Roland Levillain981e4542014-11-14 11:47:14 +00002571 case Primitive::kPrimChar:
2572 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002573 case Primitive::kPrimLong:
2574 // Type conversion from long to short is a result of code transformations.
2575 if (in.IsRegisterPair()) {
2576 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2577 } else if (in.IsDoubleStackSlot()) {
2578 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2579 } else {
2580 DCHECK(in.GetConstant()->IsLongConstant());
2581 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2582 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2583 }
2584 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002585 case Primitive::kPrimBoolean:
2586 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002587 case Primitive::kPrimByte:
2588 case Primitive::kPrimShort:
2589 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002590 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2591 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002592 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002593 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002594 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002595 } else {
2596 DCHECK(in.GetConstant()->IsIntConstant());
2597 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002598 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002599 }
2600 break;
2601
2602 default:
2603 LOG(FATAL) << "Unexpected type conversion from " << input_type
2604 << " to " << result_type;
2605 }
2606 break;
2607
Roland Levillaindff1f282014-11-05 14:15:05 +00002608 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002609 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002610 case Primitive::kPrimBoolean:
2611 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002612 case Primitive::kPrimByte:
2613 case Primitive::kPrimShort:
2614 case Primitive::kPrimInt:
2615 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002616 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002617 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002618 break;
2619
Roland Levillain6d0e4832014-11-27 18:31:21 +00002620 case Primitive::kPrimLong: {
2621 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002622 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002623
Roland Levillain232ade02015-04-20 15:14:36 +01002624 // Create stack space for the call to
2625 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2626 // TODO: enhance register allocator to ask for stack temporaries.
2627 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2628 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2629 __ subl(ESP, Immediate(adjustment));
2630 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002631
Roland Levillain232ade02015-04-20 15:14:36 +01002632 // Load the value to the FP stack, using temporaries if needed.
2633 PushOntoFPStack(in, 0, adjustment, false, true);
2634
2635 if (out.IsStackSlot()) {
2636 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2637 } else {
2638 __ fstps(Address(ESP, 0));
2639 Location stack_temp = Location::StackSlot(0);
2640 codegen_->Move32(out, stack_temp);
2641 }
2642
2643 // Remove the temporary stack space we allocated.
2644 if (adjustment != 0) {
2645 __ addl(ESP, Immediate(adjustment));
2646 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002647 break;
2648 }
2649
Roland Levillaincff13742014-11-17 14:32:17 +00002650 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002651 // Processing a Dex `double-to-float' instruction.
2652 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002653 break;
2654
2655 default:
2656 LOG(FATAL) << "Unexpected type conversion from " << input_type
2657 << " to " << result_type;
2658 };
2659 break;
2660
Roland Levillaindff1f282014-11-05 14:15:05 +00002661 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002662 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002663 case Primitive::kPrimBoolean:
2664 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002665 case Primitive::kPrimByte:
2666 case Primitive::kPrimShort:
2667 case Primitive::kPrimInt:
2668 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002669 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002670 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002671 break;
2672
Roland Levillain647b9ed2014-11-27 12:06:00 +00002673 case Primitive::kPrimLong: {
2674 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002675 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002676
Roland Levillain232ade02015-04-20 15:14:36 +01002677 // Create stack space for the call to
2678 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2679 // TODO: enhance register allocator to ask for stack temporaries.
2680 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2681 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2682 __ subl(ESP, Immediate(adjustment));
2683 }
2684
2685 // Load the value to the FP stack, using temporaries if needed.
2686 PushOntoFPStack(in, 0, adjustment, false, true);
2687
2688 if (out.IsDoubleStackSlot()) {
2689 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2690 } else {
2691 __ fstpl(Address(ESP, 0));
2692 Location stack_temp = Location::DoubleStackSlot(0);
2693 codegen_->Move64(out, stack_temp);
2694 }
2695
2696 // Remove the temporary stack space we allocated.
2697 if (adjustment != 0) {
2698 __ addl(ESP, Immediate(adjustment));
2699 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002700 break;
2701 }
2702
Roland Levillaincff13742014-11-17 14:32:17 +00002703 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002704 // Processing a Dex `float-to-double' instruction.
2705 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002706 break;
2707
2708 default:
2709 LOG(FATAL) << "Unexpected type conversion from " << input_type
2710 << " to " << result_type;
2711 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002712 break;
2713
2714 default:
2715 LOG(FATAL) << "Unexpected type conversion from " << input_type
2716 << " to " << result_type;
2717 }
2718}
2719
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002720void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002721 LocationSummary* locations =
2722 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002723 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002724 case Primitive::kPrimInt: {
2725 locations->SetInAt(0, Location::RequiresRegister());
2726 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2728 break;
2729 }
2730
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002731 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002732 locations->SetInAt(0, Location::RequiresRegister());
2733 locations->SetInAt(1, Location::Any());
2734 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002735 break;
2736 }
2737
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002738 case Primitive::kPrimFloat:
2739 case Primitive::kPrimDouble: {
2740 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002741 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2742 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002743 } else if (add->InputAt(1)->IsConstant()) {
2744 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002745 } else {
2746 locations->SetInAt(1, Location::Any());
2747 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002748 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002749 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002750 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002751
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002752 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002753 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2754 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002755 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002756}
2757
2758void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2759 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002760 Location first = locations->InAt(0);
2761 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002762 Location out = locations->Out();
2763
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002764 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002765 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002766 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002767 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2768 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002769 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2770 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002771 } else {
2772 __ leal(out.AsRegister<Register>(), Address(
2773 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2774 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002775 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002776 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2777 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2778 __ addl(out.AsRegister<Register>(), Immediate(value));
2779 } else {
2780 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2781 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002782 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002783 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002785 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002786 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002787 }
2788
2789 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002790 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002791 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2792 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002793 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002794 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2795 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002796 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002797 } else {
2798 DCHECK(second.IsConstant()) << second;
2799 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2800 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2801 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002802 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002803 break;
2804 }
2805
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002806 case Primitive::kPrimFloat: {
2807 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002808 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002809 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2810 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002811 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002812 __ addss(first.AsFpuRegister<XmmRegister>(),
2813 codegen_->LiteralFloatAddress(
2814 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2815 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2816 } else {
2817 DCHECK(second.IsStackSlot());
2818 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002819 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002820 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002821 }
2822
2823 case Primitive::kPrimDouble: {
2824 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002825 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002826 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2827 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002828 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002829 __ addsd(first.AsFpuRegister<XmmRegister>(),
2830 codegen_->LiteralDoubleAddress(
2831 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2832 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2833 } else {
2834 DCHECK(second.IsDoubleStackSlot());
2835 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002836 }
2837 break;
2838 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002839
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002840 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002841 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002842 }
2843}
2844
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002845void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002846 LocationSummary* locations =
2847 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002848 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002849 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002850 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002851 locations->SetInAt(0, Location::RequiresRegister());
2852 locations->SetInAt(1, Location::Any());
2853 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002854 break;
2855 }
Calin Juravle11351682014-10-23 15:38:15 +01002856 case Primitive::kPrimFloat:
2857 case Primitive::kPrimDouble: {
2858 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002859 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2860 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002861 } else if (sub->InputAt(1)->IsConstant()) {
2862 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002863 } else {
2864 locations->SetInAt(1, Location::Any());
2865 }
Calin Juravle11351682014-10-23 15:38:15 +01002866 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002867 break;
Calin Juravle11351682014-10-23 15:38:15 +01002868 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002869
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002870 default:
Calin Juravle11351682014-10-23 15:38:15 +01002871 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002872 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002873}
2874
2875void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2876 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002877 Location first = locations->InAt(0);
2878 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002879 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002880 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002881 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002882 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002883 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002884 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002885 __ subl(first.AsRegister<Register>(),
2886 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002887 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002888 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002889 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002890 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002891 }
2892
2893 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002894 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002895 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2896 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002897 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002898 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002899 __ sbbl(first.AsRegisterPairHigh<Register>(),
2900 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002901 } else {
2902 DCHECK(second.IsConstant()) << second;
2903 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2904 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2905 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002906 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002907 break;
2908 }
2909
Calin Juravle11351682014-10-23 15:38:15 +01002910 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002911 if (second.IsFpuRegister()) {
2912 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2913 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2914 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002915 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002916 __ subss(first.AsFpuRegister<XmmRegister>(),
2917 codegen_->LiteralFloatAddress(
2918 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2919 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2920 } else {
2921 DCHECK(second.IsStackSlot());
2922 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2923 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002924 break;
Calin Juravle11351682014-10-23 15:38:15 +01002925 }
2926
2927 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002928 if (second.IsFpuRegister()) {
2929 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2930 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2931 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002932 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002933 __ subsd(first.AsFpuRegister<XmmRegister>(),
2934 codegen_->LiteralDoubleAddress(
2935 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2936 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2937 } else {
2938 DCHECK(second.IsDoubleStackSlot());
2939 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2940 }
Calin Juravle11351682014-10-23 15:38:15 +01002941 break;
2942 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002943
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002944 default:
Calin Juravle11351682014-10-23 15:38:15 +01002945 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002946 }
2947}
2948
Calin Juravle34bacdf2014-10-07 20:23:36 +01002949void LocationsBuilderX86::VisitMul(HMul* mul) {
2950 LocationSummary* locations =
2951 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2952 switch (mul->GetResultType()) {
2953 case Primitive::kPrimInt:
2954 locations->SetInAt(0, Location::RequiresRegister());
2955 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002956 if (mul->InputAt(1)->IsIntConstant()) {
2957 // Can use 3 operand multiply.
2958 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2959 } else {
2960 locations->SetOut(Location::SameAsFirstInput());
2961 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002962 break;
2963 case Primitive::kPrimLong: {
2964 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002965 locations->SetInAt(1, Location::Any());
2966 locations->SetOut(Location::SameAsFirstInput());
2967 // Needed for imul on 32bits with 64bits output.
2968 locations->AddTemp(Location::RegisterLocation(EAX));
2969 locations->AddTemp(Location::RegisterLocation(EDX));
2970 break;
2971 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002972 case Primitive::kPrimFloat:
2973 case Primitive::kPrimDouble: {
2974 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002975 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2976 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002977 } else if (mul->InputAt(1)->IsConstant()) {
2978 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002979 } else {
2980 locations->SetInAt(1, Location::Any());
2981 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002982 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002983 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002984 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002985
2986 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002987 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002988 }
2989}
2990
2991void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2992 LocationSummary* locations = mul->GetLocations();
2993 Location first = locations->InAt(0);
2994 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002995 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002996
2997 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002998 case Primitive::kPrimInt:
2999 // The constant may have ended up in a register, so test explicitly to avoid
3000 // problems where the output may not be the same as the first operand.
3001 if (mul->InputAt(1)->IsIntConstant()) {
3002 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3003 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3004 } else if (second.IsRegister()) {
3005 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003006 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003007 } else {
3008 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003009 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003011 }
3012 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003013
3014 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003015 Register in1_hi = first.AsRegisterPairHigh<Register>();
3016 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003017 Register eax = locations->GetTemp(0).AsRegister<Register>();
3018 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003019
3020 DCHECK_EQ(EAX, eax);
3021 DCHECK_EQ(EDX, edx);
3022
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003023 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003024 // output: in1
3025 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3026 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3027 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003028 if (second.IsConstant()) {
3029 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003030
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3032 int32_t low_value = Low32Bits(value);
3033 int32_t high_value = High32Bits(value);
3034 Immediate low(low_value);
3035 Immediate high(high_value);
3036
3037 __ movl(eax, high);
3038 // eax <- in1.lo * in2.hi
3039 __ imull(eax, in1_lo);
3040 // in1.hi <- in1.hi * in2.lo
3041 __ imull(in1_hi, low);
3042 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3043 __ addl(in1_hi, eax);
3044 // move in2_lo to eax to prepare for double precision
3045 __ movl(eax, low);
3046 // edx:eax <- in1.lo * in2.lo
3047 __ mull(in1_lo);
3048 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3049 __ addl(in1_hi, edx);
3050 // in1.lo <- (in1.lo * in2.lo)[31:0];
3051 __ movl(in1_lo, eax);
3052 } else if (second.IsRegisterPair()) {
3053 Register in2_hi = second.AsRegisterPairHigh<Register>();
3054 Register in2_lo = second.AsRegisterPairLow<Register>();
3055
3056 __ movl(eax, in2_hi);
3057 // eax <- in1.lo * in2.hi
3058 __ imull(eax, in1_lo);
3059 // in1.hi <- in1.hi * in2.lo
3060 __ imull(in1_hi, in2_lo);
3061 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3062 __ addl(in1_hi, eax);
3063 // move in1_lo to eax to prepare for double precision
3064 __ movl(eax, in1_lo);
3065 // edx:eax <- in1.lo * in2.lo
3066 __ mull(in2_lo);
3067 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3068 __ addl(in1_hi, edx);
3069 // in1.lo <- (in1.lo * in2.lo)[31:0];
3070 __ movl(in1_lo, eax);
3071 } else {
3072 DCHECK(second.IsDoubleStackSlot()) << second;
3073 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3074 Address in2_lo(ESP, second.GetStackIndex());
3075
3076 __ movl(eax, in2_hi);
3077 // eax <- in1.lo * in2.hi
3078 __ imull(eax, in1_lo);
3079 // in1.hi <- in1.hi * in2.lo
3080 __ imull(in1_hi, in2_lo);
3081 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3082 __ addl(in1_hi, eax);
3083 // move in1_lo to eax to prepare for double precision
3084 __ movl(eax, in1_lo);
3085 // edx:eax <- in1.lo * in2.lo
3086 __ mull(in2_lo);
3087 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3088 __ addl(in1_hi, edx);
3089 // in1.lo <- (in1.lo * in2.lo)[31:0];
3090 __ movl(in1_lo, eax);
3091 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003092
3093 break;
3094 }
3095
Calin Juravleb5bfa962014-10-21 18:02:24 +01003096 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003097 DCHECK(first.Equals(locations->Out()));
3098 if (second.IsFpuRegister()) {
3099 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3100 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3101 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003102 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003103 __ mulss(first.AsFpuRegister<XmmRegister>(),
3104 codegen_->LiteralFloatAddress(
3105 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3106 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3107 } else {
3108 DCHECK(second.IsStackSlot());
3109 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3110 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003111 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003112 }
3113
3114 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003115 DCHECK(first.Equals(locations->Out()));
3116 if (second.IsFpuRegister()) {
3117 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3118 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3119 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003120 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003121 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3122 codegen_->LiteralDoubleAddress(
3123 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3124 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3125 } else {
3126 DCHECK(second.IsDoubleStackSlot());
3127 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3128 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003129 break;
3130 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003131
3132 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003133 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134 }
3135}
3136
Roland Levillain232ade02015-04-20 15:14:36 +01003137void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3138 uint32_t temp_offset,
3139 uint32_t stack_adjustment,
3140 bool is_fp,
3141 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003142 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003143 DCHECK(!is_wide);
3144 if (is_fp) {
3145 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3146 } else {
3147 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3148 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003149 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003150 DCHECK(is_wide);
3151 if (is_fp) {
3152 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3153 } else {
3154 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3155 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003156 } else {
3157 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003158 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003159 Location stack_temp = Location::StackSlot(temp_offset);
3160 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003161 if (is_fp) {
3162 __ flds(Address(ESP, temp_offset));
3163 } else {
3164 __ filds(Address(ESP, temp_offset));
3165 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003166 } else {
3167 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3168 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003169 if (is_fp) {
3170 __ fldl(Address(ESP, temp_offset));
3171 } else {
3172 __ fildl(Address(ESP, temp_offset));
3173 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003174 }
3175 }
3176}
3177
3178void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3179 Primitive::Type type = rem->GetResultType();
3180 bool is_float = type == Primitive::kPrimFloat;
3181 size_t elem_size = Primitive::ComponentSize(type);
3182 LocationSummary* locations = rem->GetLocations();
3183 Location first = locations->InAt(0);
3184 Location second = locations->InAt(1);
3185 Location out = locations->Out();
3186
3187 // Create stack space for 2 elements.
3188 // TODO: enhance register allocator to ask for stack temporaries.
3189 __ subl(ESP, Immediate(2 * elem_size));
3190
3191 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003192 const bool is_wide = !is_float;
3193 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3194 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003195
3196 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003197 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003198 __ Bind(&retry);
3199 __ fprem();
3200
3201 // Move FP status to AX.
3202 __ fstsw();
3203
3204 // And see if the argument reduction is complete. This is signaled by the
3205 // C2 FPU flag bit set to 0.
3206 __ andl(EAX, Immediate(kC2ConditionMask));
3207 __ j(kNotEqual, &retry);
3208
3209 // We have settled on the final value. Retrieve it into an XMM register.
3210 // Store FP top of stack to real stack.
3211 if (is_float) {
3212 __ fsts(Address(ESP, 0));
3213 } else {
3214 __ fstl(Address(ESP, 0));
3215 }
3216
3217 // Pop the 2 items from the FP stack.
3218 __ fucompp();
3219
3220 // Load the value from the stack into an XMM register.
3221 DCHECK(out.IsFpuRegister()) << out;
3222 if (is_float) {
3223 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3224 } else {
3225 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3226 }
3227
3228 // And remove the temporary stack space we allocated.
3229 __ addl(ESP, Immediate(2 * elem_size));
3230}
3231
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003232
3233void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3234 DCHECK(instruction->IsDiv() || instruction->IsRem());
3235
3236 LocationSummary* locations = instruction->GetLocations();
3237 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003238 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003239
3240 Register out_register = locations->Out().AsRegister<Register>();
3241 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003242 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003243
3244 DCHECK(imm == 1 || imm == -1);
3245
3246 if (instruction->IsRem()) {
3247 __ xorl(out_register, out_register);
3248 } else {
3249 __ movl(out_register, input_register);
3250 if (imm == -1) {
3251 __ negl(out_register);
3252 }
3253 }
3254}
3255
3256
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003257void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003258 LocationSummary* locations = instruction->GetLocations();
3259
3260 Register out_register = locations->Out().AsRegister<Register>();
3261 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003262 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003263 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3264 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003265
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003266 Register num = locations->GetTemp(0).AsRegister<Register>();
3267
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003268 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003269 __ testl(input_register, input_register);
3270 __ cmovl(kGreaterEqual, num, input_register);
3271 int shift = CTZ(imm);
3272 __ sarl(num, Immediate(shift));
3273
3274 if (imm < 0) {
3275 __ negl(num);
3276 }
3277
3278 __ movl(out_register, num);
3279}
3280
3281void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3282 DCHECK(instruction->IsDiv() || instruction->IsRem());
3283
3284 LocationSummary* locations = instruction->GetLocations();
3285 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3286
3287 Register eax = locations->InAt(0).AsRegister<Register>();
3288 Register out = locations->Out().AsRegister<Register>();
3289 Register num;
3290 Register edx;
3291
3292 if (instruction->IsDiv()) {
3293 edx = locations->GetTemp(0).AsRegister<Register>();
3294 num = locations->GetTemp(1).AsRegister<Register>();
3295 } else {
3296 edx = locations->Out().AsRegister<Register>();
3297 num = locations->GetTemp(0).AsRegister<Register>();
3298 }
3299
3300 DCHECK_EQ(EAX, eax);
3301 DCHECK_EQ(EDX, edx);
3302 if (instruction->IsDiv()) {
3303 DCHECK_EQ(EAX, out);
3304 } else {
3305 DCHECK_EQ(EDX, out);
3306 }
3307
3308 int64_t magic;
3309 int shift;
3310 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3311
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003312 // Save the numerator.
3313 __ movl(num, eax);
3314
3315 // EAX = magic
3316 __ movl(eax, Immediate(magic));
3317
3318 // EDX:EAX = magic * numerator
3319 __ imull(num);
3320
3321 if (imm > 0 && magic < 0) {
3322 // EDX += num
3323 __ addl(edx, num);
3324 } else if (imm < 0 && magic > 0) {
3325 __ subl(edx, num);
3326 }
3327
3328 // Shift if needed.
3329 if (shift != 0) {
3330 __ sarl(edx, Immediate(shift));
3331 }
3332
3333 // EDX += 1 if EDX < 0
3334 __ movl(eax, edx);
3335 __ shrl(edx, Immediate(31));
3336 __ addl(edx, eax);
3337
3338 if (instruction->IsRem()) {
3339 __ movl(eax, num);
3340 __ imull(edx, Immediate(imm));
3341 __ subl(eax, edx);
3342 __ movl(edx, eax);
3343 } else {
3344 __ movl(eax, edx);
3345 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003346}
3347
Calin Juravlebacfec32014-11-14 15:54:36 +00003348void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3349 DCHECK(instruction->IsDiv() || instruction->IsRem());
3350
3351 LocationSummary* locations = instruction->GetLocations();
3352 Location out = locations->Out();
3353 Location first = locations->InAt(0);
3354 Location second = locations->InAt(1);
3355 bool is_div = instruction->IsDiv();
3356
3357 switch (instruction->GetResultType()) {
3358 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003359 DCHECK_EQ(EAX, first.AsRegister<Register>());
3360 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003361
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003362 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003363 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003364
3365 if (imm == 0) {
3366 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3367 } else if (imm == 1 || imm == -1) {
3368 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003369 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003370 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003371 } else {
3372 DCHECK(imm <= -2 || imm >= 2);
3373 GenerateDivRemWithAnyConstant(instruction);
3374 }
3375 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003376 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3377 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003378 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003379
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003380 Register second_reg = second.AsRegister<Register>();
3381 // 0x80000000/-1 triggers an arithmetic exception!
3382 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3383 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003384
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003385 __ cmpl(second_reg, Immediate(-1));
3386 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003387
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 // edx:eax <- sign-extended of eax
3389 __ cdq();
3390 // eax = quotient, edx = remainder
3391 __ idivl(second_reg);
3392 __ Bind(slow_path->GetExitLabel());
3393 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003394 break;
3395 }
3396
3397 case Primitive::kPrimLong: {
3398 InvokeRuntimeCallingConvention calling_convention;
3399 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3400 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3401 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3402 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3403 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3404 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3405
3406 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003407 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3408 instruction,
3409 instruction->GetDexPc(),
3410 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003411 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003412 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003413 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3414 instruction,
3415 instruction->GetDexPc(),
3416 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003417 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003418 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003419 break;
3420 }
3421
3422 default:
3423 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3424 }
3425}
3426
Calin Juravle7c4954d2014-10-28 16:57:40 +00003427void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003428 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003429 ? LocationSummary::kCall
3430 : LocationSummary::kNoCall;
3431 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3432
Calin Juravle7c4954d2014-10-28 16:57:40 +00003433 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003434 case Primitive::kPrimInt: {
3435 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003436 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003437 locations->SetOut(Location::SameAsFirstInput());
3438 // Intel uses edx:eax as the dividend.
3439 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003440 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3441 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3442 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003443 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003444 locations->AddTemp(Location::RequiresRegister());
3445 }
Calin Juravled0d48522014-11-04 16:40:20 +00003446 break;
3447 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003448 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003449 InvokeRuntimeCallingConvention calling_convention;
3450 locations->SetInAt(0, Location::RegisterPairLocation(
3451 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3452 locations->SetInAt(1, Location::RegisterPairLocation(
3453 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3454 // Runtime helper puts the result in EAX, EDX.
3455 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003456 break;
3457 }
3458 case Primitive::kPrimFloat:
3459 case Primitive::kPrimDouble: {
3460 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003461 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3462 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003463 } else if (div->InputAt(1)->IsConstant()) {
3464 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003465 } else {
3466 locations->SetInAt(1, Location::Any());
3467 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003468 locations->SetOut(Location::SameAsFirstInput());
3469 break;
3470 }
3471
3472 default:
3473 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3474 }
3475}
3476
3477void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3478 LocationSummary* locations = div->GetLocations();
3479 Location first = locations->InAt(0);
3480 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003481
3482 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003483 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003484 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003485 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003486 break;
3487 }
3488
3489 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003490 if (second.IsFpuRegister()) {
3491 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3492 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3493 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003494 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003495 __ divss(first.AsFpuRegister<XmmRegister>(),
3496 codegen_->LiteralFloatAddress(
3497 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3498 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3499 } else {
3500 DCHECK(second.IsStackSlot());
3501 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3502 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003503 break;
3504 }
3505
3506 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003507 if (second.IsFpuRegister()) {
3508 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3509 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3510 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003511 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003512 __ divsd(first.AsFpuRegister<XmmRegister>(),
3513 codegen_->LiteralDoubleAddress(
3514 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3515 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3516 } else {
3517 DCHECK(second.IsDoubleStackSlot());
3518 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3519 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003520 break;
3521 }
3522
3523 default:
3524 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3525 }
3526}
3527
Calin Juravlebacfec32014-11-14 15:54:36 +00003528void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003529 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003530
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003531 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3532 ? LocationSummary::kCall
3533 : LocationSummary::kNoCall;
3534 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003535
Calin Juravled2ec87d2014-12-08 14:24:46 +00003536 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003537 case Primitive::kPrimInt: {
3538 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003539 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003541 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3542 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3543 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003544 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003545 locations->AddTemp(Location::RequiresRegister());
3546 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003547 break;
3548 }
3549 case Primitive::kPrimLong: {
3550 InvokeRuntimeCallingConvention calling_convention;
3551 locations->SetInAt(0, Location::RegisterPairLocation(
3552 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3553 locations->SetInAt(1, Location::RegisterPairLocation(
3554 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3555 // Runtime helper puts the result in EAX, EDX.
3556 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3557 break;
3558 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003559 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003560 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003561 locations->SetInAt(0, Location::Any());
3562 locations->SetInAt(1, Location::Any());
3563 locations->SetOut(Location::RequiresFpuRegister());
3564 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003565 break;
3566 }
3567
3568 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003569 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003570 }
3571}
3572
3573void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3574 Primitive::Type type = rem->GetResultType();
3575 switch (type) {
3576 case Primitive::kPrimInt:
3577 case Primitive::kPrimLong: {
3578 GenerateDivRemIntegral(rem);
3579 break;
3580 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003581 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003582 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003583 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003584 break;
3585 }
3586 default:
3587 LOG(FATAL) << "Unexpected rem type " << type;
3588 }
3589}
3590
Calin Juravled0d48522014-11-04 16:40:20 +00003591void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003592 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3593 ? LocationSummary::kCallOnSlowPath
3594 : LocationSummary::kNoCall;
3595 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003596 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003597 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003598 case Primitive::kPrimByte:
3599 case Primitive::kPrimChar:
3600 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003601 case Primitive::kPrimInt: {
3602 locations->SetInAt(0, Location::Any());
3603 break;
3604 }
3605 case Primitive::kPrimLong: {
3606 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3607 if (!instruction->IsConstant()) {
3608 locations->AddTemp(Location::RequiresRegister());
3609 }
3610 break;
3611 }
3612 default:
3613 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3614 }
Calin Juravled0d48522014-11-04 16:40:20 +00003615 if (instruction->HasUses()) {
3616 locations->SetOut(Location::SameAsFirstInput());
3617 }
3618}
3619
3620void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003621 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003622 codegen_->AddSlowPath(slow_path);
3623
3624 LocationSummary* locations = instruction->GetLocations();
3625 Location value = locations->InAt(0);
3626
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003627 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003628 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003629 case Primitive::kPrimByte:
3630 case Primitive::kPrimChar:
3631 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003632 case Primitive::kPrimInt: {
3633 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003634 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003635 __ j(kEqual, slow_path->GetEntryLabel());
3636 } else if (value.IsStackSlot()) {
3637 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3638 __ j(kEqual, slow_path->GetEntryLabel());
3639 } else {
3640 DCHECK(value.IsConstant()) << value;
3641 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3642 __ jmp(slow_path->GetEntryLabel());
3643 }
3644 }
3645 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003646 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003647 case Primitive::kPrimLong: {
3648 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003649 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003650 __ movl(temp, value.AsRegisterPairLow<Register>());
3651 __ orl(temp, value.AsRegisterPairHigh<Register>());
3652 __ j(kEqual, slow_path->GetEntryLabel());
3653 } else {
3654 DCHECK(value.IsConstant()) << value;
3655 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3656 __ jmp(slow_path->GetEntryLabel());
3657 }
3658 }
3659 break;
3660 }
3661 default:
3662 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003663 }
Calin Juravled0d48522014-11-04 16:40:20 +00003664}
3665
Calin Juravle9aec02f2014-11-18 23:06:35 +00003666void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3667 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3668
3669 LocationSummary* locations =
3670 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3671
3672 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003673 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003674 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003675 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003676 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003677 // The shift count needs to be in CL or a constant.
3678 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003679 locations->SetOut(Location::SameAsFirstInput());
3680 break;
3681 }
3682 default:
3683 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3684 }
3685}
3686
3687void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3688 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3689
3690 LocationSummary* locations = op->GetLocations();
3691 Location first = locations->InAt(0);
3692 Location second = locations->InAt(1);
3693 DCHECK(first.Equals(locations->Out()));
3694
3695 switch (op->GetResultType()) {
3696 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003697 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003698 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003699 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003700 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003701 DCHECK_EQ(ECX, second_reg);
3702 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003703 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003704 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003705 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003706 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003707 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003708 }
3709 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003710 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003711 if (shift == 0) {
3712 return;
3713 }
3714 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003715 if (op->IsShl()) {
3716 __ shll(first_reg, imm);
3717 } else if (op->IsShr()) {
3718 __ sarl(first_reg, imm);
3719 } else {
3720 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003721 }
3722 }
3723 break;
3724 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003725 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003726 if (second.IsRegister()) {
3727 Register second_reg = second.AsRegister<Register>();
3728 DCHECK_EQ(ECX, second_reg);
3729 if (op->IsShl()) {
3730 GenerateShlLong(first, second_reg);
3731 } else if (op->IsShr()) {
3732 GenerateShrLong(first, second_reg);
3733 } else {
3734 GenerateUShrLong(first, second_reg);
3735 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003736 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003737 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003738 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003739 // Nothing to do if the shift is 0, as the input is already the output.
3740 if (shift != 0) {
3741 if (op->IsShl()) {
3742 GenerateShlLong(first, shift);
3743 } else if (op->IsShr()) {
3744 GenerateShrLong(first, shift);
3745 } else {
3746 GenerateUShrLong(first, shift);
3747 }
3748 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003749 }
3750 break;
3751 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003752 default:
3753 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3754 }
3755}
3756
Mark P Mendell73945692015-04-29 14:56:17 +00003757void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3758 Register low = loc.AsRegisterPairLow<Register>();
3759 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003760 if (shift == 1) {
3761 // This is just an addition.
3762 __ addl(low, low);
3763 __ adcl(high, high);
3764 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003765 // Shift by 32 is easy. High gets low, and low gets 0.
3766 codegen_->EmitParallelMoves(
3767 loc.ToLow(),
3768 loc.ToHigh(),
3769 Primitive::kPrimInt,
3770 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3771 loc.ToLow(),
3772 Primitive::kPrimInt);
3773 } else if (shift > 32) {
3774 // Low part becomes 0. High part is low part << (shift-32).
3775 __ movl(high, low);
3776 __ shll(high, Immediate(shift - 32));
3777 __ xorl(low, low);
3778 } else {
3779 // Between 1 and 31.
3780 __ shld(high, low, Immediate(shift));
3781 __ shll(low, Immediate(shift));
3782 }
3783}
3784
Calin Juravle9aec02f2014-11-18 23:06:35 +00003785void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003786 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003787 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3788 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3789 __ testl(shifter, Immediate(32));
3790 __ j(kEqual, &done);
3791 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3792 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3793 __ Bind(&done);
3794}
3795
Mark P Mendell73945692015-04-29 14:56:17 +00003796void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3797 Register low = loc.AsRegisterPairLow<Register>();
3798 Register high = loc.AsRegisterPairHigh<Register>();
3799 if (shift == 32) {
3800 // Need to copy the sign.
3801 DCHECK_NE(low, high);
3802 __ movl(low, high);
3803 __ sarl(high, Immediate(31));
3804 } else if (shift > 32) {
3805 DCHECK_NE(low, high);
3806 // High part becomes sign. Low part is shifted by shift - 32.
3807 __ movl(low, high);
3808 __ sarl(high, Immediate(31));
3809 __ sarl(low, Immediate(shift - 32));
3810 } else {
3811 // Between 1 and 31.
3812 __ shrd(low, high, Immediate(shift));
3813 __ sarl(high, Immediate(shift));
3814 }
3815}
3816
Calin Juravle9aec02f2014-11-18 23:06:35 +00003817void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003818 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003819 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3820 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3821 __ testl(shifter, Immediate(32));
3822 __ j(kEqual, &done);
3823 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3824 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3825 __ Bind(&done);
3826}
3827
Mark P Mendell73945692015-04-29 14:56:17 +00003828void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3829 Register low = loc.AsRegisterPairLow<Register>();
3830 Register high = loc.AsRegisterPairHigh<Register>();
3831 if (shift == 32) {
3832 // Shift by 32 is easy. Low gets high, and high gets 0.
3833 codegen_->EmitParallelMoves(
3834 loc.ToHigh(),
3835 loc.ToLow(),
3836 Primitive::kPrimInt,
3837 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3838 loc.ToHigh(),
3839 Primitive::kPrimInt);
3840 } else if (shift > 32) {
3841 // Low part is high >> (shift - 32). High part becomes 0.
3842 __ movl(low, high);
3843 __ shrl(low, Immediate(shift - 32));
3844 __ xorl(high, high);
3845 } else {
3846 // Between 1 and 31.
3847 __ shrd(low, high, Immediate(shift));
3848 __ shrl(high, Immediate(shift));
3849 }
3850}
3851
Calin Juravle9aec02f2014-11-18 23:06:35 +00003852void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003853 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003854 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3855 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3856 __ testl(shifter, Immediate(32));
3857 __ j(kEqual, &done);
3858 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3859 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3860 __ Bind(&done);
3861}
3862
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003863void LocationsBuilderX86::VisitRor(HRor* ror) {
3864 LocationSummary* locations =
3865 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3866
3867 switch (ror->GetResultType()) {
3868 case Primitive::kPrimLong:
3869 // Add the temporary needed.
3870 locations->AddTemp(Location::RequiresRegister());
3871 FALLTHROUGH_INTENDED;
3872 case Primitive::kPrimInt:
3873 locations->SetInAt(0, Location::RequiresRegister());
3874 // The shift count needs to be in CL (unless it is a constant).
3875 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3876 locations->SetOut(Location::SameAsFirstInput());
3877 break;
3878 default:
3879 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3880 UNREACHABLE();
3881 }
3882}
3883
3884void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3885 LocationSummary* locations = ror->GetLocations();
3886 Location first = locations->InAt(0);
3887 Location second = locations->InAt(1);
3888
3889 if (ror->GetResultType() == Primitive::kPrimInt) {
3890 Register first_reg = first.AsRegister<Register>();
3891 if (second.IsRegister()) {
3892 Register second_reg = second.AsRegister<Register>();
3893 __ rorl(first_reg, second_reg);
3894 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003895 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003896 __ rorl(first_reg, imm);
3897 }
3898 return;
3899 }
3900
3901 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3902 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3903 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3904 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3905 if (second.IsRegister()) {
3906 Register second_reg = second.AsRegister<Register>();
3907 DCHECK_EQ(second_reg, ECX);
3908 __ movl(temp_reg, first_reg_hi);
3909 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3910 __ shrd(first_reg_lo, temp_reg, second_reg);
3911 __ movl(temp_reg, first_reg_hi);
3912 __ testl(second_reg, Immediate(32));
3913 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3914 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3915 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003916 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003917 if (shift_amt == 0) {
3918 // Already fine.
3919 return;
3920 }
3921 if (shift_amt == 32) {
3922 // Just swap.
3923 __ movl(temp_reg, first_reg_lo);
3924 __ movl(first_reg_lo, first_reg_hi);
3925 __ movl(first_reg_hi, temp_reg);
3926 return;
3927 }
3928
3929 Immediate imm(shift_amt);
3930 // Save the constents of the low value.
3931 __ movl(temp_reg, first_reg_lo);
3932
3933 // Shift right into low, feeding bits from high.
3934 __ shrd(first_reg_lo, first_reg_hi, imm);
3935
3936 // Shift right into high, feeding bits from the original low.
3937 __ shrd(first_reg_hi, temp_reg, imm);
3938
3939 // Swap if needed.
3940 if (shift_amt > 32) {
3941 __ movl(temp_reg, first_reg_lo);
3942 __ movl(first_reg_lo, first_reg_hi);
3943 __ movl(first_reg_hi, temp_reg);
3944 }
3945 }
3946}
3947
Calin Juravle9aec02f2014-11-18 23:06:35 +00003948void LocationsBuilderX86::VisitShl(HShl* shl) {
3949 HandleShift(shl);
3950}
3951
3952void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3953 HandleShift(shl);
3954}
3955
3956void LocationsBuilderX86::VisitShr(HShr* shr) {
3957 HandleShift(shr);
3958}
3959
3960void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3961 HandleShift(shr);
3962}
3963
3964void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3965 HandleShift(ushr);
3966}
3967
3968void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3969 HandleShift(ushr);
3970}
3971
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003972void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003973 LocationSummary* locations =
3974 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003975 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003976 if (instruction->IsStringAlloc()) {
3977 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3978 } else {
3979 InvokeRuntimeCallingConvention calling_convention;
3980 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3981 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3982 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003983}
3984
3985void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003986 // Note: if heap poisoning is enabled, the entry point takes cares
3987 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003988 if (instruction->IsStringAlloc()) {
3989 // String is allocated through StringFactory. Call NewEmptyString entry point.
3990 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3991 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3992 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3993 __ call(Address(temp, code_offset.Int32Value()));
3994 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3995 } else {
3996 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
3997 instruction,
3998 instruction->GetDexPc(),
3999 nullptr);
4000 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4001 DCHECK(!codegen_->IsLeafMethod());
4002 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004003}
4004
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004005void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4006 LocationSummary* locations =
4007 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4008 locations->SetOut(Location::RegisterLocation(EAX));
4009 InvokeRuntimeCallingConvention calling_convention;
4010 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004011 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004012 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004013}
4014
4015void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4016 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004017 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004018 // Note: if heap poisoning is enabled, the entry point takes cares
4019 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004020 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4021 instruction,
4022 instruction->GetDexPc(),
4023 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004024 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004025 DCHECK(!codegen_->IsLeafMethod());
4026}
4027
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004028void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004029 LocationSummary* locations =
4030 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004031 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4032 if (location.IsStackSlot()) {
4033 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4034 } else if (location.IsDoubleStackSlot()) {
4035 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004036 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004037 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004038}
4039
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004040void InstructionCodeGeneratorX86::VisitParameterValue(
4041 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4042}
4043
4044void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4045 LocationSummary* locations =
4046 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4047 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4048}
4049
4050void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004051}
4052
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004053void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4054 LocationSummary* locations =
4055 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4056 locations->SetInAt(0, Location::RequiresRegister());
4057 locations->SetOut(Location::RequiresRegister());
4058}
4059
4060void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4061 LocationSummary* locations = instruction->GetLocations();
4062 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004063 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004064 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4065 instruction->GetIndex(), kX86PointerSize).SizeValue();
4066 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03004067 __ movl(locations->InAt(0).AsRegister<Register>(),
4068 Address(locations->InAt(0).AsRegister<Register>(),
4069 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4070 // temp = temp->GetImtEntryAt(method_offset);
4071 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07004072 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004073 }
4074 __ movl(locations->Out().AsRegister<Register>(),
4075 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4076}
4077
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004078void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004079 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004080 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004081 locations->SetInAt(0, Location::RequiresRegister());
4082 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004083}
4084
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004085void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4086 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004087 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004088 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004089 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004090 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004091 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004092 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004093 break;
4094
4095 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004096 __ notl(out.AsRegisterPairLow<Register>());
4097 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004098 break;
4099
4100 default:
4101 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4102 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004103}
4104
David Brazdil66d126e2015-04-03 16:02:44 +01004105void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4106 LocationSummary* locations =
4107 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4108 locations->SetInAt(0, Location::RequiresRegister());
4109 locations->SetOut(Location::SameAsFirstInput());
4110}
4111
4112void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004113 LocationSummary* locations = bool_not->GetLocations();
4114 Location in = locations->InAt(0);
4115 Location out = locations->Out();
4116 DCHECK(in.Equals(out));
4117 __ xorl(out.AsRegister<Register>(), Immediate(1));
4118}
4119
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004120void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004121 LocationSummary* locations =
4122 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004123 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004124 case Primitive::kPrimBoolean:
4125 case Primitive::kPrimByte:
4126 case Primitive::kPrimShort:
4127 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004128 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004129 case Primitive::kPrimLong: {
4130 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004131 locations->SetInAt(1, Location::Any());
4132 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4133 break;
4134 }
4135 case Primitive::kPrimFloat:
4136 case Primitive::kPrimDouble: {
4137 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004138 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4139 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4140 } else if (compare->InputAt(1)->IsConstant()) {
4141 locations->SetInAt(1, Location::RequiresFpuRegister());
4142 } else {
4143 locations->SetInAt(1, Location::Any());
4144 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004145 locations->SetOut(Location::RequiresRegister());
4146 break;
4147 }
4148 default:
4149 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4150 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004151}
4152
4153void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004154 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004155 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004156 Location left = locations->InAt(0);
4157 Location right = locations->InAt(1);
4158
Mark Mendell0c9497d2015-08-21 09:30:05 -04004159 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004160 Condition less_cond = kLess;
4161
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004162 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004163 case Primitive::kPrimBoolean:
4164 case Primitive::kPrimByte:
4165 case Primitive::kPrimShort:
4166 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004167 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004168 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004169 break;
4170 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004171 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004172 Register left_low = left.AsRegisterPairLow<Register>();
4173 Register left_high = left.AsRegisterPairHigh<Register>();
4174 int32_t val_low = 0;
4175 int32_t val_high = 0;
4176 bool right_is_const = false;
4177
4178 if (right.IsConstant()) {
4179 DCHECK(right.GetConstant()->IsLongConstant());
4180 right_is_const = true;
4181 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4182 val_low = Low32Bits(val);
4183 val_high = High32Bits(val);
4184 }
4185
Calin Juravleddb7df22014-11-25 20:56:51 +00004186 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004187 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004188 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004189 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004190 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004191 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004192 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004193 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004194 __ j(kLess, &less); // Signed compare.
4195 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004196 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004197 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004198 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004199 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004200 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004201 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004202 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004203 }
Aart Bika19616e2016-02-01 18:57:58 -08004204 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004205 break;
4206 }
4207 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004208 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004209 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004210 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004211 break;
4212 }
4213 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004214 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004215 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004216 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004217 break;
4218 }
4219 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004220 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004221 }
Aart Bika19616e2016-02-01 18:57:58 -08004222
Calin Juravleddb7df22014-11-25 20:56:51 +00004223 __ movl(out, Immediate(0));
4224 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004225 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004226
4227 __ Bind(&greater);
4228 __ movl(out, Immediate(1));
4229 __ jmp(&done);
4230
4231 __ Bind(&less);
4232 __ movl(out, Immediate(-1));
4233
4234 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004235}
4236
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004237void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004238 LocationSummary* locations =
4239 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004240 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004241 locations->SetInAt(i, Location::Any());
4242 }
4243 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004244}
4245
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004246void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004247 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004248}
4249
Roland Levillain7c1559a2015-12-15 10:55:36 +00004250void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004251 /*
4252 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4253 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4254 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4255 */
4256 switch (kind) {
4257 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004258 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004259 break;
4260 }
4261 case MemBarrierKind::kAnyStore:
4262 case MemBarrierKind::kLoadAny:
4263 case MemBarrierKind::kStoreStore: {
4264 // nop
4265 break;
4266 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004267 case MemBarrierKind::kNTStoreStore:
4268 // Non-Temporal Store/Store needs an explicit fence.
4269 MemoryFence(/* non-temporal */ true);
4270 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004271 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004272}
4273
Vladimir Markodc151b22015-10-15 18:02:30 +01004274HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4275 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4276 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004277 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4278
4279 // We disable pc-relative load when there is an irreducible loop, as the optimization
4280 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004281 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4282 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004283 if (GetGraph()->HasIrreducibleLoops() &&
4284 (dispatch_info.method_load_kind ==
4285 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4286 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4287 }
4288 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004289 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4290 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4291 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4292 // (Though the direct CALL ptr16:32 is available for consideration).
4293 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004294 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004295 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004296 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004297 0u
4298 };
4299 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004300 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004301 }
4302}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004303
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004304Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4305 Register temp) {
4306 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004307 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004308 if (!invoke->GetLocations()->Intrinsified()) {
4309 return location.AsRegister<Register>();
4310 }
4311 // For intrinsics we allow any location, so it may be on the stack.
4312 if (!location.IsRegister()) {
4313 __ movl(temp, Address(ESP, location.GetStackIndex()));
4314 return temp;
4315 }
4316 // For register locations, check if the register was saved. If so, get it from the stack.
4317 // Note: There is a chance that the register was saved but not overwritten, so we could
4318 // save one load. However, since this is just an intrinsic slow path we prefer this
4319 // simple and more robust approach rather that trying to determine if that's the case.
4320 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004321 if (slow_path != nullptr) {
4322 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4323 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4324 __ movl(temp, Address(ESP, stack_offset));
4325 return temp;
4326 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004327 }
4328 return location.AsRegister<Register>();
4329}
4330
Serguei Katkov288c7a82016-05-16 11:53:15 +06004331Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4332 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004333 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4334 switch (invoke->GetMethodLoadKind()) {
4335 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4336 // temp = thread->string_init_entrypoint
4337 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4338 break;
4339 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004340 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004341 break;
4342 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4343 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4344 break;
4345 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004346 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004347 method_patches_.emplace_back(invoke->GetTargetMethod());
4348 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4349 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004350 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4351 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4352 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004353 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004354 // Bind a new fixup label at the end of the "movl" insn.
4355 uint32_t offset = invoke->GetDexCacheArrayOffset();
4356 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004357 break;
4358 }
Vladimir Marko58155012015-08-19 12:49:41 +00004359 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004360 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004361 Register method_reg;
4362 Register reg = temp.AsRegister<Register>();
4363 if (current_method.IsRegister()) {
4364 method_reg = current_method.AsRegister<Register>();
4365 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004366 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004367 DCHECK(!current_method.IsValid());
4368 method_reg = reg;
4369 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4370 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004371 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004372 __ movl(reg, Address(method_reg,
4373 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004374 // temp = temp[index_in_cache];
4375 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4376 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004377 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4378 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004379 }
Vladimir Marko58155012015-08-19 12:49:41 +00004380 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004381 return callee_method;
4382}
4383
4384void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4385 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004386
4387 switch (invoke->GetCodePtrLocation()) {
4388 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4389 __ call(GetFrameEntryLabel());
4390 break;
4391 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4392 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4393 Label* label = &relative_call_patches_.back().label;
4394 __ call(label); // Bind to the patch label, override at link time.
4395 __ Bind(label); // Bind the label at the end of the "call" insn.
4396 break;
4397 }
4398 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4399 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004400 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4401 LOG(FATAL) << "Unsupported";
4402 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004403 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4404 // (callee_method + offset_of_quick_compiled_code)()
4405 __ call(Address(callee_method.AsRegister<Register>(),
4406 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4407 kX86WordSize).Int32Value()));
4408 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004409 }
4410
4411 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004412}
4413
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004414void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4415 Register temp = temp_in.AsRegister<Register>();
4416 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4417 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004418
4419 // Use the calling convention instead of the location of the receiver, as
4420 // intrinsics may have put the receiver in a different register. In the intrinsics
4421 // slow path, the arguments have been moved to the right place, so here we are
4422 // guaranteed that the receiver is the first register of the calling convention.
4423 InvokeDexCallingConvention calling_convention;
4424 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004425 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004426 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004427 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004428 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004429 // Instead of simply (possibly) unpoisoning `temp` here, we should
4430 // emit a read barrier for the previous class reference load.
4431 // However this is not required in practice, as this is an
4432 // intermediate/temporary reference and because the current
4433 // concurrent copying collector keeps the from-space memory
4434 // intact/accessible until the end of the marking phase (the
4435 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004436 __ MaybeUnpoisonHeapReference(temp);
4437 // temp = temp->GetMethodAt(method_offset);
4438 __ movl(temp, Address(temp, method_offset));
4439 // call temp->GetEntryPoint();
4440 __ call(Address(
4441 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4442}
4443
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004444void CodeGeneratorX86::RecordSimplePatch() {
4445 if (GetCompilerOptions().GetIncludePatchInformation()) {
4446 simple_patches_.emplace_back();
4447 __ Bind(&simple_patches_.back());
4448 }
4449}
4450
4451void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4452 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4453 __ Bind(&string_patches_.back().label);
4454}
4455
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004456void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
4457 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex());
4458 __ Bind(&type_patches_.back().label);
4459}
4460
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004461Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4462 uint32_t element_offset) {
4463 // Add the patch entry and bind its label at the end of the instruction.
4464 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4465 return &pc_relative_dex_cache_patches_.back().label;
4466}
4467
Vladimir Marko58155012015-08-19 12:49:41 +00004468void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4469 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004470 size_t size =
4471 method_patches_.size() +
4472 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004473 pc_relative_dex_cache_patches_.size() +
4474 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004475 string_patches_.size() +
4476 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004477 linker_patches->reserve(size);
4478 // The label points to the end of the "movl" insn but the literal offset for method
4479 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4480 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004481 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004482 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004483 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4484 info.target_method.dex_file,
4485 info.target_method.dex_method_index));
4486 }
4487 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004488 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004489 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4490 info.target_method.dex_file,
4491 info.target_method.dex_method_index));
4492 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004493 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4494 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4495 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4496 &info.target_dex_file,
4497 GetMethodAddressOffset(),
4498 info.element_offset));
4499 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004500 for (const Label& label : simple_patches_) {
4501 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4502 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4503 }
4504 if (GetCompilerOptions().GetCompilePic()) {
4505 for (const StringPatchInfo<Label>& info : string_patches_) {
4506 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4507 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4508 &info.dex_file,
4509 GetMethodAddressOffset(),
4510 info.string_index));
4511 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004512 for (const TypePatchInfo<Label>& info : type_patches_) {
4513 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4514 linker_patches->push_back(LinkerPatch::RelativeTypePatch(literal_offset,
4515 &info.dex_file,
4516 GetMethodAddressOffset(),
4517 info.type_index));
4518 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004519 } else {
4520 for (const StringPatchInfo<Label>& info : string_patches_) {
4521 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4522 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4523 &info.dex_file,
4524 info.string_index));
4525 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004526 for (const TypePatchInfo<Label>& info : type_patches_) {
4527 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4528 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset,
4529 &info.dex_file,
4530 info.type_index));
4531 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004532 }
Vladimir Marko58155012015-08-19 12:49:41 +00004533}
4534
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004535void CodeGeneratorX86::MarkGCCard(Register temp,
4536 Register card,
4537 Register object,
4538 Register value,
4539 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004540 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004541 if (value_can_be_null) {
4542 __ testl(value, value);
4543 __ j(kEqual, &is_null);
4544 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004545 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4546 __ movl(temp, object);
4547 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004548 __ movb(Address(temp, card, TIMES_1, 0),
4549 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004550 if (value_can_be_null) {
4551 __ Bind(&is_null);
4552 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004553}
4554
Calin Juravle52c48962014-12-16 17:02:57 +00004555void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4556 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004557
4558 bool object_field_get_with_read_barrier =
4559 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004560 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004561 new (GetGraph()->GetArena()) LocationSummary(instruction,
4562 kEmitCompilerReadBarrier ?
4563 LocationSummary::kCallOnSlowPath :
4564 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004565 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004566
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004567 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4568 locations->SetOut(Location::RequiresFpuRegister());
4569 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004570 // The output overlaps in case of long: we don't want the low move
4571 // to overwrite the object's location. Likewise, in the case of
4572 // an object field get with read barriers enabled, we do not want
4573 // the move to overwrite the object's location, as we need it to emit
4574 // the read barrier.
4575 locations->SetOut(
4576 Location::RequiresRegister(),
4577 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4578 Location::kOutputOverlap :
4579 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004580 }
Calin Juravle52c48962014-12-16 17:02:57 +00004581
4582 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4583 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004584 // So we use an XMM register as a temp to achieve atomicity (first
4585 // load the temp into the XMM and then copy the XMM into the
4586 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004587 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004588 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4589 // We need a temporary register for the read barrier marking slow
4590 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4591 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004592 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004593}
4594
Calin Juravle52c48962014-12-16 17:02:57 +00004595void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4596 const FieldInfo& field_info) {
4597 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004598
Calin Juravle52c48962014-12-16 17:02:57 +00004599 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004600 Location base_loc = locations->InAt(0);
4601 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004602 Location out = locations->Out();
4603 bool is_volatile = field_info.IsVolatile();
4604 Primitive::Type field_type = field_info.GetFieldType();
4605 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4606
4607 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004608 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004609 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004610 break;
4611 }
4612
4613 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004614 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004615 break;
4616 }
4617
4618 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004619 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004620 break;
4621 }
4622
4623 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004624 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004625 break;
4626 }
4627
4628 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004629 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004630 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004631
4632 case Primitive::kPrimNot: {
4633 // /* HeapReference<Object> */ out = *(base + offset)
4634 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4635 Location temp_loc = locations->GetTemp(0);
4636 // Note that a potential implicit null check is handled in this
4637 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4638 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4639 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4640 if (is_volatile) {
4641 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4642 }
4643 } else {
4644 __ movl(out.AsRegister<Register>(), Address(base, offset));
4645 codegen_->MaybeRecordImplicitNullCheck(instruction);
4646 if (is_volatile) {
4647 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4648 }
4649 // If read barriers are enabled, emit read barriers other than
4650 // Baker's using a slow path (and also unpoison the loaded
4651 // reference, if heap poisoning is enabled).
4652 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4653 }
4654 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 }
4656
4657 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004658 if (is_volatile) {
4659 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4660 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004661 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004662 __ movd(out.AsRegisterPairLow<Register>(), temp);
4663 __ psrlq(temp, Immediate(32));
4664 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4665 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004666 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004667 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004668 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004669 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4670 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671 break;
4672 }
4673
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004674 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004675 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004676 break;
4677 }
4678
4679 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004680 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004681 break;
4682 }
4683
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004684 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004685 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004686 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004687 }
Calin Juravle52c48962014-12-16 17:02:57 +00004688
Roland Levillain7c1559a2015-12-15 10:55:36 +00004689 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4690 // Potential implicit null checks, in the case of reference or
4691 // long fields, are handled in the previous switch statement.
4692 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004693 codegen_->MaybeRecordImplicitNullCheck(instruction);
4694 }
4695
Calin Juravle52c48962014-12-16 17:02:57 +00004696 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004697 if (field_type == Primitive::kPrimNot) {
4698 // Memory barriers, in the case of references, are also handled
4699 // in the previous switch statement.
4700 } else {
4701 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4702 }
Roland Levillain4d027112015-07-01 15:41:14 +01004703 }
Calin Juravle52c48962014-12-16 17:02:57 +00004704}
4705
4706void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4707 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4708
4709 LocationSummary* locations =
4710 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4711 locations->SetInAt(0, Location::RequiresRegister());
4712 bool is_volatile = field_info.IsVolatile();
4713 Primitive::Type field_type = field_info.GetFieldType();
4714 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4715 || (field_type == Primitive::kPrimByte);
4716
4717 // The register allocator does not support multiple
4718 // inputs that die at entry with one in a specific register.
4719 if (is_byte_type) {
4720 // Ensure the value is in a byte register.
4721 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004722 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004723 if (is_volatile && field_type == Primitive::kPrimDouble) {
4724 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4725 locations->SetInAt(1, Location::RequiresFpuRegister());
4726 } else {
4727 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4728 }
4729 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4730 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004731 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004732
Calin Juravle52c48962014-12-16 17:02:57 +00004733 // 64bits value can be atomically written to an address with movsd and an XMM register.
4734 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4735 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4736 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4737 // isolated cases when we need this it isn't worth adding the extra complexity.
4738 locations->AddTemp(Location::RequiresFpuRegister());
4739 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004740 } else {
4741 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4742
4743 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4744 // Temporary registers for the write barrier.
4745 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4746 // Ensure the card is in a byte register.
4747 locations->AddTemp(Location::RegisterLocation(ECX));
4748 }
Calin Juravle52c48962014-12-16 17:02:57 +00004749 }
4750}
4751
4752void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004753 const FieldInfo& field_info,
4754 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004755 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4756
4757 LocationSummary* locations = instruction->GetLocations();
4758 Register base = locations->InAt(0).AsRegister<Register>();
4759 Location value = locations->InAt(1);
4760 bool is_volatile = field_info.IsVolatile();
4761 Primitive::Type field_type = field_info.GetFieldType();
4762 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004763 bool needs_write_barrier =
4764 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004765
4766 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004767 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004768 }
4769
Mark Mendell81489372015-11-04 11:30:41 -05004770 bool maybe_record_implicit_null_check_done = false;
4771
Calin Juravle52c48962014-12-16 17:02:57 +00004772 switch (field_type) {
4773 case Primitive::kPrimBoolean:
4774 case Primitive::kPrimByte: {
4775 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4776 break;
4777 }
4778
4779 case Primitive::kPrimShort:
4780 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004781 if (value.IsConstant()) {
4782 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4783 __ movw(Address(base, offset), Immediate(v));
4784 } else {
4785 __ movw(Address(base, offset), value.AsRegister<Register>());
4786 }
Calin Juravle52c48962014-12-16 17:02:57 +00004787 break;
4788 }
4789
4790 case Primitive::kPrimInt:
4791 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004792 if (kPoisonHeapReferences && needs_write_barrier) {
4793 // Note that in the case where `value` is a null reference,
4794 // we do not enter this block, as the reference does not
4795 // need poisoning.
4796 DCHECK_EQ(field_type, Primitive::kPrimNot);
4797 Register temp = locations->GetTemp(0).AsRegister<Register>();
4798 __ movl(temp, value.AsRegister<Register>());
4799 __ PoisonHeapReference(temp);
4800 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004801 } else if (value.IsConstant()) {
4802 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4803 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004804 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004805 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004806 __ movl(Address(base, offset), value.AsRegister<Register>());
4807 }
Calin Juravle52c48962014-12-16 17:02:57 +00004808 break;
4809 }
4810
4811 case Primitive::kPrimLong: {
4812 if (is_volatile) {
4813 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4814 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4815 __ movd(temp1, value.AsRegisterPairLow<Register>());
4816 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4817 __ punpckldq(temp1, temp2);
4818 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004819 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004820 } else if (value.IsConstant()) {
4821 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4822 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4823 codegen_->MaybeRecordImplicitNullCheck(instruction);
4824 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004825 } else {
4826 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004827 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004828 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4829 }
Mark Mendell81489372015-11-04 11:30:41 -05004830 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004831 break;
4832 }
4833
4834 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004835 if (value.IsConstant()) {
4836 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4837 __ movl(Address(base, offset), Immediate(v));
4838 } else {
4839 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4840 }
Calin Juravle52c48962014-12-16 17:02:57 +00004841 break;
4842 }
4843
4844 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004845 if (value.IsConstant()) {
4846 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4847 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4848 codegen_->MaybeRecordImplicitNullCheck(instruction);
4849 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4850 maybe_record_implicit_null_check_done = true;
4851 } else {
4852 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4853 }
Calin Juravle52c48962014-12-16 17:02:57 +00004854 break;
4855 }
4856
4857 case Primitive::kPrimVoid:
4858 LOG(FATAL) << "Unreachable type " << field_type;
4859 UNREACHABLE();
4860 }
4861
Mark Mendell81489372015-11-04 11:30:41 -05004862 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004863 codegen_->MaybeRecordImplicitNullCheck(instruction);
4864 }
4865
Roland Levillain4d027112015-07-01 15:41:14 +01004866 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004867 Register temp = locations->GetTemp(0).AsRegister<Register>();
4868 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004869 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004870 }
4871
Calin Juravle52c48962014-12-16 17:02:57 +00004872 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004873 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004874 }
4875}
4876
4877void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4878 HandleFieldGet(instruction, instruction->GetFieldInfo());
4879}
4880
4881void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4882 HandleFieldGet(instruction, instruction->GetFieldInfo());
4883}
4884
4885void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4886 HandleFieldSet(instruction, instruction->GetFieldInfo());
4887}
4888
4889void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004890 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004891}
4892
4893void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4894 HandleFieldSet(instruction, instruction->GetFieldInfo());
4895}
4896
4897void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004898 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004899}
4900
4901void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4902 HandleFieldGet(instruction, instruction->GetFieldInfo());
4903}
4904
4905void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4906 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004907}
4908
Calin Juravlee460d1d2015-09-29 04:52:17 +01004909void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4910 HUnresolvedInstanceFieldGet* instruction) {
4911 FieldAccessCallingConventionX86 calling_convention;
4912 codegen_->CreateUnresolvedFieldLocationSummary(
4913 instruction, instruction->GetFieldType(), calling_convention);
4914}
4915
4916void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4917 HUnresolvedInstanceFieldGet* instruction) {
4918 FieldAccessCallingConventionX86 calling_convention;
4919 codegen_->GenerateUnresolvedFieldAccess(instruction,
4920 instruction->GetFieldType(),
4921 instruction->GetFieldIndex(),
4922 instruction->GetDexPc(),
4923 calling_convention);
4924}
4925
4926void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4927 HUnresolvedInstanceFieldSet* instruction) {
4928 FieldAccessCallingConventionX86 calling_convention;
4929 codegen_->CreateUnresolvedFieldLocationSummary(
4930 instruction, instruction->GetFieldType(), calling_convention);
4931}
4932
4933void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4934 HUnresolvedInstanceFieldSet* instruction) {
4935 FieldAccessCallingConventionX86 calling_convention;
4936 codegen_->GenerateUnresolvedFieldAccess(instruction,
4937 instruction->GetFieldType(),
4938 instruction->GetFieldIndex(),
4939 instruction->GetDexPc(),
4940 calling_convention);
4941}
4942
4943void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4944 HUnresolvedStaticFieldGet* instruction) {
4945 FieldAccessCallingConventionX86 calling_convention;
4946 codegen_->CreateUnresolvedFieldLocationSummary(
4947 instruction, instruction->GetFieldType(), calling_convention);
4948}
4949
4950void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4951 HUnresolvedStaticFieldGet* instruction) {
4952 FieldAccessCallingConventionX86 calling_convention;
4953 codegen_->GenerateUnresolvedFieldAccess(instruction,
4954 instruction->GetFieldType(),
4955 instruction->GetFieldIndex(),
4956 instruction->GetDexPc(),
4957 calling_convention);
4958}
4959
4960void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4961 HUnresolvedStaticFieldSet* instruction) {
4962 FieldAccessCallingConventionX86 calling_convention;
4963 codegen_->CreateUnresolvedFieldLocationSummary(
4964 instruction, instruction->GetFieldType(), calling_convention);
4965}
4966
4967void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4968 HUnresolvedStaticFieldSet* instruction) {
4969 FieldAccessCallingConventionX86 calling_convention;
4970 codegen_->GenerateUnresolvedFieldAccess(instruction,
4971 instruction->GetFieldType(),
4972 instruction->GetFieldIndex(),
4973 instruction->GetDexPc(),
4974 calling_convention);
4975}
4976
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004977void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004978 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4979 ? LocationSummary::kCallOnSlowPath
4980 : LocationSummary::kNoCall;
4981 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4982 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004983 ? Location::RequiresRegister()
4984 : Location::Any();
4985 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004986 if (instruction->HasUses()) {
4987 locations->SetOut(Location::SameAsFirstInput());
4988 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989}
4990
Calin Juravle2ae48182016-03-16 14:05:09 +00004991void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4992 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004993 return;
4994 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004995 LocationSummary* locations = instruction->GetLocations();
4996 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004997
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004998 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004999 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005000}
5001
Calin Juravle2ae48182016-03-16 14:05:09 +00005002void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005003 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005004 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005005
5006 LocationSummary* locations = instruction->GetLocations();
5007 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005008
5009 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005010 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005011 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005012 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005013 } else {
5014 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005015 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005016 __ jmp(slow_path->GetEntryLabel());
5017 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005018 }
5019 __ j(kEqual, slow_path->GetEntryLabel());
5020}
5021
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005022void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005023 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005024}
5025
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005026void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005027 bool object_array_get_with_read_barrier =
5028 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005029 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005030 new (GetGraph()->GetArena()) LocationSummary(instruction,
5031 object_array_get_with_read_barrier ?
5032 LocationSummary::kCallOnSlowPath :
5033 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005034 locations->SetInAt(0, Location::RequiresRegister());
5035 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005036 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5037 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5038 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005039 // The output overlaps in case of long: we don't want the low move
5040 // to overwrite the array's location. Likewise, in the case of an
5041 // object array get with read barriers enabled, we do not want the
5042 // move to overwrite the array's location, as we need it to emit
5043 // the read barrier.
5044 locations->SetOut(
5045 Location::RequiresRegister(),
5046 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5047 Location::kOutputOverlap :
5048 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005049 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005050 // We need a temporary register for the read barrier marking slow
5051 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5052 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5053 locations->AddTemp(Location::RequiresRegister());
5054 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005055}
5056
5057void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5058 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005059 Location obj_loc = locations->InAt(0);
5060 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005061 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005062 Location out_loc = locations->Out();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005063
Calin Juravle77520bc2015-01-12 18:45:46 +00005064 Primitive::Type type = instruction->GetType();
5065 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005066 case Primitive::kPrimBoolean: {
5067 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005068 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 if (index.IsConstant()) {
5070 __ movzxb(out, Address(obj,
5071 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5072 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005073 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005074 }
5075 break;
5076 }
5077
5078 case Primitive::kPrimByte: {
5079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int8_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005080 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005081 if (index.IsConstant()) {
5082 __ movsxb(out, Address(obj,
5083 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5084 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005085 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 }
5087 break;
5088 }
5089
5090 case Primitive::kPrimShort: {
5091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005092 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005093 if (index.IsConstant()) {
5094 __ movsxw(out, Address(obj,
5095 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5096 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005097 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005098 }
5099 break;
5100 }
5101
5102 case Primitive::kPrimChar: {
5103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005104 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005105 if (index.IsConstant()) {
5106 __ movzxw(out, Address(obj,
5107 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5108 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005109 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005110 }
5111 break;
5112 }
5113
Roland Levillain7c1559a2015-12-15 10:55:36 +00005114 case Primitive::kPrimInt: {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005115 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005116 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005117 if (index.IsConstant()) {
5118 __ movl(out, Address(obj,
5119 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5120 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005121 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005122 }
5123 break;
5124 }
5125
Roland Levillain7c1559a2015-12-15 10:55:36 +00005126 case Primitive::kPrimNot: {
5127 static_assert(
5128 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5129 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
5130 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5131 // /* HeapReference<Object> */ out =
5132 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5133 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5134 Location temp = locations->GetTemp(0);
5135 // Note that a potential implicit null check is handled in this
5136 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5137 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5138 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5139 } else {
5140 Register out = out_loc.AsRegister<Register>();
5141 if (index.IsConstant()) {
5142 uint32_t offset =
5143 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5144 __ movl(out, Address(obj, offset));
5145 codegen_->MaybeRecordImplicitNullCheck(instruction);
5146 // If read barriers are enabled, emit read barriers other than
5147 // Baker's using a slow path (and also unpoison the loaded
5148 // reference, if heap poisoning is enabled).
5149 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5150 } else {
5151 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5152 codegen_->MaybeRecordImplicitNullCheck(instruction);
5153 // If read barriers are enabled, emit read barriers other than
5154 // Baker's using a slow path (and also unpoison the loaded
5155 // reference, if heap poisoning is enabled).
5156 codegen_->MaybeGenerateReadBarrierSlow(
5157 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5158 }
5159 }
5160 break;
5161 }
5162
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005163 case Primitive::kPrimLong: {
5164 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005165 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005166 if (index.IsConstant()) {
5167 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005168 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005169 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005170 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005171 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005172 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005173 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005174 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005175 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005176 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005177 }
5178 break;
5179 }
5180
Mark Mendell7c8d0092015-01-26 11:21:33 -05005181 case Primitive::kPrimFloat: {
5182 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005183 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005184 if (index.IsConstant()) {
5185 __ movss(out, Address(obj,
5186 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5187 } else {
5188 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5189 }
5190 break;
5191 }
5192
5193 case Primitive::kPrimDouble: {
5194 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Roland Levillain7c1559a2015-12-15 10:55:36 +00005195 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005196 if (index.IsConstant()) {
5197 __ movsd(out, Address(obj,
5198 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5199 } else {
5200 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5201 }
5202 break;
5203 }
5204
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005205 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005206 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005207 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005208 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005209
Roland Levillain7c1559a2015-12-15 10:55:36 +00005210 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5211 // Potential implicit null checks, in the case of reference or
5212 // long arrays, are handled in the previous switch statement.
5213 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005214 codegen_->MaybeRecordImplicitNullCheck(instruction);
5215 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005216}
5217
5218void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005219 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005220
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005221 bool needs_write_barrier =
5222 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005223 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5224 bool object_array_set_with_read_barrier =
5225 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005226
Nicolas Geoffray39468442014-09-02 15:17:15 +01005227 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5228 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005229 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5230 LocationSummary::kCallOnSlowPath :
5231 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005232
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005233 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5234 || (value_type == Primitive::kPrimByte);
5235 // We need the inputs to be different than the output in case of long operation.
5236 // In case of a byte operation, the register allocator does not support multiple
5237 // inputs that die at entry with one in a specific register.
5238 locations->SetInAt(0, Location::RequiresRegister());
5239 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5240 if (is_byte_type) {
5241 // Ensure the value is in a byte register.
5242 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5243 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005244 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005246 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5247 }
5248 if (needs_write_barrier) {
5249 // Temporary registers for the write barrier.
5250 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5251 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005252 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005253 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005254}
5255
5256void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5257 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005258 Location array_loc = locations->InAt(0);
5259 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005260 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005261 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005262 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005263 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5264 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5265 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005266 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005267 bool needs_write_barrier =
5268 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005269
5270 switch (value_type) {
5271 case Primitive::kPrimBoolean:
5272 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005273 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5274 Address address = index.IsConstant()
5275 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5276 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5277 if (value.IsRegister()) {
5278 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005279 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005280 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005281 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005282 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005283 break;
5284 }
5285
5286 case Primitive::kPrimShort:
5287 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005288 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5289 Address address = index.IsConstant()
5290 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5291 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5292 if (value.IsRegister()) {
5293 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005294 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005295 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005296 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005297 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005298 break;
5299 }
5300
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005301 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005302 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5303 Address address = index.IsConstant()
5304 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5305 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005306
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005307 if (!value.IsRegister()) {
5308 // Just setting null.
5309 DCHECK(instruction->InputAt(2)->IsNullConstant());
5310 DCHECK(value.IsConstant()) << value;
5311 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005312 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005313 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005314 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005315 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005317
5318 DCHECK(needs_write_barrier);
5319 Register register_value = value.AsRegister<Register>();
5320 NearLabel done, not_null, do_put;
5321 SlowPathCode* slow_path = nullptr;
5322 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005323 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005324 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5325 codegen_->AddSlowPath(slow_path);
5326 if (instruction->GetValueCanBeNull()) {
5327 __ testl(register_value, register_value);
5328 __ j(kNotEqual, &not_null);
5329 __ movl(address, Immediate(0));
5330 codegen_->MaybeRecordImplicitNullCheck(instruction);
5331 __ jmp(&done);
5332 __ Bind(&not_null);
5333 }
5334
Roland Levillain0d5a2812015-11-13 10:07:31 +00005335 if (kEmitCompilerReadBarrier) {
5336 // When read barriers are enabled, the type checking
5337 // instrumentation requires two read barriers:
5338 //
5339 // __ movl(temp2, temp);
5340 // // /* HeapReference<Class> */ temp = temp->component_type_
5341 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005342 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005343 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5344 //
5345 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5346 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005347 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005348 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5349 //
5350 // __ cmpl(temp, temp2);
5351 //
5352 // However, the second read barrier may trash `temp`, as it
5353 // is a temporary register, and as such would not be saved
5354 // along with live registers before calling the runtime (nor
5355 // restored afterwards). So in this case, we bail out and
5356 // delegate the work to the array set slow path.
5357 //
5358 // TODO: Extend the register allocator to support a new
5359 // "(locally) live temp" location so as to avoid always
5360 // going into the slow path when read barriers are enabled.
5361 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005362 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005363 // /* HeapReference<Class> */ temp = array->klass_
5364 __ movl(temp, Address(array, class_offset));
5365 codegen_->MaybeRecordImplicitNullCheck(instruction);
5366 __ MaybeUnpoisonHeapReference(temp);
5367
5368 // /* HeapReference<Class> */ temp = temp->component_type_
5369 __ movl(temp, Address(temp, component_offset));
5370 // If heap poisoning is enabled, no need to unpoison `temp`
5371 // nor the object reference in `register_value->klass`, as
5372 // we are comparing two poisoned references.
5373 __ cmpl(temp, Address(register_value, class_offset));
5374
5375 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5376 __ j(kEqual, &do_put);
5377 // If heap poisoning is enabled, the `temp` reference has
5378 // not been unpoisoned yet; unpoison it now.
5379 __ MaybeUnpoisonHeapReference(temp);
5380
5381 // /* HeapReference<Class> */ temp = temp->super_class_
5382 __ movl(temp, Address(temp, super_offset));
5383 // If heap poisoning is enabled, no need to unpoison
5384 // `temp`, as we are comparing against null below.
5385 __ testl(temp, temp);
5386 __ j(kNotEqual, slow_path->GetEntryLabel());
5387 __ Bind(&do_put);
5388 } else {
5389 __ j(kNotEqual, slow_path->GetEntryLabel());
5390 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005391 }
5392 }
5393
5394 if (kPoisonHeapReferences) {
5395 __ movl(temp, register_value);
5396 __ PoisonHeapReference(temp);
5397 __ movl(address, temp);
5398 } else {
5399 __ movl(address, register_value);
5400 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005401 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 codegen_->MaybeRecordImplicitNullCheck(instruction);
5403 }
5404
5405 Register card = locations->GetTemp(1).AsRegister<Register>();
5406 codegen_->MarkGCCard(
5407 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5408 __ Bind(&done);
5409
5410 if (slow_path != nullptr) {
5411 __ Bind(slow_path->GetExitLabel());
5412 }
5413
5414 break;
5415 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005416
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005417 case Primitive::kPrimInt: {
5418 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5419 Address address = index.IsConstant()
5420 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5421 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5422 if (value.IsRegister()) {
5423 __ movl(address, value.AsRegister<Register>());
5424 } else {
5425 DCHECK(value.IsConstant()) << value;
5426 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5427 __ movl(address, Immediate(v));
5428 }
5429 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005430 break;
5431 }
5432
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005433 case Primitive::kPrimLong: {
5434 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 if (index.IsConstant()) {
5436 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005437 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005438 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005439 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005440 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005441 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005442 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005443 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005444 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005445 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005446 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005447 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005448 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005449 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005450 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005451 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005452 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005453 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005454 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005455 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005456 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005457 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005458 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005459 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005460 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005461 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005462 Immediate(High32Bits(val)));
5463 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005464 }
5465 break;
5466 }
5467
Mark Mendell7c8d0092015-01-26 11:21:33 -05005468 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005469 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5470 Address address = index.IsConstant()
5471 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5472 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005473 if (value.IsFpuRegister()) {
5474 __ movss(address, value.AsFpuRegister<XmmRegister>());
5475 } else {
5476 DCHECK(value.IsConstant());
5477 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5478 __ movl(address, Immediate(v));
5479 }
5480 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005481 break;
5482 }
5483
5484 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005485 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5486 Address address = index.IsConstant()
5487 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5488 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005489 if (value.IsFpuRegister()) {
5490 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5491 } else {
5492 DCHECK(value.IsConstant());
5493 Address address_hi = index.IsConstant() ?
5494 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5495 offset + kX86WordSize) :
5496 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5497 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5498 __ movl(address, Immediate(Low32Bits(v)));
5499 codegen_->MaybeRecordImplicitNullCheck(instruction);
5500 __ movl(address_hi, Immediate(High32Bits(v)));
5501 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005502 break;
5503 }
5504
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005505 case Primitive::kPrimVoid:
5506 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005507 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005508 }
5509}
5510
5511void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5512 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005513 locations->SetInAt(0, Location::RequiresRegister());
5514 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005515}
5516
5517void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5518 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005519 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005520 Register obj = locations->InAt(0).AsRegister<Register>();
5521 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005522 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005523 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005524}
5525
5526void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005527 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5528 ? LocationSummary::kCallOnSlowPath
5529 : LocationSummary::kNoCall;
5530 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005531 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005532 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005533 if (instruction->HasUses()) {
5534 locations->SetOut(Location::SameAsFirstInput());
5535 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005536}
5537
5538void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5539 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005540 Location index_loc = locations->InAt(0);
5541 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005542 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005543 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005544
Mark Mendell99dbd682015-04-22 16:18:52 -04005545 if (length_loc.IsConstant()) {
5546 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5547 if (index_loc.IsConstant()) {
5548 // BCE will remove the bounds check if we are guarenteed to pass.
5549 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5550 if (index < 0 || index >= length) {
5551 codegen_->AddSlowPath(slow_path);
5552 __ jmp(slow_path->GetEntryLabel());
5553 } else {
5554 // Some optimization after BCE may have generated this, and we should not
5555 // generate a bounds check if it is a valid range.
5556 }
5557 return;
5558 }
5559
5560 // We have to reverse the jump condition because the length is the constant.
5561 Register index_reg = index_loc.AsRegister<Register>();
5562 __ cmpl(index_reg, Immediate(length));
5563 codegen_->AddSlowPath(slow_path);
5564 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005565 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005566 Register length = length_loc.AsRegister<Register>();
5567 if (index_loc.IsConstant()) {
5568 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5569 __ cmpl(length, Immediate(value));
5570 } else {
5571 __ cmpl(length, index_loc.AsRegister<Register>());
5572 }
5573 codegen_->AddSlowPath(slow_path);
5574 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005575 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005576}
5577
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005578void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005579 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005580}
5581
5582void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005583 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5584}
5585
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005586void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5587 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5588}
5589
5590void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005591 HBasicBlock* block = instruction->GetBlock();
5592 if (block->GetLoopInformation() != nullptr) {
5593 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5594 // The back edge will generate the suspend check.
5595 return;
5596 }
5597 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5598 // The goto will generate the suspend check.
5599 return;
5600 }
5601 GenerateSuspendCheck(instruction, nullptr);
5602}
5603
5604void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5605 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005606 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005607 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5608 if (slow_path == nullptr) {
5609 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5610 instruction->SetSlowPath(slow_path);
5611 codegen_->AddSlowPath(slow_path);
5612 if (successor != nullptr) {
5613 DCHECK(successor->IsLoopHeader());
5614 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5615 }
5616 } else {
5617 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5618 }
5619
Roland Levillain7c1559a2015-12-15 10:55:36 +00005620 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5621 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005622 if (successor == nullptr) {
5623 __ j(kNotEqual, slow_path->GetEntryLabel());
5624 __ Bind(slow_path->GetReturnLabel());
5625 } else {
5626 __ j(kEqual, codegen_->GetLabelOf(successor));
5627 __ jmp(slow_path->GetEntryLabel());
5628 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005629}
5630
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005631X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5632 return codegen_->GetAssembler();
5633}
5634
Mark Mendell7c8d0092015-01-26 11:21:33 -05005635void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005636 ScratchRegisterScope ensure_scratch(
5637 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5638 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5639 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5640 __ movl(temp_reg, Address(ESP, src + stack_offset));
5641 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005642}
5643
5644void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005645 ScratchRegisterScope ensure_scratch(
5646 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5647 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5648 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5649 __ movl(temp_reg, Address(ESP, src + stack_offset));
5650 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5651 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5652 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005653}
5654
5655void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005656 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005657 Location source = move->GetSource();
5658 Location destination = move->GetDestination();
5659
5660 if (source.IsRegister()) {
5661 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005662 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005663 } else if (destination.IsFpuRegister()) {
5664 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005665 } else {
5666 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005667 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005668 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005669 } else if (source.IsRegisterPair()) {
5670 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5671 // Create stack space for 2 elements.
5672 __ subl(ESP, Immediate(2 * elem_size));
5673 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5674 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5675 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5676 // And remove the temporary stack space we allocated.
5677 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005678 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005679 if (destination.IsRegister()) {
5680 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5681 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005682 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005683 } else if (destination.IsRegisterPair()) {
5684 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5685 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5686 __ psrlq(src_reg, Immediate(32));
5687 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005688 } else if (destination.IsStackSlot()) {
5689 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5690 } else {
5691 DCHECK(destination.IsDoubleStackSlot());
5692 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5693 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005694 } else if (source.IsStackSlot()) {
5695 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005696 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005697 } else if (destination.IsFpuRegister()) {
5698 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005699 } else {
5700 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005701 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5702 }
5703 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005704 if (destination.IsRegisterPair()) {
5705 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5706 __ movl(destination.AsRegisterPairHigh<Register>(),
5707 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5708 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005709 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5710 } else {
5711 DCHECK(destination.IsDoubleStackSlot()) << destination;
5712 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005713 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005714 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005715 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005716 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005717 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005718 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005719 if (value == 0) {
5720 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5721 } else {
5722 __ movl(destination.AsRegister<Register>(), Immediate(value));
5723 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005724 } else {
5725 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005726 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005727 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005728 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005729 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005730 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005731 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005732 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005733 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5734 if (value == 0) {
5735 // Easy handling of 0.0.
5736 __ xorps(dest, dest);
5737 } else {
5738 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005739 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5740 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5741 __ movl(temp, Immediate(value));
5742 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005743 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005744 } else {
5745 DCHECK(destination.IsStackSlot()) << destination;
5746 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5747 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005748 } else if (constant->IsLongConstant()) {
5749 int64_t value = constant->AsLongConstant()->GetValue();
5750 int32_t low_value = Low32Bits(value);
5751 int32_t high_value = High32Bits(value);
5752 Immediate low(low_value);
5753 Immediate high(high_value);
5754 if (destination.IsDoubleStackSlot()) {
5755 __ movl(Address(ESP, destination.GetStackIndex()), low);
5756 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5757 } else {
5758 __ movl(destination.AsRegisterPairLow<Register>(), low);
5759 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5760 }
5761 } else {
5762 DCHECK(constant->IsDoubleConstant());
5763 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005764 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005765 int32_t low_value = Low32Bits(value);
5766 int32_t high_value = High32Bits(value);
5767 Immediate low(low_value);
5768 Immediate high(high_value);
5769 if (destination.IsFpuRegister()) {
5770 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5771 if (value == 0) {
5772 // Easy handling of 0.0.
5773 __ xorpd(dest, dest);
5774 } else {
5775 __ pushl(high);
5776 __ pushl(low);
5777 __ movsd(dest, Address(ESP, 0));
5778 __ addl(ESP, Immediate(8));
5779 }
5780 } else {
5781 DCHECK(destination.IsDoubleStackSlot()) << destination;
5782 __ movl(Address(ESP, destination.GetStackIndex()), low);
5783 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5784 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005785 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005786 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005787 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005788 }
5789}
5790
Mark Mendella5c19ce2015-04-01 12:51:05 -04005791void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005792 Register suggested_scratch = reg == EAX ? EBX : EAX;
5793 ScratchRegisterScope ensure_scratch(
5794 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5795
5796 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5797 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5798 __ movl(Address(ESP, mem + stack_offset), reg);
5799 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005800}
5801
Mark Mendell7c8d0092015-01-26 11:21:33 -05005802void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005803 ScratchRegisterScope ensure_scratch(
5804 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5805
5806 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5807 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5808 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5809 __ movss(Address(ESP, mem + stack_offset), reg);
5810 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005811}
5812
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005813void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005814 ScratchRegisterScope ensure_scratch1(
5815 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005816
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005817 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5818 ScratchRegisterScope ensure_scratch2(
5819 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005820
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005821 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5822 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5823 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5824 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5825 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5826 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005827}
5828
5829void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005830 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005831 Location source = move->GetSource();
5832 Location destination = move->GetDestination();
5833
5834 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005835 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5836 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5837 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5838 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5839 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005840 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005841 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005842 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005843 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005844 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5845 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005846 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5847 // Use XOR Swap algorithm to avoid a temporary.
5848 DCHECK_NE(source.reg(), destination.reg());
5849 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5850 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5851 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5852 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5853 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5854 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5855 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005856 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5857 // Take advantage of the 16 bytes in the XMM register.
5858 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5859 Address stack(ESP, destination.GetStackIndex());
5860 // Load the double into the high doubleword.
5861 __ movhpd(reg, stack);
5862
5863 // Store the low double into the destination.
5864 __ movsd(stack, reg);
5865
5866 // Move the high double to the low double.
5867 __ psrldq(reg, Immediate(8));
5868 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5869 // Take advantage of the 16 bytes in the XMM register.
5870 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5871 Address stack(ESP, source.GetStackIndex());
5872 // Load the double into the high doubleword.
5873 __ movhpd(reg, stack);
5874
5875 // Store the low double into the destination.
5876 __ movsd(stack, reg);
5877
5878 // Move the high double to the low double.
5879 __ psrldq(reg, Immediate(8));
5880 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5881 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5882 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005883 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005884 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005885 }
5886}
5887
5888void ParallelMoveResolverX86::SpillScratch(int reg) {
5889 __ pushl(static_cast<Register>(reg));
5890}
5891
5892void ParallelMoveResolverX86::RestoreScratch(int reg) {
5893 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005894}
5895
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005896HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
5897 HLoadClass::LoadKind desired_class_load_kind) {
5898 if (kEmitCompilerReadBarrier) {
5899 switch (desired_class_load_kind) {
5900 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5901 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5902 case HLoadClass::LoadKind::kBootImageAddress:
5903 // TODO: Implement for read barrier.
5904 return HLoadClass::LoadKind::kDexCacheViaMethod;
5905 default:
5906 break;
5907 }
5908 }
5909 switch (desired_class_load_kind) {
5910 case HLoadClass::LoadKind::kReferrersClass:
5911 break;
5912 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
5913 DCHECK(!GetCompilerOptions().GetCompilePic());
5914 break;
5915 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
5916 DCHECK(GetCompilerOptions().GetCompilePic());
5917 FALLTHROUGH_INTENDED;
5918 case HLoadClass::LoadKind::kDexCachePcRelative:
5919 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
5920 // We disable pc-relative load when there is an irreducible loop, as the optimization
5921 // is incompatible with it.
5922 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5923 // with irreducible loops.
5924 if (GetGraph()->HasIrreducibleLoops()) {
5925 return HLoadClass::LoadKind::kDexCacheViaMethod;
5926 }
5927 break;
5928 case HLoadClass::LoadKind::kBootImageAddress:
5929 break;
5930 case HLoadClass::LoadKind::kDexCacheAddress:
5931 DCHECK(Runtime::Current()->UseJitCompilation());
5932 break;
5933 case HLoadClass::LoadKind::kDexCacheViaMethod:
5934 break;
5935 }
5936 return desired_class_load_kind;
5937}
5938
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005939void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005940 if (cls->NeedsAccessCheck()) {
5941 InvokeRuntimeCallingConvention calling_convention;
5942 CodeGenerator::CreateLoadClassLocationSummary(
5943 cls,
5944 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
5945 Location::RegisterLocation(EAX),
5946 /* code_generator_supports_read_barrier */ true);
5947 return;
5948 }
5949
5950 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || kEmitCompilerReadBarrier)
5951 ? LocationSummary::kCallOnSlowPath
5952 : LocationSummary::kNoCall;
5953 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
5954 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
5955 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
5956 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
5957 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
5958 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
5959 locations->SetInAt(0, Location::RequiresRegister());
5960 }
5961 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005962}
5963
5964void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005965 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005966 if (cls->NeedsAccessCheck()) {
5967 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5968 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5969 cls,
5970 cls->GetDexPc(),
5971 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005972 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005973 return;
5974 }
5975
Roland Levillain0d5a2812015-11-13 10:07:31 +00005976 Location out_loc = locations->Out();
5977 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005978
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01005979 bool generate_null_check = false;
5980 switch (cls->GetLoadKind()) {
5981 case HLoadClass::LoadKind::kReferrersClass: {
5982 DCHECK(!cls->CanCallRuntime());
5983 DCHECK(!cls->MustGenerateClinitCheck());
5984 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5985 Register current_method = locations->InAt(0).AsRegister<Register>();
5986 GenerateGcRootFieldLoad(
5987 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
5988 break;
5989 }
5990 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
5991 DCHECK(!kEmitCompilerReadBarrier);
5992 __ movl(out, Immediate(/* placeholder */ 0));
5993 codegen_->RecordTypePatch(cls);
5994 break;
5995 }
5996 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
5997 DCHECK(!kEmitCompilerReadBarrier);
5998 Register method_address = locations->InAt(0).AsRegister<Register>();
5999 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6000 codegen_->RecordTypePatch(cls);
6001 break;
6002 }
6003 case HLoadClass::LoadKind::kBootImageAddress: {
6004 DCHECK(!kEmitCompilerReadBarrier);
6005 DCHECK_NE(cls->GetAddress(), 0u);
6006 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6007 __ movl(out, Immediate(address));
6008 codegen_->RecordSimplePatch();
6009 break;
6010 }
6011 case HLoadClass::LoadKind::kDexCacheAddress: {
6012 DCHECK_NE(cls->GetAddress(), 0u);
6013 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6014 // /* GcRoot<mirror::Class> */ out = *address
6015 GenerateGcRootFieldLoad(cls, out_loc, Address::Absolute(address));
6016 generate_null_check = !cls->IsInDexCache();
6017 break;
6018 }
6019 case HLoadClass::LoadKind::kDexCachePcRelative: {
6020 Register base_reg = locations->InAt(0).AsRegister<Register>();
6021 uint32_t offset = cls->GetDexCacheElementOffset();
6022 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6023 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
6024 GenerateGcRootFieldLoad(
6025 cls, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6026 generate_null_check = !cls->IsInDexCache();
6027 break;
6028 }
6029 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6030 // /* GcRoot<mirror::Class>[] */ out =
6031 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6032 Register current_method = locations->InAt(0).AsRegister<Register>();
6033 __ movl(out, Address(current_method,
6034 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6035 // /* GcRoot<mirror::Class> */ out = out[type_index]
6036 GenerateGcRootFieldLoad(
6037 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
6038 generate_null_check = !cls->IsInDexCache();
6039 break;
6040 }
6041 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006042
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006043 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6044 DCHECK(cls->CanCallRuntime());
6045 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6046 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6047 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006048
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006049 if (generate_null_check) {
6050 __ testl(out, out);
6051 __ j(kEqual, slow_path->GetEntryLabel());
6052 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006053
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006054 if (cls->MustGenerateClinitCheck()) {
6055 GenerateClassInitializationCheck(slow_path, out);
6056 } else {
6057 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006058 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006059 }
6060}
6061
6062void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6063 LocationSummary* locations =
6064 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6065 locations->SetInAt(0, Location::RequiresRegister());
6066 if (check->HasUses()) {
6067 locations->SetOut(Location::SameAsFirstInput());
6068 }
6069}
6070
6071void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006072 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006073 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006074 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006075 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006076 GenerateClassInitializationCheck(slow_path,
6077 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006078}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006079
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006080void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006081 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006082 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6083 Immediate(mirror::Class::kStatusInitialized));
6084 __ j(kLess, slow_path->GetEntryLabel());
6085 __ Bind(slow_path->GetExitLabel());
6086 // No need for memory fence, thanks to the X86 memory model.
6087}
6088
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006089HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6090 HLoadString::LoadKind desired_string_load_kind) {
6091 if (kEmitCompilerReadBarrier) {
6092 switch (desired_string_load_kind) {
6093 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6094 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6095 case HLoadString::LoadKind::kBootImageAddress:
6096 // TODO: Implement for read barrier.
6097 return HLoadString::LoadKind::kDexCacheViaMethod;
6098 default:
6099 break;
6100 }
6101 }
6102 switch (desired_string_load_kind) {
6103 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6104 DCHECK(!GetCompilerOptions().GetCompilePic());
6105 break;
6106 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6107 DCHECK(GetCompilerOptions().GetCompilePic());
6108 FALLTHROUGH_INTENDED;
6109 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01006110 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006111 // We disable pc-relative load when there is an irreducible loop, as the optimization
6112 // is incompatible with it.
6113 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6114 // with irreducible loops.
6115 if (GetGraph()->HasIrreducibleLoops()) {
6116 return HLoadString::LoadKind::kDexCacheViaMethod;
6117 }
6118 break;
6119 case HLoadString::LoadKind::kBootImageAddress:
6120 break;
6121 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01006122 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006123 break;
6124 case HLoadString::LoadKind::kDexCacheViaMethod:
6125 break;
6126 }
6127 return desired_string_load_kind;
6128}
6129
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006130void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006131 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006132 ? LocationSummary::kCallOnSlowPath
6133 : LocationSummary::kNoCall;
6134 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006135 HLoadString::LoadKind load_kind = load->GetLoadKind();
6136 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6137 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6138 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6139 locations->SetInAt(0, Location::RequiresRegister());
6140 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006141 locations->SetOut(Location::RequiresRegister());
6142}
6143
6144void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006145 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006146 Location out_loc = locations->Out();
6147 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006148
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006149 switch (load->GetLoadKind()) {
6150 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6151 DCHECK(!kEmitCompilerReadBarrier);
6152 __ movl(out, Immediate(/* placeholder */ 0));
6153 codegen_->RecordStringPatch(load);
6154 return; // No dex cache slow path.
6155 }
6156 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6157 DCHECK(!kEmitCompilerReadBarrier);
6158 Register method_address = locations->InAt(0).AsRegister<Register>();
6159 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6160 codegen_->RecordStringPatch(load);
6161 return; // No dex cache slow path.
6162 }
6163 case HLoadString::LoadKind::kBootImageAddress: {
6164 DCHECK(!kEmitCompilerReadBarrier);
6165 DCHECK_NE(load->GetAddress(), 0u);
6166 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6167 __ movl(out, Immediate(address));
6168 codegen_->RecordSimplePatch();
6169 return; // No dex cache slow path.
6170 }
6171 case HLoadString::LoadKind::kDexCacheAddress: {
6172 DCHECK_NE(load->GetAddress(), 0u);
6173 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006174 // /* GcRoot<mirror::String> */ out = *address
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006175 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6176 break;
6177 }
6178 case HLoadString::LoadKind::kDexCachePcRelative: {
6179 Register base_reg = locations->InAt(0).AsRegister<Register>();
6180 uint32_t offset = load->GetDexCacheElementOffset();
6181 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006182 // /* GcRoot<mirror::String> */ out = *(base + offset) /* PC-relative */
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006183 GenerateGcRootFieldLoad(
6184 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6185 break;
6186 }
6187 case HLoadString::LoadKind::kDexCacheViaMethod: {
6188 Register current_method = locations->InAt(0).AsRegister<Register>();
6189
6190 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6191 GenerateGcRootFieldLoad(
6192 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6193
6194 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6195 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6196 // /* GcRoot<mirror::String> */ out = out[string_index]
6197 GenerateGcRootFieldLoad(
6198 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6199 break;
6200 }
6201 default:
6202 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6203 UNREACHABLE();
6204 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006205
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006206 if (!load->IsInDexCache()) {
6207 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6208 codegen_->AddSlowPath(slow_path);
6209 __ testl(out, out);
6210 __ j(kEqual, slow_path->GetEntryLabel());
6211 __ Bind(slow_path->GetExitLabel());
6212 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006213}
6214
David Brazdilcb1c0552015-08-04 16:22:25 +01006215static Address GetExceptionTlsAddress() {
6216 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6217}
6218
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006219void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6220 LocationSummary* locations =
6221 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6222 locations->SetOut(Location::RequiresRegister());
6223}
6224
6225void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006226 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6227}
6228
6229void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6230 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6231}
6232
6233void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6234 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006235}
6236
6237void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6238 LocationSummary* locations =
6239 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6240 InvokeRuntimeCallingConvention calling_convention;
6241 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6242}
6243
6244void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006245 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6246 instruction,
6247 instruction->GetDexPc(),
6248 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006249 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006250}
6251
Roland Levillain7c1559a2015-12-15 10:55:36 +00006252static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6253 return kEmitCompilerReadBarrier &&
6254 (kUseBakerReadBarrier ||
6255 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6256 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6257 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6258}
6259
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006260void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006261 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6263 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 case TypeCheckKind::kExactCheck:
6265 case TypeCheckKind::kAbstractClassCheck:
6266 case TypeCheckKind::kClassHierarchyCheck:
6267 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006268 call_kind =
6269 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 break;
6271 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006272 case TypeCheckKind::kUnresolvedCheck:
6273 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006274 call_kind = LocationSummary::kCallOnSlowPath;
6275 break;
6276 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006277
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006278 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006279 locations->SetInAt(0, Location::RequiresRegister());
6280 locations->SetInAt(1, Location::Any());
6281 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6282 locations->SetOut(Location::RequiresRegister());
6283 // When read barriers are enabled, we need a temporary register for
6284 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006285 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006286 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006287 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006288}
6289
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006290void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006291 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006292 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006293 Location obj_loc = locations->InAt(0);
6294 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006295 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006296 Location out_loc = locations->Out();
6297 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006298 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006299 locations->GetTemp(0) :
6300 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006301 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006302 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6303 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6304 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006305 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006306 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006307
6308 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006309 // Avoid null check if we know obj is not null.
6310 if (instruction->MustDoNullCheck()) {
6311 __ testl(obj, obj);
6312 __ j(kEqual, &zero);
6313 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006314
Roland Levillain0d5a2812015-11-13 10:07:31 +00006315 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006316 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006317
Roland Levillain7c1559a2015-12-15 10:55:36 +00006318 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 case TypeCheckKind::kExactCheck: {
6320 if (cls.IsRegister()) {
6321 __ cmpl(out, cls.AsRegister<Register>());
6322 } else {
6323 DCHECK(cls.IsStackSlot()) << cls;
6324 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6325 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006326
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006327 // Classes must be equal for the instanceof to succeed.
6328 __ j(kNotEqual, &zero);
6329 __ movl(out, Immediate(1));
6330 __ jmp(&done);
6331 break;
6332 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006333
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006334 case TypeCheckKind::kAbstractClassCheck: {
6335 // If the class is abstract, we eagerly fetch the super class of the
6336 // object to avoid doing a comparison we know will fail.
6337 NearLabel loop;
6338 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006339 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006340 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 __ testl(out, out);
6342 // If `out` is null, we use it for the result, and jump to `done`.
6343 __ j(kEqual, &done);
6344 if (cls.IsRegister()) {
6345 __ cmpl(out, cls.AsRegister<Register>());
6346 } else {
6347 DCHECK(cls.IsStackSlot()) << cls;
6348 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6349 }
6350 __ j(kNotEqual, &loop);
6351 __ movl(out, Immediate(1));
6352 if (zero.IsLinked()) {
6353 __ jmp(&done);
6354 }
6355 break;
6356 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006357
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006358 case TypeCheckKind::kClassHierarchyCheck: {
6359 // Walk over the class hierarchy to find a match.
6360 NearLabel loop, success;
6361 __ Bind(&loop);
6362 if (cls.IsRegister()) {
6363 __ cmpl(out, cls.AsRegister<Register>());
6364 } else {
6365 DCHECK(cls.IsStackSlot()) << cls;
6366 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6367 }
6368 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006369 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006370 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006371 __ testl(out, out);
6372 __ j(kNotEqual, &loop);
6373 // If `out` is null, we use it for the result, and jump to `done`.
6374 __ jmp(&done);
6375 __ Bind(&success);
6376 __ movl(out, Immediate(1));
6377 if (zero.IsLinked()) {
6378 __ jmp(&done);
6379 }
6380 break;
6381 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006382
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006383 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006384 // Do an exact check.
6385 NearLabel exact_check;
6386 if (cls.IsRegister()) {
6387 __ cmpl(out, cls.AsRegister<Register>());
6388 } else {
6389 DCHECK(cls.IsStackSlot()) << cls;
6390 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6391 }
6392 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006393 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006394 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006395 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006396 __ testl(out, out);
6397 // If `out` is null, we use it for the result, and jump to `done`.
6398 __ j(kEqual, &done);
6399 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6400 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006401 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 __ movl(out, Immediate(1));
6403 __ jmp(&done);
6404 break;
6405 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006406
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006407 case TypeCheckKind::kArrayCheck: {
6408 if (cls.IsRegister()) {
6409 __ cmpl(out, cls.AsRegister<Register>());
6410 } else {
6411 DCHECK(cls.IsStackSlot()) << cls;
6412 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6413 }
6414 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6416 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 codegen_->AddSlowPath(slow_path);
6418 __ j(kNotEqual, slow_path->GetEntryLabel());
6419 __ movl(out, Immediate(1));
6420 if (zero.IsLinked()) {
6421 __ jmp(&done);
6422 }
6423 break;
6424 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425
Calin Juravle98893e12015-10-02 21:05:03 +01006426 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006427 case TypeCheckKind::kInterfaceCheck: {
6428 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006429 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430 // cases.
6431 //
6432 // We cannot directly call the InstanceofNonTrivial runtime
6433 // entry point without resorting to a type checking slow path
6434 // here (i.e. by calling InvokeRuntime directly), as it would
6435 // require to assign fixed registers for the inputs of this
6436 // HInstanceOf instruction (following the runtime calling
6437 // convention), which might be cluttered by the potential first
6438 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006439 //
6440 // TODO: Introduce a new runtime entry point taking the object
6441 // to test (instead of its class) as argument, and let it deal
6442 // with the read barrier issues. This will let us refactor this
6443 // case of the `switch` code as it was previously (with a direct
6444 // call to the runtime not using a type checking slow path).
6445 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006446 DCHECK(locations->OnlyCallsOnSlowPath());
6447 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6448 /* is_fatal */ false);
6449 codegen_->AddSlowPath(slow_path);
6450 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006451 if (zero.IsLinked()) {
6452 __ jmp(&done);
6453 }
6454 break;
6455 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006456 }
6457
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006458 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006459 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 __ xorl(out, out);
6461 }
6462
6463 if (done.IsLinked()) {
6464 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006465 }
6466
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006467 if (slow_path != nullptr) {
6468 __ Bind(slow_path->GetExitLabel());
6469 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006470}
6471
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006472void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6474 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006475 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6476 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006477 case TypeCheckKind::kExactCheck:
6478 case TypeCheckKind::kAbstractClassCheck:
6479 case TypeCheckKind::kClassHierarchyCheck:
6480 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006481 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6482 LocationSummary::kCallOnSlowPath :
6483 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006484 break;
6485 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006486 case TypeCheckKind::kUnresolvedCheck:
6487 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 call_kind = LocationSummary::kCallOnSlowPath;
6489 break;
6490 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6492 locations->SetInAt(0, Location::RequiresRegister());
6493 locations->SetInAt(1, Location::Any());
6494 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6495 locations->AddTemp(Location::RequiresRegister());
6496 // When read barriers are enabled, we need an additional temporary
6497 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006498 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006499 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006500 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006501}
6502
6503void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006504 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006505 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006506 Location obj_loc = locations->InAt(0);
6507 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006508 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006509 Location temp_loc = locations->GetTemp(0);
6510 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006511 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006512 locations->GetTemp(1) :
6513 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6515 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6516 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6517 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006518
Roland Levillain0d5a2812015-11-13 10:07:31 +00006519 bool is_type_check_slow_path_fatal =
6520 (type_check_kind == TypeCheckKind::kExactCheck ||
6521 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6522 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6523 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6524 !instruction->CanThrowIntoCatchBlock();
6525 SlowPathCode* type_check_slow_path =
6526 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6527 is_type_check_slow_path_fatal);
6528 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006529
Roland Levillain0d5a2812015-11-13 10:07:31 +00006530 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006531 // Avoid null check if we know obj is not null.
6532 if (instruction->MustDoNullCheck()) {
6533 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006534 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006535 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006536
Roland Levillain0d5a2812015-11-13 10:07:31 +00006537 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006538 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006539
Roland Levillain0d5a2812015-11-13 10:07:31 +00006540 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006541 case TypeCheckKind::kExactCheck:
6542 case TypeCheckKind::kArrayCheck: {
6543 if (cls.IsRegister()) {
6544 __ cmpl(temp, cls.AsRegister<Register>());
6545 } else {
6546 DCHECK(cls.IsStackSlot()) << cls;
6547 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6548 }
6549 // Jump to slow path for throwing the exception or doing a
6550 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006551 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006552 break;
6553 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006555 case TypeCheckKind::kAbstractClassCheck: {
6556 // If the class is abstract, we eagerly fetch the super class of the
6557 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006559 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006560 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006561 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006562
6563 // If the class reference currently in `temp` is not null, jump
6564 // to the `compare_classes` label to compare it with the checked
6565 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006566 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006567 __ j(kNotEqual, &compare_classes);
6568 // Otherwise, jump to the slow path to throw the exception.
6569 //
6570 // But before, move back the object's class into `temp` before
6571 // going into the slow path, as it has been overwritten in the
6572 // meantime.
6573 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006574 GenerateReferenceLoadTwoRegisters(
6575 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006576 __ jmp(type_check_slow_path->GetEntryLabel());
6577
6578 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006579 if (cls.IsRegister()) {
6580 __ cmpl(temp, cls.AsRegister<Register>());
6581 } else {
6582 DCHECK(cls.IsStackSlot()) << cls;
6583 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6584 }
6585 __ j(kNotEqual, &loop);
6586 break;
6587 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006588
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006589 case TypeCheckKind::kClassHierarchyCheck: {
6590 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006591 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006592 __ Bind(&loop);
6593 if (cls.IsRegister()) {
6594 __ cmpl(temp, cls.AsRegister<Register>());
6595 } else {
6596 DCHECK(cls.IsStackSlot()) << cls;
6597 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6598 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006599 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006600
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006602 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603
6604 // If the class reference currently in `temp` is not null, jump
6605 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006606 __ testl(temp, temp);
6607 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608 // Otherwise, jump to the slow path to throw the exception.
6609 //
6610 // But before, move back the object's class into `temp` before
6611 // going into the slow path, as it has been overwritten in the
6612 // meantime.
6613 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006614 GenerateReferenceLoadTwoRegisters(
6615 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006616 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006617 break;
6618 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006619
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006620 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006621 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006623 if (cls.IsRegister()) {
6624 __ cmpl(temp, cls.AsRegister<Register>());
6625 } else {
6626 DCHECK(cls.IsStackSlot()) << cls;
6627 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6628 }
6629 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006630
6631 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006632 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006633 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006634
6635 // If the component type is not null (i.e. the object is indeed
6636 // an array), jump to label `check_non_primitive_component_type`
6637 // to further check that this component type is not a primitive
6638 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006639 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006640 __ j(kNotEqual, &check_non_primitive_component_type);
6641 // Otherwise, jump to the slow path to throw the exception.
6642 //
6643 // But before, move back the object's class into `temp` before
6644 // going into the slow path, as it has been overwritten in the
6645 // meantime.
6646 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006647 GenerateReferenceLoadTwoRegisters(
6648 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006649 __ jmp(type_check_slow_path->GetEntryLabel());
6650
6651 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006652 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006653 __ j(kEqual, &done);
6654 // Same comment as above regarding `temp` and the slow path.
6655 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006656 GenerateReferenceLoadTwoRegisters(
6657 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006658 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006659 break;
6660 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006661
Calin Juravle98893e12015-10-02 21:05:03 +01006662 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006663 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006664 // We always go into the type check slow path for the unresolved
6665 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006666 //
6667 // We cannot directly call the CheckCast runtime entry point
6668 // without resorting to a type checking slow path here (i.e. by
6669 // calling InvokeRuntime directly), as it would require to
6670 // assign fixed registers for the inputs of this HInstanceOf
6671 // instruction (following the runtime calling convention), which
6672 // might be cluttered by the potential first read barrier
6673 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006674 //
6675 // TODO: Introduce a new runtime entry point taking the object
6676 // to test (instead of its class) as argument, and let it deal
6677 // with the read barrier issues. This will let us refactor this
6678 // case of the `switch` code as it was previously (with a direct
6679 // call to the runtime not using a type checking slow path).
6680 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006682 break;
6683 }
6684 __ Bind(&done);
6685
Roland Levillain0d5a2812015-11-13 10:07:31 +00006686 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006687}
6688
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006689void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6690 LocationSummary* locations =
6691 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6692 InvokeRuntimeCallingConvention calling_convention;
6693 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6694}
6695
6696void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006697 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6698 : QUICK_ENTRY_POINT(pUnlockObject),
6699 instruction,
6700 instruction->GetDexPc(),
6701 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006702 if (instruction->IsEnter()) {
6703 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6704 } else {
6705 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6706 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006707}
6708
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006709void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6710void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6711void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6712
6713void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6714 LocationSummary* locations =
6715 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6716 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6717 || instruction->GetResultType() == Primitive::kPrimLong);
6718 locations->SetInAt(0, Location::RequiresRegister());
6719 locations->SetInAt(1, Location::Any());
6720 locations->SetOut(Location::SameAsFirstInput());
6721}
6722
6723void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6724 HandleBitwiseOperation(instruction);
6725}
6726
6727void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6728 HandleBitwiseOperation(instruction);
6729}
6730
6731void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6732 HandleBitwiseOperation(instruction);
6733}
6734
6735void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6736 LocationSummary* locations = instruction->GetLocations();
6737 Location first = locations->InAt(0);
6738 Location second = locations->InAt(1);
6739 DCHECK(first.Equals(locations->Out()));
6740
6741 if (instruction->GetResultType() == Primitive::kPrimInt) {
6742 if (second.IsRegister()) {
6743 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006744 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006745 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006746 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006747 } else {
6748 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006749 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006750 }
6751 } else if (second.IsConstant()) {
6752 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006753 __ andl(first.AsRegister<Register>(),
6754 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006755 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006756 __ orl(first.AsRegister<Register>(),
6757 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006758 } else {
6759 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006760 __ xorl(first.AsRegister<Register>(),
6761 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006762 }
6763 } else {
6764 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006765 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006766 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006767 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006768 } else {
6769 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006770 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006771 }
6772 }
6773 } else {
6774 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6775 if (second.IsRegisterPair()) {
6776 if (instruction->IsAnd()) {
6777 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6778 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6779 } else if (instruction->IsOr()) {
6780 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6781 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6782 } else {
6783 DCHECK(instruction->IsXor());
6784 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6785 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6786 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006787 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006788 if (instruction->IsAnd()) {
6789 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6790 __ andl(first.AsRegisterPairHigh<Register>(),
6791 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6792 } else if (instruction->IsOr()) {
6793 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6794 __ orl(first.AsRegisterPairHigh<Register>(),
6795 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6796 } else {
6797 DCHECK(instruction->IsXor());
6798 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6799 __ xorl(first.AsRegisterPairHigh<Register>(),
6800 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6801 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006802 } else {
6803 DCHECK(second.IsConstant()) << second;
6804 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006805 int32_t low_value = Low32Bits(value);
6806 int32_t high_value = High32Bits(value);
6807 Immediate low(low_value);
6808 Immediate high(high_value);
6809 Register first_low = first.AsRegisterPairLow<Register>();
6810 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006811 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006812 if (low_value == 0) {
6813 __ xorl(first_low, first_low);
6814 } else if (low_value != -1) {
6815 __ andl(first_low, low);
6816 }
6817 if (high_value == 0) {
6818 __ xorl(first_high, first_high);
6819 } else if (high_value != -1) {
6820 __ andl(first_high, high);
6821 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006822 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006823 if (low_value != 0) {
6824 __ orl(first_low, low);
6825 }
6826 if (high_value != 0) {
6827 __ orl(first_high, high);
6828 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006829 } else {
6830 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006831 if (low_value != 0) {
6832 __ xorl(first_low, low);
6833 }
6834 if (high_value != 0) {
6835 __ xorl(first_high, high);
6836 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006837 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006838 }
6839 }
6840}
6841
Roland Levillain7c1559a2015-12-15 10:55:36 +00006842void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6843 Location out,
6844 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006845 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006846 Register out_reg = out.AsRegister<Register>();
6847 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006848 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006849 if (kUseBakerReadBarrier) {
6850 // Load with fast path based Baker's read barrier.
6851 // /* HeapReference<Object> */ out = *(out + offset)
6852 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006853 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006854 } else {
6855 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006856 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006857 // in the following move operation, as we will need it for the
6858 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006859 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006860 // /* HeapReference<Object> */ out = *(out + offset)
6861 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006862 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006863 }
6864 } else {
6865 // Plain load with no read barrier.
6866 // /* HeapReference<Object> */ out = *(out + offset)
6867 __ movl(out_reg, Address(out_reg, offset));
6868 __ MaybeUnpoisonHeapReference(out_reg);
6869 }
6870}
6871
6872void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6873 Location out,
6874 Location obj,
6875 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006876 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006877 Register out_reg = out.AsRegister<Register>();
6878 Register obj_reg = obj.AsRegister<Register>();
6879 if (kEmitCompilerReadBarrier) {
6880 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006881 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006882 // Load with fast path based Baker's read barrier.
6883 // /* HeapReference<Object> */ out = *(obj + offset)
6884 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006885 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006886 } else {
6887 // Load with slow path based read barrier.
6888 // /* HeapReference<Object> */ out = *(obj + offset)
6889 __ movl(out_reg, Address(obj_reg, offset));
6890 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6891 }
6892 } else {
6893 // Plain load with no read barrier.
6894 // /* HeapReference<Object> */ out = *(obj + offset)
6895 __ movl(out_reg, Address(obj_reg, offset));
6896 __ MaybeUnpoisonHeapReference(out_reg);
6897 }
6898}
6899
6900void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6901 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006902 const Address& address,
6903 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006904 Register root_reg = root.AsRegister<Register>();
6905 if (kEmitCompilerReadBarrier) {
6906 if (kUseBakerReadBarrier) {
6907 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6908 // Baker's read barrier are used:
6909 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006910 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006911 // if (Thread::Current()->GetIsGcMarking()) {
6912 // root = ReadBarrier::Mark(root)
6913 // }
6914
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006915 // /* GcRoot<mirror::Object> */ root = *address
6916 __ movl(root_reg, address);
6917 if (fixup_label != nullptr) {
6918 __ Bind(fixup_label);
6919 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006920 static_assert(
6921 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6922 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6923 "have different sizes.");
6924 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6925 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6926 "have different sizes.");
6927
6928 // Slow path used to mark the GC root `root`.
6929 SlowPathCode* slow_path =
6930 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6931 codegen_->AddSlowPath(slow_path);
6932
6933 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6934 Immediate(0));
6935 __ j(kNotEqual, slow_path->GetEntryLabel());
6936 __ Bind(slow_path->GetExitLabel());
6937 } else {
6938 // GC root loaded through a slow path for read barriers other
6939 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006940 // /* GcRoot<mirror::Object>* */ root = address
6941 __ leal(root_reg, address);
6942 if (fixup_label != nullptr) {
6943 __ Bind(fixup_label);
6944 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006945 // /* mirror::Object* */ root = root->Read()
6946 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6947 }
6948 } else {
6949 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006950 // /* GcRoot<mirror::Object> */ root = *address
6951 __ movl(root_reg, address);
6952 if (fixup_label != nullptr) {
6953 __ Bind(fixup_label);
6954 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006955 // Note that GC roots are not affected by heap poisoning, thus we
6956 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006957 }
6958}
6959
6960void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6961 Location ref,
6962 Register obj,
6963 uint32_t offset,
6964 Location temp,
6965 bool needs_null_check) {
6966 DCHECK(kEmitCompilerReadBarrier);
6967 DCHECK(kUseBakerReadBarrier);
6968
6969 // /* HeapReference<Object> */ ref = *(obj + offset)
6970 Address src(obj, offset);
6971 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6972}
6973
6974void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6975 Location ref,
6976 Register obj,
6977 uint32_t data_offset,
6978 Location index,
6979 Location temp,
6980 bool needs_null_check) {
6981 DCHECK(kEmitCompilerReadBarrier);
6982 DCHECK(kUseBakerReadBarrier);
6983
6984 // /* HeapReference<Object> */ ref =
6985 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6986 Address src = index.IsConstant() ?
6987 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6988 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6989 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6990}
6991
6992void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6993 Location ref,
6994 Register obj,
6995 const Address& src,
6996 Location temp,
6997 bool needs_null_check) {
6998 DCHECK(kEmitCompilerReadBarrier);
6999 DCHECK(kUseBakerReadBarrier);
7000
7001 // In slow path based read barriers, the read barrier call is
7002 // inserted after the original load. However, in fast path based
7003 // Baker's read barriers, we need to perform the load of
7004 // mirror::Object::monitor_ *before* the original reference load.
7005 // This load-load ordering is required by the read barrier.
7006 // The fast path/slow path (for Baker's algorithm) should look like:
7007 //
7008 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7009 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7010 // HeapReference<Object> ref = *src; // Original reference load.
7011 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
7012 // if (is_gray) {
7013 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7014 // }
7015 //
7016 // Note: the original implementation in ReadBarrier::Barrier is
7017 // slightly more complex as:
7018 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007019 // the high-bits of rb_state, which are expected to be all zeroes
7020 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7021 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007022 // - it performs additional checks that we do not do here for
7023 // performance reasons.
7024
7025 Register ref_reg = ref.AsRegister<Register>();
7026 Register temp_reg = temp.AsRegister<Register>();
7027 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7028
7029 // /* int32_t */ monitor = obj->monitor_
7030 __ movl(temp_reg, Address(obj, monitor_offset));
7031 if (needs_null_check) {
7032 MaybeRecordImplicitNullCheck(instruction);
7033 }
7034 // /* LockWord */ lock_word = LockWord(monitor)
7035 static_assert(sizeof(LockWord) == sizeof(int32_t),
7036 "art::LockWord and int32_t have different sizes.");
7037 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
7038 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
7039 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
7040 static_assert(
7041 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
7042 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
7043
7044 // Load fence to prevent load-load reordering.
7045 // Note that this is a no-op, thanks to the x86 memory model.
7046 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7047
7048 // The actual reference load.
7049 // /* HeapReference<Object> */ ref = *src
7050 __ movl(ref_reg, src);
7051
7052 // Object* ref = ref_addr->AsMirrorPtr()
7053 __ MaybeUnpoisonHeapReference(ref_reg);
7054
7055 // Slow path used to mark the object `ref` when it is gray.
7056 SlowPathCode* slow_path =
7057 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
7058 AddSlowPath(slow_path);
7059
7060 // if (rb_state == ReadBarrier::gray_ptr_)
7061 // ref = ReadBarrier::Mark(ref);
7062 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
7063 __ j(kEqual, slow_path->GetEntryLabel());
7064 __ Bind(slow_path->GetExitLabel());
7065}
7066
7067void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7068 Location out,
7069 Location ref,
7070 Location obj,
7071 uint32_t offset,
7072 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007073 DCHECK(kEmitCompilerReadBarrier);
7074
Roland Levillain7c1559a2015-12-15 10:55:36 +00007075 // Insert a slow path based read barrier *after* the reference load.
7076 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007077 // If heap poisoning is enabled, the unpoisoning of the loaded
7078 // reference will be carried out by the runtime within the slow
7079 // path.
7080 //
7081 // Note that `ref` currently does not get unpoisoned (when heap
7082 // poisoning is enabled), which is alright as the `ref` argument is
7083 // not used by the artReadBarrierSlow entry point.
7084 //
7085 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7086 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7087 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7088 AddSlowPath(slow_path);
7089
Roland Levillain0d5a2812015-11-13 10:07:31 +00007090 __ jmp(slow_path->GetEntryLabel());
7091 __ Bind(slow_path->GetExitLabel());
7092}
7093
Roland Levillain7c1559a2015-12-15 10:55:36 +00007094void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7095 Location out,
7096 Location ref,
7097 Location obj,
7098 uint32_t offset,
7099 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007100 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007101 // Baker's read barriers shall be handled by the fast path
7102 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7103 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007104 // If heap poisoning is enabled, unpoisoning will be taken care of
7105 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007106 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007107 } else if (kPoisonHeapReferences) {
7108 __ UnpoisonHeapReference(out.AsRegister<Register>());
7109 }
7110}
7111
Roland Levillain7c1559a2015-12-15 10:55:36 +00007112void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7113 Location out,
7114 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007115 DCHECK(kEmitCompilerReadBarrier);
7116
Roland Levillain7c1559a2015-12-15 10:55:36 +00007117 // Insert a slow path based read barrier *after* the GC root load.
7118 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007119 // Note that GC roots are not affected by heap poisoning, so we do
7120 // not need to do anything special for this here.
7121 SlowPathCode* slow_path =
7122 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7123 AddSlowPath(slow_path);
7124
Roland Levillain0d5a2812015-11-13 10:07:31 +00007125 __ jmp(slow_path->GetEntryLabel());
7126 __ Bind(slow_path->GetExitLabel());
7127}
7128
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007129void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007130 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007131 LOG(FATAL) << "Unreachable";
7132}
7133
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007134void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007135 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007136 LOG(FATAL) << "Unreachable";
7137}
7138
Mark Mendellfe57faa2015-09-18 09:26:15 -04007139// Simple implementation of packed switch - generate cascaded compare/jumps.
7140void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7141 LocationSummary* locations =
7142 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7143 locations->SetInAt(0, Location::RequiresRegister());
7144}
7145
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007146void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7147 int32_t lower_bound,
7148 uint32_t num_entries,
7149 HBasicBlock* switch_block,
7150 HBasicBlock* default_block) {
7151 // Figure out the correct compare values and jump conditions.
7152 // Handle the first compare/branch as a special case because it might
7153 // jump to the default case.
7154 DCHECK_GT(num_entries, 2u);
7155 Condition first_condition;
7156 uint32_t index;
7157 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7158 if (lower_bound != 0) {
7159 first_condition = kLess;
7160 __ cmpl(value_reg, Immediate(lower_bound));
7161 __ j(first_condition, codegen_->GetLabelOf(default_block));
7162 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007163
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007164 index = 1;
7165 } else {
7166 // Handle all the compare/jumps below.
7167 first_condition = kBelow;
7168 index = 0;
7169 }
7170
7171 // Handle the rest of the compare/jumps.
7172 for (; index + 1 < num_entries; index += 2) {
7173 int32_t compare_to_value = lower_bound + index + 1;
7174 __ cmpl(value_reg, Immediate(compare_to_value));
7175 // Jump to successors[index] if value < case_value[index].
7176 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7177 // Jump to successors[index + 1] if value == case_value[index + 1].
7178 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7179 }
7180
7181 if (index != num_entries) {
7182 // There are an odd number of entries. Handle the last one.
7183 DCHECK_EQ(index + 1, num_entries);
7184 __ cmpl(value_reg, Immediate(lower_bound + index));
7185 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007186 }
7187
7188 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007189 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7190 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007191 }
7192}
7193
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007194void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7195 int32_t lower_bound = switch_instr->GetStartValue();
7196 uint32_t num_entries = switch_instr->GetNumEntries();
7197 LocationSummary* locations = switch_instr->GetLocations();
7198 Register value_reg = locations->InAt(0).AsRegister<Register>();
7199
7200 GenPackedSwitchWithCompares(value_reg,
7201 lower_bound,
7202 num_entries,
7203 switch_instr->GetBlock(),
7204 switch_instr->GetDefaultBlock());
7205}
7206
Mark Mendell805b3b52015-09-18 14:10:29 -04007207void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7208 LocationSummary* locations =
7209 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7210 locations->SetInAt(0, Location::RequiresRegister());
7211
7212 // Constant area pointer.
7213 locations->SetInAt(1, Location::RequiresRegister());
7214
7215 // And the temporary we need.
7216 locations->AddTemp(Location::RequiresRegister());
7217}
7218
7219void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7220 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007221 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007222 LocationSummary* locations = switch_instr->GetLocations();
7223 Register value_reg = locations->InAt(0).AsRegister<Register>();
7224 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7225
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007226 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7227 GenPackedSwitchWithCompares(value_reg,
7228 lower_bound,
7229 num_entries,
7230 switch_instr->GetBlock(),
7231 default_block);
7232 return;
7233 }
7234
Mark Mendell805b3b52015-09-18 14:10:29 -04007235 // Optimizing has a jump area.
7236 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7237 Register constant_area = locations->InAt(1).AsRegister<Register>();
7238
7239 // Remove the bias, if needed.
7240 if (lower_bound != 0) {
7241 __ leal(temp_reg, Address(value_reg, -lower_bound));
7242 value_reg = temp_reg;
7243 }
7244
7245 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007246 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007247 __ cmpl(value_reg, Immediate(num_entries - 1));
7248 __ j(kAbove, codegen_->GetLabelOf(default_block));
7249
7250 // We are in the range of the table.
7251 // Load (target-constant_area) from the jump table, indexing by the value.
7252 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7253
7254 // Compute the actual target address by adding in constant_area.
7255 __ addl(temp_reg, constant_area);
7256
7257 // And jump.
7258 __ jmp(temp_reg);
7259}
7260
Mark Mendell0616ae02015-04-17 12:49:27 -04007261void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7262 HX86ComputeBaseMethodAddress* insn) {
7263 LocationSummary* locations =
7264 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7265 locations->SetOut(Location::RequiresRegister());
7266}
7267
7268void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7269 HX86ComputeBaseMethodAddress* insn) {
7270 LocationSummary* locations = insn->GetLocations();
7271 Register reg = locations->Out().AsRegister<Register>();
7272
7273 // Generate call to next instruction.
7274 Label next_instruction;
7275 __ call(&next_instruction);
7276 __ Bind(&next_instruction);
7277
7278 // Remember this offset for later use with constant area.
7279 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7280
7281 // Grab the return address off the stack.
7282 __ popl(reg);
7283}
7284
7285void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7286 HX86LoadFromConstantTable* insn) {
7287 LocationSummary* locations =
7288 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7289
7290 locations->SetInAt(0, Location::RequiresRegister());
7291 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7292
7293 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007294 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007295 return;
7296 }
7297
7298 switch (insn->GetType()) {
7299 case Primitive::kPrimFloat:
7300 case Primitive::kPrimDouble:
7301 locations->SetOut(Location::RequiresFpuRegister());
7302 break;
7303
7304 case Primitive::kPrimInt:
7305 locations->SetOut(Location::RequiresRegister());
7306 break;
7307
7308 default:
7309 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7310 }
7311}
7312
7313void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007314 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007315 return;
7316 }
7317
7318 LocationSummary* locations = insn->GetLocations();
7319 Location out = locations->Out();
7320 Register const_area = locations->InAt(0).AsRegister<Register>();
7321 HConstant *value = insn->GetConstant();
7322
7323 switch (insn->GetType()) {
7324 case Primitive::kPrimFloat:
7325 __ movss(out.AsFpuRegister<XmmRegister>(),
7326 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7327 break;
7328
7329 case Primitive::kPrimDouble:
7330 __ movsd(out.AsFpuRegister<XmmRegister>(),
7331 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7332 break;
7333
7334 case Primitive::kPrimInt:
7335 __ movl(out.AsRegister<Register>(),
7336 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7337 break;
7338
7339 default:
7340 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7341 }
7342}
7343
Mark Mendell0616ae02015-04-17 12:49:27 -04007344/**
7345 * Class to handle late fixup of offsets into constant area.
7346 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007347class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007348 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007349 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7350 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7351
7352 protected:
7353 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7354
7355 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007356
7357 private:
7358 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7359 // Patch the correct offset for the instruction. The place to patch is the
7360 // last 4 bytes of the instruction.
7361 // The value to patch is the distance from the offset in the constant area
7362 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007363 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7364 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007365
7366 // Patch in the right value.
7367 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7368 }
7369
Mark Mendell0616ae02015-04-17 12:49:27 -04007370 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007371 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007372};
7373
Mark Mendell805b3b52015-09-18 14:10:29 -04007374/**
7375 * Class to handle late fixup of offsets to a jump table that will be created in the
7376 * constant area.
7377 */
7378class JumpTableRIPFixup : public RIPFixup {
7379 public:
7380 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7381 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7382
7383 void CreateJumpTable() {
7384 X86Assembler* assembler = codegen_->GetAssembler();
7385
7386 // Ensure that the reference to the jump table has the correct offset.
7387 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7388 SetOffset(offset_in_constant_table);
7389
7390 // The label values in the jump table are computed relative to the
7391 // instruction addressing the constant area.
7392 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7393
7394 // Populate the jump table with the correct values for the jump table.
7395 int32_t num_entries = switch_instr_->GetNumEntries();
7396 HBasicBlock* block = switch_instr_->GetBlock();
7397 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7398 // The value that we want is the target offset - the position of the table.
7399 for (int32_t i = 0; i < num_entries; i++) {
7400 HBasicBlock* b = successors[i];
7401 Label* l = codegen_->GetLabelOf(b);
7402 DCHECK(l->IsBound());
7403 int32_t offset_to_block = l->Position() - relative_offset;
7404 assembler->AppendInt32(offset_to_block);
7405 }
7406 }
7407
7408 private:
7409 const HX86PackedSwitch* switch_instr_;
7410};
7411
7412void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7413 // Generate the constant area if needed.
7414 X86Assembler* assembler = GetAssembler();
7415 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7416 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7417 // byte values.
7418 assembler->Align(4, 0);
7419 constant_area_start_ = assembler->CodeSize();
7420
7421 // Populate any jump tables.
7422 for (auto jump_table : fixups_to_jump_tables_) {
7423 jump_table->CreateJumpTable();
7424 }
7425
7426 // And now add the constant area to the generated code.
7427 assembler->AddConstantArea();
7428 }
7429
7430 // And finish up.
7431 CodeGenerator::Finalize(allocator);
7432}
7433
Mark Mendell0616ae02015-04-17 12:49:27 -04007434Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7435 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7436 return Address(reg, kDummy32BitOffset, fixup);
7437}
7438
7439Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7440 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7441 return Address(reg, kDummy32BitOffset, fixup);
7442}
7443
7444Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7445 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7446 return Address(reg, kDummy32BitOffset, fixup);
7447}
7448
7449Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7450 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7451 return Address(reg, kDummy32BitOffset, fixup);
7452}
7453
Aart Bika19616e2016-02-01 18:57:58 -08007454void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7455 if (value == 0) {
7456 __ xorl(dest, dest);
7457 } else {
7458 __ movl(dest, Immediate(value));
7459 }
7460}
7461
7462void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7463 if (value == 0) {
7464 __ testl(dest, dest);
7465 } else {
7466 __ cmpl(dest, Immediate(value));
7467 }
7468}
7469
Mark Mendell805b3b52015-09-18 14:10:29 -04007470Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7471 Register reg,
7472 Register value) {
7473 // Create a fixup to be used to create and address the jump table.
7474 JumpTableRIPFixup* table_fixup =
7475 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7476
7477 // We have to populate the jump tables.
7478 fixups_to_jump_tables_.push_back(table_fixup);
7479
7480 // We want a scaled address, as we are extracting the correct offset from the table.
7481 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7482}
7483
Andreas Gampe85b62f22015-09-09 13:15:38 -07007484// TODO: target as memory.
7485void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7486 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007487 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007488 return;
7489 }
7490
7491 DCHECK_NE(type, Primitive::kPrimVoid);
7492
7493 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7494 if (target.Equals(return_loc)) {
7495 return;
7496 }
7497
7498 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7499 // with the else branch.
7500 if (type == Primitive::kPrimLong) {
7501 HParallelMove parallel_move(GetGraph()->GetArena());
7502 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7503 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7504 GetMoveResolver()->EmitNativeCode(&parallel_move);
7505 } else {
7506 // Let the parallel move resolver take care of all of this.
7507 HParallelMove parallel_move(GetGraph()->GetArena());
7508 parallel_move.AddMove(return_loc, target, type, nullptr);
7509 GetMoveResolver()->EmitNativeCode(&parallel_move);
7510 }
7511}
7512
Roland Levillain4d027112015-07-01 15:41:14 +01007513#undef __
7514
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007515} // namespace x86
7516} // namespace art