blob: c6a727d584b12d788b0a212defe6c3950172367f [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);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100151 uint32_t entry_point_offset = instruction_->AsBoundsCheck()->IsStringCharAt()
152 ? QUICK_ENTRY_POINT(pThrowStringBounds)
153 : QUICK_ENTRY_POINT(pThrowArrayBounds);
154 x86_codegen->InvokeRuntime(entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100155 instruction_,
156 instruction_->GetDexPc(),
157 this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100158 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000159 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100160 }
161
Alexandre Rames8158f282015-08-07 10:26:17 +0100162 bool IsFatal() const OVERRIDE { return true; }
163
Alexandre Rames9931f312015-06-19 14:47:01 +0100164 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
165
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100166 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100167 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
168};
169
Andreas Gampe85b62f22015-09-09 13:15:38 -0700170class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000171 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000172 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000173 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000174
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000175 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100176 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000177 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000178 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100179 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pTestSuspend),
180 instruction_,
181 instruction_->GetDexPc(),
182 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000183 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000184 RestoreLiveRegisters(codegen, instruction_->GetLocations());
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100185 if (successor_ == nullptr) {
186 __ jmp(GetReturnLabel());
187 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100188 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100189 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000190 }
191
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100192 Label* GetReturnLabel() {
193 DCHECK(successor_ == nullptr);
194 return &return_label_;
195 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000196
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100197 HBasicBlock* GetSuccessor() const {
198 return successor_;
199 }
200
Alexandre Rames9931f312015-06-19 14:47:01 +0100201 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
202
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000203 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100204 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000205 Label return_label_;
206
207 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
208};
209
Andreas Gampe85b62f22015-09-09 13:15:38 -0700210class LoadStringSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000211 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000212 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000213
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000214 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000215 LocationSummary* locations = instruction_->GetLocations();
216 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
217
218 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
219 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000220 SaveLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000221
222 InvokeRuntimeCallingConvention calling_convention;
David Srbecky9cd6d372016-02-09 15:24:47 +0000223 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex();
224 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
Alexandre Rames8158f282015-08-07 10:26:17 +0100225 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pResolveString),
226 instruction_,
227 instruction_->GetDexPc(),
228 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000229 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000230 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000231 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000232
233 __ jmp(GetExitLabel());
234 }
235
Alexandre Rames9931f312015-06-19 14:47:01 +0100236 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
237
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000238 private:
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +0000239 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
240};
241
Andreas Gampe85b62f22015-09-09 13:15:38 -0700242class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000243 public:
244 LoadClassSlowPathX86(HLoadClass* cls,
245 HInstruction* at,
246 uint32_t dex_pc,
247 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000248 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000249 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
250 }
251
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000252 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000253 LocationSummary* locations = at_->GetLocations();
254 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
255 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000256 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000257
258 InvokeRuntimeCallingConvention calling_convention;
259 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex()));
Alexandre Rames8158f282015-08-07 10:26:17 +0100260 x86_codegen->InvokeRuntime(do_clinit_ ? QUICK_ENTRY_POINT(pInitializeStaticStorage)
261 : QUICK_ENTRY_POINT(pInitializeType),
262 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000263 if (do_clinit_) {
264 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
265 } else {
266 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
267 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000268
269 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000270 Location out = locations->Out();
271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
273 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000274 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000275
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000276 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277 __ jmp(GetExitLabel());
278 }
279
Alexandre Rames9931f312015-06-19 14:47:01 +0100280 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
281
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000282 private:
283 // The class this slow path will load.
284 HLoadClass* const cls_;
285
286 // The instruction where this slow path is happening.
287 // (Might be the load class or an initialization check).
288 HInstruction* const at_;
289
290 // The dex PC of `at_`.
291 const uint32_t dex_pc_;
292
293 // Whether to initialize the class.
294 const bool do_clinit_;
295
296 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
297};
298
Andreas Gampe85b62f22015-09-09 13:15:38 -0700299class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000300 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000301 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000303
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000305 LocationSummary* locations = instruction_->GetLocations();
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100306 Location object_class = instruction_->IsCheckCast() ? locations->GetTemp(0)
307 : locations->Out();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000308 DCHECK(instruction_->IsCheckCast()
309 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000310
311 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
312 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000313
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000314 if (!is_fatal_) {
315 SaveLiveRegisters(codegen, locations);
316 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 // We're moving two locations to locations that could overlap, so we need a parallel
319 // move resolver.
320 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000321 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100322 locations->InAt(1),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000323 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100324 Primitive::kPrimNot,
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100325 object_class,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100326 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
327 Primitive::kPrimNot);
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000328
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000329 if (instruction_->IsInstanceOf()) {
Alexandre Rames8158f282015-08-07 10:26:17 +0100330 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pInstanceofNonTrivial),
331 instruction_,
332 instruction_->GetDexPc(),
333 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000334 CheckEntrypointTypes<
335 kQuickInstanceofNonTrivial, uint32_t, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000336 } else {
337 DCHECK(instruction_->IsCheckCast());
Alexandre Rames8158f282015-08-07 10:26:17 +0100338 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pCheckCast),
339 instruction_,
340 instruction_->GetDexPc(),
341 this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000342 CheckEntrypointTypes<kQuickCheckCast, void, const mirror::Class*, const mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000343 }
344
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000345 if (!is_fatal_) {
346 if (instruction_->IsInstanceOf()) {
347 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
348 }
349 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000350
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000351 __ jmp(GetExitLabel());
352 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000353 }
354
Alexandre Rames9931f312015-06-19 14:47:01 +0100355 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000356 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100357
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000358 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000359 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000360
361 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
362};
363
Andreas Gampe85b62f22015-09-09 13:15:38 -0700364class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700365 public:
Aart Bik42249c32016-01-07 15:33:50 -0800366 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000367 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700368
369 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100370 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700371 __ Bind(GetEntryLabel());
372 SaveLiveRegisters(codegen, instruction_->GetLocations());
Alexandre Rames8158f282015-08-07 10:26:17 +0100373 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pDeoptimize),
374 instruction_,
375 instruction_->GetDexPc(),
376 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000377 CheckEntrypointTypes<kQuickDeoptimize, void, void>();
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700378 }
379
Alexandre Rames9931f312015-06-19 14:47:01 +0100380 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathX86"; }
381
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700382 private:
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700383 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathX86);
384};
385
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100386class ArraySetSlowPathX86 : public SlowPathCode {
387 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000388 explicit ArraySetSlowPathX86(HInstruction* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
392 __ Bind(GetEntryLabel());
393 SaveLiveRegisters(codegen, locations);
394
395 InvokeRuntimeCallingConvention calling_convention;
396 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
397 parallel_move.AddMove(
398 locations->InAt(0),
399 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
400 Primitive::kPrimNot,
401 nullptr);
402 parallel_move.AddMove(
403 locations->InAt(1),
404 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
405 Primitive::kPrimInt,
406 nullptr);
407 parallel_move.AddMove(
408 locations->InAt(2),
409 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
410 Primitive::kPrimNot,
411 nullptr);
412 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
413
414 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
415 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pAputObject),
416 instruction_,
417 instruction_->GetDexPc(),
418 this);
Roland Levillain888d0672015-11-23 18:53:50 +0000419 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100420 RestoreLiveRegisters(codegen, locations);
421 __ jmp(GetExitLabel());
422 }
423
424 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
425
426 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100427 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
428};
429
Roland Levillain7c1559a2015-12-15 10:55:36 +0000430// Slow path marking an object during a read barrier.
431class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
432 public:
433 ReadBarrierMarkSlowPathX86(HInstruction* instruction, Location out, Location obj)
David Srbecky9cd6d372016-02-09 15:24:47 +0000434 : SlowPathCode(instruction), out_(out), obj_(obj) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000435 DCHECK(kEmitCompilerReadBarrier);
436 }
437
438 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
441 LocationSummary* locations = instruction_->GetLocations();
442 Register reg_out = out_.AsRegister<Register>();
443 DCHECK(locations->CanCall());
444 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
445 DCHECK(instruction_->IsInstanceFieldGet() ||
446 instruction_->IsStaticFieldGet() ||
447 instruction_->IsArrayGet() ||
448 instruction_->IsLoadClass() ||
449 instruction_->IsLoadString() ||
450 instruction_->IsInstanceOf() ||
451 instruction_->IsCheckCast())
452 << "Unexpected instruction in read barrier marking slow path: "
453 << instruction_->DebugName();
454
455 __ Bind(GetEntryLabel());
456 SaveLiveRegisters(codegen, locations);
457
458 InvokeRuntimeCallingConvention calling_convention;
459 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
460 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), obj_);
461 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierMark),
462 instruction_,
463 instruction_->GetDexPc(),
464 this);
465 CheckEntrypointTypes<kQuickReadBarrierMark, mirror::Object*, mirror::Object*>();
466 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
467
468 RestoreLiveRegisters(codegen, locations);
469 __ jmp(GetExitLabel());
470 }
471
472 private:
Roland Levillain7c1559a2015-12-15 10:55:36 +0000473 const Location out_;
474 const Location obj_;
475
476 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
477};
478
Roland Levillain0d5a2812015-11-13 10:07:31 +0000479// Slow path generating a read barrier for a heap reference.
480class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
481 public:
482 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
483 Location out,
484 Location ref,
485 Location obj,
486 uint32_t offset,
487 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000488 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000489 out_(out),
490 ref_(ref),
491 obj_(obj),
492 offset_(offset),
493 index_(index) {
494 DCHECK(kEmitCompilerReadBarrier);
495 // If `obj` is equal to `out` or `ref`, it means the initial object
496 // has been overwritten by (or after) the heap object reference load
497 // to be instrumented, e.g.:
498 //
499 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000500 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000501 //
502 // In that case, we have lost the information about the original
503 // object, and the emitted read barrier cannot work properly.
504 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
505 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
506 }
507
508 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
509 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
510 LocationSummary* locations = instruction_->GetLocations();
511 Register reg_out = out_.AsRegister<Register>();
512 DCHECK(locations->CanCall());
513 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
514 DCHECK(!instruction_->IsInvoke() ||
515 (instruction_->IsInvokeStaticOrDirect() &&
Roland Levillain7c1559a2015-12-15 10:55:36 +0000516 instruction_->GetLocations()->Intrinsified()))
517 << "Unexpected instruction in read barrier for heap reference slow path: "
518 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000519
520 __ Bind(GetEntryLabel());
521 SaveLiveRegisters(codegen, locations);
522
523 // We may have to change the index's value, but as `index_` is a
524 // constant member (like other "inputs" of this slow path),
525 // introduce a copy of it, `index`.
526 Location index = index_;
527 if (index_.IsValid()) {
528 // Handle `index_` for HArrayGet and intrinsic UnsafeGetObject.
529 if (instruction_->IsArrayGet()) {
530 // Compute the actual memory offset and store it in `index`.
531 Register index_reg = index_.AsRegister<Register>();
532 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
533 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
534 // We are about to change the value of `index_reg` (see the
535 // calls to art::x86::X86Assembler::shll and
536 // art::x86::X86Assembler::AddImmediate below), but it has
537 // not been saved by the previous call to
538 // art::SlowPathCode::SaveLiveRegisters, as it is a
539 // callee-save register --
540 // art::SlowPathCode::SaveLiveRegisters does not consider
541 // callee-save registers, as it has been designed with the
542 // assumption that callee-save registers are supposed to be
543 // handled by the called function. So, as a callee-save
544 // register, `index_reg` _would_ eventually be saved onto
545 // the stack, but it would be too late: we would have
546 // changed its value earlier. Therefore, we manually save
547 // it here into another freely available register,
548 // `free_reg`, chosen of course among the caller-save
549 // registers (as a callee-save `free_reg` register would
550 // exhibit the same problem).
551 //
552 // Note we could have requested a temporary register from
553 // the register allocator instead; but we prefer not to, as
554 // this is a slow path, and we know we can find a
555 // caller-save register that is available.
556 Register free_reg = FindAvailableCallerSaveRegister(codegen);
557 __ movl(free_reg, index_reg);
558 index_reg = free_reg;
559 index = Location::RegisterLocation(index_reg);
560 } else {
561 // The initial register stored in `index_` has already been
562 // saved in the call to art::SlowPathCode::SaveLiveRegisters
563 // (as it is not a callee-save register), so we can freely
564 // use it.
565 }
566 // Shifting the index value contained in `index_reg` by the scale
567 // factor (2) cannot overflow in practice, as the runtime is
568 // unable to allocate object arrays with a size larger than
569 // 2^26 - 1 (that is, 2^28 - 4 bytes).
570 __ shll(index_reg, Immediate(TIMES_4));
571 static_assert(
572 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
573 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
574 __ AddImmediate(index_reg, Immediate(offset_));
575 } else {
576 DCHECK(instruction_->IsInvoke());
577 DCHECK(instruction_->GetLocations()->Intrinsified());
578 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
579 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
580 << instruction_->AsInvoke()->GetIntrinsic();
581 DCHECK_EQ(offset_, 0U);
582 DCHECK(index_.IsRegisterPair());
583 // UnsafeGet's offset location is a register pair, the low
584 // part contains the correct offset.
585 index = index_.ToLow();
586 }
587 }
588
589 // We're moving two or three locations to locations that could
590 // overlap, so we need a parallel move resolver.
591 InvokeRuntimeCallingConvention calling_convention;
592 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
593 parallel_move.AddMove(ref_,
594 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
595 Primitive::kPrimNot,
596 nullptr);
597 parallel_move.AddMove(obj_,
598 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
599 Primitive::kPrimNot,
600 nullptr);
601 if (index.IsValid()) {
602 parallel_move.AddMove(index,
603 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
604 Primitive::kPrimInt,
605 nullptr);
606 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
607 } else {
608 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
609 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
610 }
611 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierSlow),
612 instruction_,
613 instruction_->GetDexPc(),
614 this);
615 CheckEntrypointTypes<
616 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
617 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
618
619 RestoreLiveRegisters(codegen, locations);
620 __ jmp(GetExitLabel());
621 }
622
623 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
624
625 private:
626 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
627 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
628 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
629 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
630 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
631 return static_cast<Register>(i);
632 }
633 }
634 // We shall never fail to find a free caller-save register, as
635 // there are more than two core caller-save registers on x86
636 // (meaning it is possible to find one which is different from
637 // `ref` and `obj`).
638 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
639 LOG(FATAL) << "Could not find a free caller-save register";
640 UNREACHABLE();
641 }
642
Roland Levillain0d5a2812015-11-13 10:07:31 +0000643 const Location out_;
644 const Location ref_;
645 const Location obj_;
646 const uint32_t offset_;
647 // An additional location containing an index to an array.
648 // Only used for HArrayGet and the UnsafeGetObject &
649 // UnsafeGetObjectVolatile intrinsics.
650 const Location index_;
651
652 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
653};
654
655// Slow path generating a read barrier for a GC root.
656class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
657 public:
658 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000659 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000660 DCHECK(kEmitCompilerReadBarrier);
661 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000662
663 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
664 LocationSummary* locations = instruction_->GetLocations();
665 Register reg_out = out_.AsRegister<Register>();
666 DCHECK(locations->CanCall());
667 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000668 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
669 << "Unexpected instruction in read barrier for GC root slow path: "
670 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000671
672 __ Bind(GetEntryLabel());
673 SaveLiveRegisters(codegen, locations);
674
675 InvokeRuntimeCallingConvention calling_convention;
676 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
677 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
678 x86_codegen->InvokeRuntime(QUICK_ENTRY_POINT(pReadBarrierForRootSlow),
679 instruction_,
680 instruction_->GetDexPc(),
681 this);
682 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
683 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
684
685 RestoreLiveRegisters(codegen, locations);
686 __ jmp(GetExitLabel());
687 }
688
689 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
690
691 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000692 const Location out_;
693 const Location root_;
694
695 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
696};
697
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100698#undef __
Chih-Hung Hsiehfba39972016-05-11 11:26:48 -0700699// NOLINT on __ macro to suppress wrong warning/fix from clang-tidy.
700#define __ down_cast<X86Assembler*>(GetAssembler())-> /* NOLINT */
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100701
Aart Bike9f37602015-10-09 11:15:55 -0700702inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700703 switch (cond) {
704 case kCondEQ: return kEqual;
705 case kCondNE: return kNotEqual;
706 case kCondLT: return kLess;
707 case kCondLE: return kLessEqual;
708 case kCondGT: return kGreater;
709 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700710 case kCondB: return kBelow;
711 case kCondBE: return kBelowEqual;
712 case kCondA: return kAbove;
713 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700714 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100715 LOG(FATAL) << "Unreachable";
716 UNREACHABLE();
717}
718
Aart Bike9f37602015-10-09 11:15:55 -0700719// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100720inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
721 switch (cond) {
722 case kCondEQ: return kEqual;
723 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700724 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100725 case kCondLT: return kBelow;
726 case kCondLE: return kBelowEqual;
727 case kCondGT: return kAbove;
728 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700729 // Unsigned remain unchanged.
730 case kCondB: return kBelow;
731 case kCondBE: return kBelowEqual;
732 case kCondA: return kAbove;
733 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100734 }
735 LOG(FATAL) << "Unreachable";
736 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700737}
738
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100739void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100740 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100741}
742
743void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100744 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100745}
746
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100747size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
748 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
749 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100750}
751
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100752size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
753 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
754 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100755}
756
Mark Mendell7c8d0092015-01-26 11:21:33 -0500757size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
758 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
759 return GetFloatingPointSpillSlotSize();
760}
761
762size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
763 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
764 return GetFloatingPointSpillSlotSize();
765}
766
Calin Juravle175dc732015-08-25 15:42:32 +0100767void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
768 HInstruction* instruction,
769 uint32_t dex_pc,
770 SlowPathCode* slow_path) {
771 InvokeRuntime(GetThreadOffset<kX86WordSize>(entrypoint).Int32Value(),
772 instruction,
773 dex_pc,
774 slow_path);
775}
776
777void CodeGeneratorX86::InvokeRuntime(int32_t entry_point_offset,
Alexandre Rames8158f282015-08-07 10:26:17 +0100778 HInstruction* instruction,
779 uint32_t dex_pc,
780 SlowPathCode* slow_path) {
Alexandre Rames78e3ef62015-08-12 13:43:29 +0100781 ValidateInvokeRuntime(instruction, slow_path);
Calin Juravle175dc732015-08-25 15:42:32 +0100782 __ fs()->call(Address::Absolute(entry_point_offset));
Alexandre Rames8158f282015-08-07 10:26:17 +0100783 RecordPcInfo(instruction, dex_pc, slow_path);
Alexandre Rames8158f282015-08-07 10:26:17 +0100784}
785
Mark Mendellfb8d2792015-03-31 22:16:59 -0400786CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000787 const X86InstructionSetFeatures& isa_features,
788 const CompilerOptions& compiler_options,
789 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500790 : CodeGenerator(graph,
791 kNumberOfCpuRegisters,
792 kNumberOfXmmRegisters,
793 kNumberOfRegisterPairs,
794 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
795 arraysize(kCoreCalleeSaves))
796 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +0100797 0,
798 compiler_options,
799 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +0100800 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100801 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100802 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -0400803 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +0100804 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +0000805 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +0100806 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -0400807 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +0000808 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000809 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
810 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +0100811 constant_area_start_(-1),
812 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
813 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000814 // Use a fake return address register to mimic Quick.
815 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +0100816}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100817
David Brazdil58282f42016-01-14 12:45:10 +0000818void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100819 // Don't allocate the dalvik style register pair passing.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100820 blocked_register_pairs_[ECX_EDX] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100821
822 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +0100823 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100824
Calin Juravle34bacdf2014-10-07 20:23:36 +0100825 UpdateBlockedPairRegisters();
826}
827
828void CodeGeneratorX86::UpdateBlockedPairRegisters() const {
829 for (int i = 0; i < kNumberOfRegisterPairs; i++) {
830 X86ManagedRegister current =
831 X86ManagedRegister::FromRegisterPair(static_cast<RegisterPair>(i));
832 if (blocked_core_registers_[current.AsRegisterPairLow()]
833 || blocked_core_registers_[current.AsRegisterPairHigh()]) {
834 blocked_register_pairs_[i] = true;
835 }
836 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +0100837}
838
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100839InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -0800840 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +0100841 assembler_(codegen->GetAssembler()),
842 codegen_(codegen) {}
843
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100844static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +0100845 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100846}
847
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000848void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100849 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +0000850 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +0000851 bool skip_overflow_check =
852 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000853 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +0000854
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +0000855 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100856 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +0100857 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +0100858 }
859
Mark Mendell5f874182015-03-04 15:42:45 -0500860 if (HasEmptyFrame()) {
861 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000862 }
Mark Mendell5f874182015-03-04 15:42:45 -0500863
864 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
865 Register reg = kCoreCalleeSaves[i];
866 if (allocated_registers_.ContainsCoreRegister(reg)) {
867 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100868 __ cfi().AdjustCFAOffset(kX86WordSize);
869 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -0500870 }
871 }
872
David Srbeckyc6b4dd82015-04-07 20:32:43 +0100873 int adjust = GetFrameSize() - FrameEntrySpillSize();
874 __ subl(ESP, Immediate(adjust));
875 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray76b1e172015-05-27 17:18:33 +0100876 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000877}
878
879void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +0100880 __ cfi().RememberState();
881 if (!HasEmptyFrame()) {
882 int adjust = GetFrameSize() - FrameEntrySpillSize();
883 __ addl(ESP, Immediate(adjust));
884 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -0500885
David Srbeckyc34dc932015-04-12 09:27:43 +0100886 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
887 Register reg = kCoreCalleeSaves[i];
888 if (allocated_registers_.ContainsCoreRegister(reg)) {
889 __ popl(reg);
890 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
891 __ cfi().Restore(DWARFReg(reg));
892 }
Mark Mendell5f874182015-03-04 15:42:45 -0500893 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +0000894 }
David Srbeckyc34dc932015-04-12 09:27:43 +0100895 __ ret();
896 __ cfi().RestoreState();
897 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000898}
899
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100900void CodeGeneratorX86::Bind(HBasicBlock* block) {
901 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000902}
903
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100904Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
905 switch (type) {
906 case Primitive::kPrimBoolean:
907 case Primitive::kPrimByte:
908 case Primitive::kPrimChar:
909 case Primitive::kPrimShort:
910 case Primitive::kPrimInt:
911 case Primitive::kPrimNot:
912 return Location::RegisterLocation(EAX);
913
914 case Primitive::kPrimLong:
915 return Location::RegisterPairLocation(EAX, EDX);
916
917 case Primitive::kPrimVoid:
918 return Location::NoLocation();
919
920 case Primitive::kPrimDouble:
921 case Primitive::kPrimFloat:
922 return Location::FpuRegisterLocation(XMM0);
923 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +0100924
925 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +0100926}
927
928Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
929 return Location::RegisterLocation(kMethodRegisterArgument);
930}
931
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100932Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100933 switch (type) {
934 case Primitive::kPrimBoolean:
935 case Primitive::kPrimByte:
936 case Primitive::kPrimChar:
937 case Primitive::kPrimShort:
938 case Primitive::kPrimInt:
939 case Primitive::kPrimNot: {
940 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000941 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100942 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100943 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100944 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000945 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100946 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100947 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100948
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000949 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100950 uint32_t index = gp_index_;
951 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000952 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100953 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +0100954 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
955 calling_convention.GetRegisterPairAt(index));
956 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100957 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000958 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
959 }
960 }
961
962 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100963 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000964 stack_index_++;
965 if (index < calling_convention.GetNumberOfFpuRegisters()) {
966 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
967 } else {
968 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
969 }
970 }
971
972 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +0100973 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000974 stack_index_ += 2;
975 if (index < calling_convention.GetNumberOfFpuRegisters()) {
976 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
977 } else {
978 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100979 }
980 }
981
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100982 case Primitive::kPrimVoid:
983 LOG(FATAL) << "Unexpected parameter type " << type;
984 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100985 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000986 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +0100987}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +0100988
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100989void CodeGeneratorX86::Move32(Location destination, Location source) {
990 if (source.Equals(destination)) {
991 return;
992 }
993 if (destination.IsRegister()) {
994 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000995 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +0100996 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +0000997 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +0100998 } else {
999 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001000 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001001 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001002 } else if (destination.IsFpuRegister()) {
1003 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001004 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001005 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001006 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001007 } else {
1008 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001009 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001010 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001011 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001012 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001013 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001014 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001015 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001016 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001017 } else if (source.IsConstant()) {
1018 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001019 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001020 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001021 } else {
1022 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001023 __ pushl(Address(ESP, source.GetStackIndex()));
1024 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001025 }
1026 }
1027}
1028
1029void CodeGeneratorX86::Move64(Location destination, Location source) {
1030 if (source.Equals(destination)) {
1031 return;
1032 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001033 if (destination.IsRegisterPair()) {
1034 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001035 EmitParallelMoves(
1036 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1037 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001038 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001039 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001040 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1041 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001042 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001043 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1044 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1045 __ psrlq(src_reg, Immediate(32));
1046 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001047 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001048 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001049 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001050 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1051 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001052 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1053 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001054 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001055 if (source.IsFpuRegister()) {
1056 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1057 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001058 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001059 } else if (source.IsRegisterPair()) {
1060 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1061 // Create stack space for 2 elements.
1062 __ subl(ESP, Immediate(2 * elem_size));
1063 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1064 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1065 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1066 // And remove the temporary stack space we allocated.
1067 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001068 } else {
1069 LOG(FATAL) << "Unimplemented";
1070 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001071 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001072 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001073 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001074 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001075 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001076 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001077 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001078 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001079 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001080 } else if (source.IsConstant()) {
1081 HConstant* constant = source.GetConstant();
1082 int64_t value;
1083 if (constant->IsLongConstant()) {
1084 value = constant->AsLongConstant()->GetValue();
1085 } else {
1086 DCHECK(constant->IsDoubleConstant());
Roland Levillainda4d79b2015-03-24 14:36:11 +00001087 value = bit_cast<int64_t, double>(constant->AsDoubleConstant()->GetValue());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001088 }
1089 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
1090 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001091 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001092 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001093 EmitParallelMoves(
1094 Location::StackSlot(source.GetStackIndex()),
1095 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001096 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001097 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001098 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1099 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001100 }
1101 }
1102}
1103
Calin Juravle175dc732015-08-25 15:42:32 +01001104void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1105 DCHECK(location.IsRegister());
1106 __ movl(location.AsRegister<Register>(), Immediate(value));
1107}
1108
Calin Juravlee460d1d2015-09-29 04:52:17 +01001109void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001110 HParallelMove move(GetGraph()->GetArena());
1111 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1112 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1113 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001114 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001115 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001116 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001117 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001118}
1119
1120void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1121 if (location.IsRegister()) {
1122 locations->AddTemp(location);
1123 } else if (location.IsRegisterPair()) {
1124 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1125 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1126 } else {
1127 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1128 }
1129}
1130
David Brazdilfc6a86a2015-06-26 10:33:45 +00001131void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001132 DCHECK(!successor->IsExitBlock());
1133
1134 HBasicBlock* block = got->GetBlock();
1135 HInstruction* previous = got->GetPrevious();
1136
1137 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001138 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001139 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1140 return;
1141 }
1142
1143 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1144 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1145 }
1146 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001147 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001148 }
1149}
1150
David Brazdilfc6a86a2015-06-26 10:33:45 +00001151void LocationsBuilderX86::VisitGoto(HGoto* got) {
1152 got->SetLocations(nullptr);
1153}
1154
1155void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1156 HandleGoto(got, got->GetSuccessor());
1157}
1158
1159void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1160 try_boundary->SetLocations(nullptr);
1161}
1162
1163void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1164 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1165 if (!successor->IsExitBlock()) {
1166 HandleGoto(try_boundary, successor);
1167 }
1168}
1169
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001170void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001171 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001172}
1173
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001174void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001175}
1176
Mark Mendell152408f2015-12-31 12:28:50 -05001177template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001178void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001179 LabelType* true_label,
1180 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001181 if (cond->IsFPConditionTrueIfNaN()) {
1182 __ j(kUnordered, true_label);
1183 } else if (cond->IsFPConditionFalseIfNaN()) {
1184 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001185 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001186 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001187}
1188
Mark Mendell152408f2015-12-31 12:28:50 -05001189template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001190void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001191 LabelType* true_label,
1192 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001193 LocationSummary* locations = cond->GetLocations();
1194 Location left = locations->InAt(0);
1195 Location right = locations->InAt(1);
1196 IfCondition if_cond = cond->GetCondition();
1197
Mark Mendellc4701932015-04-10 13:18:51 -04001198 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001199 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001200 IfCondition true_high_cond = if_cond;
1201 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001202 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001203
1204 // Set the conditions for the test, remembering that == needs to be
1205 // decided using the low words.
1206 switch (if_cond) {
1207 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001208 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001209 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001210 break;
1211 case kCondLT:
1212 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001213 break;
1214 case kCondLE:
1215 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001216 break;
1217 case kCondGT:
1218 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001219 break;
1220 case kCondGE:
1221 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001222 break;
Aart Bike9f37602015-10-09 11:15:55 -07001223 case kCondB:
1224 false_high_cond = kCondA;
1225 break;
1226 case kCondBE:
1227 true_high_cond = kCondB;
1228 break;
1229 case kCondA:
1230 false_high_cond = kCondB;
1231 break;
1232 case kCondAE:
1233 true_high_cond = kCondA;
1234 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001235 }
1236
1237 if (right.IsConstant()) {
1238 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001239 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001240 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001241
Aart Bika19616e2016-02-01 18:57:58 -08001242 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001243 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001244 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001245 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001246 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001247 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001248 __ j(X86Condition(true_high_cond), true_label);
1249 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001250 }
1251 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001252 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001253 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001254 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001255 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001256
1257 __ cmpl(left_high, right_high);
1258 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001259 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001260 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001261 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001262 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001263 __ j(X86Condition(true_high_cond), true_label);
1264 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001265 }
1266 // Must be equal high, so compare the lows.
1267 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001268 } else {
1269 DCHECK(right.IsDoubleStackSlot());
1270 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1271 if (if_cond == kCondNE) {
1272 __ j(X86Condition(true_high_cond), true_label);
1273 } else if (if_cond == kCondEQ) {
1274 __ j(X86Condition(false_high_cond), false_label);
1275 } else {
1276 __ j(X86Condition(true_high_cond), true_label);
1277 __ j(X86Condition(false_high_cond), false_label);
1278 }
1279 // Must be equal high, so compare the lows.
1280 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001281 }
1282 // The last comparison might be unsigned.
1283 __ j(final_condition, true_label);
1284}
1285
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001286void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1287 Location rhs,
1288 HInstruction* insn,
1289 bool is_double) {
1290 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1291 if (is_double) {
1292 if (rhs.IsFpuRegister()) {
1293 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1294 } else if (const_area != nullptr) {
1295 DCHECK(const_area->IsEmittedAtUseSite());
1296 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1297 codegen_->LiteralDoubleAddress(
1298 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1299 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1300 } else {
1301 DCHECK(rhs.IsDoubleStackSlot());
1302 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1303 }
1304 } else {
1305 if (rhs.IsFpuRegister()) {
1306 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1307 } else if (const_area != nullptr) {
1308 DCHECK(const_area->IsEmittedAtUseSite());
1309 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1310 codegen_->LiteralFloatAddress(
1311 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1312 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1313 } else {
1314 DCHECK(rhs.IsStackSlot());
1315 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1316 }
1317 }
1318}
1319
Mark Mendell152408f2015-12-31 12:28:50 -05001320template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001321void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001322 LabelType* true_target_in,
1323 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001324 // Generated branching requires both targets to be explicit. If either of the
1325 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001326 LabelType fallthrough_target;
1327 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1328 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001329
Mark Mendellc4701932015-04-10 13:18:51 -04001330 LocationSummary* locations = condition->GetLocations();
1331 Location left = locations->InAt(0);
1332 Location right = locations->InAt(1);
1333
Mark Mendellc4701932015-04-10 13:18:51 -04001334 Primitive::Type type = condition->InputAt(0)->GetType();
1335 switch (type) {
1336 case Primitive::kPrimLong:
1337 GenerateLongComparesAndJumps(condition, true_target, false_target);
1338 break;
1339 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001340 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001341 GenerateFPJumps(condition, true_target, false_target);
1342 break;
1343 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001344 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001345 GenerateFPJumps(condition, true_target, false_target);
1346 break;
1347 default:
1348 LOG(FATAL) << "Unexpected compare type " << type;
1349 }
1350
David Brazdil0debae72015-11-12 18:37:00 +00001351 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001352 __ jmp(false_target);
1353 }
David Brazdil0debae72015-11-12 18:37:00 +00001354
1355 if (fallthrough_target.IsLinked()) {
1356 __ Bind(&fallthrough_target);
1357 }
Mark Mendellc4701932015-04-10 13:18:51 -04001358}
1359
David Brazdil0debae72015-11-12 18:37:00 +00001360static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1361 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1362 // are set only strictly before `branch`. We can't use the eflags on long/FP
1363 // conditions if they are materialized due to the complex branching.
1364 return cond->IsCondition() &&
1365 cond->GetNext() == branch &&
1366 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1367 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1368}
1369
Mark Mendell152408f2015-12-31 12:28:50 -05001370template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001371void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001372 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001373 LabelType* true_target,
1374 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001375 HInstruction* cond = instruction->InputAt(condition_input_index);
1376
1377 if (true_target == nullptr && false_target == nullptr) {
1378 // Nothing to do. The code always falls through.
1379 return;
1380 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001381 // Constant condition, statically compared against "true" (integer value 1).
1382 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001383 if (true_target != nullptr) {
1384 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001385 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001386 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001387 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001388 if (false_target != nullptr) {
1389 __ jmp(false_target);
1390 }
1391 }
1392 return;
1393 }
1394
1395 // The following code generates these patterns:
1396 // (1) true_target == nullptr && false_target != nullptr
1397 // - opposite condition true => branch to false_target
1398 // (2) true_target != nullptr && false_target == nullptr
1399 // - condition true => branch to true_target
1400 // (3) true_target != nullptr && false_target != nullptr
1401 // - condition true => branch to true_target
1402 // - branch to false_target
1403 if (IsBooleanValueOrMaterializedCondition(cond)) {
1404 if (AreEflagsSetFrom(cond, instruction)) {
1405 if (true_target == nullptr) {
1406 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1407 } else {
1408 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1409 }
1410 } else {
1411 // Materialized condition, compare against 0.
1412 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1413 if (lhs.IsRegister()) {
1414 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1415 } else {
1416 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1417 }
1418 if (true_target == nullptr) {
1419 __ j(kEqual, false_target);
1420 } else {
1421 __ j(kNotEqual, true_target);
1422 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001423 }
1424 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001425 // Condition has not been materialized, use its inputs as the comparison and
1426 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001427 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001428
1429 // If this is a long or FP comparison that has been folded into
1430 // the HCondition, generate the comparison directly.
1431 Primitive::Type type = condition->InputAt(0)->GetType();
1432 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1433 GenerateCompareTestAndBranch(condition, true_target, false_target);
1434 return;
1435 }
1436
1437 Location lhs = condition->GetLocations()->InAt(0);
1438 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001439 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
David Brazdil0debae72015-11-12 18:37:00 +00001440 if (rhs.IsRegister()) {
1441 __ cmpl(lhs.AsRegister<Register>(), rhs.AsRegister<Register>());
1442 } else if (rhs.IsConstant()) {
1443 int32_t constant = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
Aart Bika19616e2016-02-01 18:57:58 -08001444 codegen_->Compare32BitValue(lhs.AsRegister<Register>(), constant);
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001445 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001446 __ cmpl(lhs.AsRegister<Register>(), Address(ESP, rhs.GetStackIndex()));
1447 }
1448 if (true_target == nullptr) {
1449 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1450 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001451 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001452 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001453 }
David Brazdil0debae72015-11-12 18:37:00 +00001454
1455 // If neither branch falls through (case 3), the conditional branch to `true_target`
1456 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1457 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001458 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001459 }
1460}
1461
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001462void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001463 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1464 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001465 locations->SetInAt(0, Location::Any());
1466 }
1467}
1468
1469void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001470 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1471 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1472 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1473 nullptr : codegen_->GetLabelOf(true_successor);
1474 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1475 nullptr : codegen_->GetLabelOf(false_successor);
1476 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001477}
1478
1479void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1480 LocationSummary* locations = new (GetGraph()->GetArena())
1481 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
David Brazdil0debae72015-11-12 18:37:00 +00001482 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001483 locations->SetInAt(0, Location::Any());
1484 }
1485}
1486
1487void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001488 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001489 GenerateTestAndBranch<Label>(deoptimize,
1490 /* condition_input_index */ 0,
1491 slow_path->GetEntryLabel(),
1492 /* false_target */ nullptr);
1493}
1494
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001495static bool SelectCanUseCMOV(HSelect* select) {
1496 // There are no conditional move instructions for XMMs.
1497 if (Primitive::IsFloatingPointType(select->GetType())) {
1498 return false;
1499 }
1500
1501 // A FP condition doesn't generate the single CC that we need.
1502 // In 32 bit mode, a long condition doesn't generate a single CC either.
1503 HInstruction* condition = select->GetCondition();
1504 if (condition->IsCondition()) {
1505 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1506 if (compare_type == Primitive::kPrimLong ||
1507 Primitive::IsFloatingPointType(compare_type)) {
1508 return false;
1509 }
1510 }
1511
1512 // We can generate a CMOV for this Select.
1513 return true;
1514}
1515
David Brazdil74eb1b22015-12-14 11:44:01 +00001516void LocationsBuilderX86::VisitSelect(HSelect* select) {
1517 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001518 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001519 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001520 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001521 } else {
1522 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001523 if (SelectCanUseCMOV(select)) {
1524 if (select->InputAt(1)->IsConstant()) {
1525 // Cmov can't handle a constant value.
1526 locations->SetInAt(1, Location::RequiresRegister());
1527 } else {
1528 locations->SetInAt(1, Location::Any());
1529 }
1530 } else {
1531 locations->SetInAt(1, Location::Any());
1532 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001533 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001534 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1535 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001536 }
1537 locations->SetOut(Location::SameAsFirstInput());
1538}
1539
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001540void InstructionCodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
1541 Register lhs_reg = lhs.AsRegister<Register>();
1542 if (rhs.IsConstant()) {
1543 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
1544 codegen_->Compare32BitValue(lhs_reg, value);
1545 } else if (rhs.IsStackSlot()) {
1546 __ cmpl(lhs_reg, Address(ESP, rhs.GetStackIndex()));
1547 } else {
1548 __ cmpl(lhs_reg, rhs.AsRegister<Register>());
1549 }
1550}
1551
David Brazdil74eb1b22015-12-14 11:44:01 +00001552void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1553 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001554 DCHECK(locations->InAt(0).Equals(locations->Out()));
1555 if (SelectCanUseCMOV(select)) {
1556 // If both the condition and the source types are integer, we can generate
1557 // a CMOV to implement Select.
1558
1559 HInstruction* select_condition = select->GetCondition();
1560 Condition cond = kNotEqual;
1561
1562 // Figure out how to test the 'condition'.
1563 if (select_condition->IsCondition()) {
1564 HCondition* condition = select_condition->AsCondition();
1565 if (!condition->IsEmittedAtUseSite()) {
1566 // This was a previously materialized condition.
1567 // Can we use the existing condition code?
1568 if (AreEflagsSetFrom(condition, select)) {
1569 // Materialization was the previous instruction. Condition codes are right.
1570 cond = X86Condition(condition->GetCondition());
1571 } else {
1572 // No, we have to recreate the condition code.
1573 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1574 __ testl(cond_reg, cond_reg);
1575 }
1576 } else {
1577 // We can't handle FP or long here.
1578 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1579 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1580 LocationSummary* cond_locations = condition->GetLocations();
1581 GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
1582 cond = X86Condition(condition->GetCondition());
1583 }
1584 } else {
1585 // Must be a boolean condition, which needs to be compared to 0.
1586 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1587 __ testl(cond_reg, cond_reg);
1588 }
1589
1590 // If the condition is true, overwrite the output, which already contains false.
1591 Location false_loc = locations->InAt(0);
1592 Location true_loc = locations->InAt(1);
1593 if (select->GetType() == Primitive::kPrimLong) {
1594 // 64 bit conditional move.
1595 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1596 Register false_low = false_loc.AsRegisterPairLow<Register>();
1597 if (true_loc.IsRegisterPair()) {
1598 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1599 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1600 } else {
1601 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1602 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1603 }
1604 } else {
1605 // 32 bit conditional move.
1606 Register false_reg = false_loc.AsRegister<Register>();
1607 if (true_loc.IsRegister()) {
1608 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1609 } else {
1610 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1611 }
1612 }
1613 } else {
1614 NearLabel false_target;
1615 GenerateTestAndBranch<NearLabel>(
1616 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1617 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1618 __ Bind(&false_target);
1619 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001620}
1621
David Srbecky0cf44932015-12-09 14:09:59 +00001622void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1623 new (GetGraph()->GetArena()) LocationSummary(info);
1624}
1625
David Srbeckyd28f4a02016-03-14 17:14:24 +00001626void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1627 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001628}
1629
1630void CodeGeneratorX86::GenerateNop() {
1631 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001632}
1633
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001634void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001635 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001636 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001637 // Handle the long/FP comparisons made in instruction simplification.
1638 switch (cond->InputAt(0)->GetType()) {
1639 case Primitive::kPrimLong: {
1640 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001641 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001642 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001643 locations->SetOut(Location::RequiresRegister());
1644 }
1645 break;
1646 }
1647 case Primitive::kPrimFloat:
1648 case Primitive::kPrimDouble: {
1649 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001650 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1651 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1652 } else if (cond->InputAt(1)->IsConstant()) {
1653 locations->SetInAt(1, Location::RequiresFpuRegister());
1654 } else {
1655 locations->SetInAt(1, Location::Any());
1656 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001657 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001658 locations->SetOut(Location::RequiresRegister());
1659 }
1660 break;
1661 }
1662 default:
1663 locations->SetInAt(0, Location::RequiresRegister());
1664 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001665 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001666 // We need a byte register.
1667 locations->SetOut(Location::RegisterLocation(ECX));
1668 }
1669 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001670 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001671}
1672
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001673void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001674 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001675 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001676 }
Mark Mendellc4701932015-04-10 13:18:51 -04001677
1678 LocationSummary* locations = cond->GetLocations();
1679 Location lhs = locations->InAt(0);
1680 Location rhs = locations->InAt(1);
1681 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001682 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001683
1684 switch (cond->InputAt(0)->GetType()) {
1685 default: {
1686 // Integer case.
1687
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001688 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001689 __ xorl(reg, reg);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001690 GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001691 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001692 return;
1693 }
1694 case Primitive::kPrimLong:
1695 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1696 break;
1697 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001698 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001699 GenerateFPJumps(cond, &true_label, &false_label);
1700 break;
1701 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001702 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001703 GenerateFPJumps(cond, &true_label, &false_label);
1704 break;
1705 }
1706
1707 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001708 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001709
Roland Levillain4fa13f62015-07-06 18:11:54 +01001710 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001711 __ Bind(&false_label);
1712 __ xorl(reg, reg);
1713 __ jmp(&done_label);
1714
Roland Levillain4fa13f62015-07-06 18:11:54 +01001715 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001716 __ Bind(&true_label);
1717 __ movl(reg, Immediate(1));
1718 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001719}
1720
1721void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001722 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001723}
1724
1725void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001726 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001727}
1728
1729void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001730 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001731}
1732
1733void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001734 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001735}
1736
1737void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001738 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001739}
1740
1741void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001742 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001743}
1744
1745void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001746 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001747}
1748
1749void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001750 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001751}
1752
1753void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001754 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001755}
1756
1757void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001758 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001759}
1760
1761void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001762 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001763}
1764
1765void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001766 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001767}
1768
Aart Bike9f37602015-10-09 11:15:55 -07001769void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001770 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001771}
1772
1773void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001774 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001775}
1776
1777void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001778 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001779}
1780
1781void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001782 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001783}
1784
1785void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001786 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001787}
1788
1789void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001790 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001791}
1792
1793void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001794 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001795}
1796
1797void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001798 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001799}
1800
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001801void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001802 LocationSummary* locations =
1803 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001804 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001805}
1806
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001807void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001808 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001809}
1810
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001811void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
1812 LocationSummary* locations =
1813 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1814 locations->SetOut(Location::ConstantLocation(constant));
1815}
1816
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001817void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001818 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001819}
1820
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001821void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001822 LocationSummary* locations =
1823 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001824 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001825}
1826
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001827void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001828 // Will be generated at use site.
1829}
1830
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001831void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
1832 LocationSummary* locations =
1833 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1834 locations->SetOut(Location::ConstantLocation(constant));
1835}
1836
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001837void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001838 // Will be generated at use site.
1839}
1840
1841void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
1842 LocationSummary* locations =
1843 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
1844 locations->SetOut(Location::ConstantLocation(constant));
1845}
1846
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001847void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01001848 // Will be generated at use site.
1849}
1850
Calin Juravle27df7582015-04-17 19:12:31 +01001851void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
1852 memory_barrier->SetLocations(nullptr);
1853}
1854
1855void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00001856 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01001857}
1858
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001859void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001860 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001861}
1862
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001863void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001864 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001865}
1866
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001867void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001868 LocationSummary* locations =
1869 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001870 switch (ret->InputAt(0)->GetType()) {
1871 case Primitive::kPrimBoolean:
1872 case Primitive::kPrimByte:
1873 case Primitive::kPrimChar:
1874 case Primitive::kPrimShort:
1875 case Primitive::kPrimInt:
1876 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001877 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001878 break;
1879
1880 case Primitive::kPrimLong:
1881 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001882 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001883 break;
1884
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001885 case Primitive::kPrimFloat:
1886 case Primitive::kPrimDouble:
1887 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001888 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001889 break;
1890
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001891 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001892 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001893 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001894}
1895
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001896void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001897 if (kIsDebugBuild) {
1898 switch (ret->InputAt(0)->GetType()) {
1899 case Primitive::kPrimBoolean:
1900 case Primitive::kPrimByte:
1901 case Primitive::kPrimChar:
1902 case Primitive::kPrimShort:
1903 case Primitive::kPrimInt:
1904 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001905 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001906 break;
1907
1908 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001909 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
1910 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001911 break;
1912
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001913 case Primitive::kPrimFloat:
1914 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00001915 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001916 break;
1917
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001918 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001919 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001920 }
1921 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001922 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001923}
1924
Calin Juravle175dc732015-08-25 15:42:32 +01001925void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1926 // The trampoline uses the same calling convention as dex calling conventions,
1927 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
1928 // the method_idx.
1929 HandleInvoke(invoke);
1930}
1931
1932void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
1933 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
1934}
1935
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001936void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001937 // Explicit clinit checks triggered by static invokes must have been pruned by
1938 // art::PrepareForRegisterAllocation.
1939 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001940
Mark Mendellfb8d2792015-03-31 22:16:59 -04001941 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04001942 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001943 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00001944 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001945 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04001946 return;
1947 }
1948
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001949 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001950
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001951 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
1952 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00001953 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001954 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001955}
1956
Mark Mendell09ed1a32015-03-25 08:30:06 -04001957static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
1958 if (invoke->GetLocations()->Intrinsified()) {
1959 IntrinsicCodeGeneratorX86 intrinsic(codegen);
1960 intrinsic.Dispatch(invoke);
1961 return true;
1962 }
1963 return false;
1964}
1965
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00001966void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00001967 // Explicit clinit checks triggered by static invokes must have been pruned by
1968 // art::PrepareForRegisterAllocation.
1969 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01001970
Mark Mendell09ed1a32015-03-25 08:30:06 -04001971 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1972 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001973 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001974
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001975 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04001976 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01001977 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07001978 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001979}
1980
1981void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001982 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
1983 if (intrinsic.TryDispatch(invoke)) {
1984 return;
1985 }
1986
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001987 HandleInvoke(invoke);
1988}
1989
1990void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001991 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001992 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001993}
1994
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01001995void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00001996 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
1997 return;
1998 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00001999
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002000 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002001 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002002 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002003}
2004
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002005void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002006 // This call to HandleInvoke allocates a temporary (core) register
2007 // which is also used to transfer the hidden argument from FP to
2008 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002009 HandleInvoke(invoke);
2010 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002011 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002012}
2013
2014void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2015 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002016 LocationSummary* locations = invoke->GetLocations();
2017 Register temp = locations->GetTemp(0).AsRegister<Register>();
2018 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002019 Location receiver = locations->InAt(0);
2020 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2021
Roland Levillain0d5a2812015-11-13 10:07:31 +00002022 // Set the hidden argument. This is safe to do this here, as XMM7
2023 // won't be modified thereafter, before the `call` instruction.
2024 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002025 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002026 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002027
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002028 if (receiver.IsStackSlot()) {
2029 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002030 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002031 __ movl(temp, Address(temp, class_offset));
2032 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002033 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002034 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002035 }
Roland Levillain4d027112015-07-01 15:41:14 +01002036 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002037 // Instead of simply (possibly) unpoisoning `temp` here, we should
2038 // emit a read barrier for the previous class reference load.
2039 // However this is not required in practice, as this is an
2040 // intermediate/temporary reference and because the current
2041 // concurrent copying collector keeps the from-space memory
2042 // intact/accessible until the end of the marking phase (the
2043 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002044 __ MaybeUnpoisonHeapReference(temp);
Nelli Kimbadee982016-05-13 13:08:53 +03002045 // temp = temp->GetAddressOfIMT()
2046 __ movl(temp,
2047 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002048 // temp = temp->GetImtEntryAt(method_offset);
Nelli Kimbadee982016-05-13 13:08:53 +03002049 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07002050 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002051 __ movl(temp, Address(temp, method_offset));
2052 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002053 __ call(Address(temp,
2054 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002055
2056 DCHECK(!codegen_->IsLeafMethod());
2057 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2058}
2059
Roland Levillain88cb1752014-10-20 16:36:47 +01002060void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2061 LocationSummary* locations =
2062 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2063 switch (neg->GetResultType()) {
2064 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002065 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002066 locations->SetInAt(0, Location::RequiresRegister());
2067 locations->SetOut(Location::SameAsFirstInput());
2068 break;
2069
Roland Levillain88cb1752014-10-20 16:36:47 +01002070 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002071 locations->SetInAt(0, Location::RequiresFpuRegister());
2072 locations->SetOut(Location::SameAsFirstInput());
2073 locations->AddTemp(Location::RequiresRegister());
2074 locations->AddTemp(Location::RequiresFpuRegister());
2075 break;
2076
Roland Levillain88cb1752014-10-20 16:36:47 +01002077 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002078 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002079 locations->SetOut(Location::SameAsFirstInput());
2080 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002081 break;
2082
2083 default:
2084 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2085 }
2086}
2087
2088void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2089 LocationSummary* locations = neg->GetLocations();
2090 Location out = locations->Out();
2091 Location in = locations->InAt(0);
2092 switch (neg->GetResultType()) {
2093 case Primitive::kPrimInt:
2094 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002095 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002096 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002097 break;
2098
2099 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002100 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002101 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002102 __ negl(out.AsRegisterPairLow<Register>());
2103 // Negation is similar to subtraction from zero. The least
2104 // significant byte triggers a borrow when it is different from
2105 // zero; to take it into account, add 1 to the most significant
2106 // byte if the carry flag (CF) is set to 1 after the first NEGL
2107 // operation.
2108 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2109 __ negl(out.AsRegisterPairHigh<Register>());
2110 break;
2111
Roland Levillain5368c212014-11-27 15:03:41 +00002112 case Primitive::kPrimFloat: {
2113 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002114 Register constant = locations->GetTemp(0).AsRegister<Register>();
2115 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002116 // Implement float negation with an exclusive or with value
2117 // 0x80000000 (mask for bit 31, representing the sign of a
2118 // single-precision floating-point number).
2119 __ movl(constant, Immediate(INT32_C(0x80000000)));
2120 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002121 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002122 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002123 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002124
Roland Levillain5368c212014-11-27 15:03:41 +00002125 case Primitive::kPrimDouble: {
2126 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002127 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002128 // Implement double negation with an exclusive or with value
2129 // 0x8000000000000000 (mask for bit 63, representing the sign of
2130 // a double-precision floating-point number).
2131 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002132 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002133 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002134 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002135
2136 default:
2137 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2138 }
2139}
2140
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002141void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2142 LocationSummary* locations =
2143 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2144 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2145 locations->SetInAt(0, Location::RequiresFpuRegister());
2146 locations->SetInAt(1, Location::RequiresRegister());
2147 locations->SetOut(Location::SameAsFirstInput());
2148 locations->AddTemp(Location::RequiresFpuRegister());
2149}
2150
2151void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2152 LocationSummary* locations = neg->GetLocations();
2153 Location out = locations->Out();
2154 DCHECK(locations->InAt(0).Equals(out));
2155
2156 Register constant_area = locations->InAt(1).AsRegister<Register>();
2157 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2158 if (neg->GetType() == Primitive::kPrimFloat) {
2159 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2160 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2161 } else {
2162 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2163 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2164 }
2165}
2166
Roland Levillaindff1f282014-11-05 14:15:05 +00002167void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002168 Primitive::Type result_type = conversion->GetResultType();
2169 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002170 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002171
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002172 // The float-to-long and double-to-long type conversions rely on a
2173 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002174 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002175 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2176 && result_type == Primitive::kPrimLong)
Roland Levillain624279f2014-12-04 11:54:28 +00002177 ? LocationSummary::kCall
2178 : LocationSummary::kNoCall;
2179 LocationSummary* locations =
2180 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2181
David Brazdilb2bd1c52015-03-25 11:17:37 +00002182 // The Java language does not allow treating boolean as an integral type but
2183 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002184
Roland Levillaindff1f282014-11-05 14:15:05 +00002185 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002186 case Primitive::kPrimByte:
2187 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002188 case Primitive::kPrimLong: {
2189 // Type conversion from long to byte is a result of code transformations.
2190 HInstruction* input = conversion->InputAt(0);
2191 Location input_location = input->IsConstant()
2192 ? Location::ConstantLocation(input->AsConstant())
2193 : Location::RegisterPairLocation(EAX, EDX);
2194 locations->SetInAt(0, input_location);
2195 // Make the output overlap to please the register allocator. This greatly simplifies
2196 // the validation of the linear scan implementation
2197 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2198 break;
2199 }
David Brazdil46e2a392015-03-16 17:31:52 +00002200 case Primitive::kPrimBoolean:
2201 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002202 case Primitive::kPrimShort:
2203 case Primitive::kPrimInt:
2204 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002205 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002206 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2207 // Make the output overlap to please the register allocator. This greatly simplifies
2208 // the validation of the linear scan implementation
2209 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002210 break;
2211
2212 default:
2213 LOG(FATAL) << "Unexpected type conversion from " << input_type
2214 << " to " << result_type;
2215 }
2216 break;
2217
Roland Levillain01a8d712014-11-14 16:27:39 +00002218 case Primitive::kPrimShort:
2219 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002220 case Primitive::kPrimLong:
2221 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002222 case Primitive::kPrimBoolean:
2223 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002224 case Primitive::kPrimByte:
2225 case Primitive::kPrimInt:
2226 case Primitive::kPrimChar:
2227 // Processing a Dex `int-to-short' instruction.
2228 locations->SetInAt(0, Location::Any());
2229 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2230 break;
2231
2232 default:
2233 LOG(FATAL) << "Unexpected type conversion from " << input_type
2234 << " to " << result_type;
2235 }
2236 break;
2237
Roland Levillain946e1432014-11-11 17:35:19 +00002238 case Primitive::kPrimInt:
2239 switch (input_type) {
2240 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002241 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002242 locations->SetInAt(0, Location::Any());
2243 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2244 break;
2245
2246 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002247 // Processing a Dex `float-to-int' instruction.
2248 locations->SetInAt(0, Location::RequiresFpuRegister());
2249 locations->SetOut(Location::RequiresRegister());
2250 locations->AddTemp(Location::RequiresFpuRegister());
2251 break;
2252
Roland Levillain946e1432014-11-11 17:35:19 +00002253 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002254 // Processing a Dex `double-to-int' instruction.
2255 locations->SetInAt(0, Location::RequiresFpuRegister());
2256 locations->SetOut(Location::RequiresRegister());
2257 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002258 break;
2259
2260 default:
2261 LOG(FATAL) << "Unexpected type conversion from " << input_type
2262 << " to " << result_type;
2263 }
2264 break;
2265
Roland Levillaindff1f282014-11-05 14:15:05 +00002266 case Primitive::kPrimLong:
2267 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002268 case Primitive::kPrimBoolean:
2269 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002270 case Primitive::kPrimByte:
2271 case Primitive::kPrimShort:
2272 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002273 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002274 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002275 locations->SetInAt(0, Location::RegisterLocation(EAX));
2276 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2277 break;
2278
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002279 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002280 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002281 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002282 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002283 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2284 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2285
Vladimir Marko949c91f2015-01-27 10:48:44 +00002286 // The runtime helper puts the result in EAX, EDX.
2287 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002288 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002289 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002290
2291 default:
2292 LOG(FATAL) << "Unexpected type conversion from " << input_type
2293 << " to " << result_type;
2294 }
2295 break;
2296
Roland Levillain981e4542014-11-14 11:47:14 +00002297 case Primitive::kPrimChar:
2298 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002299 case Primitive::kPrimLong:
2300 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002301 case Primitive::kPrimBoolean:
2302 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002303 case Primitive::kPrimByte:
2304 case Primitive::kPrimShort:
2305 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002306 // Processing a Dex `int-to-char' instruction.
2307 locations->SetInAt(0, Location::Any());
2308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2309 break;
2310
2311 default:
2312 LOG(FATAL) << "Unexpected type conversion from " << input_type
2313 << " to " << result_type;
2314 }
2315 break;
2316
Roland Levillaindff1f282014-11-05 14:15:05 +00002317 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002318 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002319 case Primitive::kPrimBoolean:
2320 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002321 case Primitive::kPrimByte:
2322 case Primitive::kPrimShort:
2323 case Primitive::kPrimInt:
2324 case Primitive::kPrimChar:
2325 // Processing a Dex `int-to-float' instruction.
2326 locations->SetInAt(0, Location::RequiresRegister());
2327 locations->SetOut(Location::RequiresFpuRegister());
2328 break;
2329
2330 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002331 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002332 locations->SetInAt(0, Location::Any());
2333 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002334 break;
2335
Roland Levillaincff13742014-11-17 14:32:17 +00002336 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002337 // Processing a Dex `double-to-float' instruction.
2338 locations->SetInAt(0, Location::RequiresFpuRegister());
2339 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002340 break;
2341
2342 default:
2343 LOG(FATAL) << "Unexpected type conversion from " << input_type
2344 << " to " << result_type;
2345 };
2346 break;
2347
Roland Levillaindff1f282014-11-05 14:15:05 +00002348 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002349 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002350 case Primitive::kPrimBoolean:
2351 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002352 case Primitive::kPrimByte:
2353 case Primitive::kPrimShort:
2354 case Primitive::kPrimInt:
2355 case Primitive::kPrimChar:
2356 // Processing a Dex `int-to-double' instruction.
2357 locations->SetInAt(0, Location::RequiresRegister());
2358 locations->SetOut(Location::RequiresFpuRegister());
2359 break;
2360
2361 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002362 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002363 locations->SetInAt(0, Location::Any());
2364 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002365 break;
2366
Roland Levillaincff13742014-11-17 14:32:17 +00002367 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002368 // Processing a Dex `float-to-double' instruction.
2369 locations->SetInAt(0, Location::RequiresFpuRegister());
2370 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002371 break;
2372
2373 default:
2374 LOG(FATAL) << "Unexpected type conversion from " << input_type
2375 << " to " << result_type;
2376 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002377 break;
2378
2379 default:
2380 LOG(FATAL) << "Unexpected type conversion from " << input_type
2381 << " to " << result_type;
2382 }
2383}
2384
2385void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2386 LocationSummary* locations = conversion->GetLocations();
2387 Location out = locations->Out();
2388 Location in = locations->InAt(0);
2389 Primitive::Type result_type = conversion->GetResultType();
2390 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002391 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002392 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002393 case Primitive::kPrimByte:
2394 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002395 case Primitive::kPrimLong:
2396 // Type conversion from long to byte is a result of code transformations.
2397 if (in.IsRegisterPair()) {
2398 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2399 } else {
2400 DCHECK(in.GetConstant()->IsLongConstant());
2401 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2402 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2403 }
2404 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002405 case Primitive::kPrimBoolean:
2406 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002407 case Primitive::kPrimShort:
2408 case Primitive::kPrimInt:
2409 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002410 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002411 if (in.IsRegister()) {
2412 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002413 } else {
2414 DCHECK(in.GetConstant()->IsIntConstant());
2415 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2416 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2417 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected type conversion from " << input_type
2422 << " to " << result_type;
2423 }
2424 break;
2425
Roland Levillain01a8d712014-11-14 16:27:39 +00002426 case Primitive::kPrimShort:
2427 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002428 case Primitive::kPrimLong:
2429 // Type conversion from long to short is a result of code transformations.
2430 if (in.IsRegisterPair()) {
2431 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2432 } else if (in.IsDoubleStackSlot()) {
2433 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2434 } else {
2435 DCHECK(in.GetConstant()->IsLongConstant());
2436 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2437 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2438 }
2439 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002440 case Primitive::kPrimBoolean:
2441 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002442 case Primitive::kPrimByte:
2443 case Primitive::kPrimInt:
2444 case Primitive::kPrimChar:
2445 // Processing a Dex `int-to-short' instruction.
2446 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002447 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002448 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002449 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002450 } else {
2451 DCHECK(in.GetConstant()->IsIntConstant());
2452 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002453 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002454 }
2455 break;
2456
2457 default:
2458 LOG(FATAL) << "Unexpected type conversion from " << input_type
2459 << " to " << result_type;
2460 }
2461 break;
2462
Roland Levillain946e1432014-11-11 17:35:19 +00002463 case Primitive::kPrimInt:
2464 switch (input_type) {
2465 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002466 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002467 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002468 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002469 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002470 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002471 } else {
2472 DCHECK(in.IsConstant());
2473 DCHECK(in.GetConstant()->IsLongConstant());
2474 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002475 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002476 }
2477 break;
2478
Roland Levillain3f8f9362014-12-02 17:45:01 +00002479 case Primitive::kPrimFloat: {
2480 // Processing a Dex `float-to-int' instruction.
2481 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2482 Register output = out.AsRegister<Register>();
2483 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002484 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002485
2486 __ movl(output, Immediate(kPrimIntMax));
2487 // temp = int-to-float(output)
2488 __ cvtsi2ss(temp, output);
2489 // if input >= temp goto done
2490 __ comiss(input, temp);
2491 __ j(kAboveEqual, &done);
2492 // if input == NaN goto nan
2493 __ j(kUnordered, &nan);
2494 // output = float-to-int-truncate(input)
2495 __ cvttss2si(output, input);
2496 __ jmp(&done);
2497 __ Bind(&nan);
2498 // output = 0
2499 __ xorl(output, output);
2500 __ Bind(&done);
2501 break;
2502 }
2503
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002504 case Primitive::kPrimDouble: {
2505 // Processing a Dex `double-to-int' instruction.
2506 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2507 Register output = out.AsRegister<Register>();
2508 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002509 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002510
2511 __ movl(output, Immediate(kPrimIntMax));
2512 // temp = int-to-double(output)
2513 __ cvtsi2sd(temp, output);
2514 // if input >= temp goto done
2515 __ comisd(input, temp);
2516 __ j(kAboveEqual, &done);
2517 // if input == NaN goto nan
2518 __ j(kUnordered, &nan);
2519 // output = double-to-int-truncate(input)
2520 __ cvttsd2si(output, input);
2521 __ jmp(&done);
2522 __ Bind(&nan);
2523 // output = 0
2524 __ xorl(output, output);
2525 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002526 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002527 }
Roland Levillain946e1432014-11-11 17:35:19 +00002528
2529 default:
2530 LOG(FATAL) << "Unexpected type conversion from " << input_type
2531 << " to " << result_type;
2532 }
2533 break;
2534
Roland Levillaindff1f282014-11-05 14:15:05 +00002535 case Primitive::kPrimLong:
2536 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002537 case Primitive::kPrimBoolean:
2538 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002539 case Primitive::kPrimByte:
2540 case Primitive::kPrimShort:
2541 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002542 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002543 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002544 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2545 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002546 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002547 __ cdq();
2548 break;
2549
2550 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002551 // Processing a Dex `float-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002552 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pF2l),
2553 conversion,
2554 conversion->GetDexPc(),
2555 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002556 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002557 break;
2558
Roland Levillaindff1f282014-11-05 14:15:05 +00002559 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002560 // Processing a Dex `double-to-long' instruction.
Alexandre Rames8158f282015-08-07 10:26:17 +01002561 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pD2l),
2562 conversion,
2563 conversion->GetDexPc(),
2564 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00002565 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002566 break;
2567
2568 default:
2569 LOG(FATAL) << "Unexpected type conversion from " << input_type
2570 << " to " << result_type;
2571 }
2572 break;
2573
Roland Levillain981e4542014-11-14 11:47:14 +00002574 case Primitive::kPrimChar:
2575 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002576 case Primitive::kPrimLong:
2577 // Type conversion from long to short is a result of code transformations.
2578 if (in.IsRegisterPair()) {
2579 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2580 } else if (in.IsDoubleStackSlot()) {
2581 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2582 } else {
2583 DCHECK(in.GetConstant()->IsLongConstant());
2584 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2585 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2586 }
2587 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002588 case Primitive::kPrimBoolean:
2589 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002590 case Primitive::kPrimByte:
2591 case Primitive::kPrimShort:
2592 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002593 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2594 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002595 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002596 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002597 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002598 } else {
2599 DCHECK(in.GetConstant()->IsIntConstant());
2600 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002601 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002602 }
2603 break;
2604
2605 default:
2606 LOG(FATAL) << "Unexpected type conversion from " << input_type
2607 << " to " << result_type;
2608 }
2609 break;
2610
Roland Levillaindff1f282014-11-05 14:15:05 +00002611 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002612 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002613 case Primitive::kPrimBoolean:
2614 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002615 case Primitive::kPrimByte:
2616 case Primitive::kPrimShort:
2617 case Primitive::kPrimInt:
2618 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002619 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002620 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002621 break;
2622
Roland Levillain6d0e4832014-11-27 18:31:21 +00002623 case Primitive::kPrimLong: {
2624 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002625 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002626
Roland Levillain232ade02015-04-20 15:14:36 +01002627 // Create stack space for the call to
2628 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2629 // TODO: enhance register allocator to ask for stack temporaries.
2630 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2631 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2632 __ subl(ESP, Immediate(adjustment));
2633 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002634
Roland Levillain232ade02015-04-20 15:14:36 +01002635 // Load the value to the FP stack, using temporaries if needed.
2636 PushOntoFPStack(in, 0, adjustment, false, true);
2637
2638 if (out.IsStackSlot()) {
2639 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2640 } else {
2641 __ fstps(Address(ESP, 0));
2642 Location stack_temp = Location::StackSlot(0);
2643 codegen_->Move32(out, stack_temp);
2644 }
2645
2646 // Remove the temporary stack space we allocated.
2647 if (adjustment != 0) {
2648 __ addl(ESP, Immediate(adjustment));
2649 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002650 break;
2651 }
2652
Roland Levillaincff13742014-11-17 14:32:17 +00002653 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002654 // Processing a Dex `double-to-float' instruction.
2655 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002656 break;
2657
2658 default:
2659 LOG(FATAL) << "Unexpected type conversion from " << input_type
2660 << " to " << result_type;
2661 };
2662 break;
2663
Roland Levillaindff1f282014-11-05 14:15:05 +00002664 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002665 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002666 case Primitive::kPrimBoolean:
2667 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002668 case Primitive::kPrimByte:
2669 case Primitive::kPrimShort:
2670 case Primitive::kPrimInt:
2671 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002672 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002673 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002674 break;
2675
Roland Levillain647b9ed2014-11-27 12:06:00 +00002676 case Primitive::kPrimLong: {
2677 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002678 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002679
Roland Levillain232ade02015-04-20 15:14:36 +01002680 // Create stack space for the call to
2681 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2682 // TODO: enhance register allocator to ask for stack temporaries.
2683 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2684 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2685 __ subl(ESP, Immediate(adjustment));
2686 }
2687
2688 // Load the value to the FP stack, using temporaries if needed.
2689 PushOntoFPStack(in, 0, adjustment, false, true);
2690
2691 if (out.IsDoubleStackSlot()) {
2692 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2693 } else {
2694 __ fstpl(Address(ESP, 0));
2695 Location stack_temp = Location::DoubleStackSlot(0);
2696 codegen_->Move64(out, stack_temp);
2697 }
2698
2699 // Remove the temporary stack space we allocated.
2700 if (adjustment != 0) {
2701 __ addl(ESP, Immediate(adjustment));
2702 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002703 break;
2704 }
2705
Roland Levillaincff13742014-11-17 14:32:17 +00002706 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002707 // Processing a Dex `float-to-double' instruction.
2708 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002709 break;
2710
2711 default:
2712 LOG(FATAL) << "Unexpected type conversion from " << input_type
2713 << " to " << result_type;
2714 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002715 break;
2716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 }
2721}
2722
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002723void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002724 LocationSummary* locations =
2725 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002726 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002727 case Primitive::kPrimInt: {
2728 locations->SetInAt(0, Location::RequiresRegister());
2729 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2730 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2731 break;
2732 }
2733
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002734 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002735 locations->SetInAt(0, Location::RequiresRegister());
2736 locations->SetInAt(1, Location::Any());
2737 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002738 break;
2739 }
2740
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002741 case Primitive::kPrimFloat:
2742 case Primitive::kPrimDouble: {
2743 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002744 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2745 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002746 } else if (add->InputAt(1)->IsConstant()) {
2747 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002748 } else {
2749 locations->SetInAt(1, Location::Any());
2750 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002751 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002752 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002753 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002754
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002755 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002756 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2757 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002758 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002759}
2760
2761void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2762 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002763 Location first = locations->InAt(0);
2764 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002765 Location out = locations->Out();
2766
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002767 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002768 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002769 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002770 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2771 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002772 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2773 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002774 } else {
2775 __ leal(out.AsRegister<Register>(), Address(
2776 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2777 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002778 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002779 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2780 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2781 __ addl(out.AsRegister<Register>(), Immediate(value));
2782 } else {
2783 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2784 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002785 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002786 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002787 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002788 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002789 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002790 }
2791
2792 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002793 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002794 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2795 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002796 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002797 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2798 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002799 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002800 } else {
2801 DCHECK(second.IsConstant()) << second;
2802 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2803 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2804 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002805 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002806 break;
2807 }
2808
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002809 case Primitive::kPrimFloat: {
2810 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002811 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002812 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2813 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002814 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002815 __ addss(first.AsFpuRegister<XmmRegister>(),
2816 codegen_->LiteralFloatAddress(
2817 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2818 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2819 } else {
2820 DCHECK(second.IsStackSlot());
2821 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002822 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002823 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002824 }
2825
2826 case Primitive::kPrimDouble: {
2827 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002828 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002829 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2830 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002831 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002832 __ addsd(first.AsFpuRegister<XmmRegister>(),
2833 codegen_->LiteralDoubleAddress(
2834 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2835 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2836 } else {
2837 DCHECK(second.IsDoubleStackSlot());
2838 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002839 }
2840 break;
2841 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002842
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002843 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002844 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002845 }
2846}
2847
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002848void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002849 LocationSummary* locations =
2850 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002851 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002852 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002853 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002854 locations->SetInAt(0, Location::RequiresRegister());
2855 locations->SetInAt(1, Location::Any());
2856 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002857 break;
2858 }
Calin Juravle11351682014-10-23 15:38:15 +01002859 case Primitive::kPrimFloat:
2860 case Primitive::kPrimDouble: {
2861 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002862 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2863 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002864 } else if (sub->InputAt(1)->IsConstant()) {
2865 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002866 } else {
2867 locations->SetInAt(1, Location::Any());
2868 }
Calin Juravle11351682014-10-23 15:38:15 +01002869 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002870 break;
Calin Juravle11351682014-10-23 15:38:15 +01002871 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002872
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002873 default:
Calin Juravle11351682014-10-23 15:38:15 +01002874 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002875 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002876}
2877
2878void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
2879 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002880 Location first = locations->InAt(0);
2881 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01002882 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002883 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002884 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002885 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002886 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002887 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00002888 __ subl(first.AsRegister<Register>(),
2889 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002890 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002891 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002892 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002893 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002894 }
2895
2896 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002897 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01002898 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2899 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002900 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01002901 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002902 __ sbbl(first.AsRegisterPairHigh<Register>(),
2903 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002904 } else {
2905 DCHECK(second.IsConstant()) << second;
2906 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2907 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2908 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002909 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002910 break;
2911 }
2912
Calin Juravle11351682014-10-23 15:38:15 +01002913 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002914 if (second.IsFpuRegister()) {
2915 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2916 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2917 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002918 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002919 __ subss(first.AsFpuRegister<XmmRegister>(),
2920 codegen_->LiteralFloatAddress(
2921 const_area->GetConstant()->AsFloatConstant()->GetValue(),
2922 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2923 } else {
2924 DCHECK(second.IsStackSlot());
2925 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2926 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002927 break;
Calin Juravle11351682014-10-23 15:38:15 +01002928 }
2929
2930 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04002931 if (second.IsFpuRegister()) {
2932 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
2933 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
2934 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002935 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002936 __ subsd(first.AsFpuRegister<XmmRegister>(),
2937 codegen_->LiteralDoubleAddress(
2938 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
2939 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
2940 } else {
2941 DCHECK(second.IsDoubleStackSlot());
2942 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
2943 }
Calin Juravle11351682014-10-23 15:38:15 +01002944 break;
2945 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002946
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002947 default:
Calin Juravle11351682014-10-23 15:38:15 +01002948 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01002949 }
2950}
2951
Calin Juravle34bacdf2014-10-07 20:23:36 +01002952void LocationsBuilderX86::VisitMul(HMul* mul) {
2953 LocationSummary* locations =
2954 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
2955 switch (mul->GetResultType()) {
2956 case Primitive::kPrimInt:
2957 locations->SetInAt(0, Location::RequiresRegister());
2958 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002959 if (mul->InputAt(1)->IsIntConstant()) {
2960 // Can use 3 operand multiply.
2961 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2962 } else {
2963 locations->SetOut(Location::SameAsFirstInput());
2964 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002965 break;
2966 case Primitive::kPrimLong: {
2967 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002968 locations->SetInAt(1, Location::Any());
2969 locations->SetOut(Location::SameAsFirstInput());
2970 // Needed for imul on 32bits with 64bits output.
2971 locations->AddTemp(Location::RegisterLocation(EAX));
2972 locations->AddTemp(Location::RegisterLocation(EDX));
2973 break;
2974 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002975 case Primitive::kPrimFloat:
2976 case Primitive::kPrimDouble: {
2977 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002978 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
2979 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002980 } else if (mul->InputAt(1)->IsConstant()) {
2981 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002982 } else {
2983 locations->SetInAt(1, Location::Any());
2984 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01002985 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01002986 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01002987 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01002988
2989 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01002990 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002991 }
2992}
2993
2994void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
2995 LocationSummary* locations = mul->GetLocations();
2996 Location first = locations->InAt(0);
2997 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04002998 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01002999
3000 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003001 case Primitive::kPrimInt:
3002 // The constant may have ended up in a register, so test explicitly to avoid
3003 // problems where the output may not be the same as the first operand.
3004 if (mul->InputAt(1)->IsIntConstant()) {
3005 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3006 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3007 } else if (second.IsRegister()) {
3008 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003009 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003010 } else {
3011 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003012 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003013 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003014 }
3015 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003016
3017 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003018 Register in1_hi = first.AsRegisterPairHigh<Register>();
3019 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003020 Register eax = locations->GetTemp(0).AsRegister<Register>();
3021 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003022
3023 DCHECK_EQ(EAX, eax);
3024 DCHECK_EQ(EDX, edx);
3025
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003026 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003027 // output: in1
3028 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3029 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3030 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003031 if (second.IsConstant()) {
3032 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003033
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003034 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3035 int32_t low_value = Low32Bits(value);
3036 int32_t high_value = High32Bits(value);
3037 Immediate low(low_value);
3038 Immediate high(high_value);
3039
3040 __ movl(eax, high);
3041 // eax <- in1.lo * in2.hi
3042 __ imull(eax, in1_lo);
3043 // in1.hi <- in1.hi * in2.lo
3044 __ imull(in1_hi, low);
3045 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3046 __ addl(in1_hi, eax);
3047 // move in2_lo to eax to prepare for double precision
3048 __ movl(eax, low);
3049 // edx:eax <- in1.lo * in2.lo
3050 __ mull(in1_lo);
3051 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3052 __ addl(in1_hi, edx);
3053 // in1.lo <- (in1.lo * in2.lo)[31:0];
3054 __ movl(in1_lo, eax);
3055 } else if (second.IsRegisterPair()) {
3056 Register in2_hi = second.AsRegisterPairHigh<Register>();
3057 Register in2_lo = second.AsRegisterPairLow<Register>();
3058
3059 __ movl(eax, in2_hi);
3060 // eax <- in1.lo * in2.hi
3061 __ imull(eax, in1_lo);
3062 // in1.hi <- in1.hi * in2.lo
3063 __ imull(in1_hi, in2_lo);
3064 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3065 __ addl(in1_hi, eax);
3066 // move in1_lo to eax to prepare for double precision
3067 __ movl(eax, in1_lo);
3068 // edx:eax <- in1.lo * in2.lo
3069 __ mull(in2_lo);
3070 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3071 __ addl(in1_hi, edx);
3072 // in1.lo <- (in1.lo * in2.lo)[31:0];
3073 __ movl(in1_lo, eax);
3074 } else {
3075 DCHECK(second.IsDoubleStackSlot()) << second;
3076 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3077 Address in2_lo(ESP, second.GetStackIndex());
3078
3079 __ movl(eax, in2_hi);
3080 // eax <- in1.lo * in2.hi
3081 __ imull(eax, in1_lo);
3082 // in1.hi <- in1.hi * in2.lo
3083 __ imull(in1_hi, in2_lo);
3084 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3085 __ addl(in1_hi, eax);
3086 // move in1_lo to eax to prepare for double precision
3087 __ movl(eax, in1_lo);
3088 // edx:eax <- in1.lo * in2.lo
3089 __ mull(in2_lo);
3090 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3091 __ addl(in1_hi, edx);
3092 // in1.lo <- (in1.lo * in2.lo)[31:0];
3093 __ movl(in1_lo, eax);
3094 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003095
3096 break;
3097 }
3098
Calin Juravleb5bfa962014-10-21 18:02:24 +01003099 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003100 DCHECK(first.Equals(locations->Out()));
3101 if (second.IsFpuRegister()) {
3102 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3103 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3104 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003105 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003106 __ mulss(first.AsFpuRegister<XmmRegister>(),
3107 codegen_->LiteralFloatAddress(
3108 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3109 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3110 } else {
3111 DCHECK(second.IsStackSlot());
3112 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3113 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003114 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003115 }
3116
3117 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003118 DCHECK(first.Equals(locations->Out()));
3119 if (second.IsFpuRegister()) {
3120 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3121 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3122 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003123 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003124 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3125 codegen_->LiteralDoubleAddress(
3126 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3127 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3128 } else {
3129 DCHECK(second.IsDoubleStackSlot());
3130 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3131 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003132 break;
3133 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134
3135 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003136 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003137 }
3138}
3139
Roland Levillain232ade02015-04-20 15:14:36 +01003140void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3141 uint32_t temp_offset,
3142 uint32_t stack_adjustment,
3143 bool is_fp,
3144 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003145 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003146 DCHECK(!is_wide);
3147 if (is_fp) {
3148 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3149 } else {
3150 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3151 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003152 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003153 DCHECK(is_wide);
3154 if (is_fp) {
3155 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3156 } else {
3157 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3158 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003159 } else {
3160 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003161 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003162 Location stack_temp = Location::StackSlot(temp_offset);
3163 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003164 if (is_fp) {
3165 __ flds(Address(ESP, temp_offset));
3166 } else {
3167 __ filds(Address(ESP, temp_offset));
3168 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003169 } else {
3170 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3171 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003172 if (is_fp) {
3173 __ fldl(Address(ESP, temp_offset));
3174 } else {
3175 __ fildl(Address(ESP, temp_offset));
3176 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003177 }
3178 }
3179}
3180
3181void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3182 Primitive::Type type = rem->GetResultType();
3183 bool is_float = type == Primitive::kPrimFloat;
3184 size_t elem_size = Primitive::ComponentSize(type);
3185 LocationSummary* locations = rem->GetLocations();
3186 Location first = locations->InAt(0);
3187 Location second = locations->InAt(1);
3188 Location out = locations->Out();
3189
3190 // Create stack space for 2 elements.
3191 // TODO: enhance register allocator to ask for stack temporaries.
3192 __ subl(ESP, Immediate(2 * elem_size));
3193
3194 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003195 const bool is_wide = !is_float;
3196 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3197 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003198
3199 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003200 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003201 __ Bind(&retry);
3202 __ fprem();
3203
3204 // Move FP status to AX.
3205 __ fstsw();
3206
3207 // And see if the argument reduction is complete. This is signaled by the
3208 // C2 FPU flag bit set to 0.
3209 __ andl(EAX, Immediate(kC2ConditionMask));
3210 __ j(kNotEqual, &retry);
3211
3212 // We have settled on the final value. Retrieve it into an XMM register.
3213 // Store FP top of stack to real stack.
3214 if (is_float) {
3215 __ fsts(Address(ESP, 0));
3216 } else {
3217 __ fstl(Address(ESP, 0));
3218 }
3219
3220 // Pop the 2 items from the FP stack.
3221 __ fucompp();
3222
3223 // Load the value from the stack into an XMM register.
3224 DCHECK(out.IsFpuRegister()) << out;
3225 if (is_float) {
3226 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3227 } else {
3228 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3229 }
3230
3231 // And remove the temporary stack space we allocated.
3232 __ addl(ESP, Immediate(2 * elem_size));
3233}
3234
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003235
3236void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3237 DCHECK(instruction->IsDiv() || instruction->IsRem());
3238
3239 LocationSummary* locations = instruction->GetLocations();
3240 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003241 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003242
3243 Register out_register = locations->Out().AsRegister<Register>();
3244 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003245 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003246
3247 DCHECK(imm == 1 || imm == -1);
3248
3249 if (instruction->IsRem()) {
3250 __ xorl(out_register, out_register);
3251 } else {
3252 __ movl(out_register, input_register);
3253 if (imm == -1) {
3254 __ negl(out_register);
3255 }
3256 }
3257}
3258
3259
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003260void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003261 LocationSummary* locations = instruction->GetLocations();
3262
3263 Register out_register = locations->Out().AsRegister<Register>();
3264 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003265 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003266 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3267 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003268
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003269 Register num = locations->GetTemp(0).AsRegister<Register>();
3270
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003271 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003272 __ testl(input_register, input_register);
3273 __ cmovl(kGreaterEqual, num, input_register);
3274 int shift = CTZ(imm);
3275 __ sarl(num, Immediate(shift));
3276
3277 if (imm < 0) {
3278 __ negl(num);
3279 }
3280
3281 __ movl(out_register, num);
3282}
3283
3284void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3285 DCHECK(instruction->IsDiv() || instruction->IsRem());
3286
3287 LocationSummary* locations = instruction->GetLocations();
3288 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3289
3290 Register eax = locations->InAt(0).AsRegister<Register>();
3291 Register out = locations->Out().AsRegister<Register>();
3292 Register num;
3293 Register edx;
3294
3295 if (instruction->IsDiv()) {
3296 edx = locations->GetTemp(0).AsRegister<Register>();
3297 num = locations->GetTemp(1).AsRegister<Register>();
3298 } else {
3299 edx = locations->Out().AsRegister<Register>();
3300 num = locations->GetTemp(0).AsRegister<Register>();
3301 }
3302
3303 DCHECK_EQ(EAX, eax);
3304 DCHECK_EQ(EDX, edx);
3305 if (instruction->IsDiv()) {
3306 DCHECK_EQ(EAX, out);
3307 } else {
3308 DCHECK_EQ(EDX, out);
3309 }
3310
3311 int64_t magic;
3312 int shift;
3313 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3314
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003315 // Save the numerator.
3316 __ movl(num, eax);
3317
3318 // EAX = magic
3319 __ movl(eax, Immediate(magic));
3320
3321 // EDX:EAX = magic * numerator
3322 __ imull(num);
3323
3324 if (imm > 0 && magic < 0) {
3325 // EDX += num
3326 __ addl(edx, num);
3327 } else if (imm < 0 && magic > 0) {
3328 __ subl(edx, num);
3329 }
3330
3331 // Shift if needed.
3332 if (shift != 0) {
3333 __ sarl(edx, Immediate(shift));
3334 }
3335
3336 // EDX += 1 if EDX < 0
3337 __ movl(eax, edx);
3338 __ shrl(edx, Immediate(31));
3339 __ addl(edx, eax);
3340
3341 if (instruction->IsRem()) {
3342 __ movl(eax, num);
3343 __ imull(edx, Immediate(imm));
3344 __ subl(eax, edx);
3345 __ movl(edx, eax);
3346 } else {
3347 __ movl(eax, edx);
3348 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003349}
3350
Calin Juravlebacfec32014-11-14 15:54:36 +00003351void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3352 DCHECK(instruction->IsDiv() || instruction->IsRem());
3353
3354 LocationSummary* locations = instruction->GetLocations();
3355 Location out = locations->Out();
3356 Location first = locations->InAt(0);
3357 Location second = locations->InAt(1);
3358 bool is_div = instruction->IsDiv();
3359
3360 switch (instruction->GetResultType()) {
3361 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003362 DCHECK_EQ(EAX, first.AsRegister<Register>());
3363 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003364
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003365 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003366 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003367
3368 if (imm == 0) {
3369 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3370 } else if (imm == 1 || imm == -1) {
3371 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003372 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003373 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003374 } else {
3375 DCHECK(imm <= -2 || imm >= 2);
3376 GenerateDivRemWithAnyConstant(instruction);
3377 }
3378 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003379 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3380 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003381 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003382
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003383 Register second_reg = second.AsRegister<Register>();
3384 // 0x80000000/-1 triggers an arithmetic exception!
3385 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3386 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003387
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003388 __ cmpl(second_reg, Immediate(-1));
3389 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003390
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003391 // edx:eax <- sign-extended of eax
3392 __ cdq();
3393 // eax = quotient, edx = remainder
3394 __ idivl(second_reg);
3395 __ Bind(slow_path->GetExitLabel());
3396 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003397 break;
3398 }
3399
3400 case Primitive::kPrimLong: {
3401 InvokeRuntimeCallingConvention calling_convention;
3402 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3403 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3404 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3405 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3406 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3407 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3408
3409 if (is_div) {
Alexandre Rames8158f282015-08-07 10:26:17 +01003410 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLdiv),
3411 instruction,
3412 instruction->GetDexPc(),
3413 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003414 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003415 } else {
Alexandre Rames8158f282015-08-07 10:26:17 +01003416 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pLmod),
3417 instruction,
3418 instruction->GetDexPc(),
3419 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00003420 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003421 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003422 break;
3423 }
3424
3425 default:
3426 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3427 }
3428}
3429
Calin Juravle7c4954d2014-10-28 16:57:40 +00003430void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003431 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003432 ? LocationSummary::kCall
3433 : LocationSummary::kNoCall;
3434 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3435
Calin Juravle7c4954d2014-10-28 16:57:40 +00003436 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003437 case Primitive::kPrimInt: {
3438 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003439 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003440 locations->SetOut(Location::SameAsFirstInput());
3441 // Intel uses edx:eax as the dividend.
3442 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003443 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3444 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3445 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003446 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003447 locations->AddTemp(Location::RequiresRegister());
3448 }
Calin Juravled0d48522014-11-04 16:40:20 +00003449 break;
3450 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003451 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003452 InvokeRuntimeCallingConvention calling_convention;
3453 locations->SetInAt(0, Location::RegisterPairLocation(
3454 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3455 locations->SetInAt(1, Location::RegisterPairLocation(
3456 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3457 // Runtime helper puts the result in EAX, EDX.
3458 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003459 break;
3460 }
3461 case Primitive::kPrimFloat:
3462 case Primitive::kPrimDouble: {
3463 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003464 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3465 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003466 } else if (div->InputAt(1)->IsConstant()) {
3467 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003468 } else {
3469 locations->SetInAt(1, Location::Any());
3470 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003471 locations->SetOut(Location::SameAsFirstInput());
3472 break;
3473 }
3474
3475 default:
3476 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3477 }
3478}
3479
3480void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3481 LocationSummary* locations = div->GetLocations();
3482 Location first = locations->InAt(0);
3483 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003484
3485 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003486 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003487 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003488 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003489 break;
3490 }
3491
3492 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003493 if (second.IsFpuRegister()) {
3494 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3495 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3496 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003497 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003498 __ divss(first.AsFpuRegister<XmmRegister>(),
3499 codegen_->LiteralFloatAddress(
3500 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3501 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3502 } else {
3503 DCHECK(second.IsStackSlot());
3504 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3505 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003506 break;
3507 }
3508
3509 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003510 if (second.IsFpuRegister()) {
3511 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3512 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3513 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003514 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003515 __ divsd(first.AsFpuRegister<XmmRegister>(),
3516 codegen_->LiteralDoubleAddress(
3517 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3518 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3519 } else {
3520 DCHECK(second.IsDoubleStackSlot());
3521 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3522 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003523 break;
3524 }
3525
3526 default:
3527 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3528 }
3529}
3530
Calin Juravlebacfec32014-11-14 15:54:36 +00003531void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003532 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003533
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003534 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
3535 ? LocationSummary::kCall
3536 : LocationSummary::kNoCall;
3537 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003538
Calin Juravled2ec87d2014-12-08 14:24:46 +00003539 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003540 case Primitive::kPrimInt: {
3541 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003542 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003543 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003544 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3545 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3546 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003547 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003548 locations->AddTemp(Location::RequiresRegister());
3549 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003550 break;
3551 }
3552 case Primitive::kPrimLong: {
3553 InvokeRuntimeCallingConvention calling_convention;
3554 locations->SetInAt(0, Location::RegisterPairLocation(
3555 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3556 locations->SetInAt(1, Location::RegisterPairLocation(
3557 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3558 // Runtime helper puts the result in EAX, EDX.
3559 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3560 break;
3561 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003562 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003563 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003564 locations->SetInAt(0, Location::Any());
3565 locations->SetInAt(1, Location::Any());
3566 locations->SetOut(Location::RequiresFpuRegister());
3567 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003568 break;
3569 }
3570
3571 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003572 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003573 }
3574}
3575
3576void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3577 Primitive::Type type = rem->GetResultType();
3578 switch (type) {
3579 case Primitive::kPrimInt:
3580 case Primitive::kPrimLong: {
3581 GenerateDivRemIntegral(rem);
3582 break;
3583 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003584 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003585 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003586 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003587 break;
3588 }
3589 default:
3590 LOG(FATAL) << "Unexpected rem type " << type;
3591 }
3592}
3593
Calin Juravled0d48522014-11-04 16:40:20 +00003594void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00003595 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
3596 ? LocationSummary::kCallOnSlowPath
3597 : LocationSummary::kNoCall;
3598 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003599 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003600 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003601 case Primitive::kPrimByte:
3602 case Primitive::kPrimChar:
3603 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003604 case Primitive::kPrimInt: {
3605 locations->SetInAt(0, Location::Any());
3606 break;
3607 }
3608 case Primitive::kPrimLong: {
3609 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3610 if (!instruction->IsConstant()) {
3611 locations->AddTemp(Location::RequiresRegister());
3612 }
3613 break;
3614 }
3615 default:
3616 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3617 }
Calin Juravled0d48522014-11-04 16:40:20 +00003618 if (instruction->HasUses()) {
3619 locations->SetOut(Location::SameAsFirstInput());
3620 }
3621}
3622
3623void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003624 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003625 codegen_->AddSlowPath(slow_path);
3626
3627 LocationSummary* locations = instruction->GetLocations();
3628 Location value = locations->InAt(0);
3629
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003630 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003631 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003632 case Primitive::kPrimByte:
3633 case Primitive::kPrimChar:
3634 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003635 case Primitive::kPrimInt: {
3636 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003637 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003638 __ j(kEqual, slow_path->GetEntryLabel());
3639 } else if (value.IsStackSlot()) {
3640 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3641 __ j(kEqual, slow_path->GetEntryLabel());
3642 } else {
3643 DCHECK(value.IsConstant()) << value;
3644 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3645 __ jmp(slow_path->GetEntryLabel());
3646 }
3647 }
3648 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003649 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003650 case Primitive::kPrimLong: {
3651 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003652 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003653 __ movl(temp, value.AsRegisterPairLow<Register>());
3654 __ orl(temp, value.AsRegisterPairHigh<Register>());
3655 __ j(kEqual, slow_path->GetEntryLabel());
3656 } else {
3657 DCHECK(value.IsConstant()) << value;
3658 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3659 __ jmp(slow_path->GetEntryLabel());
3660 }
3661 }
3662 break;
3663 }
3664 default:
3665 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003666 }
Calin Juravled0d48522014-11-04 16:40:20 +00003667}
3668
Calin Juravle9aec02f2014-11-18 23:06:35 +00003669void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3670 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3671
3672 LocationSummary* locations =
3673 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3674
3675 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003676 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003677 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003678 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003679 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003680 // The shift count needs to be in CL or a constant.
3681 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003682 locations->SetOut(Location::SameAsFirstInput());
3683 break;
3684 }
3685 default:
3686 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3687 }
3688}
3689
3690void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3691 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3692
3693 LocationSummary* locations = op->GetLocations();
3694 Location first = locations->InAt(0);
3695 Location second = locations->InAt(1);
3696 DCHECK(first.Equals(locations->Out()));
3697
3698 switch (op->GetResultType()) {
3699 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003700 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003701 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003702 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003703 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003704 DCHECK_EQ(ECX, second_reg);
3705 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003706 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003707 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003708 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003709 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003710 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003711 }
3712 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003713 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003714 if (shift == 0) {
3715 return;
3716 }
3717 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003718 if (op->IsShl()) {
3719 __ shll(first_reg, imm);
3720 } else if (op->IsShr()) {
3721 __ sarl(first_reg, imm);
3722 } else {
3723 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003724 }
3725 }
3726 break;
3727 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003728 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003729 if (second.IsRegister()) {
3730 Register second_reg = second.AsRegister<Register>();
3731 DCHECK_EQ(ECX, second_reg);
3732 if (op->IsShl()) {
3733 GenerateShlLong(first, second_reg);
3734 } else if (op->IsShr()) {
3735 GenerateShrLong(first, second_reg);
3736 } else {
3737 GenerateUShrLong(first, second_reg);
3738 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003739 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003740 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003741 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003742 // Nothing to do if the shift is 0, as the input is already the output.
3743 if (shift != 0) {
3744 if (op->IsShl()) {
3745 GenerateShlLong(first, shift);
3746 } else if (op->IsShr()) {
3747 GenerateShrLong(first, shift);
3748 } else {
3749 GenerateUShrLong(first, shift);
3750 }
3751 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003752 }
3753 break;
3754 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003755 default:
3756 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3757 }
3758}
3759
Mark P Mendell73945692015-04-29 14:56:17 +00003760void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3761 Register low = loc.AsRegisterPairLow<Register>();
3762 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003763 if (shift == 1) {
3764 // This is just an addition.
3765 __ addl(low, low);
3766 __ adcl(high, high);
3767 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003768 // Shift by 32 is easy. High gets low, and low gets 0.
3769 codegen_->EmitParallelMoves(
3770 loc.ToLow(),
3771 loc.ToHigh(),
3772 Primitive::kPrimInt,
3773 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3774 loc.ToLow(),
3775 Primitive::kPrimInt);
3776 } else if (shift > 32) {
3777 // Low part becomes 0. High part is low part << (shift-32).
3778 __ movl(high, low);
3779 __ shll(high, Immediate(shift - 32));
3780 __ xorl(low, low);
3781 } else {
3782 // Between 1 and 31.
3783 __ shld(high, low, Immediate(shift));
3784 __ shll(low, Immediate(shift));
3785 }
3786}
3787
Calin Juravle9aec02f2014-11-18 23:06:35 +00003788void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003789 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003790 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3791 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3792 __ testl(shifter, Immediate(32));
3793 __ j(kEqual, &done);
3794 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3795 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3796 __ Bind(&done);
3797}
3798
Mark P Mendell73945692015-04-29 14:56:17 +00003799void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3800 Register low = loc.AsRegisterPairLow<Register>();
3801 Register high = loc.AsRegisterPairHigh<Register>();
3802 if (shift == 32) {
3803 // Need to copy the sign.
3804 DCHECK_NE(low, high);
3805 __ movl(low, high);
3806 __ sarl(high, Immediate(31));
3807 } else if (shift > 32) {
3808 DCHECK_NE(low, high);
3809 // High part becomes sign. Low part is shifted by shift - 32.
3810 __ movl(low, high);
3811 __ sarl(high, Immediate(31));
3812 __ sarl(low, Immediate(shift - 32));
3813 } else {
3814 // Between 1 and 31.
3815 __ shrd(low, high, Immediate(shift));
3816 __ sarl(high, Immediate(shift));
3817 }
3818}
3819
Calin Juravle9aec02f2014-11-18 23:06:35 +00003820void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003821 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003822 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3823 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3824 __ testl(shifter, Immediate(32));
3825 __ j(kEqual, &done);
3826 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3827 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3828 __ Bind(&done);
3829}
3830
Mark P Mendell73945692015-04-29 14:56:17 +00003831void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
3832 Register low = loc.AsRegisterPairLow<Register>();
3833 Register high = loc.AsRegisterPairHigh<Register>();
3834 if (shift == 32) {
3835 // Shift by 32 is easy. Low gets high, and high gets 0.
3836 codegen_->EmitParallelMoves(
3837 loc.ToHigh(),
3838 loc.ToLow(),
3839 Primitive::kPrimInt,
3840 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3841 loc.ToHigh(),
3842 Primitive::kPrimInt);
3843 } else if (shift > 32) {
3844 // Low part is high >> (shift - 32). High part becomes 0.
3845 __ movl(low, high);
3846 __ shrl(low, Immediate(shift - 32));
3847 __ xorl(high, high);
3848 } else {
3849 // Between 1 and 31.
3850 __ shrd(low, high, Immediate(shift));
3851 __ shrl(high, Immediate(shift));
3852 }
3853}
3854
Calin Juravle9aec02f2014-11-18 23:06:35 +00003855void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003856 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003857 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3858 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
3859 __ testl(shifter, Immediate(32));
3860 __ j(kEqual, &done);
3861 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3862 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
3863 __ Bind(&done);
3864}
3865
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003866void LocationsBuilderX86::VisitRor(HRor* ror) {
3867 LocationSummary* locations =
3868 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
3869
3870 switch (ror->GetResultType()) {
3871 case Primitive::kPrimLong:
3872 // Add the temporary needed.
3873 locations->AddTemp(Location::RequiresRegister());
3874 FALLTHROUGH_INTENDED;
3875 case Primitive::kPrimInt:
3876 locations->SetInAt(0, Location::RequiresRegister());
3877 // The shift count needs to be in CL (unless it is a constant).
3878 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
3879 locations->SetOut(Location::SameAsFirstInput());
3880 break;
3881 default:
3882 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
3883 UNREACHABLE();
3884 }
3885}
3886
3887void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
3888 LocationSummary* locations = ror->GetLocations();
3889 Location first = locations->InAt(0);
3890 Location second = locations->InAt(1);
3891
3892 if (ror->GetResultType() == Primitive::kPrimInt) {
3893 Register first_reg = first.AsRegister<Register>();
3894 if (second.IsRegister()) {
3895 Register second_reg = second.AsRegister<Register>();
3896 __ rorl(first_reg, second_reg);
3897 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003898 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003899 __ rorl(first_reg, imm);
3900 }
3901 return;
3902 }
3903
3904 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
3905 Register first_reg_lo = first.AsRegisterPairLow<Register>();
3906 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
3907 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
3908 if (second.IsRegister()) {
3909 Register second_reg = second.AsRegister<Register>();
3910 DCHECK_EQ(second_reg, ECX);
3911 __ movl(temp_reg, first_reg_hi);
3912 __ shrd(first_reg_hi, first_reg_lo, second_reg);
3913 __ shrd(first_reg_lo, temp_reg, second_reg);
3914 __ movl(temp_reg, first_reg_hi);
3915 __ testl(second_reg, Immediate(32));
3916 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
3917 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
3918 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003919 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00003920 if (shift_amt == 0) {
3921 // Already fine.
3922 return;
3923 }
3924 if (shift_amt == 32) {
3925 // Just swap.
3926 __ movl(temp_reg, first_reg_lo);
3927 __ movl(first_reg_lo, first_reg_hi);
3928 __ movl(first_reg_hi, temp_reg);
3929 return;
3930 }
3931
3932 Immediate imm(shift_amt);
3933 // Save the constents of the low value.
3934 __ movl(temp_reg, first_reg_lo);
3935
3936 // Shift right into low, feeding bits from high.
3937 __ shrd(first_reg_lo, first_reg_hi, imm);
3938
3939 // Shift right into high, feeding bits from the original low.
3940 __ shrd(first_reg_hi, temp_reg, imm);
3941
3942 // Swap if needed.
3943 if (shift_amt > 32) {
3944 __ movl(temp_reg, first_reg_lo);
3945 __ movl(first_reg_lo, first_reg_hi);
3946 __ movl(first_reg_hi, temp_reg);
3947 }
3948 }
3949}
3950
Calin Juravle9aec02f2014-11-18 23:06:35 +00003951void LocationsBuilderX86::VisitShl(HShl* shl) {
3952 HandleShift(shl);
3953}
3954
3955void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
3956 HandleShift(shl);
3957}
3958
3959void LocationsBuilderX86::VisitShr(HShr* shr) {
3960 HandleShift(shr);
3961}
3962
3963void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
3964 HandleShift(shr);
3965}
3966
3967void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
3968 HandleShift(ushr);
3969}
3970
3971void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
3972 HandleShift(ushr);
3973}
3974
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003975void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003976 LocationSummary* locations =
3977 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003978 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00003979 if (instruction->IsStringAlloc()) {
3980 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
3981 } else {
3982 InvokeRuntimeCallingConvention calling_convention;
3983 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3984 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3985 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01003986}
3987
3988void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01003989 // Note: if heap poisoning is enabled, the entry point takes cares
3990 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00003991 if (instruction->IsStringAlloc()) {
3992 // String is allocated through StringFactory. Call NewEmptyString entry point.
3993 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
3994 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize);
3995 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
3996 __ call(Address(temp, code_offset.Int32Value()));
3997 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
3998 } else {
3999 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4000 instruction,
4001 instruction->GetDexPc(),
4002 nullptr);
4003 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4004 DCHECK(!codegen_->IsLeafMethod());
4005 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004006}
4007
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004008void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4009 LocationSummary* locations =
4010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
4011 locations->SetOut(Location::RegisterLocation(EAX));
4012 InvokeRuntimeCallingConvention calling_convention;
4013 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004014 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004015 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004016}
4017
4018void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4019 InvokeRuntimeCallingConvention calling_convention;
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004020 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex()));
Roland Levillain4d027112015-07-01 15:41:14 +01004021 // Note: if heap poisoning is enabled, the entry point takes cares
4022 // of poisoning the reference.
Calin Juravle175dc732015-08-25 15:42:32 +01004023 codegen_->InvokeRuntime(instruction->GetEntrypoint(),
4024 instruction,
4025 instruction->GetDexPc(),
4026 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00004027 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004028 DCHECK(!codegen_->IsLeafMethod());
4029}
4030
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004031void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004032 LocationSummary* locations =
4033 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004034 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4035 if (location.IsStackSlot()) {
4036 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4037 } else if (location.IsDoubleStackSlot()) {
4038 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004039 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004040 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004041}
4042
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004043void InstructionCodeGeneratorX86::VisitParameterValue(
4044 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4045}
4046
4047void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4048 LocationSummary* locations =
4049 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4050 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4051}
4052
4053void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004054}
4055
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004056void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4057 LocationSummary* locations =
4058 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4059 locations->SetInAt(0, Location::RequiresRegister());
4060 locations->SetOut(Location::RequiresRegister());
4061}
4062
4063void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4064 LocationSummary* locations = instruction->GetLocations();
4065 uint32_t method_offset = 0;
Vladimir Markoa1de9182016-02-25 11:37:38 +00004066 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004067 method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4068 instruction->GetIndex(), kX86PointerSize).SizeValue();
4069 } else {
Nelli Kimbadee982016-05-13 13:08:53 +03004070 __ movl(locations->InAt(0).AsRegister<Register>(),
4071 Address(locations->InAt(0).AsRegister<Register>(),
4072 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4073 // temp = temp->GetImtEntryAt(method_offset);
4074 method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity50706432016-06-14 11:31:04 -07004075 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004076 }
4077 __ movl(locations->Out().AsRegister<Register>(),
4078 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
4079}
4080
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004081void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004082 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004083 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004084 locations->SetInAt(0, Location::RequiresRegister());
4085 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004086}
4087
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004088void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4089 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004090 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004091 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004092 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004093 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004094 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004095 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004096 break;
4097
4098 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004099 __ notl(out.AsRegisterPairLow<Register>());
4100 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004101 break;
4102
4103 default:
4104 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4105 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004106}
4107
David Brazdil66d126e2015-04-03 16:02:44 +01004108void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4109 LocationSummary* locations =
4110 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4111 locations->SetInAt(0, Location::RequiresRegister());
4112 locations->SetOut(Location::SameAsFirstInput());
4113}
4114
4115void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004116 LocationSummary* locations = bool_not->GetLocations();
4117 Location in = locations->InAt(0);
4118 Location out = locations->Out();
4119 DCHECK(in.Equals(out));
4120 __ xorl(out.AsRegister<Register>(), Immediate(1));
4121}
4122
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004123void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004124 LocationSummary* locations =
4125 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004126 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004127 case Primitive::kPrimBoolean:
4128 case Primitive::kPrimByte:
4129 case Primitive::kPrimShort:
4130 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004131 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004132 case Primitive::kPrimLong: {
4133 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004134 locations->SetInAt(1, Location::Any());
4135 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4136 break;
4137 }
4138 case Primitive::kPrimFloat:
4139 case Primitive::kPrimDouble: {
4140 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004141 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4142 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4143 } else if (compare->InputAt(1)->IsConstant()) {
4144 locations->SetInAt(1, Location::RequiresFpuRegister());
4145 } else {
4146 locations->SetInAt(1, Location::Any());
4147 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004148 locations->SetOut(Location::RequiresRegister());
4149 break;
4150 }
4151 default:
4152 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4153 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004154}
4155
4156void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004157 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004158 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004159 Location left = locations->InAt(0);
4160 Location right = locations->InAt(1);
4161
Mark Mendell0c9497d2015-08-21 09:30:05 -04004162 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004163 Condition less_cond = kLess;
4164
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004165 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004166 case Primitive::kPrimBoolean:
4167 case Primitive::kPrimByte:
4168 case Primitive::kPrimShort:
4169 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004170 case Primitive::kPrimInt: {
Mark Mendell0c5b18e2016-02-06 13:58:35 -05004171 GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004172 break;
4173 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004174 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004175 Register left_low = left.AsRegisterPairLow<Register>();
4176 Register left_high = left.AsRegisterPairHigh<Register>();
4177 int32_t val_low = 0;
4178 int32_t val_high = 0;
4179 bool right_is_const = false;
4180
4181 if (right.IsConstant()) {
4182 DCHECK(right.GetConstant()->IsLongConstant());
4183 right_is_const = true;
4184 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4185 val_low = Low32Bits(val);
4186 val_high = High32Bits(val);
4187 }
4188
Calin Juravleddb7df22014-11-25 20:56:51 +00004189 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004190 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004191 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004192 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004193 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004194 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004195 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004196 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004197 __ j(kLess, &less); // Signed compare.
4198 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004199 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004200 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004201 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004202 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004203 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004204 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004205 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004206 }
Aart Bika19616e2016-02-01 18:57:58 -08004207 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004208 break;
4209 }
4210 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004211 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004212 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004213 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004214 break;
4215 }
4216 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004217 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004218 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004219 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004220 break;
4221 }
4222 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004223 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004224 }
Aart Bika19616e2016-02-01 18:57:58 -08004225
Calin Juravleddb7df22014-11-25 20:56:51 +00004226 __ movl(out, Immediate(0));
4227 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004228 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004229
4230 __ Bind(&greater);
4231 __ movl(out, Immediate(1));
4232 __ jmp(&done);
4233
4234 __ Bind(&less);
4235 __ movl(out, Immediate(-1));
4236
4237 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004238}
4239
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004240void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004241 LocationSummary* locations =
4242 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004243 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004244 locations->SetInAt(i, Location::Any());
4245 }
4246 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004247}
4248
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004249void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004250 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004251}
4252
Roland Levillain7c1559a2015-12-15 10:55:36 +00004253void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004254 /*
4255 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4256 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4257 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4258 */
4259 switch (kind) {
4260 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004261 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004262 break;
4263 }
4264 case MemBarrierKind::kAnyStore:
4265 case MemBarrierKind::kLoadAny:
4266 case MemBarrierKind::kStoreStore: {
4267 // nop
4268 break;
4269 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004270 case MemBarrierKind::kNTStoreStore:
4271 // Non-Temporal Store/Store needs an explicit fence.
4272 MemoryFence(/* non-temporal */ true);
4273 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004274 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004275}
4276
Vladimir Markodc151b22015-10-15 18:02:30 +01004277HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4278 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
4279 MethodReference target_method ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004280 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4281
4282 // We disable pc-relative load when there is an irreducible loop, as the optimization
4283 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004284 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4285 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004286 if (GetGraph()->HasIrreducibleLoops() &&
4287 (dispatch_info.method_load_kind ==
4288 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4289 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4290 }
4291 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004292 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4293 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4294 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4295 // (Though the direct CALL ptr16:32 is available for consideration).
4296 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004297 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004298 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004299 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004300 0u
4301 };
4302 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004303 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004304 }
4305}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004306
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004307Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4308 Register temp) {
4309 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004310 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004311 if (!invoke->GetLocations()->Intrinsified()) {
4312 return location.AsRegister<Register>();
4313 }
4314 // For intrinsics we allow any location, so it may be on the stack.
4315 if (!location.IsRegister()) {
4316 __ movl(temp, Address(ESP, location.GetStackIndex()));
4317 return temp;
4318 }
4319 // For register locations, check if the register was saved. If so, get it from the stack.
4320 // Note: There is a chance that the register was saved but not overwritten, so we could
4321 // save one load. However, since this is just an intrinsic slow path we prefer this
4322 // simple and more robust approach rather that trying to determine if that's the case.
4323 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004324 if (slow_path != nullptr) {
4325 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4326 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4327 __ movl(temp, Address(ESP, stack_offset));
4328 return temp;
4329 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004330 }
4331 return location.AsRegister<Register>();
4332}
4333
Serguei Katkov288c7a82016-05-16 11:53:15 +06004334Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4335 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004336 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4337 switch (invoke->GetMethodLoadKind()) {
4338 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit:
4339 // temp = thread->string_init_entrypoint
4340 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(invoke->GetStringInitOffset()));
4341 break;
4342 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004343 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004344 break;
4345 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4346 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4347 break;
4348 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004349 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Marko58155012015-08-19 12:49:41 +00004350 method_patches_.emplace_back(invoke->GetTargetMethod());
4351 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4352 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004353 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4354 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4355 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004356 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004357 // Bind a new fixup label at the end of the "movl" insn.
4358 uint32_t offset = invoke->GetDexCacheArrayOffset();
4359 __ Bind(NewPcRelativeDexCacheArrayPatch(*invoke->GetTargetMethod().dex_file, offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004360 break;
4361 }
Vladimir Marko58155012015-08-19 12:49:41 +00004362 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004363 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004364 Register method_reg;
4365 Register reg = temp.AsRegister<Register>();
4366 if (current_method.IsRegister()) {
4367 method_reg = current_method.AsRegister<Register>();
4368 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004369 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004370 DCHECK(!current_method.IsValid());
4371 method_reg = reg;
4372 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4373 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004374 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004375 __ movl(reg, Address(method_reg,
4376 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004377 // temp = temp[index_in_cache];
4378 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4379 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004380 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4381 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004382 }
Vladimir Marko58155012015-08-19 12:49:41 +00004383 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004384 return callee_method;
4385}
4386
4387void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4388 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004389
4390 switch (invoke->GetCodePtrLocation()) {
4391 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4392 __ call(GetFrameEntryLabel());
4393 break;
4394 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
4395 relative_call_patches_.emplace_back(invoke->GetTargetMethod());
4396 Label* label = &relative_call_patches_.back().label;
4397 __ call(label); // Bind to the patch label, override at link time.
4398 __ Bind(label); // Bind the label at the end of the "call" insn.
4399 break;
4400 }
4401 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4402 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004403 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4404 LOG(FATAL) << "Unsupported";
4405 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004406 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4407 // (callee_method + offset_of_quick_compiled_code)()
4408 __ call(Address(callee_method.AsRegister<Register>(),
4409 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
4410 kX86WordSize).Int32Value()));
4411 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004412 }
4413
4414 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004415}
4416
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004417void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4418 Register temp = temp_in.AsRegister<Register>();
4419 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4420 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004421
4422 // Use the calling convention instead of the location of the receiver, as
4423 // intrinsics may have put the receiver in a different register. In the intrinsics
4424 // slow path, the arguments have been moved to the right place, so here we are
4425 // guaranteed that the receiver is the first register of the calling convention.
4426 InvokeDexCallingConvention calling_convention;
4427 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004428 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004429 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004430 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004431 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004432 // Instead of simply (possibly) unpoisoning `temp` here, we should
4433 // emit a read barrier for the previous class reference load.
4434 // However this is not required in practice, as this is an
4435 // intermediate/temporary reference and because the current
4436 // concurrent copying collector keeps the from-space memory
4437 // intact/accessible until the end of the marking phase (the
4438 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004439 __ MaybeUnpoisonHeapReference(temp);
4440 // temp = temp->GetMethodAt(method_offset);
4441 __ movl(temp, Address(temp, method_offset));
4442 // call temp->GetEntryPoint();
4443 __ call(Address(
4444 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86WordSize).Int32Value()));
4445}
4446
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004447void CodeGeneratorX86::RecordSimplePatch() {
4448 if (GetCompilerOptions().GetIncludePatchInformation()) {
4449 simple_patches_.emplace_back();
4450 __ Bind(&simple_patches_.back());
4451 }
4452}
4453
4454void CodeGeneratorX86::RecordStringPatch(HLoadString* load_string) {
4455 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex());
4456 __ Bind(&string_patches_.back().label);
4457}
4458
4459Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4460 uint32_t element_offset) {
4461 // Add the patch entry and bind its label at the end of the instruction.
4462 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4463 return &pc_relative_dex_cache_patches_.back().label;
4464}
4465
Vladimir Marko58155012015-08-19 12:49:41 +00004466void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4467 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004468 size_t size =
4469 method_patches_.size() +
4470 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004471 pc_relative_dex_cache_patches_.size() +
4472 simple_patches_.size() +
4473 string_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004474 linker_patches->reserve(size);
4475 // The label points to the end of the "movl" insn but the literal offset for method
4476 // patch needs to point to the embedded constant which occupies the last 4 bytes.
4477 constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
Vladimir Marko58155012015-08-19 12:49:41 +00004478 for (const MethodPatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004479 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004480 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset,
4481 info.target_method.dex_file,
4482 info.target_method.dex_method_index));
4483 }
4484 for (const MethodPatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004485 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Marko58155012015-08-19 12:49:41 +00004486 linker_patches->push_back(LinkerPatch::RelativeCodePatch(literal_offset,
4487 info.target_method.dex_file,
4488 info.target_method.dex_method_index));
4489 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004490 for (const PcRelativeDexCacheAccessInfo& info : pc_relative_dex_cache_patches_) {
4491 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4492 linker_patches->push_back(LinkerPatch::DexCacheArrayPatch(literal_offset,
4493 &info.target_dex_file,
4494 GetMethodAddressOffset(),
4495 info.element_offset));
4496 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004497 for (const Label& label : simple_patches_) {
4498 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4499 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4500 }
4501 if (GetCompilerOptions().GetCompilePic()) {
4502 for (const StringPatchInfo<Label>& info : string_patches_) {
4503 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4504 linker_patches->push_back(LinkerPatch::RelativeStringPatch(literal_offset,
4505 &info.dex_file,
4506 GetMethodAddressOffset(),
4507 info.string_index));
4508 }
4509 } else {
4510 for (const StringPatchInfo<Label>& info : string_patches_) {
4511 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4512 linker_patches->push_back(LinkerPatch::StringPatch(literal_offset,
4513 &info.dex_file,
4514 info.string_index));
4515 }
4516 }
Vladimir Marko58155012015-08-19 12:49:41 +00004517}
4518
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004519void CodeGeneratorX86::MarkGCCard(Register temp,
4520 Register card,
4521 Register object,
4522 Register value,
4523 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004524 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004525 if (value_can_be_null) {
4526 __ testl(value, value);
4527 __ j(kEqual, &is_null);
4528 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004529 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86WordSize>().Int32Value()));
4530 __ movl(temp, object);
4531 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004532 __ movb(Address(temp, card, TIMES_1, 0),
4533 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004534 if (value_can_be_null) {
4535 __ Bind(&is_null);
4536 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004537}
4538
Calin Juravle52c48962014-12-16 17:02:57 +00004539void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4540 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004541
4542 bool object_field_get_with_read_barrier =
4543 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004544 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004545 new (GetGraph()->GetArena()) LocationSummary(instruction,
4546 kEmitCompilerReadBarrier ?
4547 LocationSummary::kCallOnSlowPath :
4548 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004549 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004550
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004551 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4552 locations->SetOut(Location::RequiresFpuRegister());
4553 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004554 // The output overlaps in case of long: we don't want the low move
4555 // to overwrite the object's location. Likewise, in the case of
4556 // an object field get with read barriers enabled, we do not want
4557 // the move to overwrite the object's location, as we need it to emit
4558 // the read barrier.
4559 locations->SetOut(
4560 Location::RequiresRegister(),
4561 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4562 Location::kOutputOverlap :
4563 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004564 }
Calin Juravle52c48962014-12-16 17:02:57 +00004565
4566 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4567 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004568 // So we use an XMM register as a temp to achieve atomicity (first
4569 // load the temp into the XMM and then copy the XMM into the
4570 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004571 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain7c1559a2015-12-15 10:55:36 +00004572 } else if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
4573 // We need a temporary register for the read barrier marking slow
4574 // path in CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier.
4575 locations->AddTemp(Location::RequiresRegister());
Calin Juravle52c48962014-12-16 17:02:57 +00004576 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004577}
4578
Calin Juravle52c48962014-12-16 17:02:57 +00004579void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4580 const FieldInfo& field_info) {
4581 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004582
Calin Juravle52c48962014-12-16 17:02:57 +00004583 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004584 Location base_loc = locations->InAt(0);
4585 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004586 Location out = locations->Out();
4587 bool is_volatile = field_info.IsVolatile();
4588 Primitive::Type field_type = field_info.GetFieldType();
4589 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4590
4591 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004592 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004593 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004594 break;
4595 }
4596
4597 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004598 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004599 break;
4600 }
4601
4602 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004603 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004604 break;
4605 }
4606
4607 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004608 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004609 break;
4610 }
4611
4612 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004613 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004614 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004615
4616 case Primitive::kPrimNot: {
4617 // /* HeapReference<Object> */ out = *(base + offset)
4618 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
4619 Location temp_loc = locations->GetTemp(0);
4620 // Note that a potential implicit null check is handled in this
4621 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4622 codegen_->GenerateFieldLoadWithBakerReadBarrier(
4623 instruction, out, base, offset, temp_loc, /* needs_null_check */ true);
4624 if (is_volatile) {
4625 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4626 }
4627 } else {
4628 __ movl(out.AsRegister<Register>(), Address(base, offset));
4629 codegen_->MaybeRecordImplicitNullCheck(instruction);
4630 if (is_volatile) {
4631 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4632 }
4633 // If read barriers are enabled, emit read barriers other than
4634 // Baker's using a slow path (and also unpoison the loaded
4635 // reference, if heap poisoning is enabled).
4636 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4637 }
4638 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004639 }
4640
4641 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004642 if (is_volatile) {
4643 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4644 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004645 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004646 __ movd(out.AsRegisterPairLow<Register>(), temp);
4647 __ psrlq(temp, Immediate(32));
4648 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4649 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004650 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004651 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004652 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004653 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4654 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004655 break;
4656 }
4657
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004658 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004659 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004660 break;
4661 }
4662
4663 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004664 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004665 break;
4666 }
4667
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004668 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004669 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004670 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004671 }
Calin Juravle52c48962014-12-16 17:02:57 +00004672
Roland Levillain7c1559a2015-12-15 10:55:36 +00004673 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4674 // Potential implicit null checks, in the case of reference or
4675 // long fields, are handled in the previous switch statement.
4676 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004677 codegen_->MaybeRecordImplicitNullCheck(instruction);
4678 }
4679
Calin Juravle52c48962014-12-16 17:02:57 +00004680 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004681 if (field_type == Primitive::kPrimNot) {
4682 // Memory barriers, in the case of references, are also handled
4683 // in the previous switch statement.
4684 } else {
4685 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4686 }
Roland Levillain4d027112015-07-01 15:41:14 +01004687 }
Calin Juravle52c48962014-12-16 17:02:57 +00004688}
4689
4690void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4691 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4692
4693 LocationSummary* locations =
4694 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4695 locations->SetInAt(0, Location::RequiresRegister());
4696 bool is_volatile = field_info.IsVolatile();
4697 Primitive::Type field_type = field_info.GetFieldType();
4698 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4699 || (field_type == Primitive::kPrimByte);
4700
4701 // The register allocator does not support multiple
4702 // inputs that die at entry with one in a specific register.
4703 if (is_byte_type) {
4704 // Ensure the value is in a byte register.
4705 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004706 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004707 if (is_volatile && field_type == Primitive::kPrimDouble) {
4708 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4709 locations->SetInAt(1, Location::RequiresFpuRegister());
4710 } else {
4711 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4712 }
4713 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4714 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004715 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004716
Calin Juravle52c48962014-12-16 17:02:57 +00004717 // 64bits value can be atomically written to an address with movsd and an XMM register.
4718 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4719 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4720 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4721 // isolated cases when we need this it isn't worth adding the extra complexity.
4722 locations->AddTemp(Location::RequiresFpuRegister());
4723 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004724 } else {
4725 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4726
4727 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4728 // Temporary registers for the write barrier.
4729 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4730 // Ensure the card is in a byte register.
4731 locations->AddTemp(Location::RegisterLocation(ECX));
4732 }
Calin Juravle52c48962014-12-16 17:02:57 +00004733 }
4734}
4735
4736void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004737 const FieldInfo& field_info,
4738 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004739 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4740
4741 LocationSummary* locations = instruction->GetLocations();
4742 Register base = locations->InAt(0).AsRegister<Register>();
4743 Location value = locations->InAt(1);
4744 bool is_volatile = field_info.IsVolatile();
4745 Primitive::Type field_type = field_info.GetFieldType();
4746 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004747 bool needs_write_barrier =
4748 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004749
4750 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004751 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004752 }
4753
Mark Mendell81489372015-11-04 11:30:41 -05004754 bool maybe_record_implicit_null_check_done = false;
4755
Calin Juravle52c48962014-12-16 17:02:57 +00004756 switch (field_type) {
4757 case Primitive::kPrimBoolean:
4758 case Primitive::kPrimByte: {
4759 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4760 break;
4761 }
4762
4763 case Primitive::kPrimShort:
4764 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004765 if (value.IsConstant()) {
4766 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4767 __ movw(Address(base, offset), Immediate(v));
4768 } else {
4769 __ movw(Address(base, offset), value.AsRegister<Register>());
4770 }
Calin Juravle52c48962014-12-16 17:02:57 +00004771 break;
4772 }
4773
4774 case Primitive::kPrimInt:
4775 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004776 if (kPoisonHeapReferences && needs_write_barrier) {
4777 // Note that in the case where `value` is a null reference,
4778 // we do not enter this block, as the reference does not
4779 // need poisoning.
4780 DCHECK_EQ(field_type, Primitive::kPrimNot);
4781 Register temp = locations->GetTemp(0).AsRegister<Register>();
4782 __ movl(temp, value.AsRegister<Register>());
4783 __ PoisonHeapReference(temp);
4784 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004785 } else if (value.IsConstant()) {
4786 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4787 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004788 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004789 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004790 __ movl(Address(base, offset), value.AsRegister<Register>());
4791 }
Calin Juravle52c48962014-12-16 17:02:57 +00004792 break;
4793 }
4794
4795 case Primitive::kPrimLong: {
4796 if (is_volatile) {
4797 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4798 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4799 __ movd(temp1, value.AsRegisterPairLow<Register>());
4800 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4801 __ punpckldq(temp1, temp2);
4802 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004803 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004804 } else if (value.IsConstant()) {
4805 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4806 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4807 codegen_->MaybeRecordImplicitNullCheck(instruction);
4808 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004809 } else {
4810 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004811 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004812 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
4813 }
Mark Mendell81489372015-11-04 11:30:41 -05004814 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00004815 break;
4816 }
4817
4818 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05004819 if (value.IsConstant()) {
4820 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4821 __ movl(Address(base, offset), Immediate(v));
4822 } else {
4823 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4824 }
Calin Juravle52c48962014-12-16 17:02:57 +00004825 break;
4826 }
4827
4828 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05004829 if (value.IsConstant()) {
4830 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4831 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4832 codegen_->MaybeRecordImplicitNullCheck(instruction);
4833 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
4834 maybe_record_implicit_null_check_done = true;
4835 } else {
4836 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
4837 }
Calin Juravle52c48962014-12-16 17:02:57 +00004838 break;
4839 }
4840
4841 case Primitive::kPrimVoid:
4842 LOG(FATAL) << "Unreachable type " << field_type;
4843 UNREACHABLE();
4844 }
4845
Mark Mendell81489372015-11-04 11:30:41 -05004846 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004847 codegen_->MaybeRecordImplicitNullCheck(instruction);
4848 }
4849
Roland Levillain4d027112015-07-01 15:41:14 +01004850 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004851 Register temp = locations->GetTemp(0).AsRegister<Register>();
4852 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004853 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00004854 }
4855
Calin Juravle52c48962014-12-16 17:02:57 +00004856 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004857 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00004858 }
4859}
4860
4861void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4862 HandleFieldGet(instruction, instruction->GetFieldInfo());
4863}
4864
4865void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
4866 HandleFieldGet(instruction, instruction->GetFieldInfo());
4867}
4868
4869void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
4870 HandleFieldSet(instruction, instruction->GetFieldInfo());
4871}
4872
4873void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004874 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004875}
4876
4877void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
4878 HandleFieldSet(instruction, instruction->GetFieldInfo());
4879}
4880
4881void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004882 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00004883}
4884
4885void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4886 HandleFieldGet(instruction, instruction->GetFieldInfo());
4887}
4888
4889void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
4890 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004891}
4892
Calin Juravlee460d1d2015-09-29 04:52:17 +01004893void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
4894 HUnresolvedInstanceFieldGet* instruction) {
4895 FieldAccessCallingConventionX86 calling_convention;
4896 codegen_->CreateUnresolvedFieldLocationSummary(
4897 instruction, instruction->GetFieldType(), calling_convention);
4898}
4899
4900void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
4901 HUnresolvedInstanceFieldGet* instruction) {
4902 FieldAccessCallingConventionX86 calling_convention;
4903 codegen_->GenerateUnresolvedFieldAccess(instruction,
4904 instruction->GetFieldType(),
4905 instruction->GetFieldIndex(),
4906 instruction->GetDexPc(),
4907 calling_convention);
4908}
4909
4910void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
4911 HUnresolvedInstanceFieldSet* instruction) {
4912 FieldAccessCallingConventionX86 calling_convention;
4913 codegen_->CreateUnresolvedFieldLocationSummary(
4914 instruction, instruction->GetFieldType(), calling_convention);
4915}
4916
4917void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
4918 HUnresolvedInstanceFieldSet* instruction) {
4919 FieldAccessCallingConventionX86 calling_convention;
4920 codegen_->GenerateUnresolvedFieldAccess(instruction,
4921 instruction->GetFieldType(),
4922 instruction->GetFieldIndex(),
4923 instruction->GetDexPc(),
4924 calling_convention);
4925}
4926
4927void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
4928 HUnresolvedStaticFieldGet* instruction) {
4929 FieldAccessCallingConventionX86 calling_convention;
4930 codegen_->CreateUnresolvedFieldLocationSummary(
4931 instruction, instruction->GetFieldType(), calling_convention);
4932}
4933
4934void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
4935 HUnresolvedStaticFieldGet* instruction) {
4936 FieldAccessCallingConventionX86 calling_convention;
4937 codegen_->GenerateUnresolvedFieldAccess(instruction,
4938 instruction->GetFieldType(),
4939 instruction->GetFieldIndex(),
4940 instruction->GetDexPc(),
4941 calling_convention);
4942}
4943
4944void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
4945 HUnresolvedStaticFieldSet* instruction) {
4946 FieldAccessCallingConventionX86 calling_convention;
4947 codegen_->CreateUnresolvedFieldLocationSummary(
4948 instruction, instruction->GetFieldType(), calling_convention);
4949}
4950
4951void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
4952 HUnresolvedStaticFieldSet* instruction) {
4953 FieldAccessCallingConventionX86 calling_convention;
4954 codegen_->GenerateUnresolvedFieldAccess(instruction,
4955 instruction->GetFieldType(),
4956 instruction->GetFieldIndex(),
4957 instruction->GetDexPc(),
4958 calling_convention);
4959}
4960
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004961void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00004962 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
4963 ? LocationSummary::kCallOnSlowPath
4964 : LocationSummary::kNoCall;
4965 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
4966 Location loc = codegen_->IsImplicitNullCheckAllowed(instruction)
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004967 ? Location::RequiresRegister()
4968 : Location::Any();
4969 locations->SetInAt(0, loc);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004970 if (instruction->HasUses()) {
4971 locations->SetOut(Location::SameAsFirstInput());
4972 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004973}
4974
Calin Juravle2ae48182016-03-16 14:05:09 +00004975void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
4976 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00004977 return;
4978 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004979 LocationSummary* locations = instruction->GetLocations();
4980 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00004981
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004982 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00004983 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00004984}
4985
Calin Juravle2ae48182016-03-16 14:05:09 +00004986void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07004987 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00004988 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004989
4990 LocationSummary* locations = instruction->GetLocations();
4991 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004992
4993 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04004994 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004995 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004996 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01004997 } else {
4998 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00004999 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005000 __ jmp(slow_path->GetEntryLabel());
5001 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005002 }
5003 __ j(kEqual, slow_path->GetEntryLabel());
5004}
5005
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005006void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005007 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005008}
5009
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005010void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005011 bool object_array_get_with_read_barrier =
5012 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005013 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005014 new (GetGraph()->GetArena()) LocationSummary(instruction,
5015 object_array_get_with_read_barrier ?
5016 LocationSummary::kCallOnSlowPath :
5017 LocationSummary::kNoCall);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005018 locations->SetInAt(0, Location::RequiresRegister());
5019 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005020 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5021 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5022 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005023 // The output overlaps in case of long: we don't want the low move
5024 // to overwrite the array's location. Likewise, in the case of an
5025 // object array get with read barriers enabled, we do not want the
5026 // move to overwrite the array's location, as we need it to emit
5027 // the read barrier.
5028 locations->SetOut(
5029 Location::RequiresRegister(),
5030 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5031 Location::kOutputOverlap :
5032 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005033 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00005034 // We need a temporary register for the read barrier marking slow
5035 // path in CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier.
5036 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
5037 locations->AddTemp(Location::RequiresRegister());
5038 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005039}
5040
5041void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5042 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005043 Location obj_loc = locations->InAt(0);
5044 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005045 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005046 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005047 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005048
Calin Juravle77520bc2015-01-12 18:45:46 +00005049 Primitive::Type type = instruction->GetType();
5050 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005051 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005052 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005053 if (index.IsConstant()) {
5054 __ movzxb(out, Address(obj,
5055 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5056 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005057 __ movzxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005058 }
5059 break;
5060 }
5061
5062 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005063 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005064 if (index.IsConstant()) {
5065 __ movsxb(out, Address(obj,
5066 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset));
5067 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005068 __ movsxb(out, Address(obj, index.AsRegister<Register>(), TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005069 }
5070 break;
5071 }
5072
5073 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005074 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005075 if (index.IsConstant()) {
5076 __ movsxw(out, Address(obj,
5077 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5078 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005079 __ movsxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005080 }
5081 break;
5082 }
5083
5084 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005085 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005086 if (index.IsConstant()) {
5087 __ movzxw(out, Address(obj,
5088 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset));
5089 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005090 __ movzxw(out, Address(obj, index.AsRegister<Register>(), TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005091 }
5092 break;
5093 }
5094
Roland Levillain7c1559a2015-12-15 10:55:36 +00005095 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005096 Register out = out_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005097 if (index.IsConstant()) {
5098 __ movl(out, Address(obj,
5099 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5100 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005101 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005102 }
5103 break;
5104 }
5105
Roland Levillain7c1559a2015-12-15 10:55:36 +00005106 case Primitive::kPrimNot: {
5107 static_assert(
5108 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5109 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005110 // /* HeapReference<Object> */ out =
5111 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5112 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
5113 Location temp = locations->GetTemp(0);
5114 // Note that a potential implicit null check is handled in this
5115 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5116 codegen_->GenerateArrayLoadWithBakerReadBarrier(
5117 instruction, out_loc, obj, data_offset, index, temp, /* needs_null_check */ true);
5118 } else {
5119 Register out = out_loc.AsRegister<Register>();
5120 if (index.IsConstant()) {
5121 uint32_t offset =
5122 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
5123 __ movl(out, Address(obj, offset));
5124 codegen_->MaybeRecordImplicitNullCheck(instruction);
5125 // If read barriers are enabled, emit read barriers other than
5126 // Baker's using a slow path (and also unpoison the loaded
5127 // reference, if heap poisoning is enabled).
5128 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5129 } else {
5130 __ movl(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5131 codegen_->MaybeRecordImplicitNullCheck(instruction);
5132 // If read barriers are enabled, emit read barriers other than
5133 // Baker's using a slow path (and also unpoison the loaded
5134 // reference, if heap poisoning is enabled).
5135 codegen_->MaybeGenerateReadBarrierSlow(
5136 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5137 }
5138 }
5139 break;
5140 }
5141
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005142 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005143 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005144 if (index.IsConstant()) {
5145 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005146 __ movl(out_loc.AsRegisterPairLow<Register>(), Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005147 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005148 __ movl(out_loc.AsRegisterPairHigh<Register>(), Address(obj, offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005149 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005150 __ movl(out_loc.AsRegisterPairLow<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005151 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005152 codegen_->MaybeRecordImplicitNullCheck(instruction);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005153 __ movl(out_loc.AsRegisterPairHigh<Register>(),
Roland Levillain271ab9c2014-11-27 15:23:57 +00005154 Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005155 }
5156 break;
5157 }
5158
Mark Mendell7c8d0092015-01-26 11:21:33 -05005159 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005160 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005161 if (index.IsConstant()) {
5162 __ movss(out, Address(obj,
5163 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset));
5164 } else {
5165 __ movss(out, Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset));
5166 }
5167 break;
5168 }
5169
5170 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005171 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Mark Mendell7c8d0092015-01-26 11:21:33 -05005172 if (index.IsConstant()) {
5173 __ movsd(out, Address(obj,
5174 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset));
5175 } else {
5176 __ movsd(out, Address(obj, index.AsRegister<Register>(), TIMES_8, data_offset));
5177 }
5178 break;
5179 }
5180
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005181 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005182 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005183 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005184 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005185
Roland Levillain7c1559a2015-12-15 10:55:36 +00005186 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5187 // Potential implicit null checks, in the case of reference or
5188 // long arrays, are handled in the previous switch statement.
5189 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005190 codegen_->MaybeRecordImplicitNullCheck(instruction);
5191 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192}
5193
5194void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005195 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005196
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005197 bool needs_write_barrier =
5198 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005199 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
5200 bool object_array_set_with_read_barrier =
5201 kEmitCompilerReadBarrier && (value_type == Primitive::kPrimNot);
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005202
Nicolas Geoffray39468442014-09-02 15:17:15 +01005203 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5204 instruction,
Roland Levillain0d5a2812015-11-13 10:07:31 +00005205 (may_need_runtime_call_for_type_check || object_array_set_with_read_barrier) ?
5206 LocationSummary::kCallOnSlowPath :
5207 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005208
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005209 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5210 || (value_type == Primitive::kPrimByte);
5211 // We need the inputs to be different than the output in case of long operation.
5212 // In case of a byte operation, the register allocator does not support multiple
5213 // inputs that die at entry with one in a specific register.
5214 locations->SetInAt(0, Location::RequiresRegister());
5215 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5216 if (is_byte_type) {
5217 // Ensure the value is in a byte register.
5218 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5219 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005220 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005221 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005222 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5223 }
5224 if (needs_write_barrier) {
5225 // Temporary registers for the write barrier.
5226 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5227 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005228 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005230}
5231
5232void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5233 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005234 Location array_loc = locations->InAt(0);
5235 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005236 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005237 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005238 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005239 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5240 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5241 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005242 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005243 bool needs_write_barrier =
5244 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005245
5246 switch (value_type) {
5247 case Primitive::kPrimBoolean:
5248 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005249 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
5250 Address address = index.IsConstant()
5251 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + offset)
5252 : Address(array, index.AsRegister<Register>(), TIMES_1, offset);
5253 if (value.IsRegister()) {
5254 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005255 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005256 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005257 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005258 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005259 break;
5260 }
5261
5262 case Primitive::kPrimShort:
5263 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005264 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
5265 Address address = index.IsConstant()
5266 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + offset)
5267 : Address(array, index.AsRegister<Register>(), TIMES_2, offset);
5268 if (value.IsRegister()) {
5269 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005271 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005272 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005273 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005274 break;
5275 }
5276
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005277 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005278 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5279 Address address = index.IsConstant()
5280 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5281 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005282
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005283 if (!value.IsRegister()) {
5284 // Just setting null.
5285 DCHECK(instruction->InputAt(2)->IsNullConstant());
5286 DCHECK(value.IsConstant()) << value;
5287 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005288 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005289 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005290 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005291 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005292 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005293
5294 DCHECK(needs_write_barrier);
5295 Register register_value = value.AsRegister<Register>();
5296 NearLabel done, not_null, do_put;
5297 SlowPathCode* slow_path = nullptr;
5298 Register temp = locations->GetTemp(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005299 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005300 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5301 codegen_->AddSlowPath(slow_path);
5302 if (instruction->GetValueCanBeNull()) {
5303 __ testl(register_value, register_value);
5304 __ j(kNotEqual, &not_null);
5305 __ movl(address, Immediate(0));
5306 codegen_->MaybeRecordImplicitNullCheck(instruction);
5307 __ jmp(&done);
5308 __ Bind(&not_null);
5309 }
5310
Roland Levillain0d5a2812015-11-13 10:07:31 +00005311 if (kEmitCompilerReadBarrier) {
5312 // When read barriers are enabled, the type checking
5313 // instrumentation requires two read barriers:
5314 //
5315 // __ movl(temp2, temp);
5316 // // /* HeapReference<Class> */ temp = temp->component_type_
5317 // __ movl(temp, Address(temp, component_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005318 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005319 // instruction, temp_loc, temp_loc, temp2_loc, component_offset);
5320 //
5321 // // /* HeapReference<Class> */ temp2 = register_value->klass_
5322 // __ movl(temp2, Address(register_value, class_offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005323 // codegen_->GenerateReadBarrierSlow(
Roland Levillain0d5a2812015-11-13 10:07:31 +00005324 // instruction, temp2_loc, temp2_loc, value, class_offset, temp_loc);
5325 //
5326 // __ cmpl(temp, temp2);
5327 //
5328 // However, the second read barrier may trash `temp`, as it
5329 // is a temporary register, and as such would not be saved
5330 // along with live registers before calling the runtime (nor
5331 // restored afterwards). So in this case, we bail out and
5332 // delegate the work to the array set slow path.
5333 //
5334 // TODO: Extend the register allocator to support a new
5335 // "(locally) live temp" location so as to avoid always
5336 // going into the slow path when read barriers are enabled.
5337 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005338 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005339 // /* HeapReference<Class> */ temp = array->klass_
5340 __ movl(temp, Address(array, class_offset));
5341 codegen_->MaybeRecordImplicitNullCheck(instruction);
5342 __ MaybeUnpoisonHeapReference(temp);
5343
5344 // /* HeapReference<Class> */ temp = temp->component_type_
5345 __ movl(temp, Address(temp, component_offset));
5346 // If heap poisoning is enabled, no need to unpoison `temp`
5347 // nor the object reference in `register_value->klass`, as
5348 // we are comparing two poisoned references.
5349 __ cmpl(temp, Address(register_value, class_offset));
5350
5351 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5352 __ j(kEqual, &do_put);
5353 // If heap poisoning is enabled, the `temp` reference has
5354 // not been unpoisoned yet; unpoison it now.
5355 __ MaybeUnpoisonHeapReference(temp);
5356
5357 // /* HeapReference<Class> */ temp = temp->super_class_
5358 __ movl(temp, Address(temp, super_offset));
5359 // If heap poisoning is enabled, no need to unpoison
5360 // `temp`, as we are comparing against null below.
5361 __ testl(temp, temp);
5362 __ j(kNotEqual, slow_path->GetEntryLabel());
5363 __ Bind(&do_put);
5364 } else {
5365 __ j(kNotEqual, slow_path->GetEntryLabel());
5366 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005367 }
5368 }
5369
5370 if (kPoisonHeapReferences) {
5371 __ movl(temp, register_value);
5372 __ PoisonHeapReference(temp);
5373 __ movl(address, temp);
5374 } else {
5375 __ movl(address, register_value);
5376 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005377 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005378 codegen_->MaybeRecordImplicitNullCheck(instruction);
5379 }
5380
5381 Register card = locations->GetTemp(1).AsRegister<Register>();
5382 codegen_->MarkGCCard(
5383 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5384 __ Bind(&done);
5385
5386 if (slow_path != nullptr) {
5387 __ Bind(slow_path->GetExitLabel());
5388 }
5389
5390 break;
5391 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005392
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005393 case Primitive::kPrimInt: {
5394 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
5395 Address address = index.IsConstant()
5396 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5397 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
5398 if (value.IsRegister()) {
5399 __ movl(address, value.AsRegister<Register>());
5400 } else {
5401 DCHECK(value.IsConstant()) << value;
5402 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5403 __ movl(address, Immediate(v));
5404 }
5405 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 break;
5407 }
5408
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005409 case Primitive::kPrimLong: {
5410 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005411 if (index.IsConstant()) {
5412 size_t offset = (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005413 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005414 __ movl(Address(array, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005415 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 __ movl(Address(array, offset + kX86WordSize), value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005417 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005418 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005419 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005420 __ movl(Address(array, offset), Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005421 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422 __ movl(Address(array, offset + kX86WordSize), Immediate(High32Bits(val)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005423 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005424 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005425 if (value.IsRegisterPair()) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005427 value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005428 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005429 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005430 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005431 } else {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01005432 DCHECK(value.IsConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005433 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005435 Immediate(Low32Bits(val)));
Calin Juravle77520bc2015-01-12 18:45:46 +00005436 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005437 __ movl(Address(array, index.AsRegister<Register>(), TIMES_8, data_offset + kX86WordSize),
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005438 Immediate(High32Bits(val)));
5439 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005440 }
5441 break;
5442 }
5443
Mark Mendell7c8d0092015-01-26 11:21:33 -05005444 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005445 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
5446 Address address = index.IsConstant()
5447 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + offset)
5448 : Address(array, index.AsRegister<Register>(), TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005449 if (value.IsFpuRegister()) {
5450 __ movss(address, value.AsFpuRegister<XmmRegister>());
5451 } else {
5452 DCHECK(value.IsConstant());
5453 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5454 __ movl(address, Immediate(v));
5455 }
5456 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005457 break;
5458 }
5459
5460 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005461 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
5462 Address address = index.IsConstant()
5463 ? Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + offset)
5464 : Address(array, index.AsRegister<Register>(), TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005465 if (value.IsFpuRegister()) {
5466 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5467 } else {
5468 DCHECK(value.IsConstant());
5469 Address address_hi = index.IsConstant() ?
5470 Address(array, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) +
5471 offset + kX86WordSize) :
5472 Address(array, index.AsRegister<Register>(), TIMES_8, offset + kX86WordSize);
5473 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5474 __ movl(address, Immediate(Low32Bits(v)));
5475 codegen_->MaybeRecordImplicitNullCheck(instruction);
5476 __ movl(address_hi, Immediate(High32Bits(v)));
5477 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005478 break;
5479 }
5480
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005481 case Primitive::kPrimVoid:
5482 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005483 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005484 }
5485}
5486
5487void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5488 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005489 locations->SetInAt(0, Location::RequiresRegister());
5490 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005491}
5492
5493void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
5494 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005495 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005496 Register obj = locations->InAt(0).AsRegister<Register>();
5497 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005498 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005499 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005500}
5501
5502void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
David Brazdil77a48ae2015-09-15 12:34:04 +00005503 LocationSummary::CallKind call_kind = instruction->CanThrowIntoCatchBlock()
5504 ? LocationSummary::kCallOnSlowPath
5505 : LocationSummary::kNoCall;
5506 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005507 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendell99dbd682015-04-22 16:18:52 -04005508 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005509 if (instruction->HasUses()) {
5510 locations->SetOut(Location::SameAsFirstInput());
5511 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005512}
5513
5514void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
5515 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005516 Location index_loc = locations->InAt(0);
5517 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005518 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005519 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005520
Mark Mendell99dbd682015-04-22 16:18:52 -04005521 if (length_loc.IsConstant()) {
5522 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5523 if (index_loc.IsConstant()) {
5524 // BCE will remove the bounds check if we are guarenteed to pass.
5525 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5526 if (index < 0 || index >= length) {
5527 codegen_->AddSlowPath(slow_path);
5528 __ jmp(slow_path->GetEntryLabel());
5529 } else {
5530 // Some optimization after BCE may have generated this, and we should not
5531 // generate a bounds check if it is a valid range.
5532 }
5533 return;
5534 }
5535
5536 // We have to reverse the jump condition because the length is the constant.
5537 Register index_reg = index_loc.AsRegister<Register>();
5538 __ cmpl(index_reg, Immediate(length));
5539 codegen_->AddSlowPath(slow_path);
5540 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005541 } else {
Mark Mendell99dbd682015-04-22 16:18:52 -04005542 Register length = length_loc.AsRegister<Register>();
5543 if (index_loc.IsConstant()) {
5544 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5545 __ cmpl(length, Immediate(value));
5546 } else {
5547 __ cmpl(length, index_loc.AsRegister<Register>());
5548 }
5549 codegen_->AddSlowPath(slow_path);
5550 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005551 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005552}
5553
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005554void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005555 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005556}
5557
5558void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005559 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5560}
5561
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005562void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
5563 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
5564}
5565
5566void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005567 HBasicBlock* block = instruction->GetBlock();
5568 if (block->GetLoopInformation() != nullptr) {
5569 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5570 // The back edge will generate the suspend check.
5571 return;
5572 }
5573 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5574 // The goto will generate the suspend check.
5575 return;
5576 }
5577 GenerateSuspendCheck(instruction, nullptr);
5578}
5579
5580void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5581 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005582 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005583 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5584 if (slow_path == nullptr) {
5585 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5586 instruction->SetSlowPath(slow_path);
5587 codegen_->AddSlowPath(slow_path);
5588 if (successor != nullptr) {
5589 DCHECK(successor->IsLoopHeader());
5590 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5591 }
5592 } else {
5593 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5594 }
5595
Roland Levillain7c1559a2015-12-15 10:55:36 +00005596 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86WordSize>().Int32Value()),
5597 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005598 if (successor == nullptr) {
5599 __ j(kNotEqual, slow_path->GetEntryLabel());
5600 __ Bind(slow_path->GetReturnLabel());
5601 } else {
5602 __ j(kEqual, codegen_->GetLabelOf(successor));
5603 __ jmp(slow_path->GetEntryLabel());
5604 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005605}
5606
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005607X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5608 return codegen_->GetAssembler();
5609}
5610
Mark Mendell7c8d0092015-01-26 11:21:33 -05005611void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005612 ScratchRegisterScope ensure_scratch(
5613 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5614 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5615 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5616 __ movl(temp_reg, Address(ESP, src + stack_offset));
5617 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005618}
5619
5620void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005621 ScratchRegisterScope ensure_scratch(
5622 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5623 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5624 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5625 __ movl(temp_reg, Address(ESP, src + stack_offset));
5626 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5627 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5628 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005629}
5630
5631void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005632 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005633 Location source = move->GetSource();
5634 Location destination = move->GetDestination();
5635
5636 if (source.IsRegister()) {
5637 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005638 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005639 } else if (destination.IsFpuRegister()) {
5640 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005641 } else {
5642 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005643 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005644 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005645 } else if (source.IsRegisterPair()) {
5646 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5647 // Create stack space for 2 elements.
5648 __ subl(ESP, Immediate(2 * elem_size));
5649 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5650 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5651 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5652 // And remove the temporary stack space we allocated.
5653 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005654 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005655 if (destination.IsRegister()) {
5656 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5657 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005658 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005659 } else if (destination.IsRegisterPair()) {
5660 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5661 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5662 __ psrlq(src_reg, Immediate(32));
5663 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005664 } else if (destination.IsStackSlot()) {
5665 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5666 } else {
5667 DCHECK(destination.IsDoubleStackSlot());
5668 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5669 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005670 } else if (source.IsStackSlot()) {
5671 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005672 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005673 } else if (destination.IsFpuRegister()) {
5674 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005675 } else {
5676 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005677 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5678 }
5679 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005680 if (destination.IsRegisterPair()) {
5681 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5682 __ movl(destination.AsRegisterPairHigh<Register>(),
5683 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5684 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005685 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5686 } else {
5687 DCHECK(destination.IsDoubleStackSlot()) << destination;
5688 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005689 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005690 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005691 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005692 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005693 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005694 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005695 if (value == 0) {
5696 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5697 } else {
5698 __ movl(destination.AsRegister<Register>(), Immediate(value));
5699 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005700 } else {
5701 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005702 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005703 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005704 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005705 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005706 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005707 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005708 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005709 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5710 if (value == 0) {
5711 // Easy handling of 0.0.
5712 __ xorps(dest, dest);
5713 } else {
5714 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005715 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5716 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5717 __ movl(temp, Immediate(value));
5718 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005719 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005720 } else {
5721 DCHECK(destination.IsStackSlot()) << destination;
5722 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5723 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005724 } else if (constant->IsLongConstant()) {
5725 int64_t value = constant->AsLongConstant()->GetValue();
5726 int32_t low_value = Low32Bits(value);
5727 int32_t high_value = High32Bits(value);
5728 Immediate low(low_value);
5729 Immediate high(high_value);
5730 if (destination.IsDoubleStackSlot()) {
5731 __ movl(Address(ESP, destination.GetStackIndex()), low);
5732 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5733 } else {
5734 __ movl(destination.AsRegisterPairLow<Register>(), low);
5735 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5736 }
5737 } else {
5738 DCHECK(constant->IsDoubleConstant());
5739 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005740 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005741 int32_t low_value = Low32Bits(value);
5742 int32_t high_value = High32Bits(value);
5743 Immediate low(low_value);
5744 Immediate high(high_value);
5745 if (destination.IsFpuRegister()) {
5746 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5747 if (value == 0) {
5748 // Easy handling of 0.0.
5749 __ xorpd(dest, dest);
5750 } else {
5751 __ pushl(high);
5752 __ pushl(low);
5753 __ movsd(dest, Address(ESP, 0));
5754 __ addl(ESP, Immediate(8));
5755 }
5756 } else {
5757 DCHECK(destination.IsDoubleStackSlot()) << destination;
5758 __ movl(Address(ESP, destination.GetStackIndex()), low);
5759 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5760 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005761 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005762 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005763 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005764 }
5765}
5766
Mark Mendella5c19ce2015-04-01 12:51:05 -04005767void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005768 Register suggested_scratch = reg == EAX ? EBX : EAX;
5769 ScratchRegisterScope ensure_scratch(
5770 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5771
5772 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5773 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5774 __ movl(Address(ESP, mem + stack_offset), reg);
5775 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005776}
5777
Mark Mendell7c8d0092015-01-26 11:21:33 -05005778void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005779 ScratchRegisterScope ensure_scratch(
5780 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5781
5782 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5783 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5784 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5785 __ movss(Address(ESP, mem + stack_offset), reg);
5786 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005787}
5788
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005789void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005790 ScratchRegisterScope ensure_scratch1(
5791 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005792
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005793 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5794 ScratchRegisterScope ensure_scratch2(
5795 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005796
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005797 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5798 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5799 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5800 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5801 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5802 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005803}
5804
5805void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005806 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005807 Location source = move->GetSource();
5808 Location destination = move->GetDestination();
5809
5810 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005811 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5812 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5813 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5814 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5815 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005816 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005817 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005818 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005819 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005820 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5821 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005822 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5823 // Use XOR Swap algorithm to avoid a temporary.
5824 DCHECK_NE(source.reg(), destination.reg());
5825 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5826 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5827 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5828 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5829 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5830 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5831 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005832 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5833 // Take advantage of the 16 bytes in the XMM register.
5834 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5835 Address stack(ESP, destination.GetStackIndex());
5836 // Load the double into the high doubleword.
5837 __ movhpd(reg, stack);
5838
5839 // Store the low double into the destination.
5840 __ movsd(stack, reg);
5841
5842 // Move the high double to the low double.
5843 __ psrldq(reg, Immediate(8));
5844 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5845 // Take advantage of the 16 bytes in the XMM register.
5846 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5847 Address stack(ESP, source.GetStackIndex());
5848 // Load the double into the high doubleword.
5849 __ movhpd(reg, stack);
5850
5851 // Store the low double into the destination.
5852 __ movsd(stack, reg);
5853
5854 // Move the high double to the low double.
5855 __ psrldq(reg, Immediate(8));
5856 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5857 Exchange(destination.GetStackIndex(), source.GetStackIndex());
5858 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005859 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005860 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005861 }
5862}
5863
5864void ParallelMoveResolverX86::SpillScratch(int reg) {
5865 __ pushl(static_cast<Register>(reg));
5866}
5867
5868void ParallelMoveResolverX86::RestoreScratch(int reg) {
5869 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005870}
5871
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005872void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Calin Juravle98893e12015-10-02 21:05:03 +01005873 InvokeRuntimeCallingConvention calling_convention;
5874 CodeGenerator::CreateLoadClassLocationSummary(
5875 cls,
5876 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Roland Levillain0d5a2812015-11-13 10:07:31 +00005877 Location::RegisterLocation(EAX),
5878 /* code_generator_supports_read_barrier */ true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005879}
5880
5881void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01005882 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01005883 if (cls->NeedsAccessCheck()) {
5884 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex());
5885 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pInitializeTypeAndVerifyAccess),
5886 cls,
5887 cls->GetDexPc(),
5888 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00005889 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01005890 return;
5891 }
5892
Roland Levillain0d5a2812015-11-13 10:07:31 +00005893 Location out_loc = locations->Out();
5894 Register out = out_loc.AsRegister<Register>();
Calin Juravle580b6092015-10-06 17:35:58 +01005895 Register current_method = locations->InAt(0).AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005896
Calin Juravle580b6092015-10-06 17:35:58 +01005897 if (cls->IsReferrersClass()) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005898 DCHECK(!cls->CanCallRuntime());
5899 DCHECK(!cls->MustGenerateClinitCheck());
Roland Levillain7c1559a2015-12-15 10:55:36 +00005900 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
5901 GenerateGcRootFieldLoad(
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005902 cls, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005903 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005904 // /* GcRoot<mirror::Class>[] */ out =
5905 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
5906 __ movl(out, Address(current_method,
5907 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
Roland Levillain7c1559a2015-12-15 10:55:36 +00005908 // /* GcRoot<mirror::Class> */ out = out[type_index]
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005909 GenerateGcRootFieldLoad(
5910 cls, out_loc, Address(out, CodeGenerator::GetCacheOffset(cls->GetTypeIndex())));
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005911
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00005912 if (!cls->IsInDexCache() || cls->MustGenerateClinitCheck()) {
5913 DCHECK(cls->CanCallRuntime());
5914 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
5915 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
5916 codegen_->AddSlowPath(slow_path);
5917
5918 if (!cls->IsInDexCache()) {
5919 __ testl(out, out);
5920 __ j(kEqual, slow_path->GetEntryLabel());
5921 }
5922
5923 if (cls->MustGenerateClinitCheck()) {
5924 GenerateClassInitializationCheck(slow_path, out);
5925 } else {
5926 __ Bind(slow_path->GetExitLabel());
5927 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005928 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005929 }
5930}
5931
5932void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
5933 LocationSummary* locations =
5934 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
5935 locations->SetInAt(0, Location::RequiresRegister());
5936 if (check->HasUses()) {
5937 locations->SetOut(Location::SameAsFirstInput());
5938 }
5939}
5940
5941void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005942 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07005943 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005944 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005945 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00005946 GenerateClassInitializationCheck(slow_path,
5947 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005948}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005949
Nicolas Geoffray424f6762014-11-03 14:51:25 +00005950void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07005951 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01005952 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
5953 Immediate(mirror::Class::kStatusInitialized));
5954 __ j(kLess, slow_path->GetEntryLabel());
5955 __ Bind(slow_path->GetExitLabel());
5956 // No need for memory fence, thanks to the X86 memory model.
5957}
5958
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005959HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
5960 HLoadString::LoadKind desired_string_load_kind) {
5961 if (kEmitCompilerReadBarrier) {
5962 switch (desired_string_load_kind) {
5963 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5964 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5965 case HLoadString::LoadKind::kBootImageAddress:
5966 // TODO: Implement for read barrier.
5967 return HLoadString::LoadKind::kDexCacheViaMethod;
5968 default:
5969 break;
5970 }
5971 }
5972 switch (desired_string_load_kind) {
5973 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
5974 DCHECK(!GetCompilerOptions().GetCompilePic());
5975 break;
5976 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
5977 DCHECK(GetCompilerOptions().GetCompilePic());
5978 FALLTHROUGH_INTENDED;
5979 case HLoadString::LoadKind::kDexCachePcRelative:
Calin Juravleffc87072016-04-20 14:22:09 +01005980 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005981 // We disable pc-relative load when there is an irreducible loop, as the optimization
5982 // is incompatible with it.
5983 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
5984 // with irreducible loops.
5985 if (GetGraph()->HasIrreducibleLoops()) {
5986 return HLoadString::LoadKind::kDexCacheViaMethod;
5987 }
5988 break;
5989 case HLoadString::LoadKind::kBootImageAddress:
5990 break;
5991 case HLoadString::LoadKind::kDexCacheAddress:
Calin Juravleffc87072016-04-20 14:22:09 +01005992 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +00005993 break;
5994 case HLoadString::LoadKind::kDexCacheViaMethod:
5995 break;
5996 }
5997 return desired_string_load_kind;
5998}
5999
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006000void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006001 LocationSummary::CallKind call_kind = (load->NeedsEnvironment() || kEmitCompilerReadBarrier)
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006002 ? LocationSummary::kCallOnSlowPath
6003 : LocationSummary::kNoCall;
6004 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006005 HLoadString::LoadKind load_kind = load->GetLoadKind();
6006 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod ||
6007 load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
6008 load_kind == HLoadString::LoadKind::kDexCachePcRelative) {
6009 locations->SetInAt(0, Location::RequiresRegister());
6010 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006011 locations->SetOut(Location::RequiresRegister());
6012}
6013
6014void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006015 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006016 Location out_loc = locations->Out();
6017 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006018
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006019 switch (load->GetLoadKind()) {
6020 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
6021 DCHECK(!kEmitCompilerReadBarrier);
6022 __ movl(out, Immediate(/* placeholder */ 0));
6023 codegen_->RecordStringPatch(load);
6024 return; // No dex cache slow path.
6025 }
6026 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
6027 DCHECK(!kEmitCompilerReadBarrier);
6028 Register method_address = locations->InAt(0).AsRegister<Register>();
6029 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6030 codegen_->RecordStringPatch(load);
6031 return; // No dex cache slow path.
6032 }
6033 case HLoadString::LoadKind::kBootImageAddress: {
6034 DCHECK(!kEmitCompilerReadBarrier);
6035 DCHECK_NE(load->GetAddress(), 0u);
6036 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6037 __ movl(out, Immediate(address));
6038 codegen_->RecordSimplePatch();
6039 return; // No dex cache slow path.
6040 }
6041 case HLoadString::LoadKind::kDexCacheAddress: {
6042 DCHECK_NE(load->GetAddress(), 0u);
6043 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6044 GenerateGcRootFieldLoad(load, out_loc, Address::Absolute(address));
6045 break;
6046 }
6047 case HLoadString::LoadKind::kDexCachePcRelative: {
6048 Register base_reg = locations->InAt(0).AsRegister<Register>();
6049 uint32_t offset = load->GetDexCacheElementOffset();
6050 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(load->GetDexFile(), offset);
6051 GenerateGcRootFieldLoad(
6052 load, out_loc, Address(base_reg, CodeGeneratorX86::kDummy32BitOffset), fixup_label);
6053 break;
6054 }
6055 case HLoadString::LoadKind::kDexCacheViaMethod: {
6056 Register current_method = locations->InAt(0).AsRegister<Register>();
6057
6058 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6059 GenerateGcRootFieldLoad(
6060 load, out_loc, Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()));
6061
6062 // /* GcRoot<mirror::String>[] */ out = out->dex_cache_strings_
6063 __ movl(out, Address(out, mirror::Class::DexCacheStringsOffset().Int32Value()));
6064 // /* GcRoot<mirror::String> */ out = out[string_index]
6065 GenerateGcRootFieldLoad(
6066 load, out_loc, Address(out, CodeGenerator::GetCacheOffset(load->GetStringIndex())));
6067 break;
6068 }
6069 default:
6070 LOG(FATAL) << "Unexpected load kind: " << load->GetLoadKind();
6071 UNREACHABLE();
6072 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006073
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006074 if (!load->IsInDexCache()) {
6075 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6076 codegen_->AddSlowPath(slow_path);
6077 __ testl(out, out);
6078 __ j(kEqual, slow_path->GetEntryLabel());
6079 __ Bind(slow_path->GetExitLabel());
6080 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006081}
6082
David Brazdilcb1c0552015-08-04 16:22:25 +01006083static Address GetExceptionTlsAddress() {
6084 return Address::Absolute(Thread::ExceptionOffset<kX86WordSize>().Int32Value());
6085}
6086
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006087void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6088 LocationSummary* locations =
6089 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6090 locations->SetOut(Location::RequiresRegister());
6091}
6092
6093void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006094 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6095}
6096
6097void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6098 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6099}
6100
6101void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6102 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006103}
6104
6105void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6106 LocationSummary* locations =
6107 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6108 InvokeRuntimeCallingConvention calling_convention;
6109 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6110}
6111
6112void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006113 codegen_->InvokeRuntime(QUICK_ENTRY_POINT(pDeliverException),
6114 instruction,
6115 instruction->GetDexPc(),
6116 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006117 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006118}
6119
Roland Levillain7c1559a2015-12-15 10:55:36 +00006120static bool TypeCheckNeedsATemporary(TypeCheckKind type_check_kind) {
6121 return kEmitCompilerReadBarrier &&
6122 (kUseBakerReadBarrier ||
6123 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6124 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6125 type_check_kind == TypeCheckKind::kArrayObjectCheck);
6126}
6127
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006128void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006129 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006130 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6131 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006132 case TypeCheckKind::kExactCheck:
6133 case TypeCheckKind::kAbstractClassCheck:
6134 case TypeCheckKind::kClassHierarchyCheck:
6135 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006136 call_kind =
6137 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006138 break;
6139 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006140 case TypeCheckKind::kUnresolvedCheck:
6141 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006142 call_kind = LocationSummary::kCallOnSlowPath;
6143 break;
6144 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006145
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006146 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006147 locations->SetInAt(0, Location::RequiresRegister());
6148 locations->SetInAt(1, Location::Any());
6149 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6150 locations->SetOut(Location::RequiresRegister());
6151 // When read barriers are enabled, we need a temporary register for
6152 // some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006153 if (TypeCheckNeedsATemporary(type_check_kind)) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006154 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006155 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006156}
6157
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006158void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006159 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006160 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006161 Location obj_loc = locations->InAt(0);
6162 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006163 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006164 Location out_loc = locations->Out();
6165 Register out = out_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006166 Location maybe_temp_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006167 locations->GetTemp(0) :
6168 Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006169 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006170 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6171 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6172 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006173 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006174 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006175
6176 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006177 // Avoid null check if we know obj is not null.
6178 if (instruction->MustDoNullCheck()) {
6179 __ testl(obj, obj);
6180 __ j(kEqual, &zero);
6181 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006182
Roland Levillain0d5a2812015-11-13 10:07:31 +00006183 // /* HeapReference<Class> */ out = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006184 GenerateReferenceLoadTwoRegisters(instruction, out_loc, obj_loc, class_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006185
Roland Levillain7c1559a2015-12-15 10:55:36 +00006186 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006187 case TypeCheckKind::kExactCheck: {
6188 if (cls.IsRegister()) {
6189 __ cmpl(out, cls.AsRegister<Register>());
6190 } else {
6191 DCHECK(cls.IsStackSlot()) << cls;
6192 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6193 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006194
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006195 // Classes must be equal for the instanceof to succeed.
6196 __ j(kNotEqual, &zero);
6197 __ movl(out, Immediate(1));
6198 __ jmp(&done);
6199 break;
6200 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006201
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006202 case TypeCheckKind::kAbstractClassCheck: {
6203 // If the class is abstract, we eagerly fetch the super class of the
6204 // object to avoid doing a comparison we know will fail.
6205 NearLabel loop;
6206 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006207 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006208 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006209 __ testl(out, out);
6210 // If `out` is null, we use it for the result, and jump to `done`.
6211 __ j(kEqual, &done);
6212 if (cls.IsRegister()) {
6213 __ cmpl(out, cls.AsRegister<Register>());
6214 } else {
6215 DCHECK(cls.IsStackSlot()) << cls;
6216 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6217 }
6218 __ j(kNotEqual, &loop);
6219 __ movl(out, Immediate(1));
6220 if (zero.IsLinked()) {
6221 __ jmp(&done);
6222 }
6223 break;
6224 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006225
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006226 case TypeCheckKind::kClassHierarchyCheck: {
6227 // Walk over the class hierarchy to find a match.
6228 NearLabel loop, success;
6229 __ Bind(&loop);
6230 if (cls.IsRegister()) {
6231 __ cmpl(out, cls.AsRegister<Register>());
6232 } else {
6233 DCHECK(cls.IsStackSlot()) << cls;
6234 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6235 }
6236 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006237 // /* HeapReference<Class> */ out = out->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006238 GenerateReferenceLoadOneRegister(instruction, out_loc, super_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006239 __ testl(out, out);
6240 __ j(kNotEqual, &loop);
6241 // If `out` is null, we use it for the result, and jump to `done`.
6242 __ jmp(&done);
6243 __ Bind(&success);
6244 __ movl(out, Immediate(1));
6245 if (zero.IsLinked()) {
6246 __ jmp(&done);
6247 }
6248 break;
6249 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006250
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006251 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006252 // Do an exact check.
6253 NearLabel exact_check;
6254 if (cls.IsRegister()) {
6255 __ cmpl(out, cls.AsRegister<Register>());
6256 } else {
6257 DCHECK(cls.IsStackSlot()) << cls;
6258 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6259 }
6260 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006261 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006262 // /* HeapReference<Class> */ out = out->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006263 GenerateReferenceLoadOneRegister(instruction, out_loc, component_offset, maybe_temp_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006264 __ testl(out, out);
6265 // If `out` is null, we use it for the result, and jump to `done`.
6266 __ j(kEqual, &done);
6267 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6268 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006269 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006270 __ movl(out, Immediate(1));
6271 __ jmp(&done);
6272 break;
6273 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006274
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006275 case TypeCheckKind::kArrayCheck: {
6276 if (cls.IsRegister()) {
6277 __ cmpl(out, cls.AsRegister<Register>());
6278 } else {
6279 DCHECK(cls.IsStackSlot()) << cls;
6280 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6281 }
6282 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6284 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006285 codegen_->AddSlowPath(slow_path);
6286 __ j(kNotEqual, slow_path->GetEntryLabel());
6287 __ movl(out, Immediate(1));
6288 if (zero.IsLinked()) {
6289 __ jmp(&done);
6290 }
6291 break;
6292 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006293
Calin Juravle98893e12015-10-02 21:05:03 +01006294 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006295 case TypeCheckKind::kInterfaceCheck: {
6296 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006297 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006298 // cases.
6299 //
6300 // We cannot directly call the InstanceofNonTrivial runtime
6301 // entry point without resorting to a type checking slow path
6302 // here (i.e. by calling InvokeRuntime directly), as it would
6303 // require to assign fixed registers for the inputs of this
6304 // HInstanceOf instruction (following the runtime calling
6305 // convention), which might be cluttered by the potential first
6306 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006307 //
6308 // TODO: Introduce a new runtime entry point taking the object
6309 // to test (instead of its class) as argument, and let it deal
6310 // with the read barrier issues. This will let us refactor this
6311 // case of the `switch` code as it was previously (with a direct
6312 // call to the runtime not using a type checking slow path).
6313 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006314 DCHECK(locations->OnlyCallsOnSlowPath());
6315 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6316 /* is_fatal */ false);
6317 codegen_->AddSlowPath(slow_path);
6318 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006319 if (zero.IsLinked()) {
6320 __ jmp(&done);
6321 }
6322 break;
6323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006324 }
6325
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006326 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006327 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006328 __ xorl(out, out);
6329 }
6330
6331 if (done.IsLinked()) {
6332 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006333 }
6334
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006335 if (slow_path != nullptr) {
6336 __ Bind(slow_path->GetExitLabel());
6337 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006338}
6339
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006340void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006341 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6342 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006343 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
6344 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006345 case TypeCheckKind::kExactCheck:
6346 case TypeCheckKind::kAbstractClassCheck:
6347 case TypeCheckKind::kClassHierarchyCheck:
6348 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006349 call_kind = (throws_into_catch || kEmitCompilerReadBarrier) ?
6350 LocationSummary::kCallOnSlowPath :
6351 LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006352 break;
6353 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006354 case TypeCheckKind::kUnresolvedCheck:
6355 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006356 call_kind = LocationSummary::kCallOnSlowPath;
6357 break;
6358 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006359 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6360 locations->SetInAt(0, Location::RequiresRegister());
6361 locations->SetInAt(1, Location::Any());
6362 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6363 locations->AddTemp(Location::RequiresRegister());
6364 // When read barriers are enabled, we need an additional temporary
6365 // register for some cases.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006366 if (TypeCheckNeedsATemporary(type_check_kind)) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006367 locations->AddTemp(Location::RequiresRegister());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006368 }
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006369}
6370
6371void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006372 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006373 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006374 Location obj_loc = locations->InAt(0);
6375 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006376 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006377 Location temp_loc = locations->GetTemp(0);
6378 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006379 Location maybe_temp2_loc = TypeCheckNeedsATemporary(type_check_kind) ?
Roland Levillain7c1559a2015-12-15 10:55:36 +00006380 locations->GetTemp(1) :
6381 Location::NoLocation();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006382 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6383 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6384 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6385 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006386
Roland Levillain0d5a2812015-11-13 10:07:31 +00006387 bool is_type_check_slow_path_fatal =
6388 (type_check_kind == TypeCheckKind::kExactCheck ||
6389 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
6390 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
6391 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
6392 !instruction->CanThrowIntoCatchBlock();
6393 SlowPathCode* type_check_slow_path =
6394 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6395 is_type_check_slow_path_fatal);
6396 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006397
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006399 // Avoid null check if we know obj is not null.
6400 if (instruction->MustDoNullCheck()) {
6401 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006403 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006404
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006406 GenerateReferenceLoadTwoRegisters(instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006407
Roland Levillain0d5a2812015-11-13 10:07:31 +00006408 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006409 case TypeCheckKind::kExactCheck:
6410 case TypeCheckKind::kArrayCheck: {
6411 if (cls.IsRegister()) {
6412 __ cmpl(temp, cls.AsRegister<Register>());
6413 } else {
6414 DCHECK(cls.IsStackSlot()) << cls;
6415 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6416 }
6417 // Jump to slow path for throwing the exception or doing a
6418 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006419 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006420 break;
6421 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006422
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006423 case TypeCheckKind::kAbstractClassCheck: {
6424 // If the class is abstract, we eagerly fetch the super class of the
6425 // object to avoid doing a comparison we know will fail.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006426 NearLabel loop, compare_classes;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006427 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006428 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006429 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006430
6431 // If the class reference currently in `temp` is not null, jump
6432 // to the `compare_classes` label to compare it with the checked
6433 // class.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006434 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006435 __ j(kNotEqual, &compare_classes);
6436 // Otherwise, jump to the slow path to throw the exception.
6437 //
6438 // But before, move back the object's class into `temp` before
6439 // going into the slow path, as it has been overwritten in the
6440 // meantime.
6441 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006442 GenerateReferenceLoadTwoRegisters(
6443 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006444 __ jmp(type_check_slow_path->GetEntryLabel());
6445
6446 __ Bind(&compare_classes);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006447 if (cls.IsRegister()) {
6448 __ cmpl(temp, cls.AsRegister<Register>());
6449 } else {
6450 DCHECK(cls.IsStackSlot()) << cls;
6451 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6452 }
6453 __ j(kNotEqual, &loop);
6454 break;
6455 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006456
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457 case TypeCheckKind::kClassHierarchyCheck: {
6458 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006459 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006460 __ Bind(&loop);
6461 if (cls.IsRegister()) {
6462 __ cmpl(temp, cls.AsRegister<Register>());
6463 } else {
6464 DCHECK(cls.IsStackSlot()) << cls;
6465 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6466 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006467 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006468
Roland Levillain0d5a2812015-11-13 10:07:31 +00006469 // /* HeapReference<Class> */ temp = temp->super_class_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006470 GenerateReferenceLoadOneRegister(instruction, temp_loc, super_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006471
6472 // If the class reference currently in `temp` is not null, jump
6473 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006474 __ testl(temp, temp);
6475 __ j(kNotEqual, &loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006476 // Otherwise, jump to the slow path to throw the exception.
6477 //
6478 // But before, move back the object's class into `temp` before
6479 // going into the slow path, as it has been overwritten in the
6480 // meantime.
6481 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006482 GenerateReferenceLoadTwoRegisters(
6483 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006485 break;
6486 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006487
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006488 case TypeCheckKind::kArrayObjectCheck: {
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006489 // Do an exact check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006490 NearLabel check_non_primitive_component_type;
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006491 if (cls.IsRegister()) {
6492 __ cmpl(temp, cls.AsRegister<Register>());
6493 } else {
6494 DCHECK(cls.IsStackSlot()) << cls;
6495 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6496 }
6497 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006498
6499 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006500 // /* HeapReference<Class> */ temp = temp->component_type_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006501 GenerateReferenceLoadOneRegister(instruction, temp_loc, component_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006502
6503 // If the component type is not null (i.e. the object is indeed
6504 // an array), jump to label `check_non_primitive_component_type`
6505 // to further check that this component type is not a primitive
6506 // type.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 __ testl(temp, temp);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006508 __ j(kNotEqual, &check_non_primitive_component_type);
6509 // Otherwise, jump to the slow path to throw the exception.
6510 //
6511 // But before, move back the object's class into `temp` before
6512 // going into the slow path, as it has been overwritten in the
6513 // meantime.
6514 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006515 GenerateReferenceLoadTwoRegisters(
6516 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006517 __ jmp(type_check_slow_path->GetEntryLabel());
6518
6519 __ Bind(&check_non_primitive_component_type);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006520 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Roland Levillain0d5a2812015-11-13 10:07:31 +00006521 __ j(kEqual, &done);
6522 // Same comment as above regarding `temp` and the slow path.
6523 // /* HeapReference<Class> */ temp = obj->klass_
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006524 GenerateReferenceLoadTwoRegisters(
6525 instruction, temp_loc, obj_loc, class_offset, maybe_temp2_loc);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006526 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006527 break;
6528 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006529
Calin Juravle98893e12015-10-02 21:05:03 +01006530 case TypeCheckKind::kUnresolvedCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006531 case TypeCheckKind::kInterfaceCheck:
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006532 // We always go into the type check slow path for the unresolved
6533 // and interface check cases.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006534 //
6535 // We cannot directly call the CheckCast runtime entry point
6536 // without resorting to a type checking slow path here (i.e. by
6537 // calling InvokeRuntime directly), as it would require to
6538 // assign fixed registers for the inputs of this HInstanceOf
6539 // instruction (following the runtime calling convention), which
6540 // might be cluttered by the potential first read barrier
6541 // emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006542 //
6543 // TODO: Introduce a new runtime entry point taking the object
6544 // to test (instead of its class) as argument, and let it deal
6545 // with the read barrier issues. This will let us refactor this
6546 // case of the `switch` code as it was previously (with a direct
6547 // call to the runtime not using a type checking slow path).
6548 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006549 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006550 break;
6551 }
6552 __ Bind(&done);
6553
Roland Levillain0d5a2812015-11-13 10:07:31 +00006554 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006555}
6556
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006557void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6558 LocationSummary* locations =
6559 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCall);
6560 InvokeRuntimeCallingConvention calling_convention;
6561 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6562}
6563
6564void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Alexandre Rames8158f282015-08-07 10:26:17 +01006565 codegen_->InvokeRuntime(instruction->IsEnter() ? QUICK_ENTRY_POINT(pLockObject)
6566 : QUICK_ENTRY_POINT(pUnlockObject),
6567 instruction,
6568 instruction->GetDexPc(),
6569 nullptr);
Roland Levillain888d0672015-11-23 18:53:50 +00006570 if (instruction->IsEnter()) {
6571 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6572 } else {
6573 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6574 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006575}
6576
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006577void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6578void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6579void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6580
6581void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6582 LocationSummary* locations =
6583 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6584 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6585 || instruction->GetResultType() == Primitive::kPrimLong);
6586 locations->SetInAt(0, Location::RequiresRegister());
6587 locations->SetInAt(1, Location::Any());
6588 locations->SetOut(Location::SameAsFirstInput());
6589}
6590
6591void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6592 HandleBitwiseOperation(instruction);
6593}
6594
6595void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6596 HandleBitwiseOperation(instruction);
6597}
6598
6599void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6600 HandleBitwiseOperation(instruction);
6601}
6602
6603void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6604 LocationSummary* locations = instruction->GetLocations();
6605 Location first = locations->InAt(0);
6606 Location second = locations->InAt(1);
6607 DCHECK(first.Equals(locations->Out()));
6608
6609 if (instruction->GetResultType() == Primitive::kPrimInt) {
6610 if (second.IsRegister()) {
6611 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006612 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006613 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006614 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006615 } else {
6616 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006617 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006618 }
6619 } else if (second.IsConstant()) {
6620 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006621 __ andl(first.AsRegister<Register>(),
6622 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006623 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006624 __ orl(first.AsRegister<Register>(),
6625 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006626 } else {
6627 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006628 __ xorl(first.AsRegister<Register>(),
6629 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006630 }
6631 } else {
6632 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006633 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006634 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006635 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006636 } else {
6637 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006638 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006639 }
6640 }
6641 } else {
6642 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6643 if (second.IsRegisterPair()) {
6644 if (instruction->IsAnd()) {
6645 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6646 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6647 } else if (instruction->IsOr()) {
6648 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6649 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6650 } else {
6651 DCHECK(instruction->IsXor());
6652 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
6653 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
6654 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006655 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006656 if (instruction->IsAnd()) {
6657 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6658 __ andl(first.AsRegisterPairHigh<Register>(),
6659 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6660 } else if (instruction->IsOr()) {
6661 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6662 __ orl(first.AsRegisterPairHigh<Register>(),
6663 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6664 } else {
6665 DCHECK(instruction->IsXor());
6666 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
6667 __ xorl(first.AsRegisterPairHigh<Register>(),
6668 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
6669 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006670 } else {
6671 DCHECK(second.IsConstant()) << second;
6672 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006673 int32_t low_value = Low32Bits(value);
6674 int32_t high_value = High32Bits(value);
6675 Immediate low(low_value);
6676 Immediate high(high_value);
6677 Register first_low = first.AsRegisterPairLow<Register>();
6678 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006679 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006680 if (low_value == 0) {
6681 __ xorl(first_low, first_low);
6682 } else if (low_value != -1) {
6683 __ andl(first_low, low);
6684 }
6685 if (high_value == 0) {
6686 __ xorl(first_high, first_high);
6687 } else if (high_value != -1) {
6688 __ andl(first_high, high);
6689 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006690 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006691 if (low_value != 0) {
6692 __ orl(first_low, low);
6693 }
6694 if (high_value != 0) {
6695 __ orl(first_high, high);
6696 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006697 } else {
6698 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04006699 if (low_value != 0) {
6700 __ xorl(first_low, low);
6701 }
6702 if (high_value != 0) {
6703 __ xorl(first_high, high);
6704 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00006705 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006706 }
6707 }
6708}
6709
Roland Levillain7c1559a2015-12-15 10:55:36 +00006710void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(HInstruction* instruction,
6711 Location out,
6712 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006713 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006714 Register out_reg = out.AsRegister<Register>();
6715 if (kEmitCompilerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006716 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006717 if (kUseBakerReadBarrier) {
6718 // Load with fast path based Baker's read barrier.
6719 // /* HeapReference<Object> */ out = *(out + offset)
6720 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006721 instruction, out, out_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006722 } else {
6723 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006724 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00006725 // in the following move operation, as we will need it for the
6726 // read barrier below.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006727 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006728 // /* HeapReference<Object> */ out = *(out + offset)
6729 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006730 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006731 }
6732 } else {
6733 // Plain load with no read barrier.
6734 // /* HeapReference<Object> */ out = *(out + offset)
6735 __ movl(out_reg, Address(out_reg, offset));
6736 __ MaybeUnpoisonHeapReference(out_reg);
6737 }
6738}
6739
6740void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(HInstruction* instruction,
6741 Location out,
6742 Location obj,
6743 uint32_t offset,
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006744 Location maybe_temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006745 Register out_reg = out.AsRegister<Register>();
6746 Register obj_reg = obj.AsRegister<Register>();
6747 if (kEmitCompilerReadBarrier) {
6748 if (kUseBakerReadBarrier) {
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006749 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006750 // Load with fast path based Baker's read barrier.
6751 // /* HeapReference<Object> */ out = *(obj + offset)
6752 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Roland Levillain95e7ffc2016-01-22 11:57:25 +00006753 instruction, out, obj_reg, offset, maybe_temp, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006754 } else {
6755 // Load with slow path based read barrier.
6756 // /* HeapReference<Object> */ out = *(obj + offset)
6757 __ movl(out_reg, Address(obj_reg, offset));
6758 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6759 }
6760 } else {
6761 // Plain load with no read barrier.
6762 // /* HeapReference<Object> */ out = *(obj + offset)
6763 __ movl(out_reg, Address(obj_reg, offset));
6764 __ MaybeUnpoisonHeapReference(out_reg);
6765 }
6766}
6767
6768void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(HInstruction* instruction,
6769 Location root,
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006770 const Address& address,
6771 Label* fixup_label) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006772 Register root_reg = root.AsRegister<Register>();
6773 if (kEmitCompilerReadBarrier) {
6774 if (kUseBakerReadBarrier) {
6775 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6776 // Baker's read barrier are used:
6777 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006778 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00006779 // if (Thread::Current()->GetIsGcMarking()) {
6780 // root = ReadBarrier::Mark(root)
6781 // }
6782
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006783 // /* GcRoot<mirror::Object> */ root = *address
6784 __ movl(root_reg, address);
6785 if (fixup_label != nullptr) {
6786 __ Bind(fixup_label);
6787 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006788 static_assert(
6789 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6790 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6791 "have different sizes.");
6792 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6793 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6794 "have different sizes.");
6795
6796 // Slow path used to mark the GC root `root`.
6797 SlowPathCode* slow_path =
6798 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, root, root);
6799 codegen_->AddSlowPath(slow_path);
6800
6801 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86WordSize>().Int32Value()),
6802 Immediate(0));
6803 __ j(kNotEqual, slow_path->GetEntryLabel());
6804 __ Bind(slow_path->GetExitLabel());
6805 } else {
6806 // GC root loaded through a slow path for read barriers other
6807 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006808 // /* GcRoot<mirror::Object>* */ root = address
6809 __ leal(root_reg, address);
6810 if (fixup_label != nullptr) {
6811 __ Bind(fixup_label);
6812 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00006813 // /* mirror::Object* */ root = root->Read()
6814 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6815 }
6816 } else {
6817 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006818 // /* GcRoot<mirror::Object> */ root = *address
6819 __ movl(root_reg, address);
6820 if (fixup_label != nullptr) {
6821 __ Bind(fixup_label);
6822 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006823 // Note that GC roots are not affected by heap poisoning, thus we
6824 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006825 }
6826}
6827
6828void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6829 Location ref,
6830 Register obj,
6831 uint32_t offset,
6832 Location temp,
6833 bool needs_null_check) {
6834 DCHECK(kEmitCompilerReadBarrier);
6835 DCHECK(kUseBakerReadBarrier);
6836
6837 // /* HeapReference<Object> */ ref = *(obj + offset)
6838 Address src(obj, offset);
6839 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6840}
6841
6842void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6843 Location ref,
6844 Register obj,
6845 uint32_t data_offset,
6846 Location index,
6847 Location temp,
6848 bool needs_null_check) {
6849 DCHECK(kEmitCompilerReadBarrier);
6850 DCHECK(kUseBakerReadBarrier);
6851
6852 // /* HeapReference<Object> */ ref =
6853 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6854 Address src = index.IsConstant() ?
6855 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset) :
6856 Address(obj, index.AsRegister<Register>(), TIMES_4, data_offset);
6857 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, temp, needs_null_check);
6858}
6859
6860void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6861 Location ref,
6862 Register obj,
6863 const Address& src,
6864 Location temp,
6865 bool needs_null_check) {
6866 DCHECK(kEmitCompilerReadBarrier);
6867 DCHECK(kUseBakerReadBarrier);
6868
6869 // In slow path based read barriers, the read barrier call is
6870 // inserted after the original load. However, in fast path based
6871 // Baker's read barriers, we need to perform the load of
6872 // mirror::Object::monitor_ *before* the original reference load.
6873 // This load-load ordering is required by the read barrier.
6874 // The fast path/slow path (for Baker's algorithm) should look like:
6875 //
6876 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6877 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6878 // HeapReference<Object> ref = *src; // Original reference load.
6879 // bool is_gray = (rb_state == ReadBarrier::gray_ptr_);
6880 // if (is_gray) {
6881 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6882 // }
6883 //
6884 // Note: the original implementation in ReadBarrier::Barrier is
6885 // slightly more complex as:
6886 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006887 // the high-bits of rb_state, which are expected to be all zeroes
6888 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
6889 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006890 // - it performs additional checks that we do not do here for
6891 // performance reasons.
6892
6893 Register ref_reg = ref.AsRegister<Register>();
6894 Register temp_reg = temp.AsRegister<Register>();
6895 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6896
6897 // /* int32_t */ monitor = obj->monitor_
6898 __ movl(temp_reg, Address(obj, monitor_offset));
6899 if (needs_null_check) {
6900 MaybeRecordImplicitNullCheck(instruction);
6901 }
6902 // /* LockWord */ lock_word = LockWord(monitor)
6903 static_assert(sizeof(LockWord) == sizeof(int32_t),
6904 "art::LockWord and int32_t have different sizes.");
6905 // /* uint32_t */ rb_state = lock_word.ReadBarrierState()
6906 __ shrl(temp_reg, Immediate(LockWord::kReadBarrierStateShift));
6907 __ andl(temp_reg, Immediate(LockWord::kReadBarrierStateMask));
6908 static_assert(
6909 LockWord::kReadBarrierStateMask == ReadBarrier::rb_ptr_mask_,
6910 "art::LockWord::kReadBarrierStateMask is not equal to art::ReadBarrier::rb_ptr_mask_.");
6911
6912 // Load fence to prevent load-load reordering.
6913 // Note that this is a no-op, thanks to the x86 memory model.
6914 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6915
6916 // The actual reference load.
6917 // /* HeapReference<Object> */ ref = *src
6918 __ movl(ref_reg, src);
6919
6920 // Object* ref = ref_addr->AsMirrorPtr()
6921 __ MaybeUnpoisonHeapReference(ref_reg);
6922
6923 // Slow path used to mark the object `ref` when it is gray.
6924 SlowPathCode* slow_path =
6925 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(instruction, ref, ref);
6926 AddSlowPath(slow_path);
6927
6928 // if (rb_state == ReadBarrier::gray_ptr_)
6929 // ref = ReadBarrier::Mark(ref);
6930 __ cmpl(temp_reg, Immediate(ReadBarrier::gray_ptr_));
6931 __ j(kEqual, slow_path->GetEntryLabel());
6932 __ Bind(slow_path->GetExitLabel());
6933}
6934
6935void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
6936 Location out,
6937 Location ref,
6938 Location obj,
6939 uint32_t offset,
6940 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006941 DCHECK(kEmitCompilerReadBarrier);
6942
Roland Levillain7c1559a2015-12-15 10:55:36 +00006943 // Insert a slow path based read barrier *after* the reference load.
6944 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006945 // If heap poisoning is enabled, the unpoisoning of the loaded
6946 // reference will be carried out by the runtime within the slow
6947 // path.
6948 //
6949 // Note that `ref` currently does not get unpoisoned (when heap
6950 // poisoning is enabled), which is alright as the `ref` argument is
6951 // not used by the artReadBarrierSlow entry point.
6952 //
6953 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6954 SlowPathCode* slow_path = new (GetGraph()->GetArena())
6955 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
6956 AddSlowPath(slow_path);
6957
Roland Levillain0d5a2812015-11-13 10:07:31 +00006958 __ jmp(slow_path->GetEntryLabel());
6959 __ Bind(slow_path->GetExitLabel());
6960}
6961
Roland Levillain7c1559a2015-12-15 10:55:36 +00006962void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6963 Location out,
6964 Location ref,
6965 Location obj,
6966 uint32_t offset,
6967 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006968 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006969 // Baker's read barriers shall be handled by the fast path
6970 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
6971 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006972 // If heap poisoning is enabled, unpoisoning will be taken care of
6973 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006974 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006975 } else if (kPoisonHeapReferences) {
6976 __ UnpoisonHeapReference(out.AsRegister<Register>());
6977 }
6978}
6979
Roland Levillain7c1559a2015-12-15 10:55:36 +00006980void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6981 Location out,
6982 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00006983 DCHECK(kEmitCompilerReadBarrier);
6984
Roland Levillain7c1559a2015-12-15 10:55:36 +00006985 // Insert a slow path based read barrier *after* the GC root load.
6986 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00006987 // Note that GC roots are not affected by heap poisoning, so we do
6988 // not need to do anything special for this here.
6989 SlowPathCode* slow_path =
6990 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
6991 AddSlowPath(slow_path);
6992
Roland Levillain0d5a2812015-11-13 10:07:31 +00006993 __ jmp(slow_path->GetEntryLabel());
6994 __ Bind(slow_path->GetExitLabel());
6995}
6996
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01006997void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00006998 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00006999 LOG(FATAL) << "Unreachable";
7000}
7001
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007002void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007003 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007004 LOG(FATAL) << "Unreachable";
7005}
7006
Mark Mendellfe57faa2015-09-18 09:26:15 -04007007// Simple implementation of packed switch - generate cascaded compare/jumps.
7008void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7009 LocationSummary* locations =
7010 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7011 locations->SetInAt(0, Location::RequiresRegister());
7012}
7013
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007014void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7015 int32_t lower_bound,
7016 uint32_t num_entries,
7017 HBasicBlock* switch_block,
7018 HBasicBlock* default_block) {
7019 // Figure out the correct compare values and jump conditions.
7020 // Handle the first compare/branch as a special case because it might
7021 // jump to the default case.
7022 DCHECK_GT(num_entries, 2u);
7023 Condition first_condition;
7024 uint32_t index;
7025 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7026 if (lower_bound != 0) {
7027 first_condition = kLess;
7028 __ cmpl(value_reg, Immediate(lower_bound));
7029 __ j(first_condition, codegen_->GetLabelOf(default_block));
7030 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007031
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007032 index = 1;
7033 } else {
7034 // Handle all the compare/jumps below.
7035 first_condition = kBelow;
7036 index = 0;
7037 }
7038
7039 // Handle the rest of the compare/jumps.
7040 for (; index + 1 < num_entries; index += 2) {
7041 int32_t compare_to_value = lower_bound + index + 1;
7042 __ cmpl(value_reg, Immediate(compare_to_value));
7043 // Jump to successors[index] if value < case_value[index].
7044 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7045 // Jump to successors[index + 1] if value == case_value[index + 1].
7046 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7047 }
7048
7049 if (index != num_entries) {
7050 // There are an odd number of entries. Handle the last one.
7051 DCHECK_EQ(index + 1, num_entries);
7052 __ cmpl(value_reg, Immediate(lower_bound + index));
7053 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007054 }
7055
7056 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007057 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7058 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007059 }
7060}
7061
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007062void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7063 int32_t lower_bound = switch_instr->GetStartValue();
7064 uint32_t num_entries = switch_instr->GetNumEntries();
7065 LocationSummary* locations = switch_instr->GetLocations();
7066 Register value_reg = locations->InAt(0).AsRegister<Register>();
7067
7068 GenPackedSwitchWithCompares(value_reg,
7069 lower_bound,
7070 num_entries,
7071 switch_instr->GetBlock(),
7072 switch_instr->GetDefaultBlock());
7073}
7074
Mark Mendell805b3b52015-09-18 14:10:29 -04007075void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7076 LocationSummary* locations =
7077 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7078 locations->SetInAt(0, Location::RequiresRegister());
7079
7080 // Constant area pointer.
7081 locations->SetInAt(1, Location::RequiresRegister());
7082
7083 // And the temporary we need.
7084 locations->AddTemp(Location::RequiresRegister());
7085}
7086
7087void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7088 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007089 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007090 LocationSummary* locations = switch_instr->GetLocations();
7091 Register value_reg = locations->InAt(0).AsRegister<Register>();
7092 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7093
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007094 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7095 GenPackedSwitchWithCompares(value_reg,
7096 lower_bound,
7097 num_entries,
7098 switch_instr->GetBlock(),
7099 default_block);
7100 return;
7101 }
7102
Mark Mendell805b3b52015-09-18 14:10:29 -04007103 // Optimizing has a jump area.
7104 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7105 Register constant_area = locations->InAt(1).AsRegister<Register>();
7106
7107 // Remove the bias, if needed.
7108 if (lower_bound != 0) {
7109 __ leal(temp_reg, Address(value_reg, -lower_bound));
7110 value_reg = temp_reg;
7111 }
7112
7113 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007114 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007115 __ cmpl(value_reg, Immediate(num_entries - 1));
7116 __ j(kAbove, codegen_->GetLabelOf(default_block));
7117
7118 // We are in the range of the table.
7119 // Load (target-constant_area) from the jump table, indexing by the value.
7120 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7121
7122 // Compute the actual target address by adding in constant_area.
7123 __ addl(temp_reg, constant_area);
7124
7125 // And jump.
7126 __ jmp(temp_reg);
7127}
7128
Mark Mendell0616ae02015-04-17 12:49:27 -04007129void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7130 HX86ComputeBaseMethodAddress* insn) {
7131 LocationSummary* locations =
7132 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7133 locations->SetOut(Location::RequiresRegister());
7134}
7135
7136void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7137 HX86ComputeBaseMethodAddress* insn) {
7138 LocationSummary* locations = insn->GetLocations();
7139 Register reg = locations->Out().AsRegister<Register>();
7140
7141 // Generate call to next instruction.
7142 Label next_instruction;
7143 __ call(&next_instruction);
7144 __ Bind(&next_instruction);
7145
7146 // Remember this offset for later use with constant area.
7147 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7148
7149 // Grab the return address off the stack.
7150 __ popl(reg);
7151}
7152
7153void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7154 HX86LoadFromConstantTable* insn) {
7155 LocationSummary* locations =
7156 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7157
7158 locations->SetInAt(0, Location::RequiresRegister());
7159 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7160
7161 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007162 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007163 return;
7164 }
7165
7166 switch (insn->GetType()) {
7167 case Primitive::kPrimFloat:
7168 case Primitive::kPrimDouble:
7169 locations->SetOut(Location::RequiresFpuRegister());
7170 break;
7171
7172 case Primitive::kPrimInt:
7173 locations->SetOut(Location::RequiresRegister());
7174 break;
7175
7176 default:
7177 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7178 }
7179}
7180
7181void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007182 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007183 return;
7184 }
7185
7186 LocationSummary* locations = insn->GetLocations();
7187 Location out = locations->Out();
7188 Register const_area = locations->InAt(0).AsRegister<Register>();
7189 HConstant *value = insn->GetConstant();
7190
7191 switch (insn->GetType()) {
7192 case Primitive::kPrimFloat:
7193 __ movss(out.AsFpuRegister<XmmRegister>(),
7194 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7195 break;
7196
7197 case Primitive::kPrimDouble:
7198 __ movsd(out.AsFpuRegister<XmmRegister>(),
7199 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7200 break;
7201
7202 case Primitive::kPrimInt:
7203 __ movl(out.AsRegister<Register>(),
7204 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7205 break;
7206
7207 default:
7208 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7209 }
7210}
7211
Mark Mendell0616ae02015-04-17 12:49:27 -04007212/**
7213 * Class to handle late fixup of offsets into constant area.
7214 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007215class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007216 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007217 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7218 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7219
7220 protected:
7221 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7222
7223 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007224
7225 private:
7226 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7227 // Patch the correct offset for the instruction. The place to patch is the
7228 // last 4 bytes of the instruction.
7229 // The value to patch is the distance from the offset in the constant area
7230 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007231 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
7232 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();;
Mark Mendell0616ae02015-04-17 12:49:27 -04007233
7234 // Patch in the right value.
7235 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7236 }
7237
Mark Mendell0616ae02015-04-17 12:49:27 -04007238 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007239 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007240};
7241
Mark Mendell805b3b52015-09-18 14:10:29 -04007242/**
7243 * Class to handle late fixup of offsets to a jump table that will be created in the
7244 * constant area.
7245 */
7246class JumpTableRIPFixup : public RIPFixup {
7247 public:
7248 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7249 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7250
7251 void CreateJumpTable() {
7252 X86Assembler* assembler = codegen_->GetAssembler();
7253
7254 // Ensure that the reference to the jump table has the correct offset.
7255 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7256 SetOffset(offset_in_constant_table);
7257
7258 // The label values in the jump table are computed relative to the
7259 // instruction addressing the constant area.
7260 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7261
7262 // Populate the jump table with the correct values for the jump table.
7263 int32_t num_entries = switch_instr_->GetNumEntries();
7264 HBasicBlock* block = switch_instr_->GetBlock();
7265 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7266 // The value that we want is the target offset - the position of the table.
7267 for (int32_t i = 0; i < num_entries; i++) {
7268 HBasicBlock* b = successors[i];
7269 Label* l = codegen_->GetLabelOf(b);
7270 DCHECK(l->IsBound());
7271 int32_t offset_to_block = l->Position() - relative_offset;
7272 assembler->AppendInt32(offset_to_block);
7273 }
7274 }
7275
7276 private:
7277 const HX86PackedSwitch* switch_instr_;
7278};
7279
7280void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7281 // Generate the constant area if needed.
7282 X86Assembler* assembler = GetAssembler();
7283 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7284 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7285 // byte values.
7286 assembler->Align(4, 0);
7287 constant_area_start_ = assembler->CodeSize();
7288
7289 // Populate any jump tables.
7290 for (auto jump_table : fixups_to_jump_tables_) {
7291 jump_table->CreateJumpTable();
7292 }
7293
7294 // And now add the constant area to the generated code.
7295 assembler->AddConstantArea();
7296 }
7297
7298 // And finish up.
7299 CodeGenerator::Finalize(allocator);
7300}
7301
Mark Mendell0616ae02015-04-17 12:49:27 -04007302Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7303 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7304 return Address(reg, kDummy32BitOffset, fixup);
7305}
7306
7307Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7308 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7309 return Address(reg, kDummy32BitOffset, fixup);
7310}
7311
7312Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7313 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7314 return Address(reg, kDummy32BitOffset, fixup);
7315}
7316
7317Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7318 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7319 return Address(reg, kDummy32BitOffset, fixup);
7320}
7321
Aart Bika19616e2016-02-01 18:57:58 -08007322void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7323 if (value == 0) {
7324 __ xorl(dest, dest);
7325 } else {
7326 __ movl(dest, Immediate(value));
7327 }
7328}
7329
7330void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7331 if (value == 0) {
7332 __ testl(dest, dest);
7333 } else {
7334 __ cmpl(dest, Immediate(value));
7335 }
7336}
7337
Mark Mendell805b3b52015-09-18 14:10:29 -04007338Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7339 Register reg,
7340 Register value) {
7341 // Create a fixup to be used to create and address the jump table.
7342 JumpTableRIPFixup* table_fixup =
7343 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7344
7345 // We have to populate the jump tables.
7346 fixups_to_jump_tables_.push_back(table_fixup);
7347
7348 // We want a scaled address, as we are extracting the correct offset from the table.
7349 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7350}
7351
Andreas Gampe85b62f22015-09-09 13:15:38 -07007352// TODO: target as memory.
7353void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7354 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007355 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007356 return;
7357 }
7358
7359 DCHECK_NE(type, Primitive::kPrimVoid);
7360
7361 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7362 if (target.Equals(return_loc)) {
7363 return;
7364 }
7365
7366 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7367 // with the else branch.
7368 if (type == Primitive::kPrimLong) {
7369 HParallelMove parallel_move(GetGraph()->GetArena());
7370 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7371 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7372 GetMoveResolver()->EmitNativeCode(&parallel_move);
7373 } else {
7374 // Let the parallel move resolver take care of all of this.
7375 HParallelMove parallel_move(GetGraph()->GetArena());
7376 parallel_move.AddMove(return_loc, target, type, nullptr);
7377 GetMoveResolver()->EmitNativeCode(&parallel_move);
7378 }
7379}
7380
Roland Levillain4d027112015-07-01 15:41:14 +01007381#undef __
7382
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007383} // namespace x86
7384} // namespace art