blob: 8612a67c8b6fd2e6782dbd486a4b5b5fb469459d [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
Roland Levillain7cbd27f2016-08-11 23:53:33 +010050// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
51#define __ down_cast<X86Assembler*>(codegen->GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -070052#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kX86PointerSize, 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 }
Serban Constantinescuba45db02016-07-12 22:53:02 +010065 x86_codegen->InvokeRuntime(kQuickThrowNullPointer,
Alexandre Rames8158f282015-08-07 10:26:17 +010066 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());
Serban Constantinescuba45db02016-07-12 22:53:02 +010087 x86_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +000088 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
Calin Juravled0d48522014-11-04 16:40:20 +000089 }
90
Alexandre Rames8158f282015-08-07 10:26:17 +010091 bool IsFatal() const OVERRIDE { return true; }
92
Alexandre Rames9931f312015-06-19 14:47:01 +010093 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathX86"; }
94
Calin Juravled0d48522014-11-04 16:40:20 +000095 private:
Calin Juravled0d48522014-11-04 16:40:20 +000096 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathX86);
97};
98
Andreas Gampe85b62f22015-09-09 13:15:38 -070099class DivRemMinusOneSlowPathX86 : public SlowPathCode {
Calin Juravled0d48522014-11-04 16:40:20 +0000100 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000101 DivRemMinusOneSlowPathX86(HInstruction* instruction, Register reg, bool is_div)
102 : SlowPathCode(instruction), reg_(reg), is_div_(is_div) {}
Calin Juravled0d48522014-11-04 16:40:20 +0000103
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000104 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Calin Juravled0d48522014-11-04 16:40:20 +0000105 __ Bind(GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +0000106 if (is_div_) {
107 __ negl(reg_);
108 } else {
109 __ movl(reg_, Immediate(0));
110 }
Calin Juravled0d48522014-11-04 16:40:20 +0000111 __ jmp(GetExitLabel());
112 }
113
Alexandre Rames9931f312015-06-19 14:47:01 +0100114 const char* GetDescription() const OVERRIDE { return "DivRemMinusOneSlowPathX86"; }
115
Calin Juravled0d48522014-11-04 16:40:20 +0000116 private:
117 Register reg_;
Calin Juravlebacfec32014-11-14 15:54:36 +0000118 bool is_div_;
119 DISALLOW_COPY_AND_ASSIGN(DivRemMinusOneSlowPathX86);
Calin Juravled0d48522014-11-04 16:40:20 +0000120};
121
Andreas Gampe85b62f22015-09-09 13:15:38 -0700122class BoundsCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100123 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000124 explicit BoundsCheckSlowPathX86(HBoundsCheck* instruction) : SlowPathCode(instruction) {}
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100125
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000126 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100127 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100128 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100129 __ Bind(GetEntryLabel());
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000130 // We're moving two locations to locations that could overlap, so we need a parallel
131 // move resolver.
David Brazdil77a48ae2015-09-15 12:34:04 +0000132 if (instruction_->CanThrowIntoCatchBlock()) {
133 // Live registers will be restored in the catch block if caught.
134 SaveLiveRegisters(codegen, instruction_->GetLocations());
135 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400136
137 // Are we using an array length from memory?
138 HInstruction* array_length = instruction_->InputAt(1);
139 Location length_loc = locations->InAt(1);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100140 InvokeRuntimeCallingConvention calling_convention;
Mark Mendellee8d9712016-07-12 11:13:15 -0400141 if (array_length->IsArrayLength() && array_length->IsEmittedAtUseSite()) {
142 // Load the array length into our temporary.
143 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
144 Location array_loc = array_length->GetLocations()->InAt(0);
145 Address array_len(array_loc.AsRegister<Register>(), len_offset);
146 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(1));
147 // Check for conflicts with index.
148 if (length_loc.Equals(locations->InAt(0))) {
149 // We know we aren't using parameter 2.
150 length_loc = Location::RegisterLocation(calling_convention.GetRegisterAt(2));
151 }
152 __ movl(length_loc.AsRegister<Register>(), array_len);
jessicahandojo4877b792016-09-08 19:49:13 -0700153 if (mirror::kUseStringCompression) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +0100154 __ shrl(length_loc.AsRegister<Register>(), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -0700155 }
Mark Mendellee8d9712016-07-12 11:13:15 -0400156 }
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000157 x86_codegen->EmitParallelMoves(
Serban Constantinescu5a6cc492015-08-13 15:20:25 +0100158 locations->InAt(0),
Nicolas Geoffrayf0e39372014-11-12 17:50:07 +0000159 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Nicolas Geoffray90218252015-04-15 11:56:51 +0100160 Primitive::kPrimInt,
Mark Mendellee8d9712016-07-12 11:13:15 -0400161 length_loc,
Nicolas Geoffray90218252015-04-15 11:56:51 +0100162 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
163 Primitive::kPrimInt);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100164 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
165 ? kQuickThrowStringBounds
166 : kQuickThrowArrayBounds;
167 x86_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100168 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Roland Levillain888d0672015-11-23 18:53:50 +0000169 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100170 }
171
Alexandre Rames8158f282015-08-07 10:26:17 +0100172 bool IsFatal() const OVERRIDE { return true; }
173
Alexandre Rames9931f312015-06-19 14:47:01 +0100174 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathX86"; }
175
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100176 private:
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +0100177 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathX86);
178};
179
Andreas Gampe85b62f22015-09-09 13:15:38 -0700180class SuspendCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000181 public:
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000182 SuspendCheckSlowPathX86(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000183 : SlowPathCode(instruction), successor_(successor) {}
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000184
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000185 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100186 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000187 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100188 x86_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000189 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100190 if (successor_ == nullptr) {
191 __ jmp(GetReturnLabel());
192 } else {
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +0100193 __ jmp(x86_codegen->GetLabelOf(successor_));
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100194 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000195 }
196
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100197 Label* GetReturnLabel() {
198 DCHECK(successor_ == nullptr);
199 return &return_label_;
200 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000201
Nicolas Geoffraydb216f42015-05-05 17:02:20 +0100202 HBasicBlock* GetSuccessor() const {
203 return successor_;
204 }
205
Alexandre Rames9931f312015-06-19 14:47:01 +0100206 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathX86"; }
207
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000208 private:
Nicolas Geoffray3c049742014-09-24 18:10:46 +0100209 HBasicBlock* const successor_;
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +0000210 Label return_label_;
211
212 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathX86);
213};
214
Vladimir Markoaad75c62016-10-03 08:46:48 +0000215class LoadStringSlowPathX86 : public SlowPathCode {
216 public:
217 explicit LoadStringSlowPathX86(HLoadString* instruction): SlowPathCode(instruction) {}
218
219 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
220 LocationSummary* locations = instruction_->GetLocations();
221 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
222
223 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
224 __ Bind(GetEntryLabel());
225 SaveLiveRegisters(codegen, locations);
226
227 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800228 const uint32_t string_index = instruction_->AsLoadString()->GetStringIndex().index_;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000229 __ movl(calling_convention.GetRegisterAt(0), Immediate(string_index));
230 x86_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
231 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
232 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
233 RestoreLiveRegisters(codegen, locations);
234
235 // Store the resolved String to the BSS entry.
236 Register method_address = locations->InAt(0).AsRegister<Register>();
237 __ movl(Address(method_address, CodeGeneratorX86::kDummy32BitOffset),
238 locations->Out().AsRegister<Register>());
239 Label* fixup_label = x86_codegen->NewStringBssEntryPatch(instruction_->AsLoadString());
240 __ Bind(fixup_label);
241
242 __ jmp(GetExitLabel());
243 }
244
245 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathX86"; }
246
247 private:
248 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathX86);
249};
250
Andreas Gampe85b62f22015-09-09 13:15:38 -0700251class LoadClassSlowPathX86 : public SlowPathCode {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000252 public:
253 LoadClassSlowPathX86(HLoadClass* cls,
254 HInstruction* at,
255 uint32_t dex_pc,
256 bool do_clinit)
David Srbecky9cd6d372016-02-09 15:24:47 +0000257 : SlowPathCode(at), cls_(cls), at_(at), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000258 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
259 }
260
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000261 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000262 LocationSummary* locations = at_->GetLocations();
263 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
264 __ Bind(GetEntryLabel());
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000265 SaveLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000266
267 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -0800268 __ movl(calling_convention.GetRegisterAt(0), Immediate(cls_->GetTypeIndex().index_));
Serban Constantinescuba45db02016-07-12 22:53:02 +0100269 x86_codegen->InvokeRuntime(do_clinit_ ? kQuickInitializeStaticStorage
270 : kQuickInitializeType,
Alexandre Rames8158f282015-08-07 10:26:17 +0100271 at_, dex_pc_, this);
Roland Levillain888d0672015-11-23 18:53:50 +0000272 if (do_clinit_) {
273 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
274 } else {
275 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
276 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000277
278 // Move the class to the desired location.
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000279 Location out = locations->Out();
280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
282 x86_codegen->Move32(out, Location::RegisterLocation(EAX));
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000283 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000284
Nicolas Geoffraya8ac9132015-03-13 16:36:36 +0000285 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000286 __ jmp(GetExitLabel());
287 }
288
Alexandre Rames9931f312015-06-19 14:47:01 +0100289 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathX86"; }
290
Nicolas Geoffray424f6762014-11-03 14:51:25 +0000291 private:
292 // The class this slow path will load.
293 HLoadClass* const cls_;
294
295 // The instruction where this slow path is happening.
296 // (Might be the load class or an initialization check).
297 HInstruction* const at_;
298
299 // The dex PC of `at_`.
300 const uint32_t dex_pc_;
301
302 // Whether to initialize the class.
303 const bool do_clinit_;
304
305 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathX86);
306};
307
Andreas Gampe85b62f22015-09-09 13:15:38 -0700308class TypeCheckSlowPathX86 : public SlowPathCode {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000309 public:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000310 TypeCheckSlowPathX86(HInstruction* instruction, bool is_fatal)
David Srbecky9cd6d372016-02-09 15:24:47 +0000311 : SlowPathCode(instruction), is_fatal_(is_fatal) {}
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000312
Alexandre Rames2ed20af2015-03-06 13:55:35 +0000313 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000314 LocationSummary* locations = instruction_->GetLocations();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000315 DCHECK(instruction_->IsCheckCast()
316 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000317
318 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
319 __ Bind(GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000320
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000321 if (!is_fatal_) {
322 SaveLiveRegisters(codegen, locations);
323 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000324
325 // We're moving two locations to locations that could overlap, so we need a parallel
326 // move resolver.
327 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800328 x86_codegen->EmitParallelMoves(locations->InAt(0),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800329 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
330 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800331 locations->InAt(1),
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800332 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
333 Primitive::kPrimNot);
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000334 if (instruction_->IsInstanceOf()) {
Serban Constantinescuba45db02016-07-12 22:53:02 +0100335 x86_codegen->InvokeRuntime(kQuickInstanceofNonTrivial,
Alexandre Rames8158f282015-08-07 10:26:17 +0100336 instruction_,
337 instruction_->GetDexPc(),
338 this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800339 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000340 } else {
341 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800342 x86_codegen->InvokeRuntime(kQuickCheckInstanceOf,
343 instruction_,
344 instruction_->GetDexPc(),
345 this);
346 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +0000347 }
348
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000349 if (!is_fatal_) {
350 if (instruction_->IsInstanceOf()) {
351 x86_codegen->Move32(locations->Out(), Location::RegisterLocation(EAX));
352 }
353 RestoreLiveRegisters(codegen, locations);
Nicolas Geoffray75374372015-09-17 17:12:19 +0000354
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000355 __ jmp(GetExitLabel());
356 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000357 }
358
Alexandre Rames9931f312015-06-19 14:47:01 +0100359 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathX86"; }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000360 bool IsFatal() const OVERRIDE { return is_fatal_; }
Alexandre Rames9931f312015-06-19 14:47:01 +0100361
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000362 private:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +0000363 const bool is_fatal_;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +0000364
365 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathX86);
366};
367
Andreas Gampe85b62f22015-09-09 13:15:38 -0700368class DeoptimizationSlowPathX86 : public SlowPathCode {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700369 public:
Aart Bik42249c32016-01-07 15:33:50 -0800370 explicit DeoptimizationSlowPathX86(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000371 : SlowPathCode(instruction) {}
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700372
373 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexandre Rames8158f282015-08-07 10:26:17 +0100374 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -0700375 __ Bind(GetEntryLabel());
Serban Constantinescuba45db02016-07-12 22:53:02 +0100376 x86_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), 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);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100415 x86_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
Roland Levillain888d0672015-11-23 18:53:50 +0000416 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100417 RestoreLiveRegisters(codegen, locations);
418 __ jmp(GetExitLabel());
419 }
420
421 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathX86"; }
422
423 private:
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +0100424 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathX86);
425};
426
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100427// Slow path marking an object reference `ref` during a read
428// barrier. The field `obj.field` in the object `obj` holding this
429// reference does not get updated by this slow path after marking (see
430// ReadBarrierMarkAndUpdateFieldSlowPathX86 below for that).
431//
432// This means that after the execution of this slow path, `ref` will
433// always be up-to-date, but `obj.field` may not; i.e., after the
434// flip, `ref` will be a to-space reference, but `obj.field` will
435// probably still be a from-space reference (unless it gets updated by
436// another thread, or if another thread installed another object
437// reference (different from `ref`) in `obj.field`).
Roland Levillain7c1559a2015-12-15 10:55:36 +0000438class ReadBarrierMarkSlowPathX86 : public SlowPathCode {
439 public:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100440 ReadBarrierMarkSlowPathX86(HInstruction* instruction,
441 Location ref,
442 bool unpoison_ref_before_marking)
443 : SlowPathCode(instruction),
444 ref_(ref),
445 unpoison_ref_before_marking_(unpoison_ref_before_marking) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000446 DCHECK(kEmitCompilerReadBarrier);
447 }
448
449 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathX86"; }
450
451 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
452 LocationSummary* locations = instruction_->GetLocations();
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100453 Register ref_reg = ref_.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +0000454 DCHECK(locations->CanCall());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100455 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000456 DCHECK(instruction_->IsInstanceFieldGet() ||
457 instruction_->IsStaticFieldGet() ||
458 instruction_->IsArrayGet() ||
Roland Levillain16d9f942016-08-25 17:27:56 +0100459 instruction_->IsArraySet() ||
Roland Levillain7c1559a2015-12-15 10:55:36 +0000460 instruction_->IsLoadClass() ||
461 instruction_->IsLoadString() ||
462 instruction_->IsInstanceOf() ||
Roland Levillain3d312422016-06-23 13:53:42 +0100463 instruction_->IsCheckCast() ||
Roland Levillain0b671c02016-08-19 12:02:34 +0100464 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
465 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
Roland Levillain7c1559a2015-12-15 10:55:36 +0000466 << "Unexpected instruction in read barrier marking slow path: "
467 << instruction_->DebugName();
468
469 __ Bind(GetEntryLabel());
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100470 if (unpoison_ref_before_marking_) {
Vladimir Marko953437b2016-08-24 08:30:46 +0000471 // Object* ref = ref_addr->AsMirrorPtr()
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100472 __ MaybeUnpoisonHeapReference(ref_reg);
Vladimir Marko953437b2016-08-24 08:30:46 +0000473 }
Roland Levillain4359e612016-07-20 11:32:19 +0100474 // No need to save live registers; it's taken care of by the
475 // entrypoint. Also, there is no need to update the stack mask,
476 // as this runtime call will not trigger a garbage collection.
Roland Levillain7c1559a2015-12-15 10:55:36 +0000477 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100478 DCHECK_NE(ref_reg, ESP);
479 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
Roland Levillain02b75802016-07-13 11:54:35 +0100480 // "Compact" slow path, saving two moves.
481 //
482 // Instead of using the standard runtime calling convention (input
483 // and output in EAX):
484 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100485 // EAX <- ref
Roland Levillain02b75802016-07-13 11:54:35 +0100486 // EAX <- ReadBarrierMark(EAX)
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100487 // ref <- EAX
Roland Levillain02b75802016-07-13 11:54:35 +0100488 //
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100489 // we just use rX (the register containing `ref`) as input and output
Roland Levillain02b75802016-07-13 11:54:35 +0100490 // of a dedicated entrypoint:
491 //
492 // rX <- ReadBarrierMarkRegX(rX)
493 //
494 int32_t entry_point_offset =
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100495 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
Roland Levillaindec8f632016-07-22 17:10:06 +0100496 // This runtime call does not require a stack map.
497 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
Roland Levillain7c1559a2015-12-15 10:55:36 +0000498 __ jmp(GetExitLabel());
499 }
500
501 private:
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100502 // The location (register) of the marked object reference.
503 const Location ref_;
504 // Should the reference in `ref_` be unpoisoned prior to marking it?
505 const bool unpoison_ref_before_marking_;
Roland Levillain7c1559a2015-12-15 10:55:36 +0000506
507 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathX86);
508};
509
Roland Levillaina1aa3b12016-10-26 13:03:38 +0100510// Slow path marking an object reference `ref` during a read barrier,
511// and if needed, atomically updating the field `obj.field` in the
512// object `obj` holding this reference after marking (contrary to
513// ReadBarrierMarkSlowPathX86 above, which never tries to update
514// `obj.field`).
515//
516// This means that after the execution of this slow path, both `ref`
517// and `obj.field` will be up-to-date; i.e., after the flip, both will
518// hold the same to-space reference (unless another thread installed
519// another object reference (different from `ref`) in `obj.field`).
520class ReadBarrierMarkAndUpdateFieldSlowPathX86 : public SlowPathCode {
521 public:
522 ReadBarrierMarkAndUpdateFieldSlowPathX86(HInstruction* instruction,
523 Location ref,
524 Register obj,
525 const Address& field_addr,
526 bool unpoison_ref_before_marking,
527 Register temp)
528 : SlowPathCode(instruction),
529 ref_(ref),
530 obj_(obj),
531 field_addr_(field_addr),
532 unpoison_ref_before_marking_(unpoison_ref_before_marking),
533 temp_(temp) {
534 DCHECK(kEmitCompilerReadBarrier);
535 }
536
537 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkAndUpdateFieldSlowPathX86"; }
538
539 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
540 LocationSummary* locations = instruction_->GetLocations();
541 Register ref_reg = ref_.AsRegister<Register>();
542 DCHECK(locations->CanCall());
543 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
544 // This slow path is only used by the UnsafeCASObject intrinsic.
545 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
546 << "Unexpected instruction in read barrier marking and field updating slow path: "
547 << instruction_->DebugName();
548 DCHECK(instruction_->GetLocations()->Intrinsified());
549 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
550
551 __ Bind(GetEntryLabel());
552 if (unpoison_ref_before_marking_) {
553 // Object* ref = ref_addr->AsMirrorPtr()
554 __ MaybeUnpoisonHeapReference(ref_reg);
555 }
556
557 // Save the old (unpoisoned) reference.
558 __ movl(temp_, ref_reg);
559
560 // No need to save live registers; it's taken care of by the
561 // entrypoint. Also, there is no need to update the stack mask,
562 // as this runtime call will not trigger a garbage collection.
563 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
564 DCHECK_NE(ref_reg, ESP);
565 DCHECK(0 <= ref_reg && ref_reg < kNumberOfCpuRegisters) << ref_reg;
566 // "Compact" slow path, saving two moves.
567 //
568 // Instead of using the standard runtime calling convention (input
569 // and output in EAX):
570 //
571 // EAX <- ref
572 // EAX <- ReadBarrierMark(EAX)
573 // ref <- EAX
574 //
575 // we just use rX (the register containing `ref`) as input and output
576 // of a dedicated entrypoint:
577 //
578 // rX <- ReadBarrierMarkRegX(rX)
579 //
580 int32_t entry_point_offset =
581 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kX86PointerSize>(ref_reg);
582 // This runtime call does not require a stack map.
583 x86_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset, instruction_, this);
584
585 // If the new reference is different from the old reference,
586 // update the field in the holder (`*field_addr`).
587 //
588 // Note that this field could also hold a different object, if
589 // another thread had concurrently changed it. In that case, the
590 // LOCK CMPXCHGL instruction in the compare-and-set (CAS)
591 // operation below would abort the CAS, leaving the field as-is.
592 NearLabel done;
593 __ cmpl(temp_, ref_reg);
594 __ j(kEqual, &done);
595
596 // Update the the holder's field atomically. This may fail if
597 // mutator updates before us, but it's OK. This is achieved
598 // using a strong compare-and-set (CAS) operation with relaxed
599 // memory synchronization ordering, where the expected value is
600 // the old reference and the desired value is the new reference.
601 // This operation is implemented with a 32-bit LOCK CMPXLCHG
602 // instruction, which requires the expected value (the old
603 // reference) to be in EAX. Save EAX beforehand, and move the
604 // expected value (stored in `temp_`) into EAX.
605 __ pushl(EAX);
606 __ movl(EAX, temp_);
607
608 // Convenience aliases.
609 Register base = obj_;
610 Register expected = EAX;
611 Register value = ref_reg;
612
613 bool base_equals_value = (base == value);
614 if (kPoisonHeapReferences) {
615 if (base_equals_value) {
616 // If `base` and `value` are the same register location, move
617 // `value` to a temporary register. This way, poisoning
618 // `value` won't invalidate `base`.
619 value = temp_;
620 __ movl(value, base);
621 }
622
623 // Check that the register allocator did not assign the location
624 // of `expected` (EAX) to `value` nor to `base`, so that heap
625 // poisoning (when enabled) works as intended below.
626 // - If `value` were equal to `expected`, both references would
627 // be poisoned twice, meaning they would not be poisoned at
628 // all, as heap poisoning uses address negation.
629 // - If `base` were equal to `expected`, poisoning `expected`
630 // would invalidate `base`.
631 DCHECK_NE(value, expected);
632 DCHECK_NE(base, expected);
633
634 __ PoisonHeapReference(expected);
635 __ PoisonHeapReference(value);
636 }
637
638 __ LockCmpxchgl(field_addr_, value);
639
640 // If heap poisoning is enabled, we need to unpoison the values
641 // that were poisoned earlier.
642 if (kPoisonHeapReferences) {
643 if (base_equals_value) {
644 // `value` has been moved to a temporary register, no need
645 // to unpoison it.
646 } else {
647 __ UnpoisonHeapReference(value);
648 }
649 // No need to unpoison `expected` (EAX), as it is be overwritten below.
650 }
651
652 // Restore EAX.
653 __ popl(EAX);
654
655 __ Bind(&done);
656 __ jmp(GetExitLabel());
657 }
658
659 private:
660 // The location (register) of the marked object reference.
661 const Location ref_;
662 // The register containing the object holding the marked object reference field.
663 const Register obj_;
664 // The address of the marked reference field. The base of this address must be `obj_`.
665 const Address field_addr_;
666
667 // Should the reference in `ref_` be unpoisoned prior to marking it?
668 const bool unpoison_ref_before_marking_;
669
670 const Register temp_;
671
672 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathX86);
673};
674
Roland Levillain0d5a2812015-11-13 10:07:31 +0000675// Slow path generating a read barrier for a heap reference.
676class ReadBarrierForHeapReferenceSlowPathX86 : public SlowPathCode {
677 public:
678 ReadBarrierForHeapReferenceSlowPathX86(HInstruction* instruction,
679 Location out,
680 Location ref,
681 Location obj,
682 uint32_t offset,
683 Location index)
David Srbecky9cd6d372016-02-09 15:24:47 +0000684 : SlowPathCode(instruction),
Roland Levillain0d5a2812015-11-13 10:07:31 +0000685 out_(out),
686 ref_(ref),
687 obj_(obj),
688 offset_(offset),
689 index_(index) {
690 DCHECK(kEmitCompilerReadBarrier);
691 // If `obj` is equal to `out` or `ref`, it means the initial object
692 // has been overwritten by (or after) the heap object reference load
693 // to be instrumented, e.g.:
694 //
695 // __ movl(out, Address(out, offset));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000696 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000697 //
698 // In that case, we have lost the information about the original
699 // object, and the emitted read barrier cannot work properly.
700 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
701 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
702 }
703
704 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
705 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
706 LocationSummary* locations = instruction_->GetLocations();
707 Register reg_out = out_.AsRegister<Register>();
708 DCHECK(locations->CanCall());
709 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain3d312422016-06-23 13:53:42 +0100710 DCHECK(instruction_->IsInstanceFieldGet() ||
711 instruction_->IsStaticFieldGet() ||
712 instruction_->IsArrayGet() ||
713 instruction_->IsInstanceOf() ||
714 instruction_->IsCheckCast() ||
Roland Levillaindec8f632016-07-22 17:10:06 +0100715 (instruction_->IsInvokeVirtual()) && instruction_->GetLocations()->Intrinsified())
Roland Levillain7c1559a2015-12-15 10:55:36 +0000716 << "Unexpected instruction in read barrier for heap reference slow path: "
717 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000718
719 __ Bind(GetEntryLabel());
720 SaveLiveRegisters(codegen, locations);
721
722 // We may have to change the index's value, but as `index_` is a
723 // constant member (like other "inputs" of this slow path),
724 // introduce a copy of it, `index`.
725 Location index = index_;
726 if (index_.IsValid()) {
Roland Levillain3d312422016-06-23 13:53:42 +0100727 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
Roland Levillain0d5a2812015-11-13 10:07:31 +0000728 if (instruction_->IsArrayGet()) {
729 // Compute the actual memory offset and store it in `index`.
730 Register index_reg = index_.AsRegister<Register>();
731 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
732 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
733 // We are about to change the value of `index_reg` (see the
734 // calls to art::x86::X86Assembler::shll and
735 // art::x86::X86Assembler::AddImmediate below), but it has
736 // not been saved by the previous call to
737 // art::SlowPathCode::SaveLiveRegisters, as it is a
738 // callee-save register --
739 // art::SlowPathCode::SaveLiveRegisters does not consider
740 // callee-save registers, as it has been designed with the
741 // assumption that callee-save registers are supposed to be
742 // handled by the called function. So, as a callee-save
743 // register, `index_reg` _would_ eventually be saved onto
744 // the stack, but it would be too late: we would have
745 // changed its value earlier. Therefore, we manually save
746 // it here into another freely available register,
747 // `free_reg`, chosen of course among the caller-save
748 // registers (as a callee-save `free_reg` register would
749 // exhibit the same problem).
750 //
751 // Note we could have requested a temporary register from
752 // the register allocator instead; but we prefer not to, as
753 // this is a slow path, and we know we can find a
754 // caller-save register that is available.
755 Register free_reg = FindAvailableCallerSaveRegister(codegen);
756 __ movl(free_reg, index_reg);
757 index_reg = free_reg;
758 index = Location::RegisterLocation(index_reg);
759 } else {
760 // The initial register stored in `index_` has already been
761 // saved in the call to art::SlowPathCode::SaveLiveRegisters
762 // (as it is not a callee-save register), so we can freely
763 // use it.
764 }
765 // Shifting the index value contained in `index_reg` by the scale
766 // factor (2) cannot overflow in practice, as the runtime is
767 // unable to allocate object arrays with a size larger than
768 // 2^26 - 1 (that is, 2^28 - 4 bytes).
769 __ shll(index_reg, Immediate(TIMES_4));
770 static_assert(
771 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
772 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
773 __ AddImmediate(index_reg, Immediate(offset_));
774 } else {
Roland Levillain3d312422016-06-23 13:53:42 +0100775 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
776 // intrinsics, `index_` is not shifted by a scale factor of 2
777 // (as in the case of ArrayGet), as it is actually an offset
778 // to an object field within an object.
779 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000780 DCHECK(instruction_->GetLocations()->Intrinsified());
781 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
782 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
783 << instruction_->AsInvoke()->GetIntrinsic();
784 DCHECK_EQ(offset_, 0U);
785 DCHECK(index_.IsRegisterPair());
786 // UnsafeGet's offset location is a register pair, the low
787 // part contains the correct offset.
788 index = index_.ToLow();
789 }
790 }
791
792 // We're moving two or three locations to locations that could
793 // overlap, so we need a parallel move resolver.
794 InvokeRuntimeCallingConvention calling_convention;
795 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
796 parallel_move.AddMove(ref_,
797 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
798 Primitive::kPrimNot,
799 nullptr);
800 parallel_move.AddMove(obj_,
801 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
802 Primitive::kPrimNot,
803 nullptr);
804 if (index.IsValid()) {
805 parallel_move.AddMove(index,
806 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
807 Primitive::kPrimInt,
808 nullptr);
809 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
810 } else {
811 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
812 __ movl(calling_convention.GetRegisterAt(2), Immediate(offset_));
813 }
Serban Constantinescuba45db02016-07-12 22:53:02 +0100814 x86_codegen->InvokeRuntime(kQuickReadBarrierSlow, instruction_, instruction_->GetDexPc(), this);
Roland Levillain0d5a2812015-11-13 10:07:31 +0000815 CheckEntrypointTypes<
816 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
817 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
818
819 RestoreLiveRegisters(codegen, locations);
820 __ jmp(GetExitLabel());
821 }
822
823 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathX86"; }
824
825 private:
826 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
827 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
828 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
829 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
830 if (i != ref && i != obj && !codegen->IsCoreCalleeSaveRegister(i)) {
831 return static_cast<Register>(i);
832 }
833 }
834 // We shall never fail to find a free caller-save register, as
835 // there are more than two core caller-save registers on x86
836 // (meaning it is possible to find one which is different from
837 // `ref` and `obj`).
838 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
839 LOG(FATAL) << "Could not find a free caller-save register";
840 UNREACHABLE();
841 }
842
Roland Levillain0d5a2812015-11-13 10:07:31 +0000843 const Location out_;
844 const Location ref_;
845 const Location obj_;
846 const uint32_t offset_;
847 // An additional location containing an index to an array.
848 // Only used for HArrayGet and the UnsafeGetObject &
849 // UnsafeGetObjectVolatile intrinsics.
850 const Location index_;
851
852 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathX86);
853};
854
855// Slow path generating a read barrier for a GC root.
856class ReadBarrierForRootSlowPathX86 : public SlowPathCode {
857 public:
858 ReadBarrierForRootSlowPathX86(HInstruction* instruction, Location out, Location root)
David Srbecky9cd6d372016-02-09 15:24:47 +0000859 : SlowPathCode(instruction), out_(out), root_(root) {
Roland Levillain7c1559a2015-12-15 10:55:36 +0000860 DCHECK(kEmitCompilerReadBarrier);
861 }
Roland Levillain0d5a2812015-11-13 10:07:31 +0000862
863 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
864 LocationSummary* locations = instruction_->GetLocations();
865 Register reg_out = out_.AsRegister<Register>();
866 DCHECK(locations->CanCall());
867 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
Roland Levillain7c1559a2015-12-15 10:55:36 +0000868 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
869 << "Unexpected instruction in read barrier for GC root slow path: "
870 << instruction_->DebugName();
Roland Levillain0d5a2812015-11-13 10:07:31 +0000871
872 __ Bind(GetEntryLabel());
873 SaveLiveRegisters(codegen, locations);
874
875 InvokeRuntimeCallingConvention calling_convention;
876 CodeGeneratorX86* x86_codegen = down_cast<CodeGeneratorX86*>(codegen);
877 x86_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100878 x86_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000879 instruction_,
880 instruction_->GetDexPc(),
881 this);
882 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
883 x86_codegen->Move32(out_, Location::RegisterLocation(EAX));
884
885 RestoreLiveRegisters(codegen, locations);
886 __ jmp(GetExitLabel());
887 }
888
889 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathX86"; }
890
891 private:
Roland Levillain0d5a2812015-11-13 10:07:31 +0000892 const Location out_;
893 const Location root_;
894
895 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathX86);
896};
897
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100898#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100899// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
900#define __ down_cast<X86Assembler*>(GetAssembler())-> // NOLINT
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100901
Aart Bike9f37602015-10-09 11:15:55 -0700902inline Condition X86Condition(IfCondition cond) {
Dave Allison20dfc792014-06-16 20:44:29 -0700903 switch (cond) {
904 case kCondEQ: return kEqual;
905 case kCondNE: return kNotEqual;
906 case kCondLT: return kLess;
907 case kCondLE: return kLessEqual;
908 case kCondGT: return kGreater;
909 case kCondGE: return kGreaterEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700910 case kCondB: return kBelow;
911 case kCondBE: return kBelowEqual;
912 case kCondA: return kAbove;
913 case kCondAE: return kAboveEqual;
Dave Allison20dfc792014-06-16 20:44:29 -0700914 }
Roland Levillain4fa13f62015-07-06 18:11:54 +0100915 LOG(FATAL) << "Unreachable";
916 UNREACHABLE();
917}
918
Aart Bike9f37602015-10-09 11:15:55 -0700919// Maps signed condition to unsigned condition and FP condition to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100920inline Condition X86UnsignedOrFPCondition(IfCondition cond) {
921 switch (cond) {
922 case kCondEQ: return kEqual;
923 case kCondNE: return kNotEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700924 // Signed to unsigned, and FP to x86 name.
Roland Levillain4fa13f62015-07-06 18:11:54 +0100925 case kCondLT: return kBelow;
926 case kCondLE: return kBelowEqual;
927 case kCondGT: return kAbove;
928 case kCondGE: return kAboveEqual;
Aart Bike9f37602015-10-09 11:15:55 -0700929 // Unsigned remain unchanged.
930 case kCondB: return kBelow;
931 case kCondBE: return kBelowEqual;
932 case kCondA: return kAbove;
933 case kCondAE: return kAboveEqual;
Roland Levillain4fa13f62015-07-06 18:11:54 +0100934 }
935 LOG(FATAL) << "Unreachable";
936 UNREACHABLE();
Dave Allison20dfc792014-06-16 20:44:29 -0700937}
938
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100939void CodeGeneratorX86::DumpCoreRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100940 stream << Register(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100941}
942
943void CodeGeneratorX86::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
David Brazdilc74652862015-05-13 17:50:09 +0100944 stream << XmmRegister(reg);
Nicolas Geoffraya7062e02014-05-22 12:50:17 +0100945}
946
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100947size_t CodeGeneratorX86::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
948 __ movl(Address(ESP, stack_index), static_cast<Register>(reg_id));
949 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100950}
951
Nicolas Geoffray102cbed2014-10-15 18:31:05 +0100952size_t CodeGeneratorX86::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
953 __ movl(static_cast<Register>(reg_id), Address(ESP, stack_index));
954 return kX86WordSize;
Nicolas Geoffray3bca0df2014-09-19 11:01:00 +0100955}
956
Mark Mendell7c8d0092015-01-26 11:21:33 -0500957size_t CodeGeneratorX86::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
958 __ movsd(Address(ESP, stack_index), XmmRegister(reg_id));
959 return GetFloatingPointSpillSlotSize();
960}
961
962size_t CodeGeneratorX86::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
963 __ movsd(XmmRegister(reg_id), Address(ESP, stack_index));
964 return GetFloatingPointSpillSlotSize();
965}
966
Calin Juravle175dc732015-08-25 15:42:32 +0100967void CodeGeneratorX86::InvokeRuntime(QuickEntrypointEnum entrypoint,
968 HInstruction* instruction,
969 uint32_t dex_pc,
970 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +0100971 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100972 GenerateInvokeRuntime(GetThreadOffset<kX86PointerSize>(entrypoint).Int32Value());
973 if (EntrypointRequiresStackMap(entrypoint)) {
974 RecordPcInfo(instruction, dex_pc, slow_path);
975 }
Alexandre Rames8158f282015-08-07 10:26:17 +0100976}
977
Roland Levillaindec8f632016-07-22 17:10:06 +0100978void CodeGeneratorX86::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
979 HInstruction* instruction,
980 SlowPathCode* slow_path) {
981 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
Serban Constantinescuba45db02016-07-12 22:53:02 +0100982 GenerateInvokeRuntime(entry_point_offset);
983}
984
985void CodeGeneratorX86::GenerateInvokeRuntime(int32_t entry_point_offset) {
Roland Levillaindec8f632016-07-22 17:10:06 +0100986 __ fs()->call(Address::Absolute(entry_point_offset));
987}
988
Mark Mendellfb8d2792015-03-31 22:16:59 -0400989CodeGeneratorX86::CodeGeneratorX86(HGraph* graph,
Roland Levillain0d5a2812015-11-13 10:07:31 +0000990 const X86InstructionSetFeatures& isa_features,
991 const CompilerOptions& compiler_options,
992 OptimizingCompilerStats* stats)
Mark Mendell5f874182015-03-04 15:42:45 -0500993 : CodeGenerator(graph,
994 kNumberOfCpuRegisters,
995 kNumberOfXmmRegisters,
996 kNumberOfRegisterPairs,
997 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
998 arraysize(kCoreCalleeSaves))
999 | (1 << kFakeReturnRegister),
Serban Constantinescuecc43662015-08-13 13:33:12 +01001000 0,
1001 compiler_options,
1002 stats),
Vladimir Marko225b6462015-09-28 12:17:40 +01001003 block_labels_(nullptr),
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001004 location_builder_(graph, this),
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001005 instruction_visitor_(graph, this),
Mark Mendellfb8d2792015-03-31 22:16:59 -04001006 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001007 assembler_(graph->GetArena()),
Vladimir Marko58155012015-08-19 12:49:41 +00001008 isa_features_(isa_features),
Vladimir Marko5233f932015-09-29 19:01:15 +01001009 method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Mark Mendell805b3b52015-09-18 14:10:29 -04001010 relative_call_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0f7dca42015-11-02 14:36:43 +00001011 pc_relative_dex_cache_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markocac5a7e2016-02-22 10:39:50 +00001012 simple_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1013 string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01001014 type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray132d8362016-11-16 09:19:42 +00001015 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00001016 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001017 constant_area_start_(-1),
1018 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1019 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001020 // Use a fake return address register to mimic Quick.
1021 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001022}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001023
David Brazdil58282f42016-01-14 12:45:10 +00001024void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001025 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001026 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001027}
1028
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001029InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001030 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001031 assembler_(codegen->GetAssembler()),
1032 codegen_(codegen) {}
1033
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001034static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001035 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001036}
1037
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001038void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001039 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001040 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001041 bool skip_overflow_check =
1042 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001043 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001044
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001045 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001046 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001047 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001048 }
1049
Mark Mendell5f874182015-03-04 15:42:45 -05001050 if (HasEmptyFrame()) {
1051 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001052 }
Mark Mendell5f874182015-03-04 15:42:45 -05001053
1054 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1055 Register reg = kCoreCalleeSaves[i];
1056 if (allocated_registers_.ContainsCoreRegister(reg)) {
1057 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001058 __ cfi().AdjustCFAOffset(kX86WordSize);
1059 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001060 }
1061 }
1062
Mingyao Yang063fc772016-08-02 11:02:54 -07001063 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1064 // Initialize should_deoptimize flag to 0.
1065 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1066 }
1067
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001068 int adjust = GetFrameSize() - FrameEntrySpillSize();
1069 __ subl(ESP, Immediate(adjust));
1070 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001071 // Save the current method if we need it. Note that we do not
1072 // do this in HCurrentMethod, as the instruction might have been removed
1073 // in the SSA graph.
1074 if (RequiresCurrentMethod()) {
1075 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1076 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001077}
1078
1079void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001080 __ cfi().RememberState();
1081 if (!HasEmptyFrame()) {
1082 int adjust = GetFrameSize() - FrameEntrySpillSize();
1083 __ addl(ESP, Immediate(adjust));
1084 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001085
David Srbeckyc34dc932015-04-12 09:27:43 +01001086 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1087 Register reg = kCoreCalleeSaves[i];
1088 if (allocated_registers_.ContainsCoreRegister(reg)) {
1089 __ popl(reg);
1090 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1091 __ cfi().Restore(DWARFReg(reg));
1092 }
Mark Mendell5f874182015-03-04 15:42:45 -05001093 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001094 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001095 __ ret();
1096 __ cfi().RestoreState();
1097 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001098}
1099
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001100void CodeGeneratorX86::Bind(HBasicBlock* block) {
1101 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001102}
1103
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001104Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1105 switch (type) {
1106 case Primitive::kPrimBoolean:
1107 case Primitive::kPrimByte:
1108 case Primitive::kPrimChar:
1109 case Primitive::kPrimShort:
1110 case Primitive::kPrimInt:
1111 case Primitive::kPrimNot:
1112 return Location::RegisterLocation(EAX);
1113
1114 case Primitive::kPrimLong:
1115 return Location::RegisterPairLocation(EAX, EDX);
1116
1117 case Primitive::kPrimVoid:
1118 return Location::NoLocation();
1119
1120 case Primitive::kPrimDouble:
1121 case Primitive::kPrimFloat:
1122 return Location::FpuRegisterLocation(XMM0);
1123 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001124
1125 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001126}
1127
1128Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1129 return Location::RegisterLocation(kMethodRegisterArgument);
1130}
1131
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001132Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001133 switch (type) {
1134 case Primitive::kPrimBoolean:
1135 case Primitive::kPrimByte:
1136 case Primitive::kPrimChar:
1137 case Primitive::kPrimShort:
1138 case Primitive::kPrimInt:
1139 case Primitive::kPrimNot: {
1140 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001141 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001142 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001143 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001144 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001145 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001146 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001147 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001148
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001149 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001150 uint32_t index = gp_index_;
1151 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001152 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001153 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001154 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1155 calling_convention.GetRegisterPairAt(index));
1156 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001157 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001158 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1159 }
1160 }
1161
1162 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001163 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001164 stack_index_++;
1165 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1166 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1167 } else {
1168 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1169 }
1170 }
1171
1172 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001173 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001174 stack_index_ += 2;
1175 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1176 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1177 } else {
1178 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001179 }
1180 }
1181
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001182 case Primitive::kPrimVoid:
1183 LOG(FATAL) << "Unexpected parameter type " << type;
1184 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001185 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001186 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001187}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001188
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001189void CodeGeneratorX86::Move32(Location destination, Location source) {
1190 if (source.Equals(destination)) {
1191 return;
1192 }
1193 if (destination.IsRegister()) {
1194 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001195 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001196 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001197 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001198 } else {
1199 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001200 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001201 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001202 } else if (destination.IsFpuRegister()) {
1203 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001204 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001205 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001206 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001207 } else {
1208 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001209 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001210 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001211 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001212 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001213 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001214 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001215 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001216 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001217 } else if (source.IsConstant()) {
1218 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001219 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001220 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001221 } else {
1222 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001223 __ pushl(Address(ESP, source.GetStackIndex()));
1224 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001225 }
1226 }
1227}
1228
1229void CodeGeneratorX86::Move64(Location destination, Location source) {
1230 if (source.Equals(destination)) {
1231 return;
1232 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001233 if (destination.IsRegisterPair()) {
1234 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001235 EmitParallelMoves(
1236 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1237 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001238 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001239 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001240 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1241 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001242 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001243 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1244 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1245 __ psrlq(src_reg, Immediate(32));
1246 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001247 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001248 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001249 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001250 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1251 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001252 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1253 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001254 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001255 if (source.IsFpuRegister()) {
1256 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1257 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001258 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001259 } else if (source.IsRegisterPair()) {
1260 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1261 // Create stack space for 2 elements.
1262 __ subl(ESP, Immediate(2 * elem_size));
1263 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1264 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1265 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1266 // And remove the temporary stack space we allocated.
1267 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001268 } else {
1269 LOG(FATAL) << "Unimplemented";
1270 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001271 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001272 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001273 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001274 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001275 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001276 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001277 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001278 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001279 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001280 } else if (source.IsConstant()) {
1281 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001282 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1283 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001284 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001285 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1286 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001287 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001288 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001289 EmitParallelMoves(
1290 Location::StackSlot(source.GetStackIndex()),
1291 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001292 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001293 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001294 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1295 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001296 }
1297 }
1298}
1299
Calin Juravle175dc732015-08-25 15:42:32 +01001300void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1301 DCHECK(location.IsRegister());
1302 __ movl(location.AsRegister<Register>(), Immediate(value));
1303}
1304
Calin Juravlee460d1d2015-09-29 04:52:17 +01001305void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001306 HParallelMove move(GetGraph()->GetArena());
1307 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1308 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1309 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001310 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001311 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001312 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001313 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001314}
1315
1316void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1317 if (location.IsRegister()) {
1318 locations->AddTemp(location);
1319 } else if (location.IsRegisterPair()) {
1320 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1321 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1322 } else {
1323 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1324 }
1325}
1326
David Brazdilfc6a86a2015-06-26 10:33:45 +00001327void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001328 DCHECK(!successor->IsExitBlock());
1329
1330 HBasicBlock* block = got->GetBlock();
1331 HInstruction* previous = got->GetPrevious();
1332
1333 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001334 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001335 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1336 return;
1337 }
1338
1339 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1340 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1341 }
1342 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001343 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001344 }
1345}
1346
David Brazdilfc6a86a2015-06-26 10:33:45 +00001347void LocationsBuilderX86::VisitGoto(HGoto* got) {
1348 got->SetLocations(nullptr);
1349}
1350
1351void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1352 HandleGoto(got, got->GetSuccessor());
1353}
1354
1355void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1356 try_boundary->SetLocations(nullptr);
1357}
1358
1359void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1360 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1361 if (!successor->IsExitBlock()) {
1362 HandleGoto(try_boundary, successor);
1363 }
1364}
1365
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001366void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001367 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001368}
1369
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001370void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001371}
1372
Mark Mendell152408f2015-12-31 12:28:50 -05001373template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001374void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001375 LabelType* true_label,
1376 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001377 if (cond->IsFPConditionTrueIfNaN()) {
1378 __ j(kUnordered, true_label);
1379 } else if (cond->IsFPConditionFalseIfNaN()) {
1380 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001381 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001382 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001383}
1384
Mark Mendell152408f2015-12-31 12:28:50 -05001385template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001386void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001387 LabelType* true_label,
1388 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001389 LocationSummary* locations = cond->GetLocations();
1390 Location left = locations->InAt(0);
1391 Location right = locations->InAt(1);
1392 IfCondition if_cond = cond->GetCondition();
1393
Mark Mendellc4701932015-04-10 13:18:51 -04001394 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001395 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001396 IfCondition true_high_cond = if_cond;
1397 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001398 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001399
1400 // Set the conditions for the test, remembering that == needs to be
1401 // decided using the low words.
1402 switch (if_cond) {
1403 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001404 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001405 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001406 break;
1407 case kCondLT:
1408 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001409 break;
1410 case kCondLE:
1411 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001412 break;
1413 case kCondGT:
1414 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001415 break;
1416 case kCondGE:
1417 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001418 break;
Aart Bike9f37602015-10-09 11:15:55 -07001419 case kCondB:
1420 false_high_cond = kCondA;
1421 break;
1422 case kCondBE:
1423 true_high_cond = kCondB;
1424 break;
1425 case kCondA:
1426 false_high_cond = kCondB;
1427 break;
1428 case kCondAE:
1429 true_high_cond = kCondA;
1430 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001431 }
1432
1433 if (right.IsConstant()) {
1434 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001435 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001436 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001437
Aart Bika19616e2016-02-01 18:57:58 -08001438 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001439 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001440 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001441 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001442 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001443 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001444 __ j(X86Condition(true_high_cond), true_label);
1445 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001446 }
1447 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001448 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001449 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001450 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001451 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001452
1453 __ cmpl(left_high, right_high);
1454 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001455 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001456 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001457 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001458 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001459 __ j(X86Condition(true_high_cond), true_label);
1460 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001461 }
1462 // Must be equal high, so compare the lows.
1463 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001464 } else {
1465 DCHECK(right.IsDoubleStackSlot());
1466 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1467 if (if_cond == kCondNE) {
1468 __ j(X86Condition(true_high_cond), true_label);
1469 } else if (if_cond == kCondEQ) {
1470 __ j(X86Condition(false_high_cond), false_label);
1471 } else {
1472 __ j(X86Condition(true_high_cond), true_label);
1473 __ j(X86Condition(false_high_cond), false_label);
1474 }
1475 // Must be equal high, so compare the lows.
1476 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001477 }
1478 // The last comparison might be unsigned.
1479 __ j(final_condition, true_label);
1480}
1481
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001482void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1483 Location rhs,
1484 HInstruction* insn,
1485 bool is_double) {
1486 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1487 if (is_double) {
1488 if (rhs.IsFpuRegister()) {
1489 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1490 } else if (const_area != nullptr) {
1491 DCHECK(const_area->IsEmittedAtUseSite());
1492 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1493 codegen_->LiteralDoubleAddress(
1494 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1495 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1496 } else {
1497 DCHECK(rhs.IsDoubleStackSlot());
1498 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1499 }
1500 } else {
1501 if (rhs.IsFpuRegister()) {
1502 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1503 } else if (const_area != nullptr) {
1504 DCHECK(const_area->IsEmittedAtUseSite());
1505 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1506 codegen_->LiteralFloatAddress(
1507 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1508 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1509 } else {
1510 DCHECK(rhs.IsStackSlot());
1511 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1512 }
1513 }
1514}
1515
Mark Mendell152408f2015-12-31 12:28:50 -05001516template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001517void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001518 LabelType* true_target_in,
1519 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001520 // Generated branching requires both targets to be explicit. If either of the
1521 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001522 LabelType fallthrough_target;
1523 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1524 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001525
Mark Mendellc4701932015-04-10 13:18:51 -04001526 LocationSummary* locations = condition->GetLocations();
1527 Location left = locations->InAt(0);
1528 Location right = locations->InAt(1);
1529
Mark Mendellc4701932015-04-10 13:18:51 -04001530 Primitive::Type type = condition->InputAt(0)->GetType();
1531 switch (type) {
1532 case Primitive::kPrimLong:
1533 GenerateLongComparesAndJumps(condition, true_target, false_target);
1534 break;
1535 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001536 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001537 GenerateFPJumps(condition, true_target, false_target);
1538 break;
1539 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001540 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001541 GenerateFPJumps(condition, true_target, false_target);
1542 break;
1543 default:
1544 LOG(FATAL) << "Unexpected compare type " << type;
1545 }
1546
David Brazdil0debae72015-11-12 18:37:00 +00001547 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001548 __ jmp(false_target);
1549 }
David Brazdil0debae72015-11-12 18:37:00 +00001550
1551 if (fallthrough_target.IsLinked()) {
1552 __ Bind(&fallthrough_target);
1553 }
Mark Mendellc4701932015-04-10 13:18:51 -04001554}
1555
David Brazdil0debae72015-11-12 18:37:00 +00001556static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1557 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1558 // are set only strictly before `branch`. We can't use the eflags on long/FP
1559 // conditions if they are materialized due to the complex branching.
1560 return cond->IsCondition() &&
1561 cond->GetNext() == branch &&
1562 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1563 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1564}
1565
Mark Mendell152408f2015-12-31 12:28:50 -05001566template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001567void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001568 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001569 LabelType* true_target,
1570 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001571 HInstruction* cond = instruction->InputAt(condition_input_index);
1572
1573 if (true_target == nullptr && false_target == nullptr) {
1574 // Nothing to do. The code always falls through.
1575 return;
1576 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001577 // Constant condition, statically compared against "true" (integer value 1).
1578 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001579 if (true_target != nullptr) {
1580 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001581 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001582 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001583 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001584 if (false_target != nullptr) {
1585 __ jmp(false_target);
1586 }
1587 }
1588 return;
1589 }
1590
1591 // The following code generates these patterns:
1592 // (1) true_target == nullptr && false_target != nullptr
1593 // - opposite condition true => branch to false_target
1594 // (2) true_target != nullptr && false_target == nullptr
1595 // - condition true => branch to true_target
1596 // (3) true_target != nullptr && false_target != nullptr
1597 // - condition true => branch to true_target
1598 // - branch to false_target
1599 if (IsBooleanValueOrMaterializedCondition(cond)) {
1600 if (AreEflagsSetFrom(cond, instruction)) {
1601 if (true_target == nullptr) {
1602 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1603 } else {
1604 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1605 }
1606 } else {
1607 // Materialized condition, compare against 0.
1608 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1609 if (lhs.IsRegister()) {
1610 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1611 } else {
1612 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1613 }
1614 if (true_target == nullptr) {
1615 __ j(kEqual, false_target);
1616 } else {
1617 __ j(kNotEqual, true_target);
1618 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001619 }
1620 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001621 // Condition has not been materialized, use its inputs as the comparison and
1622 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001623 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001624
1625 // If this is a long or FP comparison that has been folded into
1626 // the HCondition, generate the comparison directly.
1627 Primitive::Type type = condition->InputAt(0)->GetType();
1628 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1629 GenerateCompareTestAndBranch(condition, true_target, false_target);
1630 return;
1631 }
1632
1633 Location lhs = condition->GetLocations()->InAt(0);
1634 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001635 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001636 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001637 if (true_target == nullptr) {
1638 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1639 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001640 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001641 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001642 }
David Brazdil0debae72015-11-12 18:37:00 +00001643
1644 // If neither branch falls through (case 3), the conditional branch to `true_target`
1645 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1646 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001647 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001648 }
1649}
1650
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001651void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001652 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1653 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001654 locations->SetInAt(0, Location::Any());
1655 }
1656}
1657
1658void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001659 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1660 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1661 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1662 nullptr : codegen_->GetLabelOf(true_successor);
1663 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1664 nullptr : codegen_->GetLabelOf(false_successor);
1665 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001666}
1667
1668void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1669 LocationSummary* locations = new (GetGraph()->GetArena())
1670 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001671 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001672 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001673 locations->SetInAt(0, Location::Any());
1674 }
1675}
1676
1677void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001678 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001679 GenerateTestAndBranch<Label>(deoptimize,
1680 /* condition_input_index */ 0,
1681 slow_path->GetEntryLabel(),
1682 /* false_target */ nullptr);
1683}
1684
Mingyao Yang063fc772016-08-02 11:02:54 -07001685void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1686 LocationSummary* locations = new (GetGraph()->GetArena())
1687 LocationSummary(flag, LocationSummary::kNoCall);
1688 locations->SetOut(Location::RequiresRegister());
1689}
1690
1691void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1692 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1693 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1694}
1695
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001696static bool SelectCanUseCMOV(HSelect* select) {
1697 // There are no conditional move instructions for XMMs.
1698 if (Primitive::IsFloatingPointType(select->GetType())) {
1699 return false;
1700 }
1701
1702 // A FP condition doesn't generate the single CC that we need.
1703 // In 32 bit mode, a long condition doesn't generate a single CC either.
1704 HInstruction* condition = select->GetCondition();
1705 if (condition->IsCondition()) {
1706 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1707 if (compare_type == Primitive::kPrimLong ||
1708 Primitive::IsFloatingPointType(compare_type)) {
1709 return false;
1710 }
1711 }
1712
1713 // We can generate a CMOV for this Select.
1714 return true;
1715}
1716
David Brazdil74eb1b22015-12-14 11:44:01 +00001717void LocationsBuilderX86::VisitSelect(HSelect* select) {
1718 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001719 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001720 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001721 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001722 } else {
1723 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001724 if (SelectCanUseCMOV(select)) {
1725 if (select->InputAt(1)->IsConstant()) {
1726 // Cmov can't handle a constant value.
1727 locations->SetInAt(1, Location::RequiresRegister());
1728 } else {
1729 locations->SetInAt(1, Location::Any());
1730 }
1731 } else {
1732 locations->SetInAt(1, Location::Any());
1733 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001734 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001735 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1736 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001737 }
1738 locations->SetOut(Location::SameAsFirstInput());
1739}
1740
1741void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1742 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001743 DCHECK(locations->InAt(0).Equals(locations->Out()));
1744 if (SelectCanUseCMOV(select)) {
1745 // If both the condition and the source types are integer, we can generate
1746 // a CMOV to implement Select.
1747
1748 HInstruction* select_condition = select->GetCondition();
1749 Condition cond = kNotEqual;
1750
1751 // Figure out how to test the 'condition'.
1752 if (select_condition->IsCondition()) {
1753 HCondition* condition = select_condition->AsCondition();
1754 if (!condition->IsEmittedAtUseSite()) {
1755 // This was a previously materialized condition.
1756 // Can we use the existing condition code?
1757 if (AreEflagsSetFrom(condition, select)) {
1758 // Materialization was the previous instruction. Condition codes are right.
1759 cond = X86Condition(condition->GetCondition());
1760 } else {
1761 // No, we have to recreate the condition code.
1762 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1763 __ testl(cond_reg, cond_reg);
1764 }
1765 } else {
1766 // We can't handle FP or long here.
1767 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1768 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1769 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001770 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001771 cond = X86Condition(condition->GetCondition());
1772 }
1773 } else {
1774 // Must be a boolean condition, which needs to be compared to 0.
1775 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1776 __ testl(cond_reg, cond_reg);
1777 }
1778
1779 // If the condition is true, overwrite the output, which already contains false.
1780 Location false_loc = locations->InAt(0);
1781 Location true_loc = locations->InAt(1);
1782 if (select->GetType() == Primitive::kPrimLong) {
1783 // 64 bit conditional move.
1784 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1785 Register false_low = false_loc.AsRegisterPairLow<Register>();
1786 if (true_loc.IsRegisterPair()) {
1787 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1788 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1789 } else {
1790 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1791 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1792 }
1793 } else {
1794 // 32 bit conditional move.
1795 Register false_reg = false_loc.AsRegister<Register>();
1796 if (true_loc.IsRegister()) {
1797 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1798 } else {
1799 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1800 }
1801 }
1802 } else {
1803 NearLabel false_target;
1804 GenerateTestAndBranch<NearLabel>(
1805 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1806 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1807 __ Bind(&false_target);
1808 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001809}
1810
David Srbecky0cf44932015-12-09 14:09:59 +00001811void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1812 new (GetGraph()->GetArena()) LocationSummary(info);
1813}
1814
David Srbeckyd28f4a02016-03-14 17:14:24 +00001815void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1816 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001817}
1818
1819void CodeGeneratorX86::GenerateNop() {
1820 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001821}
1822
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001823void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001824 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001825 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001826 // Handle the long/FP comparisons made in instruction simplification.
1827 switch (cond->InputAt(0)->GetType()) {
1828 case Primitive::kPrimLong: {
1829 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001830 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001831 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001832 locations->SetOut(Location::RequiresRegister());
1833 }
1834 break;
1835 }
1836 case Primitive::kPrimFloat:
1837 case Primitive::kPrimDouble: {
1838 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001839 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1840 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1841 } else if (cond->InputAt(1)->IsConstant()) {
1842 locations->SetInAt(1, Location::RequiresFpuRegister());
1843 } else {
1844 locations->SetInAt(1, Location::Any());
1845 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001846 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001847 locations->SetOut(Location::RequiresRegister());
1848 }
1849 break;
1850 }
1851 default:
1852 locations->SetInAt(0, Location::RequiresRegister());
1853 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001854 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001855 // We need a byte register.
1856 locations->SetOut(Location::RegisterLocation(ECX));
1857 }
1858 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001859 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001860}
1861
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001862void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001863 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001864 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001865 }
Mark Mendellc4701932015-04-10 13:18:51 -04001866
1867 LocationSummary* locations = cond->GetLocations();
1868 Location lhs = locations->InAt(0);
1869 Location rhs = locations->InAt(1);
1870 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001871 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001872
1873 switch (cond->InputAt(0)->GetType()) {
1874 default: {
1875 // Integer case.
1876
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001877 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001878 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001879 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001880 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001881 return;
1882 }
1883 case Primitive::kPrimLong:
1884 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1885 break;
1886 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001887 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001888 GenerateFPJumps(cond, &true_label, &false_label);
1889 break;
1890 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001891 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001892 GenerateFPJumps(cond, &true_label, &false_label);
1893 break;
1894 }
1895
1896 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001897 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001898
Roland Levillain4fa13f62015-07-06 18:11:54 +01001899 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001900 __ Bind(&false_label);
1901 __ xorl(reg, reg);
1902 __ jmp(&done_label);
1903
Roland Levillain4fa13f62015-07-06 18:11:54 +01001904 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001905 __ Bind(&true_label);
1906 __ movl(reg, Immediate(1));
1907 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001908}
1909
1910void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001911 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001912}
1913
1914void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001915 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001916}
1917
1918void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001919 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001920}
1921
1922void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001923 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001924}
1925
1926void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001927 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001928}
1929
1930void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001931 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001932}
1933
1934void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001935 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001936}
1937
1938void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001939 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001940}
1941
1942void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001943 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001944}
1945
1946void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001947 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001948}
1949
1950void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001951 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001952}
1953
1954void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001955 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001956}
1957
Aart Bike9f37602015-10-09 11:15:55 -07001958void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001959 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001960}
1961
1962void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001963 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001964}
1965
1966void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001967 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001968}
1969
1970void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001971 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001972}
1973
1974void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001975 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001976}
1977
1978void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001979 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001980}
1981
1982void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001983 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001984}
1985
1986void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001987 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001988}
1989
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001990void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001991 LocationSummary* locations =
1992 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001993 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001994}
1995
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001996void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001997 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001998}
1999
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002000void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2001 LocationSummary* locations =
2002 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2003 locations->SetOut(Location::ConstantLocation(constant));
2004}
2005
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002006void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002007 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002008}
2009
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002010void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002011 LocationSummary* locations =
2012 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002013 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002014}
2015
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002016void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002017 // Will be generated at use site.
2018}
2019
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002020void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2021 LocationSummary* locations =
2022 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2023 locations->SetOut(Location::ConstantLocation(constant));
2024}
2025
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002026void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002027 // Will be generated at use site.
2028}
2029
2030void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2031 LocationSummary* locations =
2032 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2033 locations->SetOut(Location::ConstantLocation(constant));
2034}
2035
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002036void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002037 // Will be generated at use site.
2038}
2039
Calin Juravle27df7582015-04-17 19:12:31 +01002040void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2041 memory_barrier->SetLocations(nullptr);
2042}
2043
2044void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002045 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002046}
2047
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002048void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002049 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002050}
2051
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002052void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002053 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002054}
2055
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002056void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002057 LocationSummary* locations =
2058 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002059 switch (ret->InputAt(0)->GetType()) {
2060 case Primitive::kPrimBoolean:
2061 case Primitive::kPrimByte:
2062 case Primitive::kPrimChar:
2063 case Primitive::kPrimShort:
2064 case Primitive::kPrimInt:
2065 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002066 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002067 break;
2068
2069 case Primitive::kPrimLong:
2070 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002071 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002072 break;
2073
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002074 case Primitive::kPrimFloat:
2075 case Primitive::kPrimDouble:
2076 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002077 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002078 break;
2079
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002080 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002081 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002082 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002083}
2084
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002085void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002086 if (kIsDebugBuild) {
2087 switch (ret->InputAt(0)->GetType()) {
2088 case Primitive::kPrimBoolean:
2089 case Primitive::kPrimByte:
2090 case Primitive::kPrimChar:
2091 case Primitive::kPrimShort:
2092 case Primitive::kPrimInt:
2093 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002094 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002095 break;
2096
2097 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002098 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2099 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002100 break;
2101
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002102 case Primitive::kPrimFloat:
2103 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002104 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002105 break;
2106
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002107 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002108 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002109 }
2110 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002111 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002112}
2113
Calin Juravle175dc732015-08-25 15:42:32 +01002114void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2115 // The trampoline uses the same calling convention as dex calling conventions,
2116 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2117 // the method_idx.
2118 HandleInvoke(invoke);
2119}
2120
2121void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2122 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2123}
2124
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002125void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002126 // Explicit clinit checks triggered by static invokes must have been pruned by
2127 // art::PrepareForRegisterAllocation.
2128 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002129
Mark Mendellfb8d2792015-03-31 22:16:59 -04002130 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002131 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002132 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002133 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002134 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002135 return;
2136 }
2137
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002138 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002139
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002140 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2141 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002142 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002143 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002144}
2145
Mark Mendell09ed1a32015-03-25 08:30:06 -04002146static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2147 if (invoke->GetLocations()->Intrinsified()) {
2148 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2149 intrinsic.Dispatch(invoke);
2150 return true;
2151 }
2152 return false;
2153}
2154
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002155void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002156 // Explicit clinit checks triggered by static invokes must have been pruned by
2157 // art::PrepareForRegisterAllocation.
2158 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002159
Mark Mendell09ed1a32015-03-25 08:30:06 -04002160 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2161 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002162 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002163
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002164 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002165 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002166 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002167 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002168}
2169
2170void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002171 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2172 if (intrinsic.TryDispatch(invoke)) {
2173 return;
2174 }
2175
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002176 HandleInvoke(invoke);
2177}
2178
2179void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002180 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002181 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002182}
2183
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002184void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002185 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2186 return;
2187 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002188
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002189 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002190 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002191 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002192}
2193
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002194void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002195 // This call to HandleInvoke allocates a temporary (core) register
2196 // which is also used to transfer the hidden argument from FP to
2197 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002198 HandleInvoke(invoke);
2199 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002200 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002201}
2202
2203void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2204 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002205 LocationSummary* locations = invoke->GetLocations();
2206 Register temp = locations->GetTemp(0).AsRegister<Register>();
2207 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002208 Location receiver = locations->InAt(0);
2209 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2210
Roland Levillain0d5a2812015-11-13 10:07:31 +00002211 // Set the hidden argument. This is safe to do this here, as XMM7
2212 // won't be modified thereafter, before the `call` instruction.
2213 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002214 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002215 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002216
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002217 if (receiver.IsStackSlot()) {
2218 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002219 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002220 __ movl(temp, Address(temp, class_offset));
2221 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002222 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002223 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002224 }
Roland Levillain4d027112015-07-01 15:41:14 +01002225 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002226 // Instead of simply (possibly) unpoisoning `temp` here, we should
2227 // emit a read barrier for the previous class reference load.
2228 // However this is not required in practice, as this is an
2229 // intermediate/temporary reference and because the current
2230 // concurrent copying collector keeps the from-space memory
2231 // intact/accessible until the end of the marking phase (the
2232 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002233 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002234 // temp = temp->GetAddressOfIMT()
2235 __ movl(temp,
2236 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002237 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002238 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002239 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002240 __ movl(temp, Address(temp, method_offset));
2241 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002242 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002243 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002244
2245 DCHECK(!codegen_->IsLeafMethod());
2246 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2247}
2248
Roland Levillain88cb1752014-10-20 16:36:47 +01002249void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2250 LocationSummary* locations =
2251 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2252 switch (neg->GetResultType()) {
2253 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002254 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002255 locations->SetInAt(0, Location::RequiresRegister());
2256 locations->SetOut(Location::SameAsFirstInput());
2257 break;
2258
Roland Levillain88cb1752014-10-20 16:36:47 +01002259 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002260 locations->SetInAt(0, Location::RequiresFpuRegister());
2261 locations->SetOut(Location::SameAsFirstInput());
2262 locations->AddTemp(Location::RequiresRegister());
2263 locations->AddTemp(Location::RequiresFpuRegister());
2264 break;
2265
Roland Levillain88cb1752014-10-20 16:36:47 +01002266 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002267 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002268 locations->SetOut(Location::SameAsFirstInput());
2269 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002270 break;
2271
2272 default:
2273 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2274 }
2275}
2276
2277void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2278 LocationSummary* locations = neg->GetLocations();
2279 Location out = locations->Out();
2280 Location in = locations->InAt(0);
2281 switch (neg->GetResultType()) {
2282 case Primitive::kPrimInt:
2283 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002284 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002285 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002286 break;
2287
2288 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002289 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002290 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002291 __ negl(out.AsRegisterPairLow<Register>());
2292 // Negation is similar to subtraction from zero. The least
2293 // significant byte triggers a borrow when it is different from
2294 // zero; to take it into account, add 1 to the most significant
2295 // byte if the carry flag (CF) is set to 1 after the first NEGL
2296 // operation.
2297 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2298 __ negl(out.AsRegisterPairHigh<Register>());
2299 break;
2300
Roland Levillain5368c212014-11-27 15:03:41 +00002301 case Primitive::kPrimFloat: {
2302 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002303 Register constant = locations->GetTemp(0).AsRegister<Register>();
2304 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002305 // Implement float negation with an exclusive or with value
2306 // 0x80000000 (mask for bit 31, representing the sign of a
2307 // single-precision floating-point number).
2308 __ movl(constant, Immediate(INT32_C(0x80000000)));
2309 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002310 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002311 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002312 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002313
Roland Levillain5368c212014-11-27 15:03:41 +00002314 case Primitive::kPrimDouble: {
2315 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002316 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002317 // Implement double negation with an exclusive or with value
2318 // 0x8000000000000000 (mask for bit 63, representing the sign of
2319 // a double-precision floating-point number).
2320 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002321 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002322 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002323 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002324
2325 default:
2326 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2327 }
2328}
2329
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002330void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2331 LocationSummary* locations =
2332 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2333 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2334 locations->SetInAt(0, Location::RequiresFpuRegister());
2335 locations->SetInAt(1, Location::RequiresRegister());
2336 locations->SetOut(Location::SameAsFirstInput());
2337 locations->AddTemp(Location::RequiresFpuRegister());
2338}
2339
2340void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2341 LocationSummary* locations = neg->GetLocations();
2342 Location out = locations->Out();
2343 DCHECK(locations->InAt(0).Equals(out));
2344
2345 Register constant_area = locations->InAt(1).AsRegister<Register>();
2346 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2347 if (neg->GetType() == Primitive::kPrimFloat) {
2348 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2349 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2350 } else {
2351 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2352 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2353 }
2354}
2355
Roland Levillaindff1f282014-11-05 14:15:05 +00002356void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002357 Primitive::Type result_type = conversion->GetResultType();
2358 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002359 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002360
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002361 // The float-to-long and double-to-long type conversions rely on a
2362 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002363 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002364 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2365 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002366 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002367 : LocationSummary::kNoCall;
2368 LocationSummary* locations =
2369 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2370
David Brazdilb2bd1c52015-03-25 11:17:37 +00002371 // The Java language does not allow treating boolean as an integral type but
2372 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002373
Roland Levillaindff1f282014-11-05 14:15:05 +00002374 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002375 case Primitive::kPrimByte:
2376 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002377 case Primitive::kPrimLong: {
2378 // Type conversion from long to byte is a result of code transformations.
2379 HInstruction* input = conversion->InputAt(0);
2380 Location input_location = input->IsConstant()
2381 ? Location::ConstantLocation(input->AsConstant())
2382 : Location::RegisterPairLocation(EAX, EDX);
2383 locations->SetInAt(0, input_location);
2384 // Make the output overlap to please the register allocator. This greatly simplifies
2385 // the validation of the linear scan implementation
2386 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2387 break;
2388 }
David Brazdil46e2a392015-03-16 17:31:52 +00002389 case Primitive::kPrimBoolean:
2390 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002391 case Primitive::kPrimShort:
2392 case Primitive::kPrimInt:
2393 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002394 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002395 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2396 // Make the output overlap to please the register allocator. This greatly simplifies
2397 // the validation of the linear scan implementation
2398 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002399 break;
2400
2401 default:
2402 LOG(FATAL) << "Unexpected type conversion from " << input_type
2403 << " to " << result_type;
2404 }
2405 break;
2406
Roland Levillain01a8d712014-11-14 16:27:39 +00002407 case Primitive::kPrimShort:
2408 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002409 case Primitive::kPrimLong:
2410 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002411 case Primitive::kPrimBoolean:
2412 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002413 case Primitive::kPrimByte:
2414 case Primitive::kPrimInt:
2415 case Primitive::kPrimChar:
2416 // Processing a Dex `int-to-short' instruction.
2417 locations->SetInAt(0, Location::Any());
2418 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2419 break;
2420
2421 default:
2422 LOG(FATAL) << "Unexpected type conversion from " << input_type
2423 << " to " << result_type;
2424 }
2425 break;
2426
Roland Levillain946e1432014-11-11 17:35:19 +00002427 case Primitive::kPrimInt:
2428 switch (input_type) {
2429 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002430 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002431 locations->SetInAt(0, Location::Any());
2432 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2433 break;
2434
2435 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002436 // Processing a Dex `float-to-int' instruction.
2437 locations->SetInAt(0, Location::RequiresFpuRegister());
2438 locations->SetOut(Location::RequiresRegister());
2439 locations->AddTemp(Location::RequiresFpuRegister());
2440 break;
2441
Roland Levillain946e1432014-11-11 17:35:19 +00002442 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002443 // Processing a Dex `double-to-int' instruction.
2444 locations->SetInAt(0, Location::RequiresFpuRegister());
2445 locations->SetOut(Location::RequiresRegister());
2446 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002447 break;
2448
2449 default:
2450 LOG(FATAL) << "Unexpected type conversion from " << input_type
2451 << " to " << result_type;
2452 }
2453 break;
2454
Roland Levillaindff1f282014-11-05 14:15:05 +00002455 case Primitive::kPrimLong:
2456 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002457 case Primitive::kPrimBoolean:
2458 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002459 case Primitive::kPrimByte:
2460 case Primitive::kPrimShort:
2461 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002462 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002463 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002464 locations->SetInAt(0, Location::RegisterLocation(EAX));
2465 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2466 break;
2467
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002468 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002469 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002470 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002471 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002472 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2473 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2474
Vladimir Marko949c91f2015-01-27 10:48:44 +00002475 // The runtime helper puts the result in EAX, EDX.
2476 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002477 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002478 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002479
2480 default:
2481 LOG(FATAL) << "Unexpected type conversion from " << input_type
2482 << " to " << result_type;
2483 }
2484 break;
2485
Roland Levillain981e4542014-11-14 11:47:14 +00002486 case Primitive::kPrimChar:
2487 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002488 case Primitive::kPrimLong:
2489 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002490 case Primitive::kPrimBoolean:
2491 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002492 case Primitive::kPrimByte:
2493 case Primitive::kPrimShort:
2494 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002495 // Processing a Dex `int-to-char' instruction.
2496 locations->SetInAt(0, Location::Any());
2497 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2498 break;
2499
2500 default:
2501 LOG(FATAL) << "Unexpected type conversion from " << input_type
2502 << " to " << result_type;
2503 }
2504 break;
2505
Roland Levillaindff1f282014-11-05 14:15:05 +00002506 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002507 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002508 case Primitive::kPrimBoolean:
2509 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002510 case Primitive::kPrimByte:
2511 case Primitive::kPrimShort:
2512 case Primitive::kPrimInt:
2513 case Primitive::kPrimChar:
2514 // Processing a Dex `int-to-float' instruction.
2515 locations->SetInAt(0, Location::RequiresRegister());
2516 locations->SetOut(Location::RequiresFpuRegister());
2517 break;
2518
2519 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002520 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002521 locations->SetInAt(0, Location::Any());
2522 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002523 break;
2524
Roland Levillaincff13742014-11-17 14:32:17 +00002525 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002526 // Processing a Dex `double-to-float' instruction.
2527 locations->SetInAt(0, Location::RequiresFpuRegister());
2528 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002529 break;
2530
2531 default:
2532 LOG(FATAL) << "Unexpected type conversion from " << input_type
2533 << " to " << result_type;
2534 };
2535 break;
2536
Roland Levillaindff1f282014-11-05 14:15:05 +00002537 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002538 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002539 case Primitive::kPrimBoolean:
2540 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002541 case Primitive::kPrimByte:
2542 case Primitive::kPrimShort:
2543 case Primitive::kPrimInt:
2544 case Primitive::kPrimChar:
2545 // Processing a Dex `int-to-double' instruction.
2546 locations->SetInAt(0, Location::RequiresRegister());
2547 locations->SetOut(Location::RequiresFpuRegister());
2548 break;
2549
2550 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002551 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002552 locations->SetInAt(0, Location::Any());
2553 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002554 break;
2555
Roland Levillaincff13742014-11-17 14:32:17 +00002556 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002557 // Processing a Dex `float-to-double' instruction.
2558 locations->SetInAt(0, Location::RequiresFpuRegister());
2559 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002560 break;
2561
2562 default:
2563 LOG(FATAL) << "Unexpected type conversion from " << input_type
2564 << " to " << result_type;
2565 }
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}
2573
2574void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2575 LocationSummary* locations = conversion->GetLocations();
2576 Location out = locations->Out();
2577 Location in = locations->InAt(0);
2578 Primitive::Type result_type = conversion->GetResultType();
2579 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002580 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002581 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002582 case Primitive::kPrimByte:
2583 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002584 case Primitive::kPrimLong:
2585 // Type conversion from long to byte is a result of code transformations.
2586 if (in.IsRegisterPair()) {
2587 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2588 } else {
2589 DCHECK(in.GetConstant()->IsLongConstant());
2590 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2591 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2592 }
2593 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002594 case Primitive::kPrimBoolean:
2595 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002596 case Primitive::kPrimShort:
2597 case Primitive::kPrimInt:
2598 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002599 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002600 if (in.IsRegister()) {
2601 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002602 } else {
2603 DCHECK(in.GetConstant()->IsIntConstant());
2604 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2605 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2606 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002607 break;
2608
2609 default:
2610 LOG(FATAL) << "Unexpected type conversion from " << input_type
2611 << " to " << result_type;
2612 }
2613 break;
2614
Roland Levillain01a8d712014-11-14 16:27:39 +00002615 case Primitive::kPrimShort:
2616 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002617 case Primitive::kPrimLong:
2618 // Type conversion from long to short is a result of code transformations.
2619 if (in.IsRegisterPair()) {
2620 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2621 } else if (in.IsDoubleStackSlot()) {
2622 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2623 } else {
2624 DCHECK(in.GetConstant()->IsLongConstant());
2625 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2626 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2627 }
2628 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002629 case Primitive::kPrimBoolean:
2630 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002631 case Primitive::kPrimByte:
2632 case Primitive::kPrimInt:
2633 case Primitive::kPrimChar:
2634 // Processing a Dex `int-to-short' instruction.
2635 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002636 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002637 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002638 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002639 } else {
2640 DCHECK(in.GetConstant()->IsIntConstant());
2641 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002642 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002643 }
2644 break;
2645
2646 default:
2647 LOG(FATAL) << "Unexpected type conversion from " << input_type
2648 << " to " << result_type;
2649 }
2650 break;
2651
Roland Levillain946e1432014-11-11 17:35:19 +00002652 case Primitive::kPrimInt:
2653 switch (input_type) {
2654 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002655 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002656 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002657 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002658 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002659 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002660 } else {
2661 DCHECK(in.IsConstant());
2662 DCHECK(in.GetConstant()->IsLongConstant());
2663 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002664 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002665 }
2666 break;
2667
Roland Levillain3f8f9362014-12-02 17:45:01 +00002668 case Primitive::kPrimFloat: {
2669 // Processing a Dex `float-to-int' instruction.
2670 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2671 Register output = out.AsRegister<Register>();
2672 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002673 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002674
2675 __ movl(output, Immediate(kPrimIntMax));
2676 // temp = int-to-float(output)
2677 __ cvtsi2ss(temp, output);
2678 // if input >= temp goto done
2679 __ comiss(input, temp);
2680 __ j(kAboveEqual, &done);
2681 // if input == NaN goto nan
2682 __ j(kUnordered, &nan);
2683 // output = float-to-int-truncate(input)
2684 __ cvttss2si(output, input);
2685 __ jmp(&done);
2686 __ Bind(&nan);
2687 // output = 0
2688 __ xorl(output, output);
2689 __ Bind(&done);
2690 break;
2691 }
2692
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002693 case Primitive::kPrimDouble: {
2694 // Processing a Dex `double-to-int' instruction.
2695 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2696 Register output = out.AsRegister<Register>();
2697 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002698 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002699
2700 __ movl(output, Immediate(kPrimIntMax));
2701 // temp = int-to-double(output)
2702 __ cvtsi2sd(temp, output);
2703 // if input >= temp goto done
2704 __ comisd(input, temp);
2705 __ j(kAboveEqual, &done);
2706 // if input == NaN goto nan
2707 __ j(kUnordered, &nan);
2708 // output = double-to-int-truncate(input)
2709 __ cvttsd2si(output, input);
2710 __ jmp(&done);
2711 __ Bind(&nan);
2712 // output = 0
2713 __ xorl(output, output);
2714 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002715 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002716 }
Roland Levillain946e1432014-11-11 17:35:19 +00002717
2718 default:
2719 LOG(FATAL) << "Unexpected type conversion from " << input_type
2720 << " to " << result_type;
2721 }
2722 break;
2723
Roland Levillaindff1f282014-11-05 14:15:05 +00002724 case Primitive::kPrimLong:
2725 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002726 case Primitive::kPrimBoolean:
2727 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002728 case Primitive::kPrimByte:
2729 case Primitive::kPrimShort:
2730 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002731 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002732 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002733 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2734 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002735 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002736 __ cdq();
2737 break;
2738
2739 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002740 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002741 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002742 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002743 break;
2744
Roland Levillaindff1f282014-11-05 14:15:05 +00002745 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002746 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002747 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002748 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002749 break;
2750
2751 default:
2752 LOG(FATAL) << "Unexpected type conversion from " << input_type
2753 << " to " << result_type;
2754 }
2755 break;
2756
Roland Levillain981e4542014-11-14 11:47:14 +00002757 case Primitive::kPrimChar:
2758 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002759 case Primitive::kPrimLong:
2760 // Type conversion from long to short is a result of code transformations.
2761 if (in.IsRegisterPair()) {
2762 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2763 } else if (in.IsDoubleStackSlot()) {
2764 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2765 } else {
2766 DCHECK(in.GetConstant()->IsLongConstant());
2767 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2768 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2769 }
2770 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002771 case Primitive::kPrimBoolean:
2772 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002773 case Primitive::kPrimByte:
2774 case Primitive::kPrimShort:
2775 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002776 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2777 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002778 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002779 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002780 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002781 } else {
2782 DCHECK(in.GetConstant()->IsIntConstant());
2783 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002784 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002785 }
2786 break;
2787
2788 default:
2789 LOG(FATAL) << "Unexpected type conversion from " << input_type
2790 << " to " << result_type;
2791 }
2792 break;
2793
Roland Levillaindff1f282014-11-05 14:15:05 +00002794 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002795 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002796 case Primitive::kPrimBoolean:
2797 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002798 case Primitive::kPrimByte:
2799 case Primitive::kPrimShort:
2800 case Primitive::kPrimInt:
2801 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002802 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002803 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002804 break;
2805
Roland Levillain6d0e4832014-11-27 18:31:21 +00002806 case Primitive::kPrimLong: {
2807 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002808 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002809
Roland Levillain232ade02015-04-20 15:14:36 +01002810 // Create stack space for the call to
2811 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2812 // TODO: enhance register allocator to ask for stack temporaries.
2813 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2814 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2815 __ subl(ESP, Immediate(adjustment));
2816 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002817
Roland Levillain232ade02015-04-20 15:14:36 +01002818 // Load the value to the FP stack, using temporaries if needed.
2819 PushOntoFPStack(in, 0, adjustment, false, true);
2820
2821 if (out.IsStackSlot()) {
2822 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2823 } else {
2824 __ fstps(Address(ESP, 0));
2825 Location stack_temp = Location::StackSlot(0);
2826 codegen_->Move32(out, stack_temp);
2827 }
2828
2829 // Remove the temporary stack space we allocated.
2830 if (adjustment != 0) {
2831 __ addl(ESP, Immediate(adjustment));
2832 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002833 break;
2834 }
2835
Roland Levillaincff13742014-11-17 14:32:17 +00002836 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002837 // Processing a Dex `double-to-float' instruction.
2838 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002839 break;
2840
2841 default:
2842 LOG(FATAL) << "Unexpected type conversion from " << input_type
2843 << " to " << result_type;
2844 };
2845 break;
2846
Roland Levillaindff1f282014-11-05 14:15:05 +00002847 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002848 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002849 case Primitive::kPrimBoolean:
2850 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002851 case Primitive::kPrimByte:
2852 case Primitive::kPrimShort:
2853 case Primitive::kPrimInt:
2854 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002855 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002856 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002857 break;
2858
Roland Levillain647b9ed2014-11-27 12:06:00 +00002859 case Primitive::kPrimLong: {
2860 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002861 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002862
Roland Levillain232ade02015-04-20 15:14:36 +01002863 // Create stack space for the call to
2864 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2865 // TODO: enhance register allocator to ask for stack temporaries.
2866 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2867 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2868 __ subl(ESP, Immediate(adjustment));
2869 }
2870
2871 // Load the value to the FP stack, using temporaries if needed.
2872 PushOntoFPStack(in, 0, adjustment, false, true);
2873
2874 if (out.IsDoubleStackSlot()) {
2875 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2876 } else {
2877 __ fstpl(Address(ESP, 0));
2878 Location stack_temp = Location::DoubleStackSlot(0);
2879 codegen_->Move64(out, stack_temp);
2880 }
2881
2882 // Remove the temporary stack space we allocated.
2883 if (adjustment != 0) {
2884 __ addl(ESP, Immediate(adjustment));
2885 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002886 break;
2887 }
2888
Roland Levillaincff13742014-11-17 14:32:17 +00002889 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002890 // Processing a Dex `float-to-double' instruction.
2891 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002892 break;
2893
2894 default:
2895 LOG(FATAL) << "Unexpected type conversion from " << input_type
2896 << " to " << result_type;
2897 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002898 break;
2899
2900 default:
2901 LOG(FATAL) << "Unexpected type conversion from " << input_type
2902 << " to " << result_type;
2903 }
2904}
2905
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002906void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002907 LocationSummary* locations =
2908 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002909 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002910 case Primitive::kPrimInt: {
2911 locations->SetInAt(0, Location::RequiresRegister());
2912 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2913 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2914 break;
2915 }
2916
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002917 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002918 locations->SetInAt(0, Location::RequiresRegister());
2919 locations->SetInAt(1, Location::Any());
2920 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002921 break;
2922 }
2923
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002924 case Primitive::kPrimFloat:
2925 case Primitive::kPrimDouble: {
2926 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002927 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2928 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002929 } else if (add->InputAt(1)->IsConstant()) {
2930 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002931 } else {
2932 locations->SetInAt(1, Location::Any());
2933 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002934 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002935 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002936 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002937
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002938 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002939 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2940 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002942}
2943
2944void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2945 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002946 Location first = locations->InAt(0);
2947 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002948 Location out = locations->Out();
2949
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002950 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002951 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002952 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002953 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2954 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002955 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2956 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002957 } else {
2958 __ leal(out.AsRegister<Register>(), Address(
2959 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2960 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002961 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002962 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2963 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2964 __ addl(out.AsRegister<Register>(), Immediate(value));
2965 } else {
2966 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2967 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002968 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002969 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002970 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002971 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002972 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002973 }
2974
2975 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002976 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002977 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2978 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002979 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002980 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2981 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002982 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002983 } else {
2984 DCHECK(second.IsConstant()) << second;
2985 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2986 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2987 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002988 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002989 break;
2990 }
2991
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002992 case Primitive::kPrimFloat: {
2993 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002994 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002995 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2996 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002997 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002998 __ addss(first.AsFpuRegister<XmmRegister>(),
2999 codegen_->LiteralFloatAddress(
3000 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3001 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3002 } else {
3003 DCHECK(second.IsStackSlot());
3004 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003005 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003006 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003007 }
3008
3009 case Primitive::kPrimDouble: {
3010 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003011 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003012 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3013 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003014 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003015 __ addsd(first.AsFpuRegister<XmmRegister>(),
3016 codegen_->LiteralDoubleAddress(
3017 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3018 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3019 } else {
3020 DCHECK(second.IsDoubleStackSlot());
3021 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003022 }
3023 break;
3024 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003025
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003026 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003027 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003028 }
3029}
3030
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003031void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003032 LocationSummary* locations =
3033 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003034 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003035 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003036 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003037 locations->SetInAt(0, Location::RequiresRegister());
3038 locations->SetInAt(1, Location::Any());
3039 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003040 break;
3041 }
Calin Juravle11351682014-10-23 15:38:15 +01003042 case Primitive::kPrimFloat:
3043 case Primitive::kPrimDouble: {
3044 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003045 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3046 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003047 } else if (sub->InputAt(1)->IsConstant()) {
3048 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003049 } else {
3050 locations->SetInAt(1, Location::Any());
3051 }
Calin Juravle11351682014-10-23 15:38:15 +01003052 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003053 break;
Calin Juravle11351682014-10-23 15:38:15 +01003054 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003055
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003056 default:
Calin Juravle11351682014-10-23 15:38:15 +01003057 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003058 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003059}
3060
3061void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3062 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003063 Location first = locations->InAt(0);
3064 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003065 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003066 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003067 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003068 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003069 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003070 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003071 __ subl(first.AsRegister<Register>(),
3072 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003073 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003074 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003075 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003076 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003077 }
3078
3079 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003080 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003081 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3082 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003083 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003084 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003085 __ sbbl(first.AsRegisterPairHigh<Register>(),
3086 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003087 } else {
3088 DCHECK(second.IsConstant()) << second;
3089 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3090 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3091 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003092 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003093 break;
3094 }
3095
Calin Juravle11351682014-10-23 15:38:15 +01003096 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003097 if (second.IsFpuRegister()) {
3098 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3099 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3100 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003101 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003102 __ subss(first.AsFpuRegister<XmmRegister>(),
3103 codegen_->LiteralFloatAddress(
3104 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3105 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3106 } else {
3107 DCHECK(second.IsStackSlot());
3108 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3109 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003110 break;
Calin Juravle11351682014-10-23 15:38:15 +01003111 }
3112
3113 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003114 if (second.IsFpuRegister()) {
3115 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3116 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3117 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003118 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003119 __ subsd(first.AsFpuRegister<XmmRegister>(),
3120 codegen_->LiteralDoubleAddress(
3121 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3122 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3123 } else {
3124 DCHECK(second.IsDoubleStackSlot());
3125 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3126 }
Calin Juravle11351682014-10-23 15:38:15 +01003127 break;
3128 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003129
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003130 default:
Calin Juravle11351682014-10-23 15:38:15 +01003131 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003132 }
3133}
3134
Calin Juravle34bacdf2014-10-07 20:23:36 +01003135void LocationsBuilderX86::VisitMul(HMul* mul) {
3136 LocationSummary* locations =
3137 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3138 switch (mul->GetResultType()) {
3139 case Primitive::kPrimInt:
3140 locations->SetInAt(0, Location::RequiresRegister());
3141 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003142 if (mul->InputAt(1)->IsIntConstant()) {
3143 // Can use 3 operand multiply.
3144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3145 } else {
3146 locations->SetOut(Location::SameAsFirstInput());
3147 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003148 break;
3149 case Primitive::kPrimLong: {
3150 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003151 locations->SetInAt(1, Location::Any());
3152 locations->SetOut(Location::SameAsFirstInput());
3153 // Needed for imul on 32bits with 64bits output.
3154 locations->AddTemp(Location::RegisterLocation(EAX));
3155 locations->AddTemp(Location::RegisterLocation(EDX));
3156 break;
3157 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003158 case Primitive::kPrimFloat:
3159 case Primitive::kPrimDouble: {
3160 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003161 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3162 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003163 } else if (mul->InputAt(1)->IsConstant()) {
3164 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003165 } else {
3166 locations->SetInAt(1, Location::Any());
3167 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003168 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003169 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003170 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003171
3172 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003173 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003174 }
3175}
3176
3177void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3178 LocationSummary* locations = mul->GetLocations();
3179 Location first = locations->InAt(0);
3180 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003181 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003182
3183 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003184 case Primitive::kPrimInt:
3185 // The constant may have ended up in a register, so test explicitly to avoid
3186 // problems where the output may not be the same as the first operand.
3187 if (mul->InputAt(1)->IsIntConstant()) {
3188 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3189 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3190 } else if (second.IsRegister()) {
3191 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003192 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003193 } else {
3194 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003195 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003196 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003197 }
3198 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003199
3200 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003201 Register in1_hi = first.AsRegisterPairHigh<Register>();
3202 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003203 Register eax = locations->GetTemp(0).AsRegister<Register>();
3204 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003205
3206 DCHECK_EQ(EAX, eax);
3207 DCHECK_EQ(EDX, edx);
3208
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003209 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003210 // output: in1
3211 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3212 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3213 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003214 if (second.IsConstant()) {
3215 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003216
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003217 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3218 int32_t low_value = Low32Bits(value);
3219 int32_t high_value = High32Bits(value);
3220 Immediate low(low_value);
3221 Immediate high(high_value);
3222
3223 __ movl(eax, high);
3224 // eax <- in1.lo * in2.hi
3225 __ imull(eax, in1_lo);
3226 // in1.hi <- in1.hi * in2.lo
3227 __ imull(in1_hi, low);
3228 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3229 __ addl(in1_hi, eax);
3230 // move in2_lo to eax to prepare for double precision
3231 __ movl(eax, low);
3232 // edx:eax <- in1.lo * in2.lo
3233 __ mull(in1_lo);
3234 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3235 __ addl(in1_hi, edx);
3236 // in1.lo <- (in1.lo * in2.lo)[31:0];
3237 __ movl(in1_lo, eax);
3238 } else if (second.IsRegisterPair()) {
3239 Register in2_hi = second.AsRegisterPairHigh<Register>();
3240 Register in2_lo = second.AsRegisterPairLow<Register>();
3241
3242 __ movl(eax, in2_hi);
3243 // eax <- in1.lo * in2.hi
3244 __ imull(eax, in1_lo);
3245 // in1.hi <- in1.hi * in2.lo
3246 __ imull(in1_hi, in2_lo);
3247 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3248 __ addl(in1_hi, eax);
3249 // move in1_lo to eax to prepare for double precision
3250 __ movl(eax, in1_lo);
3251 // edx:eax <- in1.lo * in2.lo
3252 __ mull(in2_lo);
3253 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3254 __ addl(in1_hi, edx);
3255 // in1.lo <- (in1.lo * in2.lo)[31:0];
3256 __ movl(in1_lo, eax);
3257 } else {
3258 DCHECK(second.IsDoubleStackSlot()) << second;
3259 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3260 Address in2_lo(ESP, second.GetStackIndex());
3261
3262 __ movl(eax, in2_hi);
3263 // eax <- in1.lo * in2.hi
3264 __ imull(eax, in1_lo);
3265 // in1.hi <- in1.hi * in2.lo
3266 __ imull(in1_hi, in2_lo);
3267 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3268 __ addl(in1_hi, eax);
3269 // move in1_lo to eax to prepare for double precision
3270 __ movl(eax, in1_lo);
3271 // edx:eax <- in1.lo * in2.lo
3272 __ mull(in2_lo);
3273 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3274 __ addl(in1_hi, edx);
3275 // in1.lo <- (in1.lo * in2.lo)[31:0];
3276 __ movl(in1_lo, eax);
3277 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003278
3279 break;
3280 }
3281
Calin Juravleb5bfa962014-10-21 18:02:24 +01003282 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003283 DCHECK(first.Equals(locations->Out()));
3284 if (second.IsFpuRegister()) {
3285 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3286 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3287 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003288 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003289 __ mulss(first.AsFpuRegister<XmmRegister>(),
3290 codegen_->LiteralFloatAddress(
3291 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3292 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3293 } else {
3294 DCHECK(second.IsStackSlot());
3295 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3296 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003297 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003298 }
3299
3300 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003301 DCHECK(first.Equals(locations->Out()));
3302 if (second.IsFpuRegister()) {
3303 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3304 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3305 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003306 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003307 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3308 codegen_->LiteralDoubleAddress(
3309 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3310 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3311 } else {
3312 DCHECK(second.IsDoubleStackSlot());
3313 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3314 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003315 break;
3316 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003317
3318 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003319 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003320 }
3321}
3322
Roland Levillain232ade02015-04-20 15:14:36 +01003323void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3324 uint32_t temp_offset,
3325 uint32_t stack_adjustment,
3326 bool is_fp,
3327 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003328 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003329 DCHECK(!is_wide);
3330 if (is_fp) {
3331 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3332 } else {
3333 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3334 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003335 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003336 DCHECK(is_wide);
3337 if (is_fp) {
3338 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3339 } else {
3340 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3341 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003342 } else {
3343 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003344 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003345 Location stack_temp = Location::StackSlot(temp_offset);
3346 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003347 if (is_fp) {
3348 __ flds(Address(ESP, temp_offset));
3349 } else {
3350 __ filds(Address(ESP, temp_offset));
3351 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003352 } else {
3353 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3354 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003355 if (is_fp) {
3356 __ fldl(Address(ESP, temp_offset));
3357 } else {
3358 __ fildl(Address(ESP, temp_offset));
3359 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003360 }
3361 }
3362}
3363
3364void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3365 Primitive::Type type = rem->GetResultType();
3366 bool is_float = type == Primitive::kPrimFloat;
3367 size_t elem_size = Primitive::ComponentSize(type);
3368 LocationSummary* locations = rem->GetLocations();
3369 Location first = locations->InAt(0);
3370 Location second = locations->InAt(1);
3371 Location out = locations->Out();
3372
3373 // Create stack space for 2 elements.
3374 // TODO: enhance register allocator to ask for stack temporaries.
3375 __ subl(ESP, Immediate(2 * elem_size));
3376
3377 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003378 const bool is_wide = !is_float;
3379 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3380 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003381
3382 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003383 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003384 __ Bind(&retry);
3385 __ fprem();
3386
3387 // Move FP status to AX.
3388 __ fstsw();
3389
3390 // And see if the argument reduction is complete. This is signaled by the
3391 // C2 FPU flag bit set to 0.
3392 __ andl(EAX, Immediate(kC2ConditionMask));
3393 __ j(kNotEqual, &retry);
3394
3395 // We have settled on the final value. Retrieve it into an XMM register.
3396 // Store FP top of stack to real stack.
3397 if (is_float) {
3398 __ fsts(Address(ESP, 0));
3399 } else {
3400 __ fstl(Address(ESP, 0));
3401 }
3402
3403 // Pop the 2 items from the FP stack.
3404 __ fucompp();
3405
3406 // Load the value from the stack into an XMM register.
3407 DCHECK(out.IsFpuRegister()) << out;
3408 if (is_float) {
3409 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3410 } else {
3411 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3412 }
3413
3414 // And remove the temporary stack space we allocated.
3415 __ addl(ESP, Immediate(2 * elem_size));
3416}
3417
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003418
3419void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3420 DCHECK(instruction->IsDiv() || instruction->IsRem());
3421
3422 LocationSummary* locations = instruction->GetLocations();
3423 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003424 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003425
3426 Register out_register = locations->Out().AsRegister<Register>();
3427 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003428 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003429
3430 DCHECK(imm == 1 || imm == -1);
3431
3432 if (instruction->IsRem()) {
3433 __ xorl(out_register, out_register);
3434 } else {
3435 __ movl(out_register, input_register);
3436 if (imm == -1) {
3437 __ negl(out_register);
3438 }
3439 }
3440}
3441
3442
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003443void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003444 LocationSummary* locations = instruction->GetLocations();
3445
3446 Register out_register = locations->Out().AsRegister<Register>();
3447 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003448 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003449 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3450 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003452 Register num = locations->GetTemp(0).AsRegister<Register>();
3453
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003454 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003455 __ testl(input_register, input_register);
3456 __ cmovl(kGreaterEqual, num, input_register);
3457 int shift = CTZ(imm);
3458 __ sarl(num, Immediate(shift));
3459
3460 if (imm < 0) {
3461 __ negl(num);
3462 }
3463
3464 __ movl(out_register, num);
3465}
3466
3467void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3468 DCHECK(instruction->IsDiv() || instruction->IsRem());
3469
3470 LocationSummary* locations = instruction->GetLocations();
3471 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3472
3473 Register eax = locations->InAt(0).AsRegister<Register>();
3474 Register out = locations->Out().AsRegister<Register>();
3475 Register num;
3476 Register edx;
3477
3478 if (instruction->IsDiv()) {
3479 edx = locations->GetTemp(0).AsRegister<Register>();
3480 num = locations->GetTemp(1).AsRegister<Register>();
3481 } else {
3482 edx = locations->Out().AsRegister<Register>();
3483 num = locations->GetTemp(0).AsRegister<Register>();
3484 }
3485
3486 DCHECK_EQ(EAX, eax);
3487 DCHECK_EQ(EDX, edx);
3488 if (instruction->IsDiv()) {
3489 DCHECK_EQ(EAX, out);
3490 } else {
3491 DCHECK_EQ(EDX, out);
3492 }
3493
3494 int64_t magic;
3495 int shift;
3496 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3497
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003498 // Save the numerator.
3499 __ movl(num, eax);
3500
3501 // EAX = magic
3502 __ movl(eax, Immediate(magic));
3503
3504 // EDX:EAX = magic * numerator
3505 __ imull(num);
3506
3507 if (imm > 0 && magic < 0) {
3508 // EDX += num
3509 __ addl(edx, num);
3510 } else if (imm < 0 && magic > 0) {
3511 __ subl(edx, num);
3512 }
3513
3514 // Shift if needed.
3515 if (shift != 0) {
3516 __ sarl(edx, Immediate(shift));
3517 }
3518
3519 // EDX += 1 if EDX < 0
3520 __ movl(eax, edx);
3521 __ shrl(edx, Immediate(31));
3522 __ addl(edx, eax);
3523
3524 if (instruction->IsRem()) {
3525 __ movl(eax, num);
3526 __ imull(edx, Immediate(imm));
3527 __ subl(eax, edx);
3528 __ movl(edx, eax);
3529 } else {
3530 __ movl(eax, edx);
3531 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003532}
3533
Calin Juravlebacfec32014-11-14 15:54:36 +00003534void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3535 DCHECK(instruction->IsDiv() || instruction->IsRem());
3536
3537 LocationSummary* locations = instruction->GetLocations();
3538 Location out = locations->Out();
3539 Location first = locations->InAt(0);
3540 Location second = locations->InAt(1);
3541 bool is_div = instruction->IsDiv();
3542
3543 switch (instruction->GetResultType()) {
3544 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003545 DCHECK_EQ(EAX, first.AsRegister<Register>());
3546 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003547
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003548 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003549 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003550
3551 if (imm == 0) {
3552 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3553 } else if (imm == 1 || imm == -1) {
3554 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003555 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003556 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003557 } else {
3558 DCHECK(imm <= -2 || imm >= 2);
3559 GenerateDivRemWithAnyConstant(instruction);
3560 }
3561 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003562 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3563 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003564 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003565
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003566 Register second_reg = second.AsRegister<Register>();
3567 // 0x80000000/-1 triggers an arithmetic exception!
3568 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3569 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003570
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003571 __ cmpl(second_reg, Immediate(-1));
3572 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003573
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003574 // edx:eax <- sign-extended of eax
3575 __ cdq();
3576 // eax = quotient, edx = remainder
3577 __ idivl(second_reg);
3578 __ Bind(slow_path->GetExitLabel());
3579 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003580 break;
3581 }
3582
3583 case Primitive::kPrimLong: {
3584 InvokeRuntimeCallingConvention calling_convention;
3585 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3586 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3587 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3588 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3589 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3590 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3591
3592 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003593 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003594 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003595 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003596 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003597 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003598 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003599 break;
3600 }
3601
3602 default:
3603 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3604 }
3605}
3606
Calin Juravle7c4954d2014-10-28 16:57:40 +00003607void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003608 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003609 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003610 : LocationSummary::kNoCall;
3611 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3612
Calin Juravle7c4954d2014-10-28 16:57:40 +00003613 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003614 case Primitive::kPrimInt: {
3615 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003616 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003617 locations->SetOut(Location::SameAsFirstInput());
3618 // Intel uses edx:eax as the dividend.
3619 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003620 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3621 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3622 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003623 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003624 locations->AddTemp(Location::RequiresRegister());
3625 }
Calin Juravled0d48522014-11-04 16:40:20 +00003626 break;
3627 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003628 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003629 InvokeRuntimeCallingConvention calling_convention;
3630 locations->SetInAt(0, Location::RegisterPairLocation(
3631 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3632 locations->SetInAt(1, Location::RegisterPairLocation(
3633 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3634 // Runtime helper puts the result in EAX, EDX.
3635 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003636 break;
3637 }
3638 case Primitive::kPrimFloat:
3639 case Primitive::kPrimDouble: {
3640 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003641 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3642 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003643 } else if (div->InputAt(1)->IsConstant()) {
3644 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003645 } else {
3646 locations->SetInAt(1, Location::Any());
3647 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003648 locations->SetOut(Location::SameAsFirstInput());
3649 break;
3650 }
3651
3652 default:
3653 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3654 }
3655}
3656
3657void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3658 LocationSummary* locations = div->GetLocations();
3659 Location first = locations->InAt(0);
3660 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003661
3662 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003663 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003664 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003665 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003666 break;
3667 }
3668
3669 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003670 if (second.IsFpuRegister()) {
3671 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3672 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3673 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003674 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003675 __ divss(first.AsFpuRegister<XmmRegister>(),
3676 codegen_->LiteralFloatAddress(
3677 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3678 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3679 } else {
3680 DCHECK(second.IsStackSlot());
3681 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3682 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003683 break;
3684 }
3685
3686 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003687 if (second.IsFpuRegister()) {
3688 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3689 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3690 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003691 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003692 __ divsd(first.AsFpuRegister<XmmRegister>(),
3693 codegen_->LiteralDoubleAddress(
3694 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3695 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3696 } else {
3697 DCHECK(second.IsDoubleStackSlot());
3698 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3699 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003700 break;
3701 }
3702
3703 default:
3704 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3705 }
3706}
3707
Calin Juravlebacfec32014-11-14 15:54:36 +00003708void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003709 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003710
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003711 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003712 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003713 : LocationSummary::kNoCall;
3714 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003715
Calin Juravled2ec87d2014-12-08 14:24:46 +00003716 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003717 case Primitive::kPrimInt: {
3718 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003719 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003720 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003721 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3722 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3723 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003724 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003725 locations->AddTemp(Location::RequiresRegister());
3726 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003727 break;
3728 }
3729 case Primitive::kPrimLong: {
3730 InvokeRuntimeCallingConvention calling_convention;
3731 locations->SetInAt(0, Location::RegisterPairLocation(
3732 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3733 locations->SetInAt(1, Location::RegisterPairLocation(
3734 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3735 // Runtime helper puts the result in EAX, EDX.
3736 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3737 break;
3738 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003739 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003740 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003741 locations->SetInAt(0, Location::Any());
3742 locations->SetInAt(1, Location::Any());
3743 locations->SetOut(Location::RequiresFpuRegister());
3744 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003745 break;
3746 }
3747
3748 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003749 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003750 }
3751}
3752
3753void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3754 Primitive::Type type = rem->GetResultType();
3755 switch (type) {
3756 case Primitive::kPrimInt:
3757 case Primitive::kPrimLong: {
3758 GenerateDivRemIntegral(rem);
3759 break;
3760 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003761 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003762 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003763 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003764 break;
3765 }
3766 default:
3767 LOG(FATAL) << "Unexpected rem type " << type;
3768 }
3769}
3770
Calin Juravled0d48522014-11-04 16:40:20 +00003771void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003772 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003773 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003774 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003775 case Primitive::kPrimByte:
3776 case Primitive::kPrimChar:
3777 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003778 case Primitive::kPrimInt: {
3779 locations->SetInAt(0, Location::Any());
3780 break;
3781 }
3782 case Primitive::kPrimLong: {
3783 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3784 if (!instruction->IsConstant()) {
3785 locations->AddTemp(Location::RequiresRegister());
3786 }
3787 break;
3788 }
3789 default:
3790 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3791 }
Calin Juravled0d48522014-11-04 16:40:20 +00003792}
3793
3794void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003795 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003796 codegen_->AddSlowPath(slow_path);
3797
3798 LocationSummary* locations = instruction->GetLocations();
3799 Location value = locations->InAt(0);
3800
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003801 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003802 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003803 case Primitive::kPrimByte:
3804 case Primitive::kPrimChar:
3805 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003806 case Primitive::kPrimInt: {
3807 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003808 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003809 __ j(kEqual, slow_path->GetEntryLabel());
3810 } else if (value.IsStackSlot()) {
3811 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3812 __ j(kEqual, slow_path->GetEntryLabel());
3813 } else {
3814 DCHECK(value.IsConstant()) << value;
3815 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003816 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003817 }
3818 }
3819 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003820 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003821 case Primitive::kPrimLong: {
3822 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003823 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003824 __ movl(temp, value.AsRegisterPairLow<Register>());
3825 __ orl(temp, value.AsRegisterPairHigh<Register>());
3826 __ j(kEqual, slow_path->GetEntryLabel());
3827 } else {
3828 DCHECK(value.IsConstant()) << value;
3829 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3830 __ jmp(slow_path->GetEntryLabel());
3831 }
3832 }
3833 break;
3834 }
3835 default:
3836 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003837 }
Calin Juravled0d48522014-11-04 16:40:20 +00003838}
3839
Calin Juravle9aec02f2014-11-18 23:06:35 +00003840void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3841 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3842
3843 LocationSummary* locations =
3844 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3845
3846 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003847 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003848 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003849 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003850 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003851 // The shift count needs to be in CL or a constant.
3852 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003853 locations->SetOut(Location::SameAsFirstInput());
3854 break;
3855 }
3856 default:
3857 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3858 }
3859}
3860
3861void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3862 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3863
3864 LocationSummary* locations = op->GetLocations();
3865 Location first = locations->InAt(0);
3866 Location second = locations->InAt(1);
3867 DCHECK(first.Equals(locations->Out()));
3868
3869 switch (op->GetResultType()) {
3870 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003871 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003872 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003873 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003874 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003875 DCHECK_EQ(ECX, second_reg);
3876 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003877 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003878 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003879 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003880 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003881 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003882 }
3883 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003884 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003885 if (shift == 0) {
3886 return;
3887 }
3888 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003889 if (op->IsShl()) {
3890 __ shll(first_reg, imm);
3891 } else if (op->IsShr()) {
3892 __ sarl(first_reg, imm);
3893 } else {
3894 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003895 }
3896 }
3897 break;
3898 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003899 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003900 if (second.IsRegister()) {
3901 Register second_reg = second.AsRegister<Register>();
3902 DCHECK_EQ(ECX, second_reg);
3903 if (op->IsShl()) {
3904 GenerateShlLong(first, second_reg);
3905 } else if (op->IsShr()) {
3906 GenerateShrLong(first, second_reg);
3907 } else {
3908 GenerateUShrLong(first, second_reg);
3909 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003910 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003911 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003912 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003913 // Nothing to do if the shift is 0, as the input is already the output.
3914 if (shift != 0) {
3915 if (op->IsShl()) {
3916 GenerateShlLong(first, shift);
3917 } else if (op->IsShr()) {
3918 GenerateShrLong(first, shift);
3919 } else {
3920 GenerateUShrLong(first, shift);
3921 }
3922 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003923 }
3924 break;
3925 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003926 default:
3927 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3928 }
3929}
3930
Mark P Mendell73945692015-04-29 14:56:17 +00003931void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3932 Register low = loc.AsRegisterPairLow<Register>();
3933 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003934 if (shift == 1) {
3935 // This is just an addition.
3936 __ addl(low, low);
3937 __ adcl(high, high);
3938 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003939 // Shift by 32 is easy. High gets low, and low gets 0.
3940 codegen_->EmitParallelMoves(
3941 loc.ToLow(),
3942 loc.ToHigh(),
3943 Primitive::kPrimInt,
3944 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3945 loc.ToLow(),
3946 Primitive::kPrimInt);
3947 } else if (shift > 32) {
3948 // Low part becomes 0. High part is low part << (shift-32).
3949 __ movl(high, low);
3950 __ shll(high, Immediate(shift - 32));
3951 __ xorl(low, low);
3952 } else {
3953 // Between 1 and 31.
3954 __ shld(high, low, Immediate(shift));
3955 __ shll(low, Immediate(shift));
3956 }
3957}
3958
Calin Juravle9aec02f2014-11-18 23:06:35 +00003959void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003960 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003961 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3962 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3963 __ testl(shifter, Immediate(32));
3964 __ j(kEqual, &done);
3965 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3966 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3967 __ Bind(&done);
3968}
3969
Mark P Mendell73945692015-04-29 14:56:17 +00003970void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3971 Register low = loc.AsRegisterPairLow<Register>();
3972 Register high = loc.AsRegisterPairHigh<Register>();
3973 if (shift == 32) {
3974 // Need to copy the sign.
3975 DCHECK_NE(low, high);
3976 __ movl(low, high);
3977 __ sarl(high, Immediate(31));
3978 } else if (shift > 32) {
3979 DCHECK_NE(low, high);
3980 // High part becomes sign. Low part is shifted by shift - 32.
3981 __ movl(low, high);
3982 __ sarl(high, Immediate(31));
3983 __ sarl(low, Immediate(shift - 32));
3984 } else {
3985 // Between 1 and 31.
3986 __ shrd(low, high, Immediate(shift));
3987 __ sarl(high, Immediate(shift));
3988 }
3989}
3990
Calin Juravle9aec02f2014-11-18 23:06:35 +00003991void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003992 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003993 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3994 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3995 __ testl(shifter, Immediate(32));
3996 __ j(kEqual, &done);
3997 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3998 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3999 __ Bind(&done);
4000}
4001
Mark P Mendell73945692015-04-29 14:56:17 +00004002void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4003 Register low = loc.AsRegisterPairLow<Register>();
4004 Register high = loc.AsRegisterPairHigh<Register>();
4005 if (shift == 32) {
4006 // Shift by 32 is easy. Low gets high, and high gets 0.
4007 codegen_->EmitParallelMoves(
4008 loc.ToHigh(),
4009 loc.ToLow(),
4010 Primitive::kPrimInt,
4011 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4012 loc.ToHigh(),
4013 Primitive::kPrimInt);
4014 } else if (shift > 32) {
4015 // Low part is high >> (shift - 32). High part becomes 0.
4016 __ movl(low, high);
4017 __ shrl(low, Immediate(shift - 32));
4018 __ xorl(high, high);
4019 } else {
4020 // Between 1 and 31.
4021 __ shrd(low, high, Immediate(shift));
4022 __ shrl(high, Immediate(shift));
4023 }
4024}
4025
Calin Juravle9aec02f2014-11-18 23:06:35 +00004026void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004027 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004028 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4029 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4030 __ testl(shifter, Immediate(32));
4031 __ j(kEqual, &done);
4032 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4033 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4034 __ Bind(&done);
4035}
4036
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004037void LocationsBuilderX86::VisitRor(HRor* ror) {
4038 LocationSummary* locations =
4039 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4040
4041 switch (ror->GetResultType()) {
4042 case Primitive::kPrimLong:
4043 // Add the temporary needed.
4044 locations->AddTemp(Location::RequiresRegister());
4045 FALLTHROUGH_INTENDED;
4046 case Primitive::kPrimInt:
4047 locations->SetInAt(0, Location::RequiresRegister());
4048 // The shift count needs to be in CL (unless it is a constant).
4049 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4050 locations->SetOut(Location::SameAsFirstInput());
4051 break;
4052 default:
4053 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4054 UNREACHABLE();
4055 }
4056}
4057
4058void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4059 LocationSummary* locations = ror->GetLocations();
4060 Location first = locations->InAt(0);
4061 Location second = locations->InAt(1);
4062
4063 if (ror->GetResultType() == Primitive::kPrimInt) {
4064 Register first_reg = first.AsRegister<Register>();
4065 if (second.IsRegister()) {
4066 Register second_reg = second.AsRegister<Register>();
4067 __ rorl(first_reg, second_reg);
4068 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004069 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004070 __ rorl(first_reg, imm);
4071 }
4072 return;
4073 }
4074
4075 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4076 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4077 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4078 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4079 if (second.IsRegister()) {
4080 Register second_reg = second.AsRegister<Register>();
4081 DCHECK_EQ(second_reg, ECX);
4082 __ movl(temp_reg, first_reg_hi);
4083 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4084 __ shrd(first_reg_lo, temp_reg, second_reg);
4085 __ movl(temp_reg, first_reg_hi);
4086 __ testl(second_reg, Immediate(32));
4087 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4088 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4089 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004090 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004091 if (shift_amt == 0) {
4092 // Already fine.
4093 return;
4094 }
4095 if (shift_amt == 32) {
4096 // Just swap.
4097 __ movl(temp_reg, first_reg_lo);
4098 __ movl(first_reg_lo, first_reg_hi);
4099 __ movl(first_reg_hi, temp_reg);
4100 return;
4101 }
4102
4103 Immediate imm(shift_amt);
4104 // Save the constents of the low value.
4105 __ movl(temp_reg, first_reg_lo);
4106
4107 // Shift right into low, feeding bits from high.
4108 __ shrd(first_reg_lo, first_reg_hi, imm);
4109
4110 // Shift right into high, feeding bits from the original low.
4111 __ shrd(first_reg_hi, temp_reg, imm);
4112
4113 // Swap if needed.
4114 if (shift_amt > 32) {
4115 __ movl(temp_reg, first_reg_lo);
4116 __ movl(first_reg_lo, first_reg_hi);
4117 __ movl(first_reg_hi, temp_reg);
4118 }
4119 }
4120}
4121
Calin Juravle9aec02f2014-11-18 23:06:35 +00004122void LocationsBuilderX86::VisitShl(HShl* shl) {
4123 HandleShift(shl);
4124}
4125
4126void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4127 HandleShift(shl);
4128}
4129
4130void LocationsBuilderX86::VisitShr(HShr* shr) {
4131 HandleShift(shr);
4132}
4133
4134void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4135 HandleShift(shr);
4136}
4137
4138void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4139 HandleShift(ushr);
4140}
4141
4142void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4143 HandleShift(ushr);
4144}
4145
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004146void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004147 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004148 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004149 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004150 if (instruction->IsStringAlloc()) {
4151 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4152 } else {
4153 InvokeRuntimeCallingConvention calling_convention;
4154 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4155 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4156 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004157}
4158
4159void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004160 // Note: if heap poisoning is enabled, the entry point takes cares
4161 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004162 if (instruction->IsStringAlloc()) {
4163 // String is allocated through StringFactory. Call NewEmptyString entry point.
4164 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004165 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004166 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4167 __ call(Address(temp, code_offset.Int32Value()));
4168 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4169 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004170 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004171 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4172 DCHECK(!codegen_->IsLeafMethod());
4173 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004174}
4175
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004176void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4177 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004178 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004179 locations->SetOut(Location::RegisterLocation(EAX));
4180 InvokeRuntimeCallingConvention calling_convention;
4181 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004182 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004183 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004184}
4185
4186void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4187 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004188 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex().index_));
Roland Levillain4d027112015-07-01 15:41:14 +01004189 // Note: if heap poisoning is enabled, the entry point takes cares
4190 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004191 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004192 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004193 DCHECK(!codegen_->IsLeafMethod());
4194}
4195
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004196void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004197 LocationSummary* locations =
4198 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004199 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4200 if (location.IsStackSlot()) {
4201 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4202 } else if (location.IsDoubleStackSlot()) {
4203 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004204 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004205 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004206}
4207
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004208void InstructionCodeGeneratorX86::VisitParameterValue(
4209 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4210}
4211
4212void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4213 LocationSummary* locations =
4214 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4215 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4216}
4217
4218void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004219}
4220
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004221void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4222 LocationSummary* locations =
4223 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4224 locations->SetInAt(0, Location::RequiresRegister());
4225 locations->SetOut(Location::RequiresRegister());
4226}
4227
4228void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4229 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004230 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004231 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004232 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004233 __ movl(locations->Out().AsRegister<Register>(),
4234 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004235 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004236 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004237 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004238 __ movl(locations->Out().AsRegister<Register>(),
4239 Address(locations->InAt(0).AsRegister<Register>(),
4240 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4241 // temp = temp->GetImtEntryAt(method_offset);
4242 __ movl(locations->Out().AsRegister<Register>(),
4243 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004244 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004245}
4246
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004247void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004248 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004249 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004250 locations->SetInAt(0, Location::RequiresRegister());
4251 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004252}
4253
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004254void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4255 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004256 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004257 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004258 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004259 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004260 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004261 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004262 break;
4263
4264 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004265 __ notl(out.AsRegisterPairLow<Register>());
4266 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004267 break;
4268
4269 default:
4270 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4271 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004272}
4273
David Brazdil66d126e2015-04-03 16:02:44 +01004274void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4275 LocationSummary* locations =
4276 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4277 locations->SetInAt(0, Location::RequiresRegister());
4278 locations->SetOut(Location::SameAsFirstInput());
4279}
4280
4281void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004282 LocationSummary* locations = bool_not->GetLocations();
4283 Location in = locations->InAt(0);
4284 Location out = locations->Out();
4285 DCHECK(in.Equals(out));
4286 __ xorl(out.AsRegister<Register>(), Immediate(1));
4287}
4288
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004289void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004290 LocationSummary* locations =
4291 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004292 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004293 case Primitive::kPrimBoolean:
4294 case Primitive::kPrimByte:
4295 case Primitive::kPrimShort:
4296 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004297 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004298 case Primitive::kPrimLong: {
4299 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004300 locations->SetInAt(1, Location::Any());
4301 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4302 break;
4303 }
4304 case Primitive::kPrimFloat:
4305 case Primitive::kPrimDouble: {
4306 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004307 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4308 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4309 } else if (compare->InputAt(1)->IsConstant()) {
4310 locations->SetInAt(1, Location::RequiresFpuRegister());
4311 } else {
4312 locations->SetInAt(1, Location::Any());
4313 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004314 locations->SetOut(Location::RequiresRegister());
4315 break;
4316 }
4317 default:
4318 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4319 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004320}
4321
4322void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004323 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004324 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004325 Location left = locations->InAt(0);
4326 Location right = locations->InAt(1);
4327
Mark Mendell0c9497d2015-08-21 09:30:05 -04004328 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004329 Condition less_cond = kLess;
4330
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004331 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004332 case Primitive::kPrimBoolean:
4333 case Primitive::kPrimByte:
4334 case Primitive::kPrimShort:
4335 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004336 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004337 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004338 break;
4339 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004340 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004341 Register left_low = left.AsRegisterPairLow<Register>();
4342 Register left_high = left.AsRegisterPairHigh<Register>();
4343 int32_t val_low = 0;
4344 int32_t val_high = 0;
4345 bool right_is_const = false;
4346
4347 if (right.IsConstant()) {
4348 DCHECK(right.GetConstant()->IsLongConstant());
4349 right_is_const = true;
4350 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4351 val_low = Low32Bits(val);
4352 val_high = High32Bits(val);
4353 }
4354
Calin Juravleddb7df22014-11-25 20:56:51 +00004355 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004356 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004357 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004358 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004359 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004360 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004361 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004362 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004363 __ j(kLess, &less); // Signed compare.
4364 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004365 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004366 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004367 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004368 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004369 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004370 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004371 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004372 }
Aart Bika19616e2016-02-01 18:57:58 -08004373 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004374 break;
4375 }
4376 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004377 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004378 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004379 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004380 break;
4381 }
4382 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004383 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004384 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004385 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004386 break;
4387 }
4388 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004389 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004390 }
Aart Bika19616e2016-02-01 18:57:58 -08004391
Calin Juravleddb7df22014-11-25 20:56:51 +00004392 __ movl(out, Immediate(0));
4393 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004394 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004395
4396 __ Bind(&greater);
4397 __ movl(out, Immediate(1));
4398 __ jmp(&done);
4399
4400 __ Bind(&less);
4401 __ movl(out, Immediate(-1));
4402
4403 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004404}
4405
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004406void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004407 LocationSummary* locations =
4408 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004409 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004410 locations->SetInAt(i, Location::Any());
4411 }
4412 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004413}
4414
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004415void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004416 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004417}
4418
Roland Levillain7c1559a2015-12-15 10:55:36 +00004419void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004420 /*
4421 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4422 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4423 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4424 */
4425 switch (kind) {
4426 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004427 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004428 break;
4429 }
4430 case MemBarrierKind::kAnyStore:
4431 case MemBarrierKind::kLoadAny:
4432 case MemBarrierKind::kStoreStore: {
4433 // nop
4434 break;
4435 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004436 case MemBarrierKind::kNTStoreStore:
4437 // Non-Temporal Store/Store needs an explicit fence.
4438 MemoryFence(/* non-temporal */ true);
4439 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004440 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004441}
4442
Vladimir Markodc151b22015-10-15 18:02:30 +01004443HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4444 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004445 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004446 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4447
4448 // We disable pc-relative load when there is an irreducible loop, as the optimization
4449 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004450 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4451 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004452 if (GetGraph()->HasIrreducibleLoops() &&
4453 (dispatch_info.method_load_kind ==
4454 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4455 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4456 }
4457 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004458 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4459 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4460 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4461 // (Though the direct CALL ptr16:32 is available for consideration).
4462 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004463 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004464 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004465 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004466 0u
4467 };
4468 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004469 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004470 }
4471}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004472
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004473Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4474 Register temp) {
4475 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004476 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004477 if (!invoke->GetLocations()->Intrinsified()) {
4478 return location.AsRegister<Register>();
4479 }
4480 // For intrinsics we allow any location, so it may be on the stack.
4481 if (!location.IsRegister()) {
4482 __ movl(temp, Address(ESP, location.GetStackIndex()));
4483 return temp;
4484 }
4485 // For register locations, check if the register was saved. If so, get it from the stack.
4486 // Note: There is a chance that the register was saved but not overwritten, so we could
4487 // save one load. However, since this is just an intrinsic slow path we prefer this
4488 // simple and more robust approach rather that trying to determine if that's the case.
4489 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004490 if (slow_path != nullptr) {
4491 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4492 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4493 __ movl(temp, Address(ESP, stack_offset));
4494 return temp;
4495 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004496 }
4497 return location.AsRegister<Register>();
4498}
4499
Serguei Katkov288c7a82016-05-16 11:53:15 +06004500Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4501 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004502 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4503 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004504 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004505 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004506 uint32_t offset =
4507 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4508 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004509 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004510 }
Vladimir Marko58155012015-08-19 12:49:41 +00004511 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004512 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004513 break;
4514 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4515 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4516 break;
4517 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004518 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004519 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4520 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004521 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4522 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004523 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4524 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4525 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004526 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004527 // Bind a new fixup label at the end of the "movl" insn.
4528 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004529 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004530 break;
4531 }
Vladimir Marko58155012015-08-19 12:49:41 +00004532 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004533 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004534 Register method_reg;
4535 Register reg = temp.AsRegister<Register>();
4536 if (current_method.IsRegister()) {
4537 method_reg = current_method.AsRegister<Register>();
4538 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004539 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004540 DCHECK(!current_method.IsValid());
4541 method_reg = reg;
4542 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4543 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004544 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004545 __ movl(reg, Address(method_reg,
4546 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004547 // temp = temp[index_in_cache];
4548 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4549 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004550 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4551 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004552 }
Vladimir Marko58155012015-08-19 12:49:41 +00004553 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004554 return callee_method;
4555}
4556
4557void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4558 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004559
4560 switch (invoke->GetCodePtrLocation()) {
4561 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4562 __ call(GetFrameEntryLabel());
4563 break;
4564 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004565 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4566 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004567 Label* label = &relative_call_patches_.back().label;
4568 __ call(label); // Bind to the patch label, override at link time.
4569 __ Bind(label); // Bind the label at the end of the "call" insn.
4570 break;
4571 }
4572 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4573 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004574 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4575 LOG(FATAL) << "Unsupported";
4576 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004577 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4578 // (callee_method + offset_of_quick_compiled_code)()
4579 __ call(Address(callee_method.AsRegister<Register>(),
4580 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004581 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004582 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004583 }
4584
4585 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004586}
4587
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004588void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4589 Register temp = temp_in.AsRegister<Register>();
4590 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4591 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004592
4593 // Use the calling convention instead of the location of the receiver, as
4594 // intrinsics may have put the receiver in a different register. In the intrinsics
4595 // slow path, the arguments have been moved to the right place, so here we are
4596 // guaranteed that the receiver is the first register of the calling convention.
4597 InvokeDexCallingConvention calling_convention;
4598 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004599 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004600 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004601 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004602 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004603 // Instead of simply (possibly) unpoisoning `temp` here, we should
4604 // emit a read barrier for the previous class reference load.
4605 // However this is not required in practice, as this is an
4606 // intermediate/temporary reference and because the current
4607 // concurrent copying collector keeps the from-space memory
4608 // intact/accessible until the end of the marking phase (the
4609 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004610 __ MaybeUnpoisonHeapReference(temp);
4611 // temp = temp->GetMethodAt(method_offset);
4612 __ movl(temp, Address(temp, method_offset));
4613 // call temp->GetEntryPoint();
4614 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004615 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004616}
4617
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004618void CodeGeneratorX86::RecordSimplePatch() {
4619 if (GetCompilerOptions().GetIncludePatchInformation()) {
4620 simple_patches_.emplace_back();
4621 __ Bind(&simple_patches_.back());
4622 }
4623}
4624
Vladimir Markoaad75c62016-10-03 08:46:48 +00004625void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4626 DCHECK(GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004627 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004628 __ Bind(&string_patches_.back().label);
4629}
4630
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004631void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004632 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004633 __ Bind(&type_patches_.back().label);
4634}
4635
Vladimir Markoaad75c62016-10-03 08:46:48 +00004636Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4637 DCHECK(!GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004638 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004639 return &string_patches_.back().label;
4640}
4641
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004642Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4643 uint32_t element_offset) {
4644 // Add the patch entry and bind its label at the end of the instruction.
4645 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4646 return &pc_relative_dex_cache_patches_.back().label;
4647}
4648
Vladimir Markoaad75c62016-10-03 08:46:48 +00004649// The label points to the end of the "movl" or another instruction but the literal offset
4650// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4651constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4652
4653template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4654inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4655 const ArenaDeque<PatchInfo<Label>>& infos,
4656 ArenaVector<LinkerPatch>* linker_patches) {
4657 for (const PatchInfo<Label>& info : infos) {
4658 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4659 linker_patches->push_back(
4660 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4661 }
4662}
4663
Vladimir Marko58155012015-08-19 12:49:41 +00004664void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4665 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004666 size_t size =
4667 method_patches_.size() +
4668 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004669 pc_relative_dex_cache_patches_.size() +
4670 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004671 string_patches_.size() +
4672 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004673 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004674 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004675 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004676 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004677 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004678 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004679 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004680 linker_patches->push_back(
4681 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004682 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004683 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4684 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004685 for (const Label& label : simple_patches_) {
4686 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4687 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4688 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004689 if (!GetCompilerOptions().IsBootImage()) {
4690 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4691 } else if (GetCompilerOptions().GetCompilePic()) {
4692 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004693 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004694 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004695 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004696 linker_patches->push_back(
4697 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004698 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004699 }
4700 if (GetCompilerOptions().GetCompilePic()) {
4701 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4702 } else {
4703 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004704 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004705 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004706 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004707 }
Vladimir Marko58155012015-08-19 12:49:41 +00004708}
4709
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004710void CodeGeneratorX86::MarkGCCard(Register temp,
4711 Register card,
4712 Register object,
4713 Register value,
4714 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004715 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004716 if (value_can_be_null) {
4717 __ testl(value, value);
4718 __ j(kEqual, &is_null);
4719 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004720 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004721 __ movl(temp, object);
4722 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004723 __ movb(Address(temp, card, TIMES_1, 0),
4724 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004725 if (value_can_be_null) {
4726 __ Bind(&is_null);
4727 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004728}
4729
Calin Juravle52c48962014-12-16 17:02:57 +00004730void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4731 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004732
4733 bool object_field_get_with_read_barrier =
4734 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004735 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004736 new (GetGraph()->GetArena()) LocationSummary(instruction,
4737 kEmitCompilerReadBarrier ?
4738 LocationSummary::kCallOnSlowPath :
4739 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004740 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004741 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004742 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004743 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004744
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004745 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4746 locations->SetOut(Location::RequiresFpuRegister());
4747 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004748 // The output overlaps in case of long: we don't want the low move
4749 // to overwrite the object's location. Likewise, in the case of
4750 // an object field get with read barriers enabled, we do not want
4751 // the move to overwrite the object's location, as we need it to emit
4752 // the read barrier.
4753 locations->SetOut(
4754 Location::RequiresRegister(),
4755 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4756 Location::kOutputOverlap :
4757 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004758 }
Calin Juravle52c48962014-12-16 17:02:57 +00004759
4760 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4761 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004762 // So we use an XMM register as a temp to achieve atomicity (first
4763 // load the temp into the XMM and then copy the XMM into the
4764 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004765 locations->AddTemp(Location::RequiresFpuRegister());
4766 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004767}
4768
Calin Juravle52c48962014-12-16 17:02:57 +00004769void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4770 const FieldInfo& field_info) {
4771 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004772
Calin Juravle52c48962014-12-16 17:02:57 +00004773 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004774 Location base_loc = locations->InAt(0);
4775 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004776 Location out = locations->Out();
4777 bool is_volatile = field_info.IsVolatile();
4778 Primitive::Type field_type = field_info.GetFieldType();
4779 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4780
4781 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004782 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004783 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004784 break;
4785 }
4786
4787 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004788 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004789 break;
4790 }
4791
4792 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004793 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004794 break;
4795 }
4796
4797 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004798 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004799 break;
4800 }
4801
4802 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004803 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004804 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004805
4806 case Primitive::kPrimNot: {
4807 // /* HeapReference<Object> */ out = *(base + offset)
4808 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004809 // Note that a potential implicit null check is handled in this
4810 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4811 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004812 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004813 if (is_volatile) {
4814 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4815 }
4816 } else {
4817 __ movl(out.AsRegister<Register>(), Address(base, offset));
4818 codegen_->MaybeRecordImplicitNullCheck(instruction);
4819 if (is_volatile) {
4820 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4821 }
4822 // If read barriers are enabled, emit read barriers other than
4823 // Baker's using a slow path (and also unpoison the loaded
4824 // reference, if heap poisoning is enabled).
4825 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4826 }
4827 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004828 }
4829
4830 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004831 if (is_volatile) {
4832 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4833 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004834 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004835 __ movd(out.AsRegisterPairLow<Register>(), temp);
4836 __ psrlq(temp, Immediate(32));
4837 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4838 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004839 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004840 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004841 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004842 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4843 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004844 break;
4845 }
4846
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004847 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004848 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004849 break;
4850 }
4851
4852 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004853 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004854 break;
4855 }
4856
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004857 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004858 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004859 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004860 }
Calin Juravle52c48962014-12-16 17:02:57 +00004861
Roland Levillain7c1559a2015-12-15 10:55:36 +00004862 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4863 // Potential implicit null checks, in the case of reference or
4864 // long fields, are handled in the previous switch statement.
4865 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004866 codegen_->MaybeRecordImplicitNullCheck(instruction);
4867 }
4868
Calin Juravle52c48962014-12-16 17:02:57 +00004869 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004870 if (field_type == Primitive::kPrimNot) {
4871 // Memory barriers, in the case of references, are also handled
4872 // in the previous switch statement.
4873 } else {
4874 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4875 }
Roland Levillain4d027112015-07-01 15:41:14 +01004876 }
Calin Juravle52c48962014-12-16 17:02:57 +00004877}
4878
4879void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4880 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4881
4882 LocationSummary* locations =
4883 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4884 locations->SetInAt(0, Location::RequiresRegister());
4885 bool is_volatile = field_info.IsVolatile();
4886 Primitive::Type field_type = field_info.GetFieldType();
4887 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4888 || (field_type == Primitive::kPrimByte);
4889
4890 // The register allocator does not support multiple
4891 // inputs that die at entry with one in a specific register.
4892 if (is_byte_type) {
4893 // Ensure the value is in a byte register.
4894 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004895 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004896 if (is_volatile && field_type == Primitive::kPrimDouble) {
4897 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4898 locations->SetInAt(1, Location::RequiresFpuRegister());
4899 } else {
4900 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4901 }
4902 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4903 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004904 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004905
Calin Juravle52c48962014-12-16 17:02:57 +00004906 // 64bits value can be atomically written to an address with movsd and an XMM register.
4907 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4908 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4909 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4910 // isolated cases when we need this it isn't worth adding the extra complexity.
4911 locations->AddTemp(Location::RequiresFpuRegister());
4912 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004913 } else {
4914 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4915
4916 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4917 // Temporary registers for the write barrier.
4918 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4919 // Ensure the card is in a byte register.
4920 locations->AddTemp(Location::RegisterLocation(ECX));
4921 }
Calin Juravle52c48962014-12-16 17:02:57 +00004922 }
4923}
4924
4925void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004926 const FieldInfo& field_info,
4927 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004928 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4929
4930 LocationSummary* locations = instruction->GetLocations();
4931 Register base = locations->InAt(0).AsRegister<Register>();
4932 Location value = locations->InAt(1);
4933 bool is_volatile = field_info.IsVolatile();
4934 Primitive::Type field_type = field_info.GetFieldType();
4935 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004936 bool needs_write_barrier =
4937 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004938
4939 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004940 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004941 }
4942
Mark Mendell81489372015-11-04 11:30:41 -05004943 bool maybe_record_implicit_null_check_done = false;
4944
Calin Juravle52c48962014-12-16 17:02:57 +00004945 switch (field_type) {
4946 case Primitive::kPrimBoolean:
4947 case Primitive::kPrimByte: {
4948 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4949 break;
4950 }
4951
4952 case Primitive::kPrimShort:
4953 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004954 if (value.IsConstant()) {
4955 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4956 __ movw(Address(base, offset), Immediate(v));
4957 } else {
4958 __ movw(Address(base, offset), value.AsRegister<Register>());
4959 }
Calin Juravle52c48962014-12-16 17:02:57 +00004960 break;
4961 }
4962
4963 case Primitive::kPrimInt:
4964 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004965 if (kPoisonHeapReferences && needs_write_barrier) {
4966 // Note that in the case where `value` is a null reference,
4967 // we do not enter this block, as the reference does not
4968 // need poisoning.
4969 DCHECK_EQ(field_type, Primitive::kPrimNot);
4970 Register temp = locations->GetTemp(0).AsRegister<Register>();
4971 __ movl(temp, value.AsRegister<Register>());
4972 __ PoisonHeapReference(temp);
4973 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004974 } else if (value.IsConstant()) {
4975 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4976 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004977 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004978 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004979 __ movl(Address(base, offset), value.AsRegister<Register>());
4980 }
Calin Juravle52c48962014-12-16 17:02:57 +00004981 break;
4982 }
4983
4984 case Primitive::kPrimLong: {
4985 if (is_volatile) {
4986 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4987 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4988 __ movd(temp1, value.AsRegisterPairLow<Register>());
4989 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4990 __ punpckldq(temp1, temp2);
4991 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004992 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004993 } else if (value.IsConstant()) {
4994 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4995 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4996 codegen_->MaybeRecordImplicitNullCheck(instruction);
4997 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004998 } else {
4999 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00005000 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005001 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5002 }
Mark Mendell81489372015-11-04 11:30:41 -05005003 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005004 break;
5005 }
5006
5007 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005008 if (value.IsConstant()) {
5009 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5010 __ movl(Address(base, offset), Immediate(v));
5011 } else {
5012 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5013 }
Calin Juravle52c48962014-12-16 17:02:57 +00005014 break;
5015 }
5016
5017 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005018 if (value.IsConstant()) {
5019 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5020 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5021 codegen_->MaybeRecordImplicitNullCheck(instruction);
5022 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5023 maybe_record_implicit_null_check_done = true;
5024 } else {
5025 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5026 }
Calin Juravle52c48962014-12-16 17:02:57 +00005027 break;
5028 }
5029
5030 case Primitive::kPrimVoid:
5031 LOG(FATAL) << "Unreachable type " << field_type;
5032 UNREACHABLE();
5033 }
5034
Mark Mendell81489372015-11-04 11:30:41 -05005035 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005036 codegen_->MaybeRecordImplicitNullCheck(instruction);
5037 }
5038
Roland Levillain4d027112015-07-01 15:41:14 +01005039 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005040 Register temp = locations->GetTemp(0).AsRegister<Register>();
5041 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005042 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005043 }
5044
Calin Juravle52c48962014-12-16 17:02:57 +00005045 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005046 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005047 }
5048}
5049
5050void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5051 HandleFieldGet(instruction, instruction->GetFieldInfo());
5052}
5053
5054void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5055 HandleFieldGet(instruction, instruction->GetFieldInfo());
5056}
5057
5058void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5059 HandleFieldSet(instruction, instruction->GetFieldInfo());
5060}
5061
5062void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005063 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005064}
5065
5066void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5067 HandleFieldSet(instruction, instruction->GetFieldInfo());
5068}
5069
5070void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005071 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005072}
5073
5074void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5075 HandleFieldGet(instruction, instruction->GetFieldInfo());
5076}
5077
5078void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5079 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005080}
5081
Calin Juravlee460d1d2015-09-29 04:52:17 +01005082void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5083 HUnresolvedInstanceFieldGet* instruction) {
5084 FieldAccessCallingConventionX86 calling_convention;
5085 codegen_->CreateUnresolvedFieldLocationSummary(
5086 instruction, instruction->GetFieldType(), calling_convention);
5087}
5088
5089void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5090 HUnresolvedInstanceFieldGet* instruction) {
5091 FieldAccessCallingConventionX86 calling_convention;
5092 codegen_->GenerateUnresolvedFieldAccess(instruction,
5093 instruction->GetFieldType(),
5094 instruction->GetFieldIndex(),
5095 instruction->GetDexPc(),
5096 calling_convention);
5097}
5098
5099void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5100 HUnresolvedInstanceFieldSet* instruction) {
5101 FieldAccessCallingConventionX86 calling_convention;
5102 codegen_->CreateUnresolvedFieldLocationSummary(
5103 instruction, instruction->GetFieldType(), calling_convention);
5104}
5105
5106void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5107 HUnresolvedInstanceFieldSet* instruction) {
5108 FieldAccessCallingConventionX86 calling_convention;
5109 codegen_->GenerateUnresolvedFieldAccess(instruction,
5110 instruction->GetFieldType(),
5111 instruction->GetFieldIndex(),
5112 instruction->GetDexPc(),
5113 calling_convention);
5114}
5115
5116void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5117 HUnresolvedStaticFieldGet* instruction) {
5118 FieldAccessCallingConventionX86 calling_convention;
5119 codegen_->CreateUnresolvedFieldLocationSummary(
5120 instruction, instruction->GetFieldType(), calling_convention);
5121}
5122
5123void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5124 HUnresolvedStaticFieldGet* instruction) {
5125 FieldAccessCallingConventionX86 calling_convention;
5126 codegen_->GenerateUnresolvedFieldAccess(instruction,
5127 instruction->GetFieldType(),
5128 instruction->GetFieldIndex(),
5129 instruction->GetDexPc(),
5130 calling_convention);
5131}
5132
5133void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5134 HUnresolvedStaticFieldSet* instruction) {
5135 FieldAccessCallingConventionX86 calling_convention;
5136 codegen_->CreateUnresolvedFieldLocationSummary(
5137 instruction, instruction->GetFieldType(), calling_convention);
5138}
5139
5140void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5141 HUnresolvedStaticFieldSet* instruction) {
5142 FieldAccessCallingConventionX86 calling_convention;
5143 codegen_->GenerateUnresolvedFieldAccess(instruction,
5144 instruction->GetFieldType(),
5145 instruction->GetFieldIndex(),
5146 instruction->GetDexPc(),
5147 calling_convention);
5148}
5149
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005150void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005151 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5152 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5153 ? Location::RequiresRegister()
5154 : Location::Any();
5155 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005156}
5157
Calin Juravle2ae48182016-03-16 14:05:09 +00005158void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5159 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005160 return;
5161 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005162 LocationSummary* locations = instruction->GetLocations();
5163 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005164
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005165 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005166 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005167}
5168
Calin Juravle2ae48182016-03-16 14:05:09 +00005169void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005170 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005171 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005172
5173 LocationSummary* locations = instruction->GetLocations();
5174 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005175
5176 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005177 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005178 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005179 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005180 } else {
5181 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005182 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005183 __ jmp(slow_path->GetEntryLabel());
5184 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005185 }
5186 __ j(kEqual, slow_path->GetEntryLabel());
5187}
5188
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005189void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005190 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005191}
5192
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005193void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005194 bool object_array_get_with_read_barrier =
5195 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005196 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005197 new (GetGraph()->GetArena()) LocationSummary(instruction,
5198 object_array_get_with_read_barrier ?
5199 LocationSummary::kCallOnSlowPath :
5200 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005201 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005202 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005203 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005204 locations->SetInAt(0, Location::RequiresRegister());
5205 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005206 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5207 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5208 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005209 // The output overlaps in case of long: we don't want the low move
5210 // to overwrite the array's location. Likewise, in the case of an
5211 // object array get with read barriers enabled, we do not want the
5212 // move to overwrite the array's location, as we need it to emit
5213 // the read barrier.
5214 locations->SetOut(
5215 Location::RequiresRegister(),
5216 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5217 Location::kOutputOverlap :
5218 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005219 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005220}
5221
5222void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5223 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005224 Location obj_loc = locations->InAt(0);
5225 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005226 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005227 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005228 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005229
Calin Juravle77520bc2015-01-12 18:45:46 +00005230 Primitive::Type type = instruction->GetType();
5231 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005232 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005233 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005234 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005235 break;
5236 }
5237
5238 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005239 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005240 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005241 break;
5242 }
5243
5244 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005245 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005246 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005247 break;
5248 }
5249
5250 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005251 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005252 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5253 // Branch cases into compressed and uncompressed for each index's type.
5254 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5255 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005256 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005257 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005258 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5259 "Expecting 0=compressed, 1=uncompressed");
5260 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005261 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5262 __ jmp(&done);
5263 __ Bind(&not_compressed);
5264 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5265 __ Bind(&done);
5266 } else {
5267 // Common case for charAt of array of char or when string compression's
5268 // feature is turned off.
5269 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5270 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005271 break;
5272 }
5273
Roland Levillain7c1559a2015-12-15 10:55:36 +00005274 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005275 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005276 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005277 break;
5278 }
5279
Roland Levillain7c1559a2015-12-15 10:55:36 +00005280 case Primitive::kPrimNot: {
5281 static_assert(
5282 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5283 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005284 // /* HeapReference<Object> */ out =
5285 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5286 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005287 // Note that a potential implicit null check is handled in this
5288 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5289 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005290 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005291 } else {
5292 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005293 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5294 codegen_->MaybeRecordImplicitNullCheck(instruction);
5295 // If read barriers are enabled, emit read barriers other than
5296 // Baker's using a slow path (and also unpoison the loaded
5297 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005298 if (index.IsConstant()) {
5299 uint32_t offset =
5300 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005301 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5302 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005303 codegen_->MaybeGenerateReadBarrierSlow(
5304 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5305 }
5306 }
5307 break;
5308 }
5309
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005310 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005311 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005312 __ movl(out_loc.AsRegisterPairLow<Register>(),
5313 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5314 codegen_->MaybeRecordImplicitNullCheck(instruction);
5315 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5316 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005317 break;
5318 }
5319
Mark Mendell7c8d0092015-01-26 11:21:33 -05005320 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005321 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005322 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005323 break;
5324 }
5325
5326 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005327 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005328 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005329 break;
5330 }
5331
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005332 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005333 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005334 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005335 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005336
Roland Levillain7c1559a2015-12-15 10:55:36 +00005337 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5338 // Potential implicit null checks, in the case of reference or
5339 // long arrays, are handled in the previous switch statement.
5340 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005341 codegen_->MaybeRecordImplicitNullCheck(instruction);
5342 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005343}
5344
5345void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005346 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005347
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005348 bool needs_write_barrier =
5349 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005350 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005351
Nicolas Geoffray39468442014-09-02 15:17:15 +01005352 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5353 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005354 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005355 LocationSummary::kCallOnSlowPath :
5356 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005357
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005358 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5359 || (value_type == Primitive::kPrimByte);
5360 // We need the inputs to be different than the output in case of long operation.
5361 // In case of a byte operation, the register allocator does not support multiple
5362 // inputs that die at entry with one in a specific register.
5363 locations->SetInAt(0, Location::RequiresRegister());
5364 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5365 if (is_byte_type) {
5366 // Ensure the value is in a byte register.
5367 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5368 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005369 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005370 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005371 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5372 }
5373 if (needs_write_barrier) {
5374 // Temporary registers for the write barrier.
5375 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5376 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005377 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005379}
5380
5381void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5382 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005383 Location array_loc = locations->InAt(0);
5384 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005385 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005386 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005387 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005388 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5389 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5390 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005391 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005392 bool needs_write_barrier =
5393 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005394
5395 switch (value_type) {
5396 case Primitive::kPrimBoolean:
5397 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005398 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005399 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005400 if (value.IsRegister()) {
5401 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005402 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005403 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005404 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005405 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005406 break;
5407 }
5408
5409 case Primitive::kPrimShort:
5410 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005411 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005412 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005413 if (value.IsRegister()) {
5414 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005415 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005416 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005417 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005418 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005419 break;
5420 }
5421
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005422 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005423 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005424 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005425
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005426 if (!value.IsRegister()) {
5427 // Just setting null.
5428 DCHECK(instruction->InputAt(2)->IsNullConstant());
5429 DCHECK(value.IsConstant()) << value;
5430 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005431 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005432 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005433 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005434 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005435 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005436
5437 DCHECK(needs_write_barrier);
5438 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005439 // We cannot use a NearLabel for `done`, as its range may be too
5440 // short when Baker read barriers are enabled.
5441 Label done;
5442 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005443 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005444 Location temp_loc = locations->GetTemp(0);
5445 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005446 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005447 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5448 codegen_->AddSlowPath(slow_path);
5449 if (instruction->GetValueCanBeNull()) {
5450 __ testl(register_value, register_value);
5451 __ j(kNotEqual, &not_null);
5452 __ movl(address, Immediate(0));
5453 codegen_->MaybeRecordImplicitNullCheck(instruction);
5454 __ jmp(&done);
5455 __ Bind(&not_null);
5456 }
5457
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005458 // Note that when Baker read barriers are enabled, the type
5459 // checks are performed without read barriers. This is fine,
5460 // even in the case where a class object is in the from-space
5461 // after the flip, as a comparison involving such a type would
5462 // not produce a false positive; it may of course produce a
5463 // false negative, in which case we would take the ArraySet
5464 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005465
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005466 // /* HeapReference<Class> */ temp = array->klass_
5467 __ movl(temp, Address(array, class_offset));
5468 codegen_->MaybeRecordImplicitNullCheck(instruction);
5469 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005470
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005471 // /* HeapReference<Class> */ temp = temp->component_type_
5472 __ movl(temp, Address(temp, component_offset));
5473 // If heap poisoning is enabled, no need to unpoison `temp`
5474 // nor the object reference in `register_value->klass`, as
5475 // we are comparing two poisoned references.
5476 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005477
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005478 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5479 __ j(kEqual, &do_put);
5480 // If heap poisoning is enabled, the `temp` reference has
5481 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005482 __ MaybeUnpoisonHeapReference(temp);
5483
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005484 // If heap poisoning is enabled, no need to unpoison the
5485 // heap reference loaded below, as it is only used for a
5486 // comparison with null.
5487 __ cmpl(Address(temp, super_offset), Immediate(0));
5488 __ j(kNotEqual, slow_path->GetEntryLabel());
5489 __ Bind(&do_put);
5490 } else {
5491 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005492 }
5493 }
5494
5495 if (kPoisonHeapReferences) {
5496 __ movl(temp, register_value);
5497 __ PoisonHeapReference(temp);
5498 __ movl(address, temp);
5499 } else {
5500 __ movl(address, register_value);
5501 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005502 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005503 codegen_->MaybeRecordImplicitNullCheck(instruction);
5504 }
5505
5506 Register card = locations->GetTemp(1).AsRegister<Register>();
5507 codegen_->MarkGCCard(
5508 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5509 __ Bind(&done);
5510
5511 if (slow_path != nullptr) {
5512 __ Bind(slow_path->GetExitLabel());
5513 }
5514
5515 break;
5516 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005517
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005518 case Primitive::kPrimInt: {
5519 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005520 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005521 if (value.IsRegister()) {
5522 __ movl(address, value.AsRegister<Register>());
5523 } else {
5524 DCHECK(value.IsConstant()) << value;
5525 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5526 __ movl(address, Immediate(v));
5527 }
5528 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005529 break;
5530 }
5531
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005532 case Primitive::kPrimLong: {
5533 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005534 if (value.IsRegisterPair()) {
5535 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5536 value.AsRegisterPairLow<Register>());
5537 codegen_->MaybeRecordImplicitNullCheck(instruction);
5538 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5539 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005540 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005541 DCHECK(value.IsConstant());
5542 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5543 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5544 Immediate(Low32Bits(val)));
5545 codegen_->MaybeRecordImplicitNullCheck(instruction);
5546 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5547 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005548 }
5549 break;
5550 }
5551
Mark Mendell7c8d0092015-01-26 11:21:33 -05005552 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005553 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005554 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005555 if (value.IsFpuRegister()) {
5556 __ movss(address, value.AsFpuRegister<XmmRegister>());
5557 } else {
5558 DCHECK(value.IsConstant());
5559 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5560 __ movl(address, Immediate(v));
5561 }
5562 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005563 break;
5564 }
5565
5566 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005567 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005568 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005569 if (value.IsFpuRegister()) {
5570 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5571 } else {
5572 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005573 Address address_hi =
5574 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005575 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5576 __ movl(address, Immediate(Low32Bits(v)));
5577 codegen_->MaybeRecordImplicitNullCheck(instruction);
5578 __ movl(address_hi, Immediate(High32Bits(v)));
5579 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005580 break;
5581 }
5582
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005583 case Primitive::kPrimVoid:
5584 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005585 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005586 }
5587}
5588
5589void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5590 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005591 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005592 if (!instruction->IsEmittedAtUseSite()) {
5593 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5594 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005595}
5596
5597void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005598 if (instruction->IsEmittedAtUseSite()) {
5599 return;
5600 }
5601
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005602 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005603 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005604 Register obj = locations->InAt(0).AsRegister<Register>();
5605 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005606 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005607 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005608 // Mask out most significant bit in case the array is String's array of char.
5609 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005610 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005611 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005612}
5613
5614void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005615 RegisterSet caller_saves = RegisterSet::Empty();
5616 InvokeRuntimeCallingConvention calling_convention;
5617 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5618 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5619 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005620 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005621 HInstruction* length = instruction->InputAt(1);
5622 if (!length->IsEmittedAtUseSite()) {
5623 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5624 }
jessicahandojo4877b792016-09-08 19:49:13 -07005625 // Need register to see array's length.
5626 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5627 locations->AddTemp(Location::RequiresRegister());
5628 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005629}
5630
5631void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005632 const bool is_string_compressed_char_at =
5633 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005634 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005635 Location index_loc = locations->InAt(0);
5636 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005637 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005638 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005639
Mark Mendell99dbd682015-04-22 16:18:52 -04005640 if (length_loc.IsConstant()) {
5641 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5642 if (index_loc.IsConstant()) {
5643 // BCE will remove the bounds check if we are guarenteed to pass.
5644 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5645 if (index < 0 || index >= length) {
5646 codegen_->AddSlowPath(slow_path);
5647 __ jmp(slow_path->GetEntryLabel());
5648 } else {
5649 // Some optimization after BCE may have generated this, and we should not
5650 // generate a bounds check if it is a valid range.
5651 }
5652 return;
5653 }
5654
5655 // We have to reverse the jump condition because the length is the constant.
5656 Register index_reg = index_loc.AsRegister<Register>();
5657 __ cmpl(index_reg, Immediate(length));
5658 codegen_->AddSlowPath(slow_path);
5659 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005660 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005661 HInstruction* array_length = instruction->InputAt(1);
5662 if (array_length->IsEmittedAtUseSite()) {
5663 // Address the length field in the array.
5664 DCHECK(array_length->IsArrayLength());
5665 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5666 Location array_loc = array_length->GetLocations()->InAt(0);
5667 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005668 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005669 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5670 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005671 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5672 __ movl(length_reg, array_len);
5673 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005674 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005675 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005676 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005677 // Checking bounds for general case:
5678 // Array of char or string's array with feature compression off.
5679 if (index_loc.IsConstant()) {
5680 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5681 __ cmpl(array_len, Immediate(value));
5682 } else {
5683 __ cmpl(array_len, index_loc.AsRegister<Register>());
5684 }
5685 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005686 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005687 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005688 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005689 }
5690 codegen_->AddSlowPath(slow_path);
5691 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005692 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005693}
5694
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005695void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005696 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005697}
5698
5699void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005700 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5701}
5702
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005703void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005704 LocationSummary* locations =
5705 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005706 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005707}
5708
5709void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005710 HBasicBlock* block = instruction->GetBlock();
5711 if (block->GetLoopInformation() != nullptr) {
5712 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5713 // The back edge will generate the suspend check.
5714 return;
5715 }
5716 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5717 // The goto will generate the suspend check.
5718 return;
5719 }
5720 GenerateSuspendCheck(instruction, nullptr);
5721}
5722
5723void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5724 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005725 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005726 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5727 if (slow_path == nullptr) {
5728 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5729 instruction->SetSlowPath(slow_path);
5730 codegen_->AddSlowPath(slow_path);
5731 if (successor != nullptr) {
5732 DCHECK(successor->IsLoopHeader());
5733 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5734 }
5735 } else {
5736 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5737 }
5738
Andreas Gampe542451c2016-07-26 09:02:02 -07005739 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005740 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005741 if (successor == nullptr) {
5742 __ j(kNotEqual, slow_path->GetEntryLabel());
5743 __ Bind(slow_path->GetReturnLabel());
5744 } else {
5745 __ j(kEqual, codegen_->GetLabelOf(successor));
5746 __ jmp(slow_path->GetEntryLabel());
5747 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005748}
5749
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005750X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5751 return codegen_->GetAssembler();
5752}
5753
Mark Mendell7c8d0092015-01-26 11:21:33 -05005754void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005755 ScratchRegisterScope ensure_scratch(
5756 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5757 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5758 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5759 __ movl(temp_reg, Address(ESP, src + stack_offset));
5760 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005761}
5762
5763void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005764 ScratchRegisterScope ensure_scratch(
5765 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5766 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5767 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5768 __ movl(temp_reg, Address(ESP, src + stack_offset));
5769 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5770 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5771 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005772}
5773
5774void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005775 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005776 Location source = move->GetSource();
5777 Location destination = move->GetDestination();
5778
5779 if (source.IsRegister()) {
5780 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005781 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005782 } else if (destination.IsFpuRegister()) {
5783 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005784 } else {
5785 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005786 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005787 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005788 } else if (source.IsRegisterPair()) {
5789 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5790 // Create stack space for 2 elements.
5791 __ subl(ESP, Immediate(2 * elem_size));
5792 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5793 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5794 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5795 // And remove the temporary stack space we allocated.
5796 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005797 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005798 if (destination.IsRegister()) {
5799 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5800 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005801 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005802 } else if (destination.IsRegisterPair()) {
5803 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5804 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5805 __ psrlq(src_reg, Immediate(32));
5806 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005807 } else if (destination.IsStackSlot()) {
5808 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5809 } else {
5810 DCHECK(destination.IsDoubleStackSlot());
5811 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5812 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005813 } else if (source.IsStackSlot()) {
5814 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005815 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005816 } else if (destination.IsFpuRegister()) {
5817 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005818 } else {
5819 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005820 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5821 }
5822 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005823 if (destination.IsRegisterPair()) {
5824 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5825 __ movl(destination.AsRegisterPairHigh<Register>(),
5826 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5827 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005828 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5829 } else {
5830 DCHECK(destination.IsDoubleStackSlot()) << destination;
5831 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005832 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005833 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005834 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005835 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005836 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005837 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005838 if (value == 0) {
5839 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5840 } else {
5841 __ movl(destination.AsRegister<Register>(), Immediate(value));
5842 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005843 } else {
5844 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005845 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005846 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005847 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005848 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005849 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005850 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005851 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005852 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5853 if (value == 0) {
5854 // Easy handling of 0.0.
5855 __ xorps(dest, dest);
5856 } else {
5857 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005858 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5859 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5860 __ movl(temp, Immediate(value));
5861 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005862 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005863 } else {
5864 DCHECK(destination.IsStackSlot()) << destination;
5865 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5866 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005867 } else if (constant->IsLongConstant()) {
5868 int64_t value = constant->AsLongConstant()->GetValue();
5869 int32_t low_value = Low32Bits(value);
5870 int32_t high_value = High32Bits(value);
5871 Immediate low(low_value);
5872 Immediate high(high_value);
5873 if (destination.IsDoubleStackSlot()) {
5874 __ movl(Address(ESP, destination.GetStackIndex()), low);
5875 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5876 } else {
5877 __ movl(destination.AsRegisterPairLow<Register>(), low);
5878 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5879 }
5880 } else {
5881 DCHECK(constant->IsDoubleConstant());
5882 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005883 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005884 int32_t low_value = Low32Bits(value);
5885 int32_t high_value = High32Bits(value);
5886 Immediate low(low_value);
5887 Immediate high(high_value);
5888 if (destination.IsFpuRegister()) {
5889 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5890 if (value == 0) {
5891 // Easy handling of 0.0.
5892 __ xorpd(dest, dest);
5893 } else {
5894 __ pushl(high);
5895 __ pushl(low);
5896 __ movsd(dest, Address(ESP, 0));
5897 __ addl(ESP, Immediate(8));
5898 }
5899 } else {
5900 DCHECK(destination.IsDoubleStackSlot()) << destination;
5901 __ movl(Address(ESP, destination.GetStackIndex()), low);
5902 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5903 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005904 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005905 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005906 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005907 }
5908}
5909
Mark Mendella5c19ce2015-04-01 12:51:05 -04005910void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005911 Register suggested_scratch = reg == EAX ? EBX : EAX;
5912 ScratchRegisterScope ensure_scratch(
5913 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5914
5915 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5916 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5917 __ movl(Address(ESP, mem + stack_offset), reg);
5918 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005919}
5920
Mark Mendell7c8d0092015-01-26 11:21:33 -05005921void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005922 ScratchRegisterScope ensure_scratch(
5923 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5924
5925 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5926 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5927 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5928 __ movss(Address(ESP, mem + stack_offset), reg);
5929 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005930}
5931
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005932void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005933 ScratchRegisterScope ensure_scratch1(
5934 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005935
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005936 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5937 ScratchRegisterScope ensure_scratch2(
5938 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005939
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005940 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5941 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5942 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5943 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5944 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5945 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005946}
5947
5948void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005949 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005950 Location source = move->GetSource();
5951 Location destination = move->GetDestination();
5952
5953 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005954 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5955 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5956 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5957 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5958 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005959 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005960 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005961 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005962 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005963 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5964 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005965 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5966 // Use XOR Swap algorithm to avoid a temporary.
5967 DCHECK_NE(source.reg(), destination.reg());
5968 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5969 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5970 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5971 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5972 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5973 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5974 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005975 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5976 // Take advantage of the 16 bytes in the XMM register.
5977 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5978 Address stack(ESP, destination.GetStackIndex());
5979 // Load the double into the high doubleword.
5980 __ movhpd(reg, stack);
5981
5982 // Store the low double into the destination.
5983 __ movsd(stack, reg);
5984
5985 // Move the high double to the low double.
5986 __ psrldq(reg, Immediate(8));
5987 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5988 // Take advantage of the 16 bytes in the XMM register.
5989 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5990 Address stack(ESP, source.GetStackIndex());
5991 // Load the double into the high doubleword.
5992 __ movhpd(reg, stack);
5993
5994 // Store the low double into the destination.
5995 __ movsd(stack, reg);
5996
5997 // Move the high double to the low double.
5998 __ psrldq(reg, Immediate(8));
5999 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
6000 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6001 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006002 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006003 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006004 }
6005}
6006
6007void ParallelMoveResolverX86::SpillScratch(int reg) {
6008 __ pushl(static_cast<Register>(reg));
6009}
6010
6011void ParallelMoveResolverX86::RestoreScratch(int reg) {
6012 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006013}
6014
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006015HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6016 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006017 switch (desired_class_load_kind) {
6018 case HLoadClass::LoadKind::kReferrersClass:
6019 break;
6020 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6021 DCHECK(!GetCompilerOptions().GetCompilePic());
6022 break;
6023 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6024 DCHECK(GetCompilerOptions().GetCompilePic());
6025 FALLTHROUGH_INTENDED;
6026 case HLoadClass::LoadKind::kDexCachePcRelative:
6027 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6028 // We disable pc-relative load when there is an irreducible loop, as the optimization
6029 // is incompatible with it.
6030 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6031 // with irreducible loops.
6032 if (GetGraph()->HasIrreducibleLoops()) {
6033 return HLoadClass::LoadKind::kDexCacheViaMethod;
6034 }
6035 break;
6036 case HLoadClass::LoadKind::kBootImageAddress:
6037 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006038 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006039 DCHECK(Runtime::Current()->UseJitCompilation());
6040 break;
6041 case HLoadClass::LoadKind::kDexCacheViaMethod:
6042 break;
6043 }
6044 return desired_class_load_kind;
6045}
6046
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006047void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006048 if (cls->NeedsAccessCheck()) {
6049 InvokeRuntimeCallingConvention calling_convention;
6050 CodeGenerator::CreateLoadClassLocationSummary(
6051 cls,
6052 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6053 Location::RegisterLocation(EAX),
6054 /* code_generator_supports_read_barrier */ true);
6055 return;
6056 }
6057
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006058 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6059 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006060 ? LocationSummary::kCallOnSlowPath
6061 : LocationSummary::kNoCall;
6062 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006063 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006064 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006065 }
6066
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006067 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6068 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6069 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6070 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6071 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6072 locations->SetInAt(0, Location::RequiresRegister());
6073 }
6074 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006075}
6076
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006077Label* CodeGeneratorX86::NewJitRootClassPatch(const DexFile& dex_file,
6078 dex::TypeIndex dex_index,
6079 uint64_t address) {
6080 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index), address);
6081 // Add a patch entry and return the label.
6082 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
6083 PatchInfo<Label>* info = &jit_class_patches_.back();
6084 return &info->label;
6085}
6086
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006087void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006088 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006089 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08006090 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescuba45db02016-07-12 22:53:02 +01006091 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006092 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006093 return;
6094 }
6095
Roland Levillain0d5a2812015-11-13 10:07:31 +00006096 Location out_loc = locations->Out();
6097 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006098
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006099 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006100 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6101 ? kWithoutReadBarrier
6102 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006103 switch (cls->GetLoadKind()) {
6104 case HLoadClass::LoadKind::kReferrersClass: {
6105 DCHECK(!cls->CanCallRuntime());
6106 DCHECK(!cls->MustGenerateClinitCheck());
6107 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6108 Register current_method = locations->InAt(0).AsRegister<Register>();
6109 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006110 cls,
6111 out_loc,
6112 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006113 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006114 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006115 break;
6116 }
6117 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006118 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006119 __ movl(out, Immediate(/* placeholder */ 0));
6120 codegen_->RecordTypePatch(cls);
6121 break;
6122 }
6123 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006124 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006125 Register method_address = locations->InAt(0).AsRegister<Register>();
6126 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6127 codegen_->RecordTypePatch(cls);
6128 break;
6129 }
6130 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006131 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006132 DCHECK_NE(cls->GetAddress(), 0u);
6133 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6134 __ movl(out, Immediate(address));
6135 codegen_->RecordSimplePatch();
6136 break;
6137 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006138 case HLoadClass::LoadKind::kJitTableAddress: {
6139 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6140 Label* fixup_label = codegen_->NewJitRootClassPatch(
6141 cls->GetDexFile(), cls->GetTypeIndex(), cls->GetAddress());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006142 // /* GcRoot<mirror::Class> */ out = *address
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00006143 GenerateGcRootFieldLoad(cls, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006144 break;
6145 }
6146 case HLoadClass::LoadKind::kDexCachePcRelative: {
6147 Register base_reg = locations->InAt(0).AsRegister<Register>();
6148 uint32_t offset = cls->GetDexCacheElementOffset();
6149 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6150 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006151 GenerateGcRootFieldLoad(cls,
6152 out_loc,
6153 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6154 fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006155 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006156 generate_null_check = !cls->IsInDexCache();
6157 break;
6158 }
6159 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6160 // /* GcRoot<mirror::Class>[] */ out =
6161 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6162 Register current_method = locations->InAt(0).AsRegister<Register>();
6163 __ movl(out, Address(current_method,
6164 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6165 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006166 GenerateGcRootFieldLoad(cls,
6167 out_loc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006168 Address(out,
6169 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_)),
Roland Levillain00468f32016-10-27 18:02:48 +01006170 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006171 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006172 generate_null_check = !cls->IsInDexCache();
6173 break;
6174 }
6175 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006176
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006177 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6178 DCHECK(cls->CanCallRuntime());
6179 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6180 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6181 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006182
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006183 if (generate_null_check) {
6184 __ testl(out, out);
6185 __ j(kEqual, slow_path->GetEntryLabel());
6186 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006187
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006188 if (cls->MustGenerateClinitCheck()) {
6189 GenerateClassInitializationCheck(slow_path, out);
6190 } else {
6191 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006192 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006193 }
6194}
6195
6196void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6197 LocationSummary* locations =
6198 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6199 locations->SetInAt(0, Location::RequiresRegister());
6200 if (check->HasUses()) {
6201 locations->SetOut(Location::SameAsFirstInput());
6202 }
6203}
6204
6205void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006206 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006207 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006208 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006209 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006210 GenerateClassInitializationCheck(slow_path,
6211 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006212}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006213
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006214void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006215 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006216 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6217 Immediate(mirror::Class::kStatusInitialized));
6218 __ j(kLess, slow_path->GetEntryLabel());
6219 __ Bind(slow_path->GetExitLabel());
6220 // No need for memory fence, thanks to the X86 memory model.
6221}
6222
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006223HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6224 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006225 switch (desired_string_load_kind) {
6226 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6227 DCHECK(!GetCompilerOptions().GetCompilePic());
6228 break;
6229 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6230 DCHECK(GetCompilerOptions().GetCompilePic());
6231 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006232 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006233 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006234 // We disable pc-relative load when there is an irreducible loop, as the optimization
6235 // is incompatible with it.
6236 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6237 // with irreducible loops.
6238 if (GetGraph()->HasIrreducibleLoops()) {
6239 return HLoadString::LoadKind::kDexCacheViaMethod;
6240 }
6241 break;
6242 case HLoadString::LoadKind::kBootImageAddress:
6243 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006244 case HLoadString::LoadKind::kDexCacheViaMethod:
6245 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006246 case HLoadString::LoadKind::kJitTableAddress:
6247 DCHECK(Runtime::Current()->UseJitCompilation());
6248 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006249 }
6250 return desired_string_load_kind;
6251}
6252
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006253void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006254 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006256 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006257 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006258 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006259 locations->SetInAt(0, Location::RequiresRegister());
6260 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006261 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6262 locations->SetOut(Location::RegisterLocation(EAX));
6263 } else {
6264 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006265 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6266 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6267 // Rely on the pResolveString and/or marking to save everything.
6268 RegisterSet caller_saves = RegisterSet::Empty();
6269 InvokeRuntimeCallingConvention calling_convention;
6270 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6271 locations->SetCustomSlowPathCallerSaves(caller_saves);
6272 } else {
6273 // For non-Baker read barrier we have a temp-clobbering call.
6274 }
6275 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006276 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006277}
6278
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006279Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
6280 dex::StringIndex dex_index) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006281 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index), /* placeholder */ 0u);
6282 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006283 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006284 PatchInfo<Label>* info = &jit_string_patches_.back();
6285 return &info->label;
6286}
6287
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006288void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006289 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006290 Location out_loc = locations->Out();
6291 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006292
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 switch (load->GetLoadKind()) {
6294 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006295 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006296 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006297 return; // No dex cache slow path.
6298 }
6299 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006300 Register method_address = locations->InAt(0).AsRegister<Register>();
6301 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006302 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006303 return; // No dex cache slow path.
6304 }
6305 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006306 DCHECK_NE(load->GetAddress(), 0u);
6307 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6308 __ movl(out, Immediate(address));
6309 codegen_->RecordSimplePatch();
6310 return; // No dex cache slow path.
6311 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006312 case HLoadString::LoadKind::kBssEntry: {
6313 Register method_address = locations->InAt(0).AsRegister<Register>();
6314 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6315 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006316 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006317 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006318 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6319 codegen_->AddSlowPath(slow_path);
6320 __ testl(out, out);
6321 __ j(kEqual, slow_path->GetEntryLabel());
6322 __ Bind(slow_path->GetExitLabel());
6323 return;
6324 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006325 case HLoadString::LoadKind::kJitTableAddress: {
6326 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6327 Label* fixup_label = codegen_->NewJitRootStringPatch(
6328 load->GetDexFile(), load->GetStringIndex());
6329 // /* GcRoot<mirror::String> */ out = *address
6330 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6331 return;
6332 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006333 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006334 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006335 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006336
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006337 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006338 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006339 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006340 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006341 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6342 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006343}
6344
David Brazdilcb1c0552015-08-04 16:22:25 +01006345static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006346 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006347}
6348
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006349void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6350 LocationSummary* locations =
6351 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6352 locations->SetOut(Location::RequiresRegister());
6353}
6354
6355void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006356 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6357}
6358
6359void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6360 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6361}
6362
6363void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6364 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006365}
6366
6367void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6368 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006369 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006370 InvokeRuntimeCallingConvention calling_convention;
6371 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6372}
6373
6374void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006375 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006376 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006377}
6378
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006379// Temp is used for read barrier.
6380static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6381 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006382 !kUseBakerReadBarrier &&
6383 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006384 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006385 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6386 return 1;
6387 }
6388 return 0;
6389}
6390
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006391// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006392// interface pointer, one for loading the current interface.
6393// The other checks have one temp for loading the object's class.
6394static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6395 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6396 return 2;
6397 }
6398 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006399}
6400
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006401void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006402 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006404 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006405 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 case TypeCheckKind::kExactCheck:
6407 case TypeCheckKind::kAbstractClassCheck:
6408 case TypeCheckKind::kClassHierarchyCheck:
6409 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006410 call_kind =
6411 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006412 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006413 break;
6414 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006415 case TypeCheckKind::kUnresolvedCheck:
6416 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006417 call_kind = LocationSummary::kCallOnSlowPath;
6418 break;
6419 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006420
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006421 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006422 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006423 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006424 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006425 locations->SetInAt(0, Location::RequiresRegister());
6426 locations->SetInAt(1, Location::Any());
6427 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6428 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006429 // When read barriers are enabled, we need a temporary register for some cases.
6430 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006431}
6432
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006433void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006434 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006435 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006436 Location obj_loc = locations->InAt(0);
6437 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006438 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006439 Location out_loc = locations->Out();
6440 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006441 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6442 DCHECK_LE(num_temps, 1u);
6443 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006444 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006445 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6446 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6447 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006448 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006449 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006450
6451 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006452 // Avoid null check if we know obj is not null.
6453 if (instruction->MustDoNullCheck()) {
6454 __ testl(obj, obj);
6455 __ j(kEqual, &zero);
6456 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006457
Roland Levillain7c1559a2015-12-15 10:55:36 +00006458 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006460 // /* HeapReference<Class> */ out = obj->klass_
6461 GenerateReferenceLoadTwoRegisters(instruction,
6462 out_loc,
6463 obj_loc,
6464 class_offset,
6465 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466 if (cls.IsRegister()) {
6467 __ cmpl(out, cls.AsRegister<Register>());
6468 } else {
6469 DCHECK(cls.IsStackSlot()) << cls;
6470 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6471 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 // Classes must be equal for the instanceof to succeed.
6474 __ j(kNotEqual, &zero);
6475 __ movl(out, Immediate(1));
6476 __ jmp(&done);
6477 break;
6478 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006479
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006481 // /* HeapReference<Class> */ out = obj->klass_
6482 GenerateReferenceLoadTwoRegisters(instruction,
6483 out_loc,
6484 obj_loc,
6485 class_offset,
6486 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006487 // If the class is abstract, we eagerly fetch the super class of the
6488 // object to avoid doing a comparison we know will fail.
6489 NearLabel loop;
6490 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006491 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006492 GenerateReferenceLoadOneRegister(instruction,
6493 out_loc,
6494 super_offset,
6495 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006496 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006497 __ testl(out, out);
6498 // If `out` is null, we use it for the result, and jump to `done`.
6499 __ j(kEqual, &done);
6500 if (cls.IsRegister()) {
6501 __ cmpl(out, cls.AsRegister<Register>());
6502 } else {
6503 DCHECK(cls.IsStackSlot()) << cls;
6504 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6505 }
6506 __ j(kNotEqual, &loop);
6507 __ movl(out, Immediate(1));
6508 if (zero.IsLinked()) {
6509 __ jmp(&done);
6510 }
6511 break;
6512 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006513
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006515 // /* HeapReference<Class> */ out = obj->klass_
6516 GenerateReferenceLoadTwoRegisters(instruction,
6517 out_loc,
6518 obj_loc,
6519 class_offset,
6520 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006521 // Walk over the class hierarchy to find a match.
6522 NearLabel loop, success;
6523 __ Bind(&loop);
6524 if (cls.IsRegister()) {
6525 __ cmpl(out, cls.AsRegister<Register>());
6526 } else {
6527 DCHECK(cls.IsStackSlot()) << cls;
6528 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6529 }
6530 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006531 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006532 GenerateReferenceLoadOneRegister(instruction,
6533 out_loc,
6534 super_offset,
6535 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006536 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006537 __ testl(out, out);
6538 __ j(kNotEqual, &loop);
6539 // If `out` is null, we use it for the result, and jump to `done`.
6540 __ jmp(&done);
6541 __ Bind(&success);
6542 __ movl(out, Immediate(1));
6543 if (zero.IsLinked()) {
6544 __ jmp(&done);
6545 }
6546 break;
6547 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006548
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006549 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006550 // /* HeapReference<Class> */ out = obj->klass_
6551 GenerateReferenceLoadTwoRegisters(instruction,
6552 out_loc,
6553 obj_loc,
6554 class_offset,
6555 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006556 // Do an exact check.
6557 NearLabel exact_check;
6558 if (cls.IsRegister()) {
6559 __ cmpl(out, cls.AsRegister<Register>());
6560 } else {
6561 DCHECK(cls.IsStackSlot()) << cls;
6562 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6563 }
6564 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006565 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006566 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006567 GenerateReferenceLoadOneRegister(instruction,
6568 out_loc,
6569 component_offset,
6570 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006571 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006572 __ testl(out, out);
6573 // If `out` is null, we use it for the result, and jump to `done`.
6574 __ j(kEqual, &done);
6575 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6576 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006577 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006578 __ movl(out, Immediate(1));
6579 __ jmp(&done);
6580 break;
6581 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006582
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006583 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006584 // No read barrier since the slow path will retry upon failure.
6585 // /* HeapReference<Class> */ out = obj->klass_
6586 GenerateReferenceLoadTwoRegisters(instruction,
6587 out_loc,
6588 obj_loc,
6589 class_offset,
6590 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006591 if (cls.IsRegister()) {
6592 __ cmpl(out, cls.AsRegister<Register>());
6593 } else {
6594 DCHECK(cls.IsStackSlot()) << cls;
6595 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6596 }
6597 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006598 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6599 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006600 codegen_->AddSlowPath(slow_path);
6601 __ j(kNotEqual, slow_path->GetEntryLabel());
6602 __ movl(out, Immediate(1));
6603 if (zero.IsLinked()) {
6604 __ jmp(&done);
6605 }
6606 break;
6607 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006608
Calin Juravle98893e12015-10-02 21:05:03 +01006609 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006610 case TypeCheckKind::kInterfaceCheck: {
6611 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006612 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006613 // cases.
6614 //
6615 // We cannot directly call the InstanceofNonTrivial runtime
6616 // entry point without resorting to a type checking slow path
6617 // here (i.e. by calling InvokeRuntime directly), as it would
6618 // require to assign fixed registers for the inputs of this
6619 // HInstanceOf instruction (following the runtime calling
6620 // convention), which might be cluttered by the potential first
6621 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006622 //
6623 // TODO: Introduce a new runtime entry point taking the object
6624 // to test (instead of its class) as argument, and let it deal
6625 // with the read barrier issues. This will let us refactor this
6626 // case of the `switch` code as it was previously (with a direct
6627 // call to the runtime not using a type checking slow path).
6628 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006629 DCHECK(locations->OnlyCallsOnSlowPath());
6630 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6631 /* is_fatal */ false);
6632 codegen_->AddSlowPath(slow_path);
6633 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006634 if (zero.IsLinked()) {
6635 __ jmp(&done);
6636 }
6637 break;
6638 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006639 }
6640
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006641 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006642 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006643 __ xorl(out, out);
6644 }
6645
6646 if (done.IsLinked()) {
6647 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006648 }
6649
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006650 if (slow_path != nullptr) {
6651 __ Bind(slow_path->GetExitLabel());
6652 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006653}
6654
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006655static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6656 switch (type_check_kind) {
6657 case TypeCheckKind::kExactCheck:
6658 case TypeCheckKind::kAbstractClassCheck:
6659 case TypeCheckKind::kClassHierarchyCheck:
6660 case TypeCheckKind::kArrayObjectCheck:
6661 return !throws_into_catch && !kEmitCompilerReadBarrier;
6662 case TypeCheckKind::kInterfaceCheck:
6663 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6664 case TypeCheckKind::kArrayCheck:
6665 case TypeCheckKind::kUnresolvedCheck:
6666 return false;
6667 }
6668 LOG(FATAL) << "Unreachable";
6669 UNREACHABLE();
6670}
6671
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006672void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006673 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006674 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006675 LocationSummary::CallKind call_kind =
6676 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6677 ? LocationSummary::kNoCall
6678 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006679 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6680 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006681 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6682 // Require a register for the interface check since there is a loop that compares the class to
6683 // a memory address.
6684 locations->SetInAt(1, Location::RequiresRegister());
6685 } else {
6686 locations->SetInAt(1, Location::Any());
6687 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006688 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6689 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006690 // When read barriers are enabled, we need an additional temporary register for some cases.
6691 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6692}
6693
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006694void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006695 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006696 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006697 Location obj_loc = locations->InAt(0);
6698 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006699 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006700 Location temp_loc = locations->GetTemp(0);
6701 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006702 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6703 DCHECK_GE(num_temps, 1u);
6704 DCHECK_LE(num_temps, 2u);
6705 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6706 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6707 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6708 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6709 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6710 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6711 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6712 const uint32_t object_array_data_offset =
6713 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006714
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006715 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6716 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6717 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006718 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006719 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6720
Roland Levillain0d5a2812015-11-13 10:07:31 +00006721 SlowPathCode* type_check_slow_path =
6722 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6723 is_type_check_slow_path_fatal);
6724 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006725
Roland Levillain0d5a2812015-11-13 10:07:31 +00006726 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006727 // Avoid null check if we know obj is not null.
6728 if (instruction->MustDoNullCheck()) {
6729 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006730 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006731 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006732
Roland Levillain0d5a2812015-11-13 10:07:31 +00006733 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006734 case TypeCheckKind::kExactCheck:
6735 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006736 // /* HeapReference<Class> */ temp = obj->klass_
6737 GenerateReferenceLoadTwoRegisters(instruction,
6738 temp_loc,
6739 obj_loc,
6740 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006741 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006742
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006743 if (cls.IsRegister()) {
6744 __ cmpl(temp, cls.AsRegister<Register>());
6745 } else {
6746 DCHECK(cls.IsStackSlot()) << cls;
6747 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6748 }
6749 // Jump to slow path for throwing the exception or doing a
6750 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006751 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006752 break;
6753 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006754
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006755 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006756 // /* HeapReference<Class> */ temp = obj->klass_
6757 GenerateReferenceLoadTwoRegisters(instruction,
6758 temp_loc,
6759 obj_loc,
6760 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006761 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006762
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006763 // If the class is abstract, we eagerly fetch the super class of the
6764 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006765 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006766 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006767 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006768 GenerateReferenceLoadOneRegister(instruction,
6769 temp_loc,
6770 super_offset,
6771 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006772 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006773
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006774 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6775 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006776 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006777 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006778
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006779 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006780 if (cls.IsRegister()) {
6781 __ cmpl(temp, cls.AsRegister<Register>());
6782 } else {
6783 DCHECK(cls.IsStackSlot()) << cls;
6784 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6785 }
6786 __ j(kNotEqual, &loop);
6787 break;
6788 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006789
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006790 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006791 // /* HeapReference<Class> */ temp = obj->klass_
6792 GenerateReferenceLoadTwoRegisters(instruction,
6793 temp_loc,
6794 obj_loc,
6795 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006796 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006797
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006798 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006799 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006800 __ Bind(&loop);
6801 if (cls.IsRegister()) {
6802 __ cmpl(temp, cls.AsRegister<Register>());
6803 } else {
6804 DCHECK(cls.IsStackSlot()) << cls;
6805 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6806 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006807 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006808
Roland Levillain0d5a2812015-11-13 10:07:31 +00006809 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006810 GenerateReferenceLoadOneRegister(instruction,
6811 temp_loc,
6812 super_offset,
6813 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006814 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006815
6816 // If the class reference currently in `temp` is not null, jump
6817 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006818 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006819 __ j(kNotZero, &loop);
6820 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006821 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006822 break;
6823 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006824
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006825 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006826 // /* HeapReference<Class> */ temp = obj->klass_
6827 GenerateReferenceLoadTwoRegisters(instruction,
6828 temp_loc,
6829 obj_loc,
6830 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006831 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006832
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006833 // Do an exact check.
6834 if (cls.IsRegister()) {
6835 __ cmpl(temp, cls.AsRegister<Register>());
6836 } else {
6837 DCHECK(cls.IsStackSlot()) << cls;
6838 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6839 }
6840 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006841
6842 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006843 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006844 GenerateReferenceLoadOneRegister(instruction,
6845 temp_loc,
6846 component_offset,
6847 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006848 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006849
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006850 // If the component type is null (i.e. the object not an array), jump to the slow path to
6851 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006852 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006853 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006854
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006855 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006856 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006857 break;
6858 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006859
Calin Juravle98893e12015-10-02 21:05:03 +01006860 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006861 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006862 // We cannot directly call the CheckCast runtime entry point
6863 // without resorting to a type checking slow path here (i.e. by
6864 // calling InvokeRuntime directly), as it would require to
6865 // assign fixed registers for the inputs of this HInstanceOf
6866 // instruction (following the runtime calling convention), which
6867 // might be cluttered by the potential first read barrier
6868 // emission at the beginning of this method.
6869 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006870 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006871
6872 case TypeCheckKind::kInterfaceCheck: {
6873 // Fast path for the interface check. Since we compare with a memory location in the inner
6874 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6875 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006876 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006877 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006878 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6879 // doing this.
6880 // /* HeapReference<Class> */ temp = obj->klass_
6881 GenerateReferenceLoadTwoRegisters(instruction,
6882 temp_loc,
6883 obj_loc,
6884 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006885 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006886
6887 // /* HeapReference<Class> */ temp = temp->iftable_
6888 GenerateReferenceLoadTwoRegisters(instruction,
6889 temp_loc,
6890 temp_loc,
6891 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006892 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006893 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006894 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006895 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006896 NearLabel start_loop;
6897 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006898 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006899 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006900 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6901 // Go to next interface if the classes do not match.
6902 __ cmpl(cls.AsRegister<Register>(),
6903 CodeGeneratorX86::ArrayAddress(temp,
6904 maybe_temp2_loc,
6905 TIMES_4,
6906 object_array_data_offset));
6907 __ j(kNotEqual, &start_loop);
6908 } else {
6909 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006910 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006911 break;
6912 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006913 }
6914 __ Bind(&done);
6915
Roland Levillain0d5a2812015-11-13 10:07:31 +00006916 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006917}
6918
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006919void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6920 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006921 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006922 InvokeRuntimeCallingConvention calling_convention;
6923 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6924}
6925
6926void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006927 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6928 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006929 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006930 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006931 if (instruction->IsEnter()) {
6932 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6933 } else {
6934 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6935 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006936}
6937
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006938void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6939void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6940void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6941
6942void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6943 LocationSummary* locations =
6944 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6945 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6946 || instruction->GetResultType() == Primitive::kPrimLong);
6947 locations->SetInAt(0, Location::RequiresRegister());
6948 locations->SetInAt(1, Location::Any());
6949 locations->SetOut(Location::SameAsFirstInput());
6950}
6951
6952void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6953 HandleBitwiseOperation(instruction);
6954}
6955
6956void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6957 HandleBitwiseOperation(instruction);
6958}
6959
6960void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6961 HandleBitwiseOperation(instruction);
6962}
6963
6964void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6965 LocationSummary* locations = instruction->GetLocations();
6966 Location first = locations->InAt(0);
6967 Location second = locations->InAt(1);
6968 DCHECK(first.Equals(locations->Out()));
6969
6970 if (instruction->GetResultType() == Primitive::kPrimInt) {
6971 if (second.IsRegister()) {
6972 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006973 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006974 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006975 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006976 } else {
6977 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006978 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006979 }
6980 } else if (second.IsConstant()) {
6981 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006982 __ andl(first.AsRegister<Register>(),
6983 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006984 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006985 __ orl(first.AsRegister<Register>(),
6986 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006987 } else {
6988 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006989 __ xorl(first.AsRegister<Register>(),
6990 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006991 }
6992 } else {
6993 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006994 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006995 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006996 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006997 } else {
6998 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006999 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007000 }
7001 }
7002 } else {
7003 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
7004 if (second.IsRegisterPair()) {
7005 if (instruction->IsAnd()) {
7006 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7007 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7008 } else if (instruction->IsOr()) {
7009 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7010 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7011 } else {
7012 DCHECK(instruction->IsXor());
7013 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7014 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7015 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007016 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007017 if (instruction->IsAnd()) {
7018 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7019 __ andl(first.AsRegisterPairHigh<Register>(),
7020 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7021 } else if (instruction->IsOr()) {
7022 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7023 __ orl(first.AsRegisterPairHigh<Register>(),
7024 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7025 } else {
7026 DCHECK(instruction->IsXor());
7027 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7028 __ xorl(first.AsRegisterPairHigh<Register>(),
7029 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7030 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007031 } else {
7032 DCHECK(second.IsConstant()) << second;
7033 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007034 int32_t low_value = Low32Bits(value);
7035 int32_t high_value = High32Bits(value);
7036 Immediate low(low_value);
7037 Immediate high(high_value);
7038 Register first_low = first.AsRegisterPairLow<Register>();
7039 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007040 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007041 if (low_value == 0) {
7042 __ xorl(first_low, first_low);
7043 } else if (low_value != -1) {
7044 __ andl(first_low, low);
7045 }
7046 if (high_value == 0) {
7047 __ xorl(first_high, first_high);
7048 } else if (high_value != -1) {
7049 __ andl(first_high, high);
7050 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007051 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007052 if (low_value != 0) {
7053 __ orl(first_low, low);
7054 }
7055 if (high_value != 0) {
7056 __ orl(first_high, high);
7057 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007058 } else {
7059 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007060 if (low_value != 0) {
7061 __ xorl(first_low, low);
7062 }
7063 if (high_value != 0) {
7064 __ xorl(first_high, high);
7065 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007066 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007067 }
7068 }
7069}
7070
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007071void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7072 HInstruction* instruction,
7073 Location out,
7074 uint32_t offset,
7075 Location maybe_temp,
7076 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007077 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007078 if (read_barrier_option == kWithReadBarrier) {
7079 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007080 if (kUseBakerReadBarrier) {
7081 // Load with fast path based Baker's read barrier.
7082 // /* HeapReference<Object> */ out = *(out + offset)
7083 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007084 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007085 } else {
7086 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007087 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007088 // in the following move operation, as we will need it for the
7089 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007090 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007091 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007092 // /* HeapReference<Object> */ out = *(out + offset)
7093 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007094 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007095 }
7096 } else {
7097 // Plain load with no read barrier.
7098 // /* HeapReference<Object> */ out = *(out + offset)
7099 __ movl(out_reg, Address(out_reg, offset));
7100 __ MaybeUnpoisonHeapReference(out_reg);
7101 }
7102}
7103
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007104void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7105 HInstruction* instruction,
7106 Location out,
7107 Location obj,
7108 uint32_t offset,
7109 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007110 Register out_reg = out.AsRegister<Register>();
7111 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007112 if (read_barrier_option == kWithReadBarrier) {
7113 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007114 if (kUseBakerReadBarrier) {
7115 // Load with fast path based Baker's read barrier.
7116 // /* HeapReference<Object> */ out = *(obj + offset)
7117 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007118 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007119 } else {
7120 // Load with slow path based read barrier.
7121 // /* HeapReference<Object> */ out = *(obj + offset)
7122 __ movl(out_reg, Address(obj_reg, offset));
7123 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7124 }
7125 } else {
7126 // Plain load with no read barrier.
7127 // /* HeapReference<Object> */ out = *(obj + offset)
7128 __ movl(out_reg, Address(obj_reg, offset));
7129 __ MaybeUnpoisonHeapReference(out_reg);
7130 }
7131}
7132
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007133void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7134 HInstruction* instruction,
7135 Location root,
7136 const Address& address,
7137 Label* fixup_label,
7138 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007139 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007140 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007141 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007142 if (kUseBakerReadBarrier) {
7143 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7144 // Baker's read barrier are used:
7145 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007146 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007147 // if (Thread::Current()->GetIsGcMarking()) {
7148 // root = ReadBarrier::Mark(root)
7149 // }
7150
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007151 // /* GcRoot<mirror::Object> */ root = *address
7152 __ movl(root_reg, address);
7153 if (fixup_label != nullptr) {
7154 __ Bind(fixup_label);
7155 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007156 static_assert(
7157 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7158 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7159 "have different sizes.");
7160 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7161 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7162 "have different sizes.");
7163
Vladimir Marko953437b2016-08-24 08:30:46 +00007164 // Slow path marking the GC root `root`.
7165 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007166 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007167 codegen_->AddSlowPath(slow_path);
7168
Andreas Gampe542451c2016-07-26 09:02:02 -07007169 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007170 Immediate(0));
7171 __ j(kNotEqual, slow_path->GetEntryLabel());
7172 __ Bind(slow_path->GetExitLabel());
7173 } else {
7174 // GC root loaded through a slow path for read barriers other
7175 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007176 // /* GcRoot<mirror::Object>* */ root = address
7177 __ leal(root_reg, address);
7178 if (fixup_label != nullptr) {
7179 __ Bind(fixup_label);
7180 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007181 // /* mirror::Object* */ root = root->Read()
7182 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7183 }
7184 } else {
7185 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007186 // /* GcRoot<mirror::Object> */ root = *address
7187 __ movl(root_reg, address);
7188 if (fixup_label != nullptr) {
7189 __ Bind(fixup_label);
7190 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007191 // Note that GC roots are not affected by heap poisoning, thus we
7192 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007193 }
7194}
7195
7196void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7197 Location ref,
7198 Register obj,
7199 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007200 bool needs_null_check) {
7201 DCHECK(kEmitCompilerReadBarrier);
7202 DCHECK(kUseBakerReadBarrier);
7203
7204 // /* HeapReference<Object> */ ref = *(obj + offset)
7205 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007206 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007207}
7208
7209void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7210 Location ref,
7211 Register obj,
7212 uint32_t data_offset,
7213 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007214 bool needs_null_check) {
7215 DCHECK(kEmitCompilerReadBarrier);
7216 DCHECK(kUseBakerReadBarrier);
7217
Roland Levillain3d312422016-06-23 13:53:42 +01007218 static_assert(
7219 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7220 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007221 // /* HeapReference<Object> */ ref =
7222 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007223 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007224 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007225}
7226
7227void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7228 Location ref,
7229 Register obj,
7230 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007231 bool needs_null_check,
7232 bool always_update_field,
7233 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007234 DCHECK(kEmitCompilerReadBarrier);
7235 DCHECK(kUseBakerReadBarrier);
7236
7237 // In slow path based read barriers, the read barrier call is
7238 // inserted after the original load. However, in fast path based
7239 // Baker's read barriers, we need to perform the load of
7240 // mirror::Object::monitor_ *before* the original reference load.
7241 // This load-load ordering is required by the read barrier.
7242 // The fast path/slow path (for Baker's algorithm) should look like:
7243 //
7244 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7245 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7246 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007247 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007248 // if (is_gray) {
7249 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7250 // }
7251 //
7252 // Note: the original implementation in ReadBarrier::Barrier is
7253 // slightly more complex as:
7254 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007255 // the high-bits of rb_state, which are expected to be all zeroes
7256 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7257 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007258 // - it performs additional checks that we do not do here for
7259 // performance reasons.
7260
7261 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007262 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7263
Vladimir Marko953437b2016-08-24 08:30:46 +00007264 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007265 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7266 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007267 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7268 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7269 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7270
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007271 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007272 // ref = ReadBarrier::Mark(ref);
7273 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7274 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007275 if (needs_null_check) {
7276 MaybeRecordImplicitNullCheck(instruction);
7277 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007278
7279 // Load fence to prevent load-load reordering.
7280 // Note that this is a no-op, thanks to the x86 memory model.
7281 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7282
7283 // The actual reference load.
7284 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007285 __ movl(ref_reg, src); // Flags are unaffected.
7286
7287 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7288 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007289 SlowPathCode* slow_path;
7290 if (always_update_field) {
7291 DCHECK(temp != nullptr);
7292 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7293 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7294 } else {
7295 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7296 instruction, ref, /* unpoison_ref_before_marking */ true);
7297 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007298 AddSlowPath(slow_path);
7299
7300 // We have done the "if" of the gray bit check above, now branch based on the flags.
7301 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007302
7303 // Object* ref = ref_addr->AsMirrorPtr()
7304 __ MaybeUnpoisonHeapReference(ref_reg);
7305
Roland Levillain7c1559a2015-12-15 10:55:36 +00007306 __ Bind(slow_path->GetExitLabel());
7307}
7308
7309void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7310 Location out,
7311 Location ref,
7312 Location obj,
7313 uint32_t offset,
7314 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007315 DCHECK(kEmitCompilerReadBarrier);
7316
Roland Levillain7c1559a2015-12-15 10:55:36 +00007317 // Insert a slow path based read barrier *after* the reference load.
7318 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007319 // If heap poisoning is enabled, the unpoisoning of the loaded
7320 // reference will be carried out by the runtime within the slow
7321 // path.
7322 //
7323 // Note that `ref` currently does not get unpoisoned (when heap
7324 // poisoning is enabled), which is alright as the `ref` argument is
7325 // not used by the artReadBarrierSlow entry point.
7326 //
7327 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7328 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7329 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7330 AddSlowPath(slow_path);
7331
Roland Levillain0d5a2812015-11-13 10:07:31 +00007332 __ jmp(slow_path->GetEntryLabel());
7333 __ Bind(slow_path->GetExitLabel());
7334}
7335
Roland Levillain7c1559a2015-12-15 10:55:36 +00007336void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7337 Location out,
7338 Location ref,
7339 Location obj,
7340 uint32_t offset,
7341 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007342 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007343 // Baker's read barriers shall be handled by the fast path
7344 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7345 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007346 // If heap poisoning is enabled, unpoisoning will be taken care of
7347 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007348 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007349 } else if (kPoisonHeapReferences) {
7350 __ UnpoisonHeapReference(out.AsRegister<Register>());
7351 }
7352}
7353
Roland Levillain7c1559a2015-12-15 10:55:36 +00007354void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7355 Location out,
7356 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007357 DCHECK(kEmitCompilerReadBarrier);
7358
Roland Levillain7c1559a2015-12-15 10:55:36 +00007359 // Insert a slow path based read barrier *after* the GC root load.
7360 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007361 // Note that GC roots are not affected by heap poisoning, so we do
7362 // not need to do anything special for this here.
7363 SlowPathCode* slow_path =
7364 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7365 AddSlowPath(slow_path);
7366
Roland Levillain0d5a2812015-11-13 10:07:31 +00007367 __ jmp(slow_path->GetEntryLabel());
7368 __ Bind(slow_path->GetExitLabel());
7369}
7370
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007371void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007372 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007373 LOG(FATAL) << "Unreachable";
7374}
7375
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007376void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007377 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007378 LOG(FATAL) << "Unreachable";
7379}
7380
Mark Mendellfe57faa2015-09-18 09:26:15 -04007381// Simple implementation of packed switch - generate cascaded compare/jumps.
7382void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7383 LocationSummary* locations =
7384 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7385 locations->SetInAt(0, Location::RequiresRegister());
7386}
7387
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007388void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7389 int32_t lower_bound,
7390 uint32_t num_entries,
7391 HBasicBlock* switch_block,
7392 HBasicBlock* default_block) {
7393 // Figure out the correct compare values and jump conditions.
7394 // Handle the first compare/branch as a special case because it might
7395 // jump to the default case.
7396 DCHECK_GT(num_entries, 2u);
7397 Condition first_condition;
7398 uint32_t index;
7399 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7400 if (lower_bound != 0) {
7401 first_condition = kLess;
7402 __ cmpl(value_reg, Immediate(lower_bound));
7403 __ j(first_condition, codegen_->GetLabelOf(default_block));
7404 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007405
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007406 index = 1;
7407 } else {
7408 // Handle all the compare/jumps below.
7409 first_condition = kBelow;
7410 index = 0;
7411 }
7412
7413 // Handle the rest of the compare/jumps.
7414 for (; index + 1 < num_entries; index += 2) {
7415 int32_t compare_to_value = lower_bound + index + 1;
7416 __ cmpl(value_reg, Immediate(compare_to_value));
7417 // Jump to successors[index] if value < case_value[index].
7418 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7419 // Jump to successors[index + 1] if value == case_value[index + 1].
7420 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7421 }
7422
7423 if (index != num_entries) {
7424 // There are an odd number of entries. Handle the last one.
7425 DCHECK_EQ(index + 1, num_entries);
7426 __ cmpl(value_reg, Immediate(lower_bound + index));
7427 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007428 }
7429
7430 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007431 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7432 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007433 }
7434}
7435
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007436void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7437 int32_t lower_bound = switch_instr->GetStartValue();
7438 uint32_t num_entries = switch_instr->GetNumEntries();
7439 LocationSummary* locations = switch_instr->GetLocations();
7440 Register value_reg = locations->InAt(0).AsRegister<Register>();
7441
7442 GenPackedSwitchWithCompares(value_reg,
7443 lower_bound,
7444 num_entries,
7445 switch_instr->GetBlock(),
7446 switch_instr->GetDefaultBlock());
7447}
7448
Mark Mendell805b3b52015-09-18 14:10:29 -04007449void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7450 LocationSummary* locations =
7451 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7452 locations->SetInAt(0, Location::RequiresRegister());
7453
7454 // Constant area pointer.
7455 locations->SetInAt(1, Location::RequiresRegister());
7456
7457 // And the temporary we need.
7458 locations->AddTemp(Location::RequiresRegister());
7459}
7460
7461void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7462 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007463 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007464 LocationSummary* locations = switch_instr->GetLocations();
7465 Register value_reg = locations->InAt(0).AsRegister<Register>();
7466 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7467
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007468 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7469 GenPackedSwitchWithCompares(value_reg,
7470 lower_bound,
7471 num_entries,
7472 switch_instr->GetBlock(),
7473 default_block);
7474 return;
7475 }
7476
Mark Mendell805b3b52015-09-18 14:10:29 -04007477 // Optimizing has a jump area.
7478 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7479 Register constant_area = locations->InAt(1).AsRegister<Register>();
7480
7481 // Remove the bias, if needed.
7482 if (lower_bound != 0) {
7483 __ leal(temp_reg, Address(value_reg, -lower_bound));
7484 value_reg = temp_reg;
7485 }
7486
7487 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007488 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007489 __ cmpl(value_reg, Immediate(num_entries - 1));
7490 __ j(kAbove, codegen_->GetLabelOf(default_block));
7491
7492 // We are in the range of the table.
7493 // Load (target-constant_area) from the jump table, indexing by the value.
7494 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7495
7496 // Compute the actual target address by adding in constant_area.
7497 __ addl(temp_reg, constant_area);
7498
7499 // And jump.
7500 __ jmp(temp_reg);
7501}
7502
Mark Mendell0616ae02015-04-17 12:49:27 -04007503void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7504 HX86ComputeBaseMethodAddress* insn) {
7505 LocationSummary* locations =
7506 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7507 locations->SetOut(Location::RequiresRegister());
7508}
7509
7510void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7511 HX86ComputeBaseMethodAddress* insn) {
7512 LocationSummary* locations = insn->GetLocations();
7513 Register reg = locations->Out().AsRegister<Register>();
7514
7515 // Generate call to next instruction.
7516 Label next_instruction;
7517 __ call(&next_instruction);
7518 __ Bind(&next_instruction);
7519
7520 // Remember this offset for later use with constant area.
7521 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7522
7523 // Grab the return address off the stack.
7524 __ popl(reg);
7525}
7526
7527void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7528 HX86LoadFromConstantTable* insn) {
7529 LocationSummary* locations =
7530 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7531
7532 locations->SetInAt(0, Location::RequiresRegister());
7533 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7534
7535 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007536 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007537 return;
7538 }
7539
7540 switch (insn->GetType()) {
7541 case Primitive::kPrimFloat:
7542 case Primitive::kPrimDouble:
7543 locations->SetOut(Location::RequiresFpuRegister());
7544 break;
7545
7546 case Primitive::kPrimInt:
7547 locations->SetOut(Location::RequiresRegister());
7548 break;
7549
7550 default:
7551 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7552 }
7553}
7554
7555void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007556 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007557 return;
7558 }
7559
7560 LocationSummary* locations = insn->GetLocations();
7561 Location out = locations->Out();
7562 Register const_area = locations->InAt(0).AsRegister<Register>();
7563 HConstant *value = insn->GetConstant();
7564
7565 switch (insn->GetType()) {
7566 case Primitive::kPrimFloat:
7567 __ movss(out.AsFpuRegister<XmmRegister>(),
7568 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7569 break;
7570
7571 case Primitive::kPrimDouble:
7572 __ movsd(out.AsFpuRegister<XmmRegister>(),
7573 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7574 break;
7575
7576 case Primitive::kPrimInt:
7577 __ movl(out.AsRegister<Register>(),
7578 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7579 break;
7580
7581 default:
7582 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7583 }
7584}
7585
Mark Mendell0616ae02015-04-17 12:49:27 -04007586/**
7587 * Class to handle late fixup of offsets into constant area.
7588 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007589class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007590 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007591 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7592 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7593
7594 protected:
7595 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7596
7597 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007598
7599 private:
7600 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7601 // Patch the correct offset for the instruction. The place to patch is the
7602 // last 4 bytes of the instruction.
7603 // The value to patch is the distance from the offset in the constant area
7604 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007605 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Mathieu Chartier6beced42016-11-15 15:51:31 -08007606 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();
Mark Mendell0616ae02015-04-17 12:49:27 -04007607
7608 // Patch in the right value.
7609 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7610 }
7611
Mark Mendell0616ae02015-04-17 12:49:27 -04007612 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007613 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007614};
7615
Mark Mendell805b3b52015-09-18 14:10:29 -04007616/**
7617 * Class to handle late fixup of offsets to a jump table that will be created in the
7618 * constant area.
7619 */
7620class JumpTableRIPFixup : public RIPFixup {
7621 public:
7622 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7623 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7624
7625 void CreateJumpTable() {
7626 X86Assembler* assembler = codegen_->GetAssembler();
7627
7628 // Ensure that the reference to the jump table has the correct offset.
7629 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7630 SetOffset(offset_in_constant_table);
7631
7632 // The label values in the jump table are computed relative to the
7633 // instruction addressing the constant area.
7634 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7635
7636 // Populate the jump table with the correct values for the jump table.
7637 int32_t num_entries = switch_instr_->GetNumEntries();
7638 HBasicBlock* block = switch_instr_->GetBlock();
7639 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7640 // The value that we want is the target offset - the position of the table.
7641 for (int32_t i = 0; i < num_entries; i++) {
7642 HBasicBlock* b = successors[i];
7643 Label* l = codegen_->GetLabelOf(b);
7644 DCHECK(l->IsBound());
7645 int32_t offset_to_block = l->Position() - relative_offset;
7646 assembler->AppendInt32(offset_to_block);
7647 }
7648 }
7649
7650 private:
7651 const HX86PackedSwitch* switch_instr_;
7652};
7653
7654void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7655 // Generate the constant area if needed.
7656 X86Assembler* assembler = GetAssembler();
7657 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7658 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7659 // byte values.
7660 assembler->Align(4, 0);
7661 constant_area_start_ = assembler->CodeSize();
7662
7663 // Populate any jump tables.
7664 for (auto jump_table : fixups_to_jump_tables_) {
7665 jump_table->CreateJumpTable();
7666 }
7667
7668 // And now add the constant area to the generated code.
7669 assembler->AddConstantArea();
7670 }
7671
7672 // And finish up.
7673 CodeGenerator::Finalize(allocator);
7674}
7675
Mark Mendell0616ae02015-04-17 12:49:27 -04007676Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7677 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7678 return Address(reg, kDummy32BitOffset, fixup);
7679}
7680
7681Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7682 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7683 return Address(reg, kDummy32BitOffset, fixup);
7684}
7685
7686Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7687 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7688 return Address(reg, kDummy32BitOffset, fixup);
7689}
7690
7691Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7692 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7693 return Address(reg, kDummy32BitOffset, fixup);
7694}
7695
Aart Bika19616e2016-02-01 18:57:58 -08007696void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7697 if (value == 0) {
7698 __ xorl(dest, dest);
7699 } else {
7700 __ movl(dest, Immediate(value));
7701 }
7702}
7703
7704void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7705 if (value == 0) {
7706 __ testl(dest, dest);
7707 } else {
7708 __ cmpl(dest, Immediate(value));
7709 }
7710}
7711
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007712void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7713 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007714 GenerateIntCompare(lhs_reg, rhs);
7715}
7716
7717void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007718 if (rhs.IsConstant()) {
7719 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007720 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007721 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007722 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007723 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007724 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007725 }
7726}
7727
7728Address CodeGeneratorX86::ArrayAddress(Register obj,
7729 Location index,
7730 ScaleFactor scale,
7731 uint32_t data_offset) {
7732 return index.IsConstant() ?
7733 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7734 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7735}
7736
Mark Mendell805b3b52015-09-18 14:10:29 -04007737Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7738 Register reg,
7739 Register value) {
7740 // Create a fixup to be used to create and address the jump table.
7741 JumpTableRIPFixup* table_fixup =
7742 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7743
7744 // We have to populate the jump tables.
7745 fixups_to_jump_tables_.push_back(table_fixup);
7746
7747 // We want a scaled address, as we are extracting the correct offset from the table.
7748 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7749}
7750
Andreas Gampe85b62f22015-09-09 13:15:38 -07007751// TODO: target as memory.
7752void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7753 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007754 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007755 return;
7756 }
7757
7758 DCHECK_NE(type, Primitive::kPrimVoid);
7759
7760 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7761 if (target.Equals(return_loc)) {
7762 return;
7763 }
7764
7765 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7766 // with the else branch.
7767 if (type == Primitive::kPrimLong) {
7768 HParallelMove parallel_move(GetGraph()->GetArena());
7769 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7770 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7771 GetMoveResolver()->EmitNativeCode(&parallel_move);
7772 } else {
7773 // Let the parallel move resolver take care of all of this.
7774 HParallelMove parallel_move(GetGraph()->GetArena());
7775 parallel_move.AddMove(return_loc, target, type, nullptr);
7776 GetMoveResolver()->EmitNativeCode(&parallel_move);
7777 }
7778}
7779
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007780void CodeGeneratorX86::PatchJitRootUse(uint8_t* code,
7781 const uint8_t* roots_data,
7782 const PatchInfo<Label>& info,
7783 uint64_t index_in_table) const {
7784 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7785 uintptr_t address =
7786 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7787 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7788 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7789 dchecked_integral_cast<uint32_t>(address);
7790}
7791
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007792void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7793 for (const PatchInfo<Label>& info : jit_string_patches_) {
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007794 const auto& it = jit_string_roots_.find(
7795 StringReference(&info.dex_file, dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007796 DCHECK(it != jit_string_roots_.end());
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007797 PatchJitRootUse(code, roots_data, info, it->second);
7798 }
7799
7800 for (const PatchInfo<Label>& info : jit_class_patches_) {
7801 const auto& it = jit_class_roots_.find(
7802 TypeReference(&info.dex_file, dex::TypeIndex(info.index)));
7803 DCHECK(it != jit_class_roots_.end());
7804 PatchJitRootUse(code, roots_data, info, it->second);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007805 }
7806}
7807
Roland Levillain4d027112015-07-01 15:41:14 +01007808#undef __
7809
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007810} // namespace x86
7811} // namespace art