blob: d6e92ccb815120bfd3161fb7ba96d4b5806979ae [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)),
Vladimir Marko93205e32016-04-13 11:59:46 +01001016 constant_area_start_(-1),
1017 fixups_to_jump_tables_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1018 method_address_offset_(-1) {
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001019 // Use a fake return address register to mimic Quick.
1020 AddAllocatedRegister(Location::RegisterLocation(kFakeReturnRegister));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001021}
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001022
David Brazdil58282f42016-01-14 12:45:10 +00001023void CodeGeneratorX86::SetupBlockedRegisters() const {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001024 // Stack register is always reserved.
Nicolas Geoffray71175b72014-10-09 22:13:55 +01001025 blocked_core_registers_[ESP] = true;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001026}
1027
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001028InstructionCodeGeneratorX86::InstructionCodeGeneratorX86(HGraph* graph, CodeGeneratorX86* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001029 : InstructionCodeGenerator(graph, codegen),
Nicolas Geoffray4a34a422014-04-03 10:38:37 +01001030 assembler_(codegen->GetAssembler()),
1031 codegen_(codegen) {}
1032
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001033static dwarf::Reg DWARFReg(Register reg) {
David Srbecky9d8606d2015-04-12 09:35:32 +01001034 return dwarf::Reg::X86Core(static_cast<int>(reg));
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001035}
1036
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001037void CodeGeneratorX86::GenerateFrameEntry() {
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001038 __ cfi().SetCurrentCFAOffset(kX86WordSize); // return address
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00001039 __ Bind(&frame_entry_label_);
Roland Levillain199f3362014-11-27 17:15:16 +00001040 bool skip_overflow_check =
1041 IsLeafMethod() && !FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kX86);
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001042 DCHECK(GetCompilerOptions().GetImplicitStackOverflowChecks());
Calin Juravle93edf732015-01-20 20:14:07 +00001043
Nicolas Geoffrayd97dc402015-01-22 13:50:01 +00001044 if (!skip_overflow_check) {
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001045 __ testl(EAX, Address(ESP, -static_cast<int32_t>(GetStackOverflowReservedBytes(kX86))));
Nicolas Geoffray39468442014-09-02 15:17:15 +01001046 RecordPcInfo(nullptr, 0);
Nicolas Geoffray397f2e42014-07-23 12:57:19 +01001047 }
1048
Mark Mendell5f874182015-03-04 15:42:45 -05001049 if (HasEmptyFrame()) {
1050 return;
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001051 }
Mark Mendell5f874182015-03-04 15:42:45 -05001052
1053 for (int i = arraysize(kCoreCalleeSaves) - 1; i >= 0; --i) {
1054 Register reg = kCoreCalleeSaves[i];
1055 if (allocated_registers_.ContainsCoreRegister(reg)) {
1056 __ pushl(reg);
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001057 __ cfi().AdjustCFAOffset(kX86WordSize);
1058 __ cfi().RelOffset(DWARFReg(reg), 0);
Mark Mendell5f874182015-03-04 15:42:45 -05001059 }
1060 }
1061
Mingyao Yang063fc772016-08-02 11:02:54 -07001062 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1063 // Initialize should_deoptimize flag to 0.
1064 __ movl(Address(ESP, -kShouldDeoptimizeFlagSize), Immediate(0));
1065 }
1066
David Srbeckyc6b4dd82015-04-07 20:32:43 +01001067 int adjust = GetFrameSize() - FrameEntrySpillSize();
1068 __ subl(ESP, Immediate(adjust));
1069 __ cfi().AdjustCFAOffset(adjust);
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001070 // Save the current method if we need it. Note that we do not
1071 // do this in HCurrentMethod, as the instruction might have been removed
1072 // in the SSA graph.
1073 if (RequiresCurrentMethod()) {
1074 __ movl(Address(ESP, kCurrentMethodStackOffset), kMethodRegisterArgument);
1075 }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001076}
1077
1078void CodeGeneratorX86::GenerateFrameExit() {
David Srbeckyc34dc932015-04-12 09:27:43 +01001079 __ cfi().RememberState();
1080 if (!HasEmptyFrame()) {
1081 int adjust = GetFrameSize() - FrameEntrySpillSize();
1082 __ addl(ESP, Immediate(adjust));
1083 __ cfi().AdjustCFAOffset(-adjust);
Mark Mendell5f874182015-03-04 15:42:45 -05001084
David Srbeckyc34dc932015-04-12 09:27:43 +01001085 for (size_t i = 0; i < arraysize(kCoreCalleeSaves); ++i) {
1086 Register reg = kCoreCalleeSaves[i];
1087 if (allocated_registers_.ContainsCoreRegister(reg)) {
1088 __ popl(reg);
1089 __ cfi().AdjustCFAOffset(-static_cast<int>(kX86WordSize));
1090 __ cfi().Restore(DWARFReg(reg));
1091 }
Mark Mendell5f874182015-03-04 15:42:45 -05001092 }
Nicolas Geoffrayc0572a42015-02-06 14:35:25 +00001093 }
David Srbeckyc34dc932015-04-12 09:27:43 +01001094 __ ret();
1095 __ cfi().RestoreState();
1096 __ cfi().DefCFAOffset(GetFrameSize());
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001097}
1098
Nicolas Geoffray92a73ae2014-10-16 11:12:52 +01001099void CodeGeneratorX86::Bind(HBasicBlock* block) {
1100 __ Bind(GetLabelOf(block));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001101}
1102
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001103Location InvokeDexCallingConventionVisitorX86::GetReturnLocation(Primitive::Type type) const {
1104 switch (type) {
1105 case Primitive::kPrimBoolean:
1106 case Primitive::kPrimByte:
1107 case Primitive::kPrimChar:
1108 case Primitive::kPrimShort:
1109 case Primitive::kPrimInt:
1110 case Primitive::kPrimNot:
1111 return Location::RegisterLocation(EAX);
1112
1113 case Primitive::kPrimLong:
1114 return Location::RegisterPairLocation(EAX, EDX);
1115
1116 case Primitive::kPrimVoid:
1117 return Location::NoLocation();
1118
1119 case Primitive::kPrimDouble:
1120 case Primitive::kPrimFloat:
1121 return Location::FpuRegisterLocation(XMM0);
1122 }
Nicolas Geoffray0d1652e2015-06-03 12:12:19 +01001123
1124 UNREACHABLE();
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01001125}
1126
1127Location InvokeDexCallingConventionVisitorX86::GetMethodLocation() const {
1128 return Location::RegisterLocation(kMethodRegisterArgument);
1129}
1130
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001131Location InvokeDexCallingConventionVisitorX86::GetNextLocation(Primitive::Type type) {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001132 switch (type) {
1133 case Primitive::kPrimBoolean:
1134 case Primitive::kPrimByte:
1135 case Primitive::kPrimChar:
1136 case Primitive::kPrimShort:
1137 case Primitive::kPrimInt:
1138 case Primitive::kPrimNot: {
1139 uint32_t index = gp_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001140 stack_index_++;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001141 if (index < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001142 return Location::RegisterLocation(calling_convention.GetRegisterAt(index));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001143 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001144 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001145 }
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001146 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001147
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001148 case Primitive::kPrimLong: {
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001149 uint32_t index = gp_index_;
1150 gp_index_ += 2;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001151 stack_index_ += 2;
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001152 if (index + 1 < calling_convention.GetNumberOfRegisters()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001153 X86ManagedRegister pair = X86ManagedRegister::FromRegisterPair(
1154 calling_convention.GetRegisterPairAt(index));
1155 return Location::RegisterPairLocation(pair.AsRegisterPairLow(), pair.AsRegisterPairHigh());
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001156 } else {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001157 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
1158 }
1159 }
1160
1161 case Primitive::kPrimFloat: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001162 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001163 stack_index_++;
1164 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1165 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1166 } else {
1167 return Location::StackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 1));
1168 }
1169 }
1170
1171 case Primitive::kPrimDouble: {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01001172 uint32_t index = float_index_++;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00001173 stack_index_ += 2;
1174 if (index < calling_convention.GetNumberOfFpuRegisters()) {
1175 return Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(index));
1176 } else {
1177 return Location::DoubleStackSlot(calling_convention.GetStackOffsetOf(stack_index_ - 2));
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001178 }
1179 }
1180
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001181 case Primitive::kPrimVoid:
1182 LOG(FATAL) << "Unexpected parameter type " << type;
1183 break;
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001184 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00001185 return Location::NoLocation();
Nicolas Geoffraya747a392014-04-17 14:56:23 +01001186}
Nicolas Geoffraydb928fc2014-04-16 17:38:32 +01001187
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001188void CodeGeneratorX86::Move32(Location destination, Location source) {
1189 if (source.Equals(destination)) {
1190 return;
1191 }
1192 if (destination.IsRegister()) {
1193 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001194 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001195 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001196 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001197 } else {
1198 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001199 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001200 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001201 } else if (destination.IsFpuRegister()) {
1202 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001203 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001204 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001205 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001206 } else {
1207 DCHECK(source.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00001208 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001209 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001210 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001211 DCHECK(destination.IsStackSlot()) << destination;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001212 if (source.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001213 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001214 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001215 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Mark Mendell7c8d0092015-01-26 11:21:33 -05001216 } else if (source.IsConstant()) {
1217 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001218 int32_t value = GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05001219 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001220 } else {
1221 DCHECK(source.IsStackSlot());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01001222 __ pushl(Address(ESP, source.GetStackIndex()));
1223 __ popl(Address(ESP, destination.GetStackIndex()));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001224 }
1225 }
1226}
1227
1228void CodeGeneratorX86::Move64(Location destination, Location source) {
1229 if (source.Equals(destination)) {
1230 return;
1231 }
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001232 if (destination.IsRegisterPair()) {
1233 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001234 EmitParallelMoves(
1235 Location::RegisterLocation(source.AsRegisterPairHigh<Register>()),
1236 Location::RegisterLocation(destination.AsRegisterPairHigh<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001237 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001238 Location::RegisterLocation(source.AsRegisterPairLow<Register>()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001239 Location::RegisterLocation(destination.AsRegisterPairLow<Register>()),
1240 Primitive::kPrimInt);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001241 } else if (source.IsFpuRegister()) {
Calin Juravlee460d1d2015-09-29 04:52:17 +01001242 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
1243 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
1244 __ psrlq(src_reg, Immediate(32));
1245 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001246 } else {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001247 // No conflict possible, so just do the moves.
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001248 DCHECK(source.IsDoubleStackSlot());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001249 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
1250 __ movl(destination.AsRegisterPairHigh<Register>(),
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001251 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
1252 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001253 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05001254 if (source.IsFpuRegister()) {
1255 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
1256 } else if (source.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001257 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Calin Juravlee460d1d2015-09-29 04:52:17 +01001258 } else if (source.IsRegisterPair()) {
1259 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
1260 // Create stack space for 2 elements.
1261 __ subl(ESP, Immediate(2 * elem_size));
1262 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
1263 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
1264 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
1265 // And remove the temporary stack space we allocated.
1266 __ addl(ESP, Immediate(2 * elem_size));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001267 } else {
1268 LOG(FATAL) << "Unimplemented";
1269 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001270 } else {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00001271 DCHECK(destination.IsDoubleStackSlot()) << destination;
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001272 if (source.IsRegisterPair()) {
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001273 // No conflict possible, so just do the moves.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001274 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegisterPairLow<Register>());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001275 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01001276 source.AsRegisterPairHigh<Register>());
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01001277 } else if (source.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00001278 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001279 } else if (source.IsConstant()) {
1280 HConstant* constant = source.GetConstant();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001281 DCHECK(constant->IsLongConstant() || constant->IsDoubleConstant());
1282 int64_t value = GetInt64ValueOf(constant);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001283 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(Low32Bits(value)));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001284 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)),
1285 Immediate(High32Bits(value)));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001286 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00001287 DCHECK(source.IsDoubleStackSlot()) << source;
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001288 EmitParallelMoves(
1289 Location::StackSlot(source.GetStackIndex()),
1290 Location::StackSlot(destination.GetStackIndex()),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001291 Primitive::kPrimInt,
Nicolas Geoffray32b2a522014-11-27 14:54:18 +00001292 Location::StackSlot(source.GetHighStackIndex(kX86WordSize)),
Nicolas Geoffray90218252015-04-15 11:56:51 +01001293 Location::StackSlot(destination.GetHighStackIndex(kX86WordSize)),
1294 Primitive::kPrimInt);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01001295 }
1296 }
1297}
1298
Calin Juravle175dc732015-08-25 15:42:32 +01001299void CodeGeneratorX86::MoveConstant(Location location, int32_t value) {
1300 DCHECK(location.IsRegister());
1301 __ movl(location.AsRegister<Register>(), Immediate(value));
1302}
1303
Calin Juravlee460d1d2015-09-29 04:52:17 +01001304void CodeGeneratorX86::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001305 HParallelMove move(GetGraph()->GetArena());
1306 if (dst_type == Primitive::kPrimLong && !src.IsConstant() && !src.IsFpuRegister()) {
1307 move.AddMove(src.ToLow(), dst.ToLow(), Primitive::kPrimInt, nullptr);
1308 move.AddMove(src.ToHigh(), dst.ToHigh(), Primitive::kPrimInt, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001309 } else {
David Brazdil74eb1b22015-12-14 11:44:01 +00001310 move.AddMove(src, dst, dst_type, nullptr);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001311 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001312 GetMoveResolver()->EmitNativeCode(&move);
Calin Juravlee460d1d2015-09-29 04:52:17 +01001313}
1314
1315void CodeGeneratorX86::AddLocationAsTemp(Location location, LocationSummary* locations) {
1316 if (location.IsRegister()) {
1317 locations->AddTemp(location);
1318 } else if (location.IsRegisterPair()) {
1319 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1320 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
1321 } else {
1322 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1323 }
1324}
1325
David Brazdilfc6a86a2015-06-26 10:33:45 +00001326void InstructionCodeGeneratorX86::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001327 DCHECK(!successor->IsExitBlock());
1328
1329 HBasicBlock* block = got->GetBlock();
1330 HInstruction* previous = got->GetPrevious();
1331
1332 HLoopInformation* info = block->GetLoopInformation();
David Brazdil46e2a392015-03-16 17:31:52 +00001333 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01001334 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
1335 return;
1336 }
1337
1338 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
1339 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
1340 }
1341 if (!codegen_->GoesToNextBlock(got->GetBlock(), successor)) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001342 __ jmp(codegen_->GetLabelOf(successor));
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001343 }
1344}
1345
David Brazdilfc6a86a2015-06-26 10:33:45 +00001346void LocationsBuilderX86::VisitGoto(HGoto* got) {
1347 got->SetLocations(nullptr);
1348}
1349
1350void InstructionCodeGeneratorX86::VisitGoto(HGoto* got) {
1351 HandleGoto(got, got->GetSuccessor());
1352}
1353
1354void LocationsBuilderX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1355 try_boundary->SetLocations(nullptr);
1356}
1357
1358void InstructionCodeGeneratorX86::VisitTryBoundary(HTryBoundary* try_boundary) {
1359 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
1360 if (!successor->IsExitBlock()) {
1361 HandleGoto(try_boundary, successor);
1362 }
1363}
1364
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001365void LocationsBuilderX86::VisitExit(HExit* exit) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00001366 exit->SetLocations(nullptr);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001367}
1368
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001369void InstructionCodeGeneratorX86::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001370}
1371
Mark Mendell152408f2015-12-31 12:28:50 -05001372template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001373void InstructionCodeGeneratorX86::GenerateFPJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001374 LabelType* true_label,
1375 LabelType* false_label) {
Roland Levillain4fa13f62015-07-06 18:11:54 +01001376 if (cond->IsFPConditionTrueIfNaN()) {
1377 __ j(kUnordered, true_label);
1378 } else if (cond->IsFPConditionFalseIfNaN()) {
1379 __ j(kUnordered, false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001380 }
Roland Levillain4fa13f62015-07-06 18:11:54 +01001381 __ j(X86UnsignedOrFPCondition(cond->GetCondition()), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001382}
1383
Mark Mendell152408f2015-12-31 12:28:50 -05001384template<class LabelType>
Mark Mendellc4701932015-04-10 13:18:51 -04001385void InstructionCodeGeneratorX86::GenerateLongComparesAndJumps(HCondition* cond,
Mark Mendell152408f2015-12-31 12:28:50 -05001386 LabelType* true_label,
1387 LabelType* false_label) {
Mark Mendellc4701932015-04-10 13:18:51 -04001388 LocationSummary* locations = cond->GetLocations();
1389 Location left = locations->InAt(0);
1390 Location right = locations->InAt(1);
1391 IfCondition if_cond = cond->GetCondition();
1392
Mark Mendellc4701932015-04-10 13:18:51 -04001393 Register left_high = left.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001394 Register left_low = left.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001395 IfCondition true_high_cond = if_cond;
1396 IfCondition false_high_cond = cond->GetOppositeCondition();
Aart Bike9f37602015-10-09 11:15:55 -07001397 Condition final_condition = X86UnsignedOrFPCondition(if_cond); // unsigned on lower part
Mark Mendellc4701932015-04-10 13:18:51 -04001398
1399 // Set the conditions for the test, remembering that == needs to be
1400 // decided using the low words.
1401 switch (if_cond) {
1402 case kCondEQ:
Mark Mendellc4701932015-04-10 13:18:51 -04001403 case kCondNE:
Roland Levillain4fa13f62015-07-06 18:11:54 +01001404 // Nothing to do.
Mark Mendellc4701932015-04-10 13:18:51 -04001405 break;
1406 case kCondLT:
1407 false_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001408 break;
1409 case kCondLE:
1410 true_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001411 break;
1412 case kCondGT:
1413 false_high_cond = kCondLT;
Mark Mendellc4701932015-04-10 13:18:51 -04001414 break;
1415 case kCondGE:
1416 true_high_cond = kCondGT;
Mark Mendellc4701932015-04-10 13:18:51 -04001417 break;
Aart Bike9f37602015-10-09 11:15:55 -07001418 case kCondB:
1419 false_high_cond = kCondA;
1420 break;
1421 case kCondBE:
1422 true_high_cond = kCondB;
1423 break;
1424 case kCondA:
1425 false_high_cond = kCondB;
1426 break;
1427 case kCondAE:
1428 true_high_cond = kCondA;
1429 break;
Mark Mendellc4701932015-04-10 13:18:51 -04001430 }
1431
1432 if (right.IsConstant()) {
1433 int64_t value = right.GetConstant()->AsLongConstant()->GetValue();
Mark Mendellc4701932015-04-10 13:18:51 -04001434 int32_t val_high = High32Bits(value);
Roland Levillain4fa13f62015-07-06 18:11:54 +01001435 int32_t val_low = Low32Bits(value);
Mark Mendellc4701932015-04-10 13:18:51 -04001436
Aart Bika19616e2016-02-01 18:57:58 -08001437 codegen_->Compare32BitValue(left_high, val_high);
Mark Mendellc4701932015-04-10 13:18:51 -04001438 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001439 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001440 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001441 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001442 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001443 __ j(X86Condition(true_high_cond), true_label);
1444 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001445 }
1446 // Must be equal high, so compare the lows.
Aart Bika19616e2016-02-01 18:57:58 -08001447 codegen_->Compare32BitValue(left_low, val_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001448 } else if (right.IsRegisterPair()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001449 Register right_high = right.AsRegisterPairHigh<Register>();
Roland Levillain4fa13f62015-07-06 18:11:54 +01001450 Register right_low = right.AsRegisterPairLow<Register>();
Mark Mendellc4701932015-04-10 13:18:51 -04001451
1452 __ cmpl(left_high, right_high);
1453 if (if_cond == kCondNE) {
Aart Bike9f37602015-10-09 11:15:55 -07001454 __ j(X86Condition(true_high_cond), true_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001455 } else if (if_cond == kCondEQ) {
Aart Bike9f37602015-10-09 11:15:55 -07001456 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001457 } else {
Aart Bike9f37602015-10-09 11:15:55 -07001458 __ j(X86Condition(true_high_cond), true_label);
1459 __ j(X86Condition(false_high_cond), false_label);
Mark Mendellc4701932015-04-10 13:18:51 -04001460 }
1461 // Must be equal high, so compare the lows.
1462 __ cmpl(left_low, right_low);
Mark Mendell8659e842016-02-16 10:41:46 -05001463 } else {
1464 DCHECK(right.IsDoubleStackSlot());
1465 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
1466 if (if_cond == kCondNE) {
1467 __ j(X86Condition(true_high_cond), true_label);
1468 } else if (if_cond == kCondEQ) {
1469 __ j(X86Condition(false_high_cond), false_label);
1470 } else {
1471 __ j(X86Condition(true_high_cond), true_label);
1472 __ j(X86Condition(false_high_cond), false_label);
1473 }
1474 // Must be equal high, so compare the lows.
1475 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Mark Mendellc4701932015-04-10 13:18:51 -04001476 }
1477 // The last comparison might be unsigned.
1478 __ j(final_condition, true_label);
1479}
1480
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001481void InstructionCodeGeneratorX86::GenerateFPCompare(Location lhs,
1482 Location rhs,
1483 HInstruction* insn,
1484 bool is_double) {
1485 HX86LoadFromConstantTable* const_area = insn->InputAt(1)->AsX86LoadFromConstantTable();
1486 if (is_double) {
1487 if (rhs.IsFpuRegister()) {
1488 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1489 } else if (const_area != nullptr) {
1490 DCHECK(const_area->IsEmittedAtUseSite());
1491 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(),
1492 codegen_->LiteralDoubleAddress(
1493 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
1494 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1495 } else {
1496 DCHECK(rhs.IsDoubleStackSlot());
1497 __ ucomisd(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1498 }
1499 } else {
1500 if (rhs.IsFpuRegister()) {
1501 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), rhs.AsFpuRegister<XmmRegister>());
1502 } else if (const_area != nullptr) {
1503 DCHECK(const_area->IsEmittedAtUseSite());
1504 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(),
1505 codegen_->LiteralFloatAddress(
1506 const_area->GetConstant()->AsFloatConstant()->GetValue(),
1507 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
1508 } else {
1509 DCHECK(rhs.IsStackSlot());
1510 __ ucomiss(lhs.AsFpuRegister<XmmRegister>(), Address(ESP, rhs.GetStackIndex()));
1511 }
1512 }
1513}
1514
Mark Mendell152408f2015-12-31 12:28:50 -05001515template<class LabelType>
David Brazdil0debae72015-11-12 18:37:00 +00001516void InstructionCodeGeneratorX86::GenerateCompareTestAndBranch(HCondition* condition,
Mark Mendell152408f2015-12-31 12:28:50 -05001517 LabelType* true_target_in,
1518 LabelType* false_target_in) {
David Brazdil0debae72015-11-12 18:37:00 +00001519 // Generated branching requires both targets to be explicit. If either of the
1520 // targets is nullptr (fallthrough) use and bind `fallthrough_target` instead.
Mark Mendell152408f2015-12-31 12:28:50 -05001521 LabelType fallthrough_target;
1522 LabelType* true_target = true_target_in == nullptr ? &fallthrough_target : true_target_in;
1523 LabelType* false_target = false_target_in == nullptr ? &fallthrough_target : false_target_in;
David Brazdil0debae72015-11-12 18:37:00 +00001524
Mark Mendellc4701932015-04-10 13:18:51 -04001525 LocationSummary* locations = condition->GetLocations();
1526 Location left = locations->InAt(0);
1527 Location right = locations->InAt(1);
1528
Mark Mendellc4701932015-04-10 13:18:51 -04001529 Primitive::Type type = condition->InputAt(0)->GetType();
1530 switch (type) {
1531 case Primitive::kPrimLong:
1532 GenerateLongComparesAndJumps(condition, true_target, false_target);
1533 break;
1534 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001535 GenerateFPCompare(left, right, condition, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001536 GenerateFPJumps(condition, true_target, false_target);
1537 break;
1538 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001539 GenerateFPCompare(left, right, condition, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001540 GenerateFPJumps(condition, true_target, false_target);
1541 break;
1542 default:
1543 LOG(FATAL) << "Unexpected compare type " << type;
1544 }
1545
David Brazdil0debae72015-11-12 18:37:00 +00001546 if (false_target != &fallthrough_target) {
Mark Mendellc4701932015-04-10 13:18:51 -04001547 __ jmp(false_target);
1548 }
David Brazdil0debae72015-11-12 18:37:00 +00001549
1550 if (fallthrough_target.IsLinked()) {
1551 __ Bind(&fallthrough_target);
1552 }
Mark Mendellc4701932015-04-10 13:18:51 -04001553}
1554
David Brazdil0debae72015-11-12 18:37:00 +00001555static bool AreEflagsSetFrom(HInstruction* cond, HInstruction* branch) {
1556 // Moves may affect the eflags register (move zero uses xorl), so the EFLAGS
1557 // are set only strictly before `branch`. We can't use the eflags on long/FP
1558 // conditions if they are materialized due to the complex branching.
1559 return cond->IsCondition() &&
1560 cond->GetNext() == branch &&
1561 cond->InputAt(0)->GetType() != Primitive::kPrimLong &&
1562 !Primitive::IsFloatingPointType(cond->InputAt(0)->GetType());
1563}
1564
Mark Mendell152408f2015-12-31 12:28:50 -05001565template<class LabelType>
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001566void InstructionCodeGeneratorX86::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00001567 size_t condition_input_index,
Mark Mendell152408f2015-12-31 12:28:50 -05001568 LabelType* true_target,
1569 LabelType* false_target) {
David Brazdil0debae72015-11-12 18:37:00 +00001570 HInstruction* cond = instruction->InputAt(condition_input_index);
1571
1572 if (true_target == nullptr && false_target == nullptr) {
1573 // Nothing to do. The code always falls through.
1574 return;
1575 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00001576 // Constant condition, statically compared against "true" (integer value 1).
1577 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00001578 if (true_target != nullptr) {
1579 __ jmp(true_target);
Nicolas Geoffray18efde52014-09-22 15:51:11 +01001580 }
Nicolas Geoffray360231a2014-10-08 21:07:48 +01001581 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00001582 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00001583 if (false_target != nullptr) {
1584 __ jmp(false_target);
1585 }
1586 }
1587 return;
1588 }
1589
1590 // The following code generates these patterns:
1591 // (1) true_target == nullptr && false_target != nullptr
1592 // - opposite condition true => branch to false_target
1593 // (2) true_target != nullptr && false_target == nullptr
1594 // - condition true => branch to true_target
1595 // (3) true_target != nullptr && false_target != nullptr
1596 // - condition true => branch to true_target
1597 // - branch to false_target
1598 if (IsBooleanValueOrMaterializedCondition(cond)) {
1599 if (AreEflagsSetFrom(cond, instruction)) {
1600 if (true_target == nullptr) {
1601 __ j(X86Condition(cond->AsCondition()->GetOppositeCondition()), false_target);
1602 } else {
1603 __ j(X86Condition(cond->AsCondition()->GetCondition()), true_target);
1604 }
1605 } else {
1606 // Materialized condition, compare against 0.
1607 Location lhs = instruction->GetLocations()->InAt(condition_input_index);
1608 if (lhs.IsRegister()) {
1609 __ testl(lhs.AsRegister<Register>(), lhs.AsRegister<Register>());
1610 } else {
1611 __ cmpl(Address(ESP, lhs.GetStackIndex()), Immediate(0));
1612 }
1613 if (true_target == nullptr) {
1614 __ j(kEqual, false_target);
1615 } else {
1616 __ j(kNotEqual, true_target);
1617 }
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001618 }
1619 } else {
David Brazdil0debae72015-11-12 18:37:00 +00001620 // Condition has not been materialized, use its inputs as the comparison and
1621 // its condition as the branch condition.
Mark Mendellb8b97692015-05-22 16:58:19 -04001622 HCondition* condition = cond->AsCondition();
David Brazdil0debae72015-11-12 18:37:00 +00001623
1624 // If this is a long or FP comparison that has been folded into
1625 // the HCondition, generate the comparison directly.
1626 Primitive::Type type = condition->InputAt(0)->GetType();
1627 if (type == Primitive::kPrimLong || Primitive::IsFloatingPointType(type)) {
1628 GenerateCompareTestAndBranch(condition, true_target, false_target);
1629 return;
1630 }
1631
1632 Location lhs = condition->GetLocations()->InAt(0);
1633 Location rhs = condition->GetLocations()->InAt(1);
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001634 // LHS is guaranteed to be in a register (see LocationsBuilderX86::HandleCondition).
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01001635 codegen_->GenerateIntCompare(lhs, rhs);
David Brazdil0debae72015-11-12 18:37:00 +00001636 if (true_target == nullptr) {
1637 __ j(X86Condition(condition->GetOppositeCondition()), false_target);
1638 } else {
Mark Mendellb8b97692015-05-22 16:58:19 -04001639 __ j(X86Condition(condition->GetCondition()), true_target);
Dave Allison20dfc792014-06-16 20:44:29 -07001640 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001641 }
David Brazdil0debae72015-11-12 18:37:00 +00001642
1643 // If neither branch falls through (case 3), the conditional branch to `true_target`
1644 // was already emitted (case 2) and we need to emit a jump to `false_target`.
1645 if (true_target != nullptr && false_target != nullptr) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001646 __ jmp(false_target);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001647 }
1648}
1649
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001650void LocationsBuilderX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001651 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
1652 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001653 locations->SetInAt(0, Location::Any());
1654 }
1655}
1656
1657void InstructionCodeGeneratorX86::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00001658 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
1659 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
1660 Label* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
1661 nullptr : codegen_->GetLabelOf(true_successor);
1662 Label* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
1663 nullptr : codegen_->GetLabelOf(false_successor);
1664 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001665}
1666
1667void LocationsBuilderX86::VisitDeoptimize(HDeoptimize* deoptimize) {
1668 LocationSummary* locations = new (GetGraph()->GetArena())
1669 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01001670 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
David Brazdil0debae72015-11-12 18:37:00 +00001671 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001672 locations->SetInAt(0, Location::Any());
1673 }
1674}
1675
1676void InstructionCodeGeneratorX86::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08001677 SlowPathCode* slow_path = deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathX86>(deoptimize);
David Brazdil74eb1b22015-12-14 11:44:01 +00001678 GenerateTestAndBranch<Label>(deoptimize,
1679 /* condition_input_index */ 0,
1680 slow_path->GetEntryLabel(),
1681 /* false_target */ nullptr);
1682}
1683
Mingyao Yang063fc772016-08-02 11:02:54 -07001684void LocationsBuilderX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1685 LocationSummary* locations = new (GetGraph()->GetArena())
1686 LocationSummary(flag, LocationSummary::kNoCall);
1687 locations->SetOut(Location::RequiresRegister());
1688}
1689
1690void InstructionCodeGeneratorX86::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
1691 __ movl(flag->GetLocations()->Out().AsRegister<Register>(),
1692 Address(ESP, codegen_->GetStackOffsetOfShouldDeoptimizeFlag()));
1693}
1694
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001695static bool SelectCanUseCMOV(HSelect* select) {
1696 // There are no conditional move instructions for XMMs.
1697 if (Primitive::IsFloatingPointType(select->GetType())) {
1698 return false;
1699 }
1700
1701 // A FP condition doesn't generate the single CC that we need.
1702 // In 32 bit mode, a long condition doesn't generate a single CC either.
1703 HInstruction* condition = select->GetCondition();
1704 if (condition->IsCondition()) {
1705 Primitive::Type compare_type = condition->InputAt(0)->GetType();
1706 if (compare_type == Primitive::kPrimLong ||
1707 Primitive::IsFloatingPointType(compare_type)) {
1708 return false;
1709 }
1710 }
1711
1712 // We can generate a CMOV for this Select.
1713 return true;
1714}
1715
David Brazdil74eb1b22015-12-14 11:44:01 +00001716void LocationsBuilderX86::VisitSelect(HSelect* select) {
1717 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001718 if (Primitive::IsFloatingPointType(select->GetType())) {
David Brazdil74eb1b22015-12-14 11:44:01 +00001719 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001720 locations->SetInAt(1, Location::Any());
David Brazdil74eb1b22015-12-14 11:44:01 +00001721 } else {
1722 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001723 if (SelectCanUseCMOV(select)) {
1724 if (select->InputAt(1)->IsConstant()) {
1725 // Cmov can't handle a constant value.
1726 locations->SetInAt(1, Location::RequiresRegister());
1727 } else {
1728 locations->SetInAt(1, Location::Any());
1729 }
1730 } else {
1731 locations->SetInAt(1, Location::Any());
1732 }
David Brazdil74eb1b22015-12-14 11:44:01 +00001733 }
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001734 if (IsBooleanValueOrMaterializedCondition(select->GetCondition())) {
1735 locations->SetInAt(2, Location::RequiresRegister());
David Brazdil74eb1b22015-12-14 11:44:01 +00001736 }
1737 locations->SetOut(Location::SameAsFirstInput());
1738}
1739
1740void InstructionCodeGeneratorX86::VisitSelect(HSelect* select) {
1741 LocationSummary* locations = select->GetLocations();
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001742 DCHECK(locations->InAt(0).Equals(locations->Out()));
1743 if (SelectCanUseCMOV(select)) {
1744 // If both the condition and the source types are integer, we can generate
1745 // a CMOV to implement Select.
1746
1747 HInstruction* select_condition = select->GetCondition();
1748 Condition cond = kNotEqual;
1749
1750 // Figure out how to test the 'condition'.
1751 if (select_condition->IsCondition()) {
1752 HCondition* condition = select_condition->AsCondition();
1753 if (!condition->IsEmittedAtUseSite()) {
1754 // This was a previously materialized condition.
1755 // Can we use the existing condition code?
1756 if (AreEflagsSetFrom(condition, select)) {
1757 // Materialization was the previous instruction. Condition codes are right.
1758 cond = X86Condition(condition->GetCondition());
1759 } else {
1760 // No, we have to recreate the condition code.
1761 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1762 __ testl(cond_reg, cond_reg);
1763 }
1764 } else {
1765 // We can't handle FP or long here.
1766 DCHECK_NE(condition->InputAt(0)->GetType(), Primitive::kPrimLong);
1767 DCHECK(!Primitive::IsFloatingPointType(condition->InputAt(0)->GetType()));
1768 LocationSummary* cond_locations = condition->GetLocations();
Roland Levillain0b671c02016-08-19 12:02:34 +01001769 codegen_->GenerateIntCompare(cond_locations->InAt(0), cond_locations->InAt(1));
Mark Mendell0c5b18e2016-02-06 13:58:35 -05001770 cond = X86Condition(condition->GetCondition());
1771 }
1772 } else {
1773 // Must be a boolean condition, which needs to be compared to 0.
1774 Register cond_reg = locations->InAt(2).AsRegister<Register>();
1775 __ testl(cond_reg, cond_reg);
1776 }
1777
1778 // If the condition is true, overwrite the output, which already contains false.
1779 Location false_loc = locations->InAt(0);
1780 Location true_loc = locations->InAt(1);
1781 if (select->GetType() == Primitive::kPrimLong) {
1782 // 64 bit conditional move.
1783 Register false_high = false_loc.AsRegisterPairHigh<Register>();
1784 Register false_low = false_loc.AsRegisterPairLow<Register>();
1785 if (true_loc.IsRegisterPair()) {
1786 __ cmovl(cond, false_high, true_loc.AsRegisterPairHigh<Register>());
1787 __ cmovl(cond, false_low, true_loc.AsRegisterPairLow<Register>());
1788 } else {
1789 __ cmovl(cond, false_high, Address(ESP, true_loc.GetHighStackIndex(kX86WordSize)));
1790 __ cmovl(cond, false_low, Address(ESP, true_loc.GetStackIndex()));
1791 }
1792 } else {
1793 // 32 bit conditional move.
1794 Register false_reg = false_loc.AsRegister<Register>();
1795 if (true_loc.IsRegister()) {
1796 __ cmovl(cond, false_reg, true_loc.AsRegister<Register>());
1797 } else {
1798 __ cmovl(cond, false_reg, Address(ESP, true_loc.GetStackIndex()));
1799 }
1800 }
1801 } else {
1802 NearLabel false_target;
1803 GenerateTestAndBranch<NearLabel>(
1804 select, /* condition_input_index */ 2, /* true_target */ nullptr, &false_target);
1805 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
1806 __ Bind(&false_target);
1807 }
Mingyao Yangd43b3ac2015-04-01 14:03:04 -07001808}
1809
David Srbecky0cf44932015-12-09 14:09:59 +00001810void LocationsBuilderX86::VisitNativeDebugInfo(HNativeDebugInfo* info) {
1811 new (GetGraph()->GetArena()) LocationSummary(info);
1812}
1813
David Srbeckyd28f4a02016-03-14 17:14:24 +00001814void InstructionCodeGeneratorX86::VisitNativeDebugInfo(HNativeDebugInfo*) {
1815 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00001816}
1817
1818void CodeGeneratorX86::GenerateNop() {
1819 __ nop();
David Srbecky0cf44932015-12-09 14:09:59 +00001820}
1821
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001822void LocationsBuilderX86::HandleCondition(HCondition* cond) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001823 LocationSummary* locations =
Roland Levillain0d37cd02015-05-27 16:39:19 +01001824 new (GetGraph()->GetArena()) LocationSummary(cond, LocationSummary::kNoCall);
Mark Mendellc4701932015-04-10 13:18:51 -04001825 // Handle the long/FP comparisons made in instruction simplification.
1826 switch (cond->InputAt(0)->GetType()) {
1827 case Primitive::kPrimLong: {
1828 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendell8659e842016-02-16 10:41:46 -05001829 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001830 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001831 locations->SetOut(Location::RequiresRegister());
1832 }
1833 break;
1834 }
1835 case Primitive::kPrimFloat:
1836 case Primitive::kPrimDouble: {
1837 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001838 if (cond->InputAt(1)->IsX86LoadFromConstantTable()) {
1839 DCHECK(cond->InputAt(1)->IsEmittedAtUseSite());
1840 } else if (cond->InputAt(1)->IsConstant()) {
1841 locations->SetInAt(1, Location::RequiresFpuRegister());
1842 } else {
1843 locations->SetInAt(1, Location::Any());
1844 }
David Brazdilb3e773e2016-01-26 11:28:37 +00001845 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001846 locations->SetOut(Location::RequiresRegister());
1847 }
1848 break;
1849 }
1850 default:
1851 locations->SetInAt(0, Location::RequiresRegister());
1852 locations->SetInAt(1, Location::Any());
David Brazdilb3e773e2016-01-26 11:28:37 +00001853 if (!cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001854 // We need a byte register.
1855 locations->SetOut(Location::RegisterLocation(ECX));
1856 }
1857 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01001858 }
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001859}
1860
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001861void InstructionCodeGeneratorX86::HandleCondition(HCondition* cond) {
David Brazdilb3e773e2016-01-26 11:28:37 +00001862 if (cond->IsEmittedAtUseSite()) {
Mark Mendellc4701932015-04-10 13:18:51 -04001863 return;
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01001864 }
Mark Mendellc4701932015-04-10 13:18:51 -04001865
1866 LocationSummary* locations = cond->GetLocations();
1867 Location lhs = locations->InAt(0);
1868 Location rhs = locations->InAt(1);
1869 Register reg = locations->Out().AsRegister<Register>();
Mark Mendell152408f2015-12-31 12:28:50 -05001870 NearLabel true_label, false_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001871
1872 switch (cond->InputAt(0)->GetType()) {
1873 default: {
1874 // Integer case.
1875
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01001876 // Clear output register: setb only sets the low byte.
Mark Mendellc4701932015-04-10 13:18:51 -04001877 __ xorl(reg, reg);
Roland Levillain0b671c02016-08-19 12:02:34 +01001878 codegen_->GenerateIntCompare(lhs, rhs);
Aart Bike9f37602015-10-09 11:15:55 -07001879 __ setb(X86Condition(cond->GetCondition()), reg);
Mark Mendellc4701932015-04-10 13:18:51 -04001880 return;
1881 }
1882 case Primitive::kPrimLong:
1883 GenerateLongComparesAndJumps(cond, &true_label, &false_label);
1884 break;
1885 case Primitive::kPrimFloat:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001886 GenerateFPCompare(lhs, rhs, cond, false);
Mark Mendellc4701932015-04-10 13:18:51 -04001887 GenerateFPJumps(cond, &true_label, &false_label);
1888 break;
1889 case Primitive::kPrimDouble:
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00001890 GenerateFPCompare(lhs, rhs, cond, true);
Mark Mendellc4701932015-04-10 13:18:51 -04001891 GenerateFPJumps(cond, &true_label, &false_label);
1892 break;
1893 }
1894
1895 // Convert the jumps into the result.
Mark Mendell0c9497d2015-08-21 09:30:05 -04001896 NearLabel done_label;
Mark Mendellc4701932015-04-10 13:18:51 -04001897
Roland Levillain4fa13f62015-07-06 18:11:54 +01001898 // False case: result = 0.
Mark Mendellc4701932015-04-10 13:18:51 -04001899 __ Bind(&false_label);
1900 __ xorl(reg, reg);
1901 __ jmp(&done_label);
1902
Roland Levillain4fa13f62015-07-06 18:11:54 +01001903 // True case: result = 1.
Mark Mendellc4701932015-04-10 13:18:51 -04001904 __ Bind(&true_label);
1905 __ movl(reg, Immediate(1));
1906 __ Bind(&done_label);
Dave Allison20dfc792014-06-16 20:44:29 -07001907}
1908
1909void LocationsBuilderX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001910 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001911}
1912
1913void InstructionCodeGeneratorX86::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001914 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001915}
1916
1917void LocationsBuilderX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001918 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001919}
1920
1921void InstructionCodeGeneratorX86::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001922 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001923}
1924
1925void LocationsBuilderX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001926 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001927}
1928
1929void InstructionCodeGeneratorX86::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001930 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001931}
1932
1933void LocationsBuilderX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001934 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001935}
1936
1937void InstructionCodeGeneratorX86::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001938 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001939}
1940
1941void LocationsBuilderX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001942 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001943}
1944
1945void InstructionCodeGeneratorX86::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001946 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001947}
1948
1949void LocationsBuilderX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001950 HandleCondition(comp);
Dave Allison20dfc792014-06-16 20:44:29 -07001951}
1952
1953void InstructionCodeGeneratorX86::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001954 HandleCondition(comp);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001955}
1956
Aart Bike9f37602015-10-09 11:15:55 -07001957void LocationsBuilderX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001958 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001959}
1960
1961void InstructionCodeGeneratorX86::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001962 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001963}
1964
1965void LocationsBuilderX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001966 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001967}
1968
1969void InstructionCodeGeneratorX86::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001970 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001971}
1972
1973void LocationsBuilderX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001974 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001975}
1976
1977void InstructionCodeGeneratorX86::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001978 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001979}
1980
1981void LocationsBuilderX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001982 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001983}
1984
1985void InstructionCodeGeneratorX86::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00001986 HandleCondition(comp);
Aart Bike9f37602015-10-09 11:15:55 -07001987}
1988
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001989void LocationsBuilderX86::VisitIntConstant(HIntConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01001990 LocationSummary* locations =
1991 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01001992 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00001993}
1994
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001995void InstructionCodeGeneratorX86::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
Roland Levillain3a3fd0f2014-10-10 13:56:31 +01001996 // Will be generated at use site.
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00001997}
1998
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00001999void LocationsBuilderX86::VisitNullConstant(HNullConstant* constant) {
2000 LocationSummary* locations =
2001 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2002 locations->SetOut(Location::ConstantLocation(constant));
2003}
2004
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002005void InstructionCodeGeneratorX86::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002006 // Will be generated at use site.
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00002007}
2008
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002009void LocationsBuilderX86::VisitLongConstant(HLongConstant* constant) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002010 LocationSummary* locations =
2011 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01002012 locations->SetOut(Location::ConstantLocation(constant));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002013}
2014
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002015void InstructionCodeGeneratorX86::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002016 // Will be generated at use site.
2017}
2018
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002019void LocationsBuilderX86::VisitFloatConstant(HFloatConstant* constant) {
2020 LocationSummary* locations =
2021 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2022 locations->SetOut(Location::ConstantLocation(constant));
2023}
2024
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002025void InstructionCodeGeneratorX86::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002026 // Will be generated at use site.
2027}
2028
2029void LocationsBuilderX86::VisitDoubleConstant(HDoubleConstant* constant) {
2030 LocationSummary* locations =
2031 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
2032 locations->SetOut(Location::ConstantLocation(constant));
2033}
2034
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002035void InstructionCodeGeneratorX86::VisitDoubleConstant(HDoubleConstant* constant ATTRIBUTE_UNUSED) {
Nicolas Geoffray102cbed2014-10-15 18:31:05 +01002036 // Will be generated at use site.
2037}
2038
Calin Juravle27df7582015-04-17 19:12:31 +01002039void LocationsBuilderX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
2040 memory_barrier->SetLocations(nullptr);
2041}
2042
2043void InstructionCodeGeneratorX86::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00002044 codegen_->GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
Calin Juravle27df7582015-04-17 19:12:31 +01002045}
2046
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002047void LocationsBuilderX86::VisitReturnVoid(HReturnVoid* ret) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002048 ret->SetLocations(nullptr);
Nicolas Geoffray3ff386a2014-03-04 14:46:47 +00002049}
2050
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01002051void InstructionCodeGeneratorX86::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002052 codegen_->GenerateFrameExit();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00002053}
2054
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002055void LocationsBuilderX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002056 LocationSummary* locations =
2057 new (GetGraph()->GetArena()) LocationSummary(ret, LocationSummary::kNoCall);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002058 switch (ret->InputAt(0)->GetType()) {
2059 case Primitive::kPrimBoolean:
2060 case Primitive::kPrimByte:
2061 case Primitive::kPrimChar:
2062 case Primitive::kPrimShort:
2063 case Primitive::kPrimInt:
2064 case Primitive::kPrimNot:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002065 locations->SetInAt(0, Location::RegisterLocation(EAX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002066 break;
2067
2068 case Primitive::kPrimLong:
2069 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002070 0, Location::RegisterPairLocation(EAX, EDX));
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002071 break;
2072
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002073 case Primitive::kPrimFloat:
2074 case Primitive::kPrimDouble:
2075 locations->SetInAt(
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002076 0, Location::FpuRegisterLocation(XMM0));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002077 break;
2078
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002079 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002080 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002081 }
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002082}
2083
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002084void InstructionCodeGeneratorX86::VisitReturn(HReturn* ret) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002085 if (kIsDebugBuild) {
2086 switch (ret->InputAt(0)->GetType()) {
2087 case Primitive::kPrimBoolean:
2088 case Primitive::kPrimByte:
2089 case Primitive::kPrimChar:
2090 case Primitive::kPrimShort:
2091 case Primitive::kPrimInt:
2092 case Primitive::kPrimNot:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002093 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegister<Register>(), EAX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002094 break;
2095
2096 case Primitive::kPrimLong:
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002097 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairLow<Register>(), EAX);
2098 DCHECK_EQ(ret->GetLocations()->InAt(0).AsRegisterPairHigh<Register>(), EDX);
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002099 break;
2100
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002101 case Primitive::kPrimFloat:
2102 case Primitive::kPrimDouble:
Roland Levillain271ab9c2014-11-27 15:23:57 +00002103 DCHECK_EQ(ret->GetLocations()->InAt(0).AsFpuRegister<XmmRegister>(), XMM0);
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002104 break;
2105
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002106 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002107 LOG(FATAL) << "Unknown return type " << ret->InputAt(0)->GetType();
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002108 }
2109 }
Nicolas Geoffray787c3072014-03-17 10:20:19 +00002110 codegen_->GenerateFrameExit();
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +00002111}
2112
Calin Juravle175dc732015-08-25 15:42:32 +01002113void LocationsBuilderX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2114 // The trampoline uses the same calling convention as dex calling conventions,
2115 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
2116 // the method_idx.
2117 HandleInvoke(invoke);
2118}
2119
2120void InstructionCodeGeneratorX86::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
2121 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
2122}
2123
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002124void LocationsBuilderX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002125 // Explicit clinit checks triggered by static invokes must have been pruned by
2126 // art::PrepareForRegisterAllocation.
2127 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002128
Mark Mendellfb8d2792015-03-31 22:16:59 -04002129 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
Mark Mendell09ed1a32015-03-25 08:30:06 -04002130 if (intrinsic.TryDispatch(invoke)) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002131 if (invoke->GetLocations()->CanCall() && invoke->HasPcRelativeDexCache()) {
Vladimir Markoc53c0792015-11-19 15:48:33 +00002132 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002133 }
Mark Mendell09ed1a32015-03-25 08:30:06 -04002134 return;
2135 }
2136
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002137 HandleInvoke(invoke);
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002138
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002139 // For PC-relative dex cache the invoke has an extra input, the PC-relative address base.
2140 if (invoke->HasPcRelativeDexCache()) {
Vladimir Markob4536b72015-11-24 13:45:23 +00002141 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00002142 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002143}
2144
Mark Mendell09ed1a32015-03-25 08:30:06 -04002145static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorX86* codegen) {
2146 if (invoke->GetLocations()->Intrinsified()) {
2147 IntrinsicCodeGeneratorX86 intrinsic(codegen);
2148 intrinsic.Dispatch(invoke);
2149 return true;
2150 }
2151 return false;
2152}
2153
Nicolas Geoffraye53798a2014-12-01 10:31:54 +00002154void InstructionCodeGeneratorX86::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00002155 // Explicit clinit checks triggered by static invokes must have been pruned by
2156 // art::PrepareForRegisterAllocation.
2157 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Roland Levillain4c0eb422015-04-24 16:43:49 +01002158
Mark Mendell09ed1a32015-03-25 08:30:06 -04002159 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2160 return;
Nicolas Geoffray1cf95282014-12-12 19:22:03 +00002161 }
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002162
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002163 LocationSummary* locations = invoke->GetLocations();
Mark Mendell09ed1a32015-03-25 08:30:06 -04002164 codegen_->GenerateStaticOrDirectCall(
Nicolas Geoffray94015b92015-06-04 18:21:04 +01002165 invoke, locations->HasTemps() ? locations->GetTemp(0) : Location::NoLocation());
Mingyao Yang8693fe12015-04-17 16:51:08 -07002166 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002167}
2168
2169void LocationsBuilderX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00002170 IntrinsicLocationsBuilderX86 intrinsic(codegen_);
2171 if (intrinsic.TryDispatch(invoke)) {
2172 return;
2173 }
2174
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002175 HandleInvoke(invoke);
2176}
2177
2178void LocationsBuilderX86::HandleInvoke(HInvoke* invoke) {
Roland Levillain2d27c8e2015-04-28 15:48:45 +01002179 InvokeDexCallingConventionVisitorX86 calling_convention_visitor;
Nicolas Geoffrayfd88f162015-06-03 11:23:52 +01002180 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002181}
2182
Nicolas Geoffraye982f0b2014-08-13 02:11:24 +01002183void InstructionCodeGeneratorX86::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002184 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
2185 return;
2186 }
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002187
Andreas Gampebfb5ba92015-09-01 15:45:02 +00002188 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Nicolas Geoffrayf12feb82014-07-17 18:32:41 +01002189 DCHECK(!codegen_->IsLeafMethod());
Nicolas Geoffray39468442014-09-02 15:17:15 +01002190 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
Nicolas Geoffray8ccc3f52014-03-19 10:34:11 +00002191}
2192
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002193void LocationsBuilderX86::VisitInvokeInterface(HInvokeInterface* invoke) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002194 // This call to HandleInvoke allocates a temporary (core) register
2195 // which is also used to transfer the hidden argument from FP to
2196 // core register.
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002197 HandleInvoke(invoke);
2198 // Add the hidden argument.
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002199 invoke->GetLocations()->AddTemp(Location::FpuRegisterLocation(XMM7));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002200}
2201
2202void InstructionCodeGeneratorX86::VisitInvokeInterface(HInvokeInterface* invoke) {
2203 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
Roland Levillain0d5a2812015-11-13 10:07:31 +00002204 LocationSummary* locations = invoke->GetLocations();
2205 Register temp = locations->GetTemp(0).AsRegister<Register>();
2206 XmmRegister hidden_reg = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002207 Location receiver = locations->InAt(0);
2208 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2209
Roland Levillain0d5a2812015-11-13 10:07:31 +00002210 // Set the hidden argument. This is safe to do this here, as XMM7
2211 // won't be modified thereafter, before the `call` instruction.
2212 DCHECK_EQ(XMM7, hidden_reg);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002213 __ movl(temp, Immediate(invoke->GetDexMethodIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002214 __ movd(hidden_reg, temp);
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002215
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002216 if (receiver.IsStackSlot()) {
2217 __ movl(temp, Address(ESP, receiver.GetStackIndex()));
Roland Levillain0d5a2812015-11-13 10:07:31 +00002218 // /* HeapReference<Class> */ temp = temp->klass_
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002219 __ movl(temp, Address(temp, class_offset));
2220 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00002221 // /* HeapReference<Class> */ temp = receiver->klass_
Roland Levillain271ab9c2014-11-27 15:23:57 +00002222 __ movl(temp, Address(receiver.AsRegister<Register>(), class_offset));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002223 }
Roland Levillain4d027112015-07-01 15:41:14 +01002224 codegen_->MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00002225 // Instead of simply (possibly) unpoisoning `temp` here, we should
2226 // emit a read barrier for the previous class reference load.
2227 // However this is not required in practice, as this is an
2228 // intermediate/temporary reference and because the current
2229 // concurrent copying collector keeps the from-space memory
2230 // intact/accessible until the end of the marking phase (the
2231 // concurrent copying collector may not in the future).
Roland Levillain4d027112015-07-01 15:41:14 +01002232 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002233 // temp = temp->GetAddressOfIMT()
2234 __ movl(temp,
2235 Address(temp, mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002236 // temp = temp->GetImtEntryAt(method_offset);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002237 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002238 invoke->GetImtIndex(), kX86PointerSize));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002239 __ movl(temp, Address(temp, method_offset));
2240 // call temp->GetEntryPoint();
Roland Levillain0d5a2812015-11-13 10:07:31 +00002241 __ call(Address(temp,
Andreas Gampe542451c2016-07-26 09:02:02 -07002242 ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Nicolas Geoffray52839d12014-11-07 17:47:25 +00002243
2244 DCHECK(!codegen_->IsLeafMethod());
2245 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
2246}
2247
Roland Levillain88cb1752014-10-20 16:36:47 +01002248void LocationsBuilderX86::VisitNeg(HNeg* neg) {
2249 LocationSummary* locations =
2250 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2251 switch (neg->GetResultType()) {
2252 case Primitive::kPrimInt:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002253 case Primitive::kPrimLong:
Roland Levillain88cb1752014-10-20 16:36:47 +01002254 locations->SetInAt(0, Location::RequiresRegister());
2255 locations->SetOut(Location::SameAsFirstInput());
2256 break;
2257
Roland Levillain88cb1752014-10-20 16:36:47 +01002258 case Primitive::kPrimFloat:
Roland Levillain5368c212014-11-27 15:03:41 +00002259 locations->SetInAt(0, Location::RequiresFpuRegister());
2260 locations->SetOut(Location::SameAsFirstInput());
2261 locations->AddTemp(Location::RequiresRegister());
2262 locations->AddTemp(Location::RequiresFpuRegister());
2263 break;
2264
Roland Levillain88cb1752014-10-20 16:36:47 +01002265 case Primitive::kPrimDouble:
Roland Levillain3dbcb382014-10-28 17:30:07 +00002266 locations->SetInAt(0, Location::RequiresFpuRegister());
Roland Levillain5368c212014-11-27 15:03:41 +00002267 locations->SetOut(Location::SameAsFirstInput());
2268 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain88cb1752014-10-20 16:36:47 +01002269 break;
2270
2271 default:
2272 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2273 }
2274}
2275
2276void InstructionCodeGeneratorX86::VisitNeg(HNeg* neg) {
2277 LocationSummary* locations = neg->GetLocations();
2278 Location out = locations->Out();
2279 Location in = locations->InAt(0);
2280 switch (neg->GetResultType()) {
2281 case Primitive::kPrimInt:
2282 DCHECK(in.IsRegister());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002283 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002284 __ negl(out.AsRegister<Register>());
Roland Levillain88cb1752014-10-20 16:36:47 +01002285 break;
2286
2287 case Primitive::kPrimLong:
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002288 DCHECK(in.IsRegisterPair());
Roland Levillain3dbcb382014-10-28 17:30:07 +00002289 DCHECK(in.Equals(out));
Roland Levillain2e07b4f2014-10-23 18:12:09 +01002290 __ negl(out.AsRegisterPairLow<Register>());
2291 // Negation is similar to subtraction from zero. The least
2292 // significant byte triggers a borrow when it is different from
2293 // zero; to take it into account, add 1 to the most significant
2294 // byte if the carry flag (CF) is set to 1 after the first NEGL
2295 // operation.
2296 __ adcl(out.AsRegisterPairHigh<Register>(), Immediate(0));
2297 __ negl(out.AsRegisterPairHigh<Register>());
2298 break;
2299
Roland Levillain5368c212014-11-27 15:03:41 +00002300 case Primitive::kPrimFloat: {
2301 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002302 Register constant = locations->GetTemp(0).AsRegister<Register>();
2303 XmmRegister mask = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002304 // Implement float negation with an exclusive or with value
2305 // 0x80000000 (mask for bit 31, representing the sign of a
2306 // single-precision floating-point number).
2307 __ movl(constant, Immediate(INT32_C(0x80000000)));
2308 __ movd(mask, constant);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002309 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain3dbcb382014-10-28 17:30:07 +00002310 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002311 }
Roland Levillain3dbcb382014-10-28 17:30:07 +00002312
Roland Levillain5368c212014-11-27 15:03:41 +00002313 case Primitive::kPrimDouble: {
2314 DCHECK(in.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002315 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Roland Levillain5368c212014-11-27 15:03:41 +00002316 // Implement double negation with an exclusive or with value
2317 // 0x8000000000000000 (mask for bit 63, representing the sign of
2318 // a double-precision floating-point number).
2319 __ LoadLongConstant(mask, INT64_C(0x8000000000000000));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002320 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
Roland Levillain88cb1752014-10-20 16:36:47 +01002321 break;
Roland Levillain5368c212014-11-27 15:03:41 +00002322 }
Roland Levillain88cb1752014-10-20 16:36:47 +01002323
2324 default:
2325 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
2326 }
2327}
2328
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00002329void LocationsBuilderX86::VisitX86FPNeg(HX86FPNeg* neg) {
2330 LocationSummary* locations =
2331 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
2332 DCHECK(Primitive::IsFloatingPointType(neg->GetType()));
2333 locations->SetInAt(0, Location::RequiresFpuRegister());
2334 locations->SetInAt(1, Location::RequiresRegister());
2335 locations->SetOut(Location::SameAsFirstInput());
2336 locations->AddTemp(Location::RequiresFpuRegister());
2337}
2338
2339void InstructionCodeGeneratorX86::VisitX86FPNeg(HX86FPNeg* neg) {
2340 LocationSummary* locations = neg->GetLocations();
2341 Location out = locations->Out();
2342 DCHECK(locations->InAt(0).Equals(out));
2343
2344 Register constant_area = locations->InAt(1).AsRegister<Register>();
2345 XmmRegister mask = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
2346 if (neg->GetType() == Primitive::kPrimFloat) {
2347 __ movss(mask, codegen_->LiteralInt32Address(INT32_C(0x80000000), constant_area));
2348 __ xorps(out.AsFpuRegister<XmmRegister>(), mask);
2349 } else {
2350 __ movsd(mask, codegen_->LiteralInt64Address(INT64_C(0x8000000000000000), constant_area));
2351 __ xorpd(out.AsFpuRegister<XmmRegister>(), mask);
2352 }
2353}
2354
Roland Levillaindff1f282014-11-05 14:15:05 +00002355void LocationsBuilderX86::VisitTypeConversion(HTypeConversion* conversion) {
Roland Levillaindff1f282014-11-05 14:15:05 +00002356 Primitive::Type result_type = conversion->GetResultType();
2357 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002358 DCHECK_NE(result_type, input_type);
Roland Levillain624279f2014-12-04 11:54:28 +00002359
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002360 // The float-to-long and double-to-long type conversions rely on a
2361 // call to the runtime.
Roland Levillain624279f2014-12-04 11:54:28 +00002362 LocationSummary::CallKind call_kind =
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002363 ((input_type == Primitive::kPrimFloat || input_type == Primitive::kPrimDouble)
2364 && result_type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01002365 ? LocationSummary::kCallOnMainOnly
Roland Levillain624279f2014-12-04 11:54:28 +00002366 : LocationSummary::kNoCall;
2367 LocationSummary* locations =
2368 new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
2369
David Brazdilb2bd1c52015-03-25 11:17:37 +00002370 // The Java language does not allow treating boolean as an integral type but
2371 // our bit representation makes it safe.
David Brazdil46e2a392015-03-16 17:31:52 +00002372
Roland Levillaindff1f282014-11-05 14:15:05 +00002373 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002374 case Primitive::kPrimByte:
2375 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002376 case Primitive::kPrimLong: {
2377 // Type conversion from long to byte is a result of code transformations.
2378 HInstruction* input = conversion->InputAt(0);
2379 Location input_location = input->IsConstant()
2380 ? Location::ConstantLocation(input->AsConstant())
2381 : Location::RegisterPairLocation(EAX, EDX);
2382 locations->SetInAt(0, input_location);
2383 // Make the output overlap to please the register allocator. This greatly simplifies
2384 // the validation of the linear scan implementation
2385 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
2386 break;
2387 }
David Brazdil46e2a392015-03-16 17:31:52 +00002388 case Primitive::kPrimBoolean:
2389 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002390 case Primitive::kPrimShort:
2391 case Primitive::kPrimInt:
2392 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002393 // Processing a Dex `int-to-byte' instruction.
Mark Mendell5f874182015-03-04 15:42:45 -05002394 locations->SetInAt(0, Location::ByteRegisterOrConstant(ECX, conversion->InputAt(0)));
2395 // Make the output overlap to please the register allocator. This greatly simplifies
2396 // the validation of the linear scan implementation
2397 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Roland Levillain51d3fc42014-11-13 14:11:42 +00002398 break;
2399
2400 default:
2401 LOG(FATAL) << "Unexpected type conversion from " << input_type
2402 << " to " << result_type;
2403 }
2404 break;
2405
Roland Levillain01a8d712014-11-14 16:27:39 +00002406 case Primitive::kPrimShort:
2407 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002408 case Primitive::kPrimLong:
2409 // Type conversion from long to short is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002410 case Primitive::kPrimBoolean:
2411 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002412 case Primitive::kPrimByte:
2413 case Primitive::kPrimInt:
2414 case Primitive::kPrimChar:
2415 // Processing a Dex `int-to-short' instruction.
2416 locations->SetInAt(0, Location::Any());
2417 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2418 break;
2419
2420 default:
2421 LOG(FATAL) << "Unexpected type conversion from " << input_type
2422 << " to " << result_type;
2423 }
2424 break;
2425
Roland Levillain946e1432014-11-11 17:35:19 +00002426 case Primitive::kPrimInt:
2427 switch (input_type) {
2428 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002429 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002430 locations->SetInAt(0, Location::Any());
2431 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2432 break;
2433
2434 case Primitive::kPrimFloat:
Roland Levillain3f8f9362014-12-02 17:45:01 +00002435 // Processing a Dex `float-to-int' instruction.
2436 locations->SetInAt(0, Location::RequiresFpuRegister());
2437 locations->SetOut(Location::RequiresRegister());
2438 locations->AddTemp(Location::RequiresFpuRegister());
2439 break;
2440
Roland Levillain946e1432014-11-11 17:35:19 +00002441 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002442 // Processing a Dex `double-to-int' instruction.
2443 locations->SetInAt(0, Location::RequiresFpuRegister());
2444 locations->SetOut(Location::RequiresRegister());
2445 locations->AddTemp(Location::RequiresFpuRegister());
Roland Levillain946e1432014-11-11 17:35:19 +00002446 break;
2447
2448 default:
2449 LOG(FATAL) << "Unexpected type conversion from " << input_type
2450 << " to " << result_type;
2451 }
2452 break;
2453
Roland Levillaindff1f282014-11-05 14:15:05 +00002454 case Primitive::kPrimLong:
2455 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002456 case Primitive::kPrimBoolean:
2457 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002458 case Primitive::kPrimByte:
2459 case Primitive::kPrimShort:
2460 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002461 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002462 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002463 locations->SetInAt(0, Location::RegisterLocation(EAX));
2464 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
2465 break;
2466
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002467 case Primitive::kPrimFloat:
Vladimir Marko949c91f2015-01-27 10:48:44 +00002468 case Primitive::kPrimDouble: {
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002469 // Processing a Dex `float-to-long' or 'double-to-long' instruction.
Vladimir Marko949c91f2015-01-27 10:48:44 +00002470 InvokeRuntimeCallingConvention calling_convention;
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002471 XmmRegister parameter = calling_convention.GetFpuRegisterAt(0);
2472 locations->SetInAt(0, Location::FpuRegisterLocation(parameter));
2473
Vladimir Marko949c91f2015-01-27 10:48:44 +00002474 // The runtime helper puts the result in EAX, EDX.
2475 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Vladimir Marko949c91f2015-01-27 10:48:44 +00002476 }
Mark P Mendell966c3ae2015-01-27 15:45:27 +00002477 break;
Roland Levillaindff1f282014-11-05 14:15:05 +00002478
2479 default:
2480 LOG(FATAL) << "Unexpected type conversion from " << input_type
2481 << " to " << result_type;
2482 }
2483 break;
2484
Roland Levillain981e4542014-11-14 11:47:14 +00002485 case Primitive::kPrimChar:
2486 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002487 case Primitive::kPrimLong:
2488 // Type conversion from long to char is a result of code transformations.
David Brazdil46e2a392015-03-16 17:31:52 +00002489 case Primitive::kPrimBoolean:
2490 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002491 case Primitive::kPrimByte:
2492 case Primitive::kPrimShort:
2493 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002494 // Processing a Dex `int-to-char' instruction.
2495 locations->SetInAt(0, Location::Any());
2496 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2497 break;
2498
2499 default:
2500 LOG(FATAL) << "Unexpected type conversion from " << input_type
2501 << " to " << result_type;
2502 }
2503 break;
2504
Roland Levillaindff1f282014-11-05 14:15:05 +00002505 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002506 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002507 case Primitive::kPrimBoolean:
2508 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002509 case Primitive::kPrimByte:
2510 case Primitive::kPrimShort:
2511 case Primitive::kPrimInt:
2512 case Primitive::kPrimChar:
2513 // Processing a Dex `int-to-float' instruction.
2514 locations->SetInAt(0, Location::RequiresRegister());
2515 locations->SetOut(Location::RequiresFpuRegister());
2516 break;
2517
2518 case Primitive::kPrimLong:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002519 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002520 locations->SetInAt(0, Location::Any());
2521 locations->SetOut(Location::Any());
Roland Levillain6d0e4832014-11-27 18:31:21 +00002522 break;
2523
Roland Levillaincff13742014-11-17 14:32:17 +00002524 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002525 // Processing a Dex `double-to-float' instruction.
2526 locations->SetInAt(0, Location::RequiresFpuRegister());
2527 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002528 break;
2529
2530 default:
2531 LOG(FATAL) << "Unexpected type conversion from " << input_type
2532 << " to " << result_type;
2533 };
2534 break;
2535
Roland Levillaindff1f282014-11-05 14:15:05 +00002536 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002537 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002538 case Primitive::kPrimBoolean:
2539 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002540 case Primitive::kPrimByte:
2541 case Primitive::kPrimShort:
2542 case Primitive::kPrimInt:
2543 case Primitive::kPrimChar:
2544 // Processing a Dex `int-to-double' instruction.
2545 locations->SetInAt(0, Location::RequiresRegister());
2546 locations->SetOut(Location::RequiresFpuRegister());
2547 break;
2548
2549 case Primitive::kPrimLong:
Roland Levillain647b9ed2014-11-27 12:06:00 +00002550 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002551 locations->SetInAt(0, Location::Any());
2552 locations->SetOut(Location::Any());
Roland Levillain647b9ed2014-11-27 12:06:00 +00002553 break;
2554
Roland Levillaincff13742014-11-17 14:32:17 +00002555 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002556 // Processing a Dex `float-to-double' instruction.
2557 locations->SetInAt(0, Location::RequiresFpuRegister());
2558 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
Roland Levillaincff13742014-11-17 14:32:17 +00002559 break;
2560
2561 default:
2562 LOG(FATAL) << "Unexpected type conversion from " << input_type
2563 << " to " << result_type;
2564 }
Roland Levillaindff1f282014-11-05 14:15:05 +00002565 break;
2566
2567 default:
2568 LOG(FATAL) << "Unexpected type conversion from " << input_type
2569 << " to " << result_type;
2570 }
2571}
2572
2573void InstructionCodeGeneratorX86::VisitTypeConversion(HTypeConversion* conversion) {
2574 LocationSummary* locations = conversion->GetLocations();
2575 Location out = locations->Out();
2576 Location in = locations->InAt(0);
2577 Primitive::Type result_type = conversion->GetResultType();
2578 Primitive::Type input_type = conversion->GetInputType();
Nicolas Geoffray01fcc9e2014-12-01 14:16:20 +00002579 DCHECK_NE(result_type, input_type);
Roland Levillaindff1f282014-11-05 14:15:05 +00002580 switch (result_type) {
Roland Levillain51d3fc42014-11-13 14:11:42 +00002581 case Primitive::kPrimByte:
2582 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002583 case Primitive::kPrimLong:
2584 // Type conversion from long to byte is a result of code transformations.
2585 if (in.IsRegisterPair()) {
2586 __ movsxb(out.AsRegister<Register>(), in.AsRegisterPairLow<ByteRegister>());
2587 } else {
2588 DCHECK(in.GetConstant()->IsLongConstant());
2589 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2590 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2591 }
2592 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002593 case Primitive::kPrimBoolean:
2594 // Boolean input is a result of code transformations.
Roland Levillain51d3fc42014-11-13 14:11:42 +00002595 case Primitive::kPrimShort:
2596 case Primitive::kPrimInt:
2597 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002598 // Processing a Dex `int-to-byte' instruction.
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002599 if (in.IsRegister()) {
2600 __ movsxb(out.AsRegister<Register>(), in.AsRegister<ByteRegister>());
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00002601 } else {
2602 DCHECK(in.GetConstant()->IsIntConstant());
2603 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
2604 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int8_t>(value)));
2605 }
Roland Levillain51d3fc42014-11-13 14:11:42 +00002606 break;
2607
2608 default:
2609 LOG(FATAL) << "Unexpected type conversion from " << input_type
2610 << " to " << result_type;
2611 }
2612 break;
2613
Roland Levillain01a8d712014-11-14 16:27:39 +00002614 case Primitive::kPrimShort:
2615 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002616 case Primitive::kPrimLong:
2617 // Type conversion from long to short is a result of code transformations.
2618 if (in.IsRegisterPair()) {
2619 __ movsxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2620 } else if (in.IsDoubleStackSlot()) {
2621 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2622 } else {
2623 DCHECK(in.GetConstant()->IsLongConstant());
2624 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2625 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
2626 }
2627 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002628 case Primitive::kPrimBoolean:
2629 // Boolean input is a result of code transformations.
Roland Levillain01a8d712014-11-14 16:27:39 +00002630 case Primitive::kPrimByte:
2631 case Primitive::kPrimInt:
2632 case Primitive::kPrimChar:
2633 // Processing a Dex `int-to-short' instruction.
2634 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002635 __ movsxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain01a8d712014-11-14 16:27:39 +00002636 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002637 __ movsxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain01a8d712014-11-14 16:27:39 +00002638 } else {
2639 DCHECK(in.GetConstant()->IsIntConstant());
2640 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002641 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int16_t>(value)));
Roland Levillain01a8d712014-11-14 16:27:39 +00002642 }
2643 break;
2644
2645 default:
2646 LOG(FATAL) << "Unexpected type conversion from " << input_type
2647 << " to " << result_type;
2648 }
2649 break;
2650
Roland Levillain946e1432014-11-11 17:35:19 +00002651 case Primitive::kPrimInt:
2652 switch (input_type) {
2653 case Primitive::kPrimLong:
Roland Levillain981e4542014-11-14 11:47:14 +00002654 // Processing a Dex `long-to-int' instruction.
Roland Levillain946e1432014-11-11 17:35:19 +00002655 if (in.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002656 __ movl(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
Roland Levillain946e1432014-11-11 17:35:19 +00002657 } else if (in.IsDoubleStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002658 __ movl(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain946e1432014-11-11 17:35:19 +00002659 } else {
2660 DCHECK(in.IsConstant());
2661 DCHECK(in.GetConstant()->IsLongConstant());
2662 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002663 __ movl(out.AsRegister<Register>(), Immediate(static_cast<int32_t>(value)));
Roland Levillain946e1432014-11-11 17:35:19 +00002664 }
2665 break;
2666
Roland Levillain3f8f9362014-12-02 17:45:01 +00002667 case Primitive::kPrimFloat: {
2668 // Processing a Dex `float-to-int' instruction.
2669 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2670 Register output = out.AsRegister<Register>();
2671 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002672 NearLabel done, nan;
Roland Levillain3f8f9362014-12-02 17:45:01 +00002673
2674 __ movl(output, Immediate(kPrimIntMax));
2675 // temp = int-to-float(output)
2676 __ cvtsi2ss(temp, output);
2677 // if input >= temp goto done
2678 __ comiss(input, temp);
2679 __ j(kAboveEqual, &done);
2680 // if input == NaN goto nan
2681 __ j(kUnordered, &nan);
2682 // output = float-to-int-truncate(input)
2683 __ cvttss2si(output, input);
2684 __ jmp(&done);
2685 __ Bind(&nan);
2686 // output = 0
2687 __ xorl(output, output);
2688 __ Bind(&done);
2689 break;
2690 }
2691
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002692 case Primitive::kPrimDouble: {
2693 // Processing a Dex `double-to-int' instruction.
2694 XmmRegister input = in.AsFpuRegister<XmmRegister>();
2695 Register output = out.AsRegister<Register>();
2696 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
Mark Mendell0c9497d2015-08-21 09:30:05 -04002697 NearLabel done, nan;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002698
2699 __ movl(output, Immediate(kPrimIntMax));
2700 // temp = int-to-double(output)
2701 __ cvtsi2sd(temp, output);
2702 // if input >= temp goto done
2703 __ comisd(input, temp);
2704 __ j(kAboveEqual, &done);
2705 // if input == NaN goto nan
2706 __ j(kUnordered, &nan);
2707 // output = double-to-int-truncate(input)
2708 __ cvttsd2si(output, input);
2709 __ jmp(&done);
2710 __ Bind(&nan);
2711 // output = 0
2712 __ xorl(output, output);
2713 __ Bind(&done);
Roland Levillain946e1432014-11-11 17:35:19 +00002714 break;
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002715 }
Roland Levillain946e1432014-11-11 17:35:19 +00002716
2717 default:
2718 LOG(FATAL) << "Unexpected type conversion from " << input_type
2719 << " to " << result_type;
2720 }
2721 break;
2722
Roland Levillaindff1f282014-11-05 14:15:05 +00002723 case Primitive::kPrimLong:
2724 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002725 case Primitive::kPrimBoolean:
2726 // Boolean input is a result of code transformations.
Roland Levillaindff1f282014-11-05 14:15:05 +00002727 case Primitive::kPrimByte:
2728 case Primitive::kPrimShort:
2729 case Primitive::kPrimInt:
Roland Levillain666c7322014-11-10 13:39:43 +00002730 case Primitive::kPrimChar:
Roland Levillain981e4542014-11-14 11:47:14 +00002731 // Processing a Dex `int-to-long' instruction.
Roland Levillaindff1f282014-11-05 14:15:05 +00002732 DCHECK_EQ(out.AsRegisterPairLow<Register>(), EAX);
2733 DCHECK_EQ(out.AsRegisterPairHigh<Register>(), EDX);
Roland Levillain271ab9c2014-11-27 15:23:57 +00002734 DCHECK_EQ(in.AsRegister<Register>(), EAX);
Roland Levillaindff1f282014-11-05 14:15:05 +00002735 __ cdq();
2736 break;
2737
2738 case Primitive::kPrimFloat:
Roland Levillain624279f2014-12-04 11:54:28 +00002739 // Processing a Dex `float-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002740 codegen_->InvokeRuntime(kQuickF2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002741 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
Roland Levillain624279f2014-12-04 11:54:28 +00002742 break;
2743
Roland Levillaindff1f282014-11-05 14:15:05 +00002744 case Primitive::kPrimDouble:
Roland Levillain4c0b61f2014-12-05 12:06:01 +00002745 // Processing a Dex `double-to-long' instruction.
Serban Constantinescuba45db02016-07-12 22:53:02 +01002746 codegen_->InvokeRuntime(kQuickD2l, conversion, conversion->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00002747 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
Roland Levillaindff1f282014-11-05 14:15:05 +00002748 break;
2749
2750 default:
2751 LOG(FATAL) << "Unexpected type conversion from " << input_type
2752 << " to " << result_type;
2753 }
2754 break;
2755
Roland Levillain981e4542014-11-14 11:47:14 +00002756 case Primitive::kPrimChar:
2757 switch (input_type) {
Vladimir Markob52bbde2016-02-12 12:06:05 +00002758 case Primitive::kPrimLong:
2759 // Type conversion from long to short is a result of code transformations.
2760 if (in.IsRegisterPair()) {
2761 __ movzxw(out.AsRegister<Register>(), in.AsRegisterPairLow<Register>());
2762 } else if (in.IsDoubleStackSlot()) {
2763 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
2764 } else {
2765 DCHECK(in.GetConstant()->IsLongConstant());
2766 int64_t value = in.GetConstant()->AsLongConstant()->GetValue();
2767 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
2768 }
2769 break;
David Brazdil46e2a392015-03-16 17:31:52 +00002770 case Primitive::kPrimBoolean:
2771 // Boolean input is a result of code transformations.
Roland Levillain981e4542014-11-14 11:47:14 +00002772 case Primitive::kPrimByte:
2773 case Primitive::kPrimShort:
2774 case Primitive::kPrimInt:
Roland Levillain981e4542014-11-14 11:47:14 +00002775 // Processing a Dex `Process a Dex `int-to-char'' instruction.
2776 if (in.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002777 __ movzxw(out.AsRegister<Register>(), in.AsRegister<Register>());
Roland Levillain981e4542014-11-14 11:47:14 +00002778 } else if (in.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002779 __ movzxw(out.AsRegister<Register>(), Address(ESP, in.GetStackIndex()));
Roland Levillain981e4542014-11-14 11:47:14 +00002780 } else {
2781 DCHECK(in.GetConstant()->IsIntConstant());
2782 int32_t value = in.GetConstant()->AsIntConstant()->GetValue();
Roland Levillain271ab9c2014-11-27 15:23:57 +00002783 __ movl(out.AsRegister<Register>(), Immediate(static_cast<uint16_t>(value)));
Roland Levillain981e4542014-11-14 11:47:14 +00002784 }
2785 break;
2786
2787 default:
2788 LOG(FATAL) << "Unexpected type conversion from " << input_type
2789 << " to " << result_type;
2790 }
2791 break;
2792
Roland Levillaindff1f282014-11-05 14:15:05 +00002793 case Primitive::kPrimFloat:
Roland Levillaincff13742014-11-17 14:32:17 +00002794 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002795 case Primitive::kPrimBoolean:
2796 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002797 case Primitive::kPrimByte:
2798 case Primitive::kPrimShort:
2799 case Primitive::kPrimInt:
2800 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002801 // Processing a Dex `int-to-float' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002802 __ cvtsi2ss(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002803 break;
2804
Roland Levillain6d0e4832014-11-27 18:31:21 +00002805 case Primitive::kPrimLong: {
2806 // Processing a Dex `long-to-float' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002807 size_t adjustment = 0;
Roland Levillain6d0e4832014-11-27 18:31:21 +00002808
Roland Levillain232ade02015-04-20 15:14:36 +01002809 // Create stack space for the call to
2810 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstps below.
2811 // TODO: enhance register allocator to ask for stack temporaries.
2812 if (!in.IsDoubleStackSlot() || !out.IsStackSlot()) {
2813 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2814 __ subl(ESP, Immediate(adjustment));
2815 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002816
Roland Levillain232ade02015-04-20 15:14:36 +01002817 // Load the value to the FP stack, using temporaries if needed.
2818 PushOntoFPStack(in, 0, adjustment, false, true);
2819
2820 if (out.IsStackSlot()) {
2821 __ fstps(Address(ESP, out.GetStackIndex() + adjustment));
2822 } else {
2823 __ fstps(Address(ESP, 0));
2824 Location stack_temp = Location::StackSlot(0);
2825 codegen_->Move32(out, stack_temp);
2826 }
2827
2828 // Remove the temporary stack space we allocated.
2829 if (adjustment != 0) {
2830 __ addl(ESP, Immediate(adjustment));
2831 }
Roland Levillain6d0e4832014-11-27 18:31:21 +00002832 break;
2833 }
2834
Roland Levillaincff13742014-11-17 14:32:17 +00002835 case Primitive::kPrimDouble:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002836 // Processing a Dex `double-to-float' instruction.
2837 __ cvtsd2ss(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002838 break;
2839
2840 default:
2841 LOG(FATAL) << "Unexpected type conversion from " << input_type
2842 << " to " << result_type;
2843 };
2844 break;
2845
Roland Levillaindff1f282014-11-05 14:15:05 +00002846 case Primitive::kPrimDouble:
Roland Levillaincff13742014-11-17 14:32:17 +00002847 switch (input_type) {
David Brazdil46e2a392015-03-16 17:31:52 +00002848 case Primitive::kPrimBoolean:
2849 // Boolean input is a result of code transformations.
Roland Levillaincff13742014-11-17 14:32:17 +00002850 case Primitive::kPrimByte:
2851 case Primitive::kPrimShort:
2852 case Primitive::kPrimInt:
2853 case Primitive::kPrimChar:
Roland Levillain6d0e4832014-11-27 18:31:21 +00002854 // Processing a Dex `int-to-double' instruction.
Roland Levillain271ab9c2014-11-27 15:23:57 +00002855 __ cvtsi2sd(out.AsFpuRegister<XmmRegister>(), in.AsRegister<Register>());
Roland Levillaincff13742014-11-17 14:32:17 +00002856 break;
2857
Roland Levillain647b9ed2014-11-27 12:06:00 +00002858 case Primitive::kPrimLong: {
2859 // Processing a Dex `long-to-double' instruction.
Roland Levillain232ade02015-04-20 15:14:36 +01002860 size_t adjustment = 0;
Roland Levillain647b9ed2014-11-27 12:06:00 +00002861
Roland Levillain232ade02015-04-20 15:14:36 +01002862 // Create stack space for the call to
2863 // InstructionCodeGeneratorX86::PushOntoFPStack and/or X86Assembler::fstpl below.
2864 // TODO: enhance register allocator to ask for stack temporaries.
2865 if (!in.IsDoubleStackSlot() || !out.IsDoubleStackSlot()) {
2866 adjustment = Primitive::ComponentSize(Primitive::kPrimLong);
2867 __ subl(ESP, Immediate(adjustment));
2868 }
2869
2870 // Load the value to the FP stack, using temporaries if needed.
2871 PushOntoFPStack(in, 0, adjustment, false, true);
2872
2873 if (out.IsDoubleStackSlot()) {
2874 __ fstpl(Address(ESP, out.GetStackIndex() + adjustment));
2875 } else {
2876 __ fstpl(Address(ESP, 0));
2877 Location stack_temp = Location::DoubleStackSlot(0);
2878 codegen_->Move64(out, stack_temp);
2879 }
2880
2881 // Remove the temporary stack space we allocated.
2882 if (adjustment != 0) {
2883 __ addl(ESP, Immediate(adjustment));
2884 }
Roland Levillain647b9ed2014-11-27 12:06:00 +00002885 break;
2886 }
2887
Roland Levillaincff13742014-11-17 14:32:17 +00002888 case Primitive::kPrimFloat:
Roland Levillain8964e2b2014-12-04 12:10:50 +00002889 // Processing a Dex `float-to-double' instruction.
2890 __ cvtss2sd(out.AsFpuRegister<XmmRegister>(), in.AsFpuRegister<XmmRegister>());
Roland Levillaincff13742014-11-17 14:32:17 +00002891 break;
2892
2893 default:
2894 LOG(FATAL) << "Unexpected type conversion from " << input_type
2895 << " to " << result_type;
2896 };
Roland Levillaindff1f282014-11-05 14:15:05 +00002897 break;
2898
2899 default:
2900 LOG(FATAL) << "Unexpected type conversion from " << input_type
2901 << " to " << result_type;
2902 }
2903}
2904
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002905void LocationsBuilderX86::VisitAdd(HAdd* add) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01002906 LocationSummary* locations =
2907 new (GetGraph()->GetArena()) LocationSummary(add, LocationSummary::kNoCall);
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002908 switch (add->GetResultType()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002909 case Primitive::kPrimInt: {
2910 locations->SetInAt(0, Location::RequiresRegister());
2911 locations->SetInAt(1, Location::RegisterOrConstant(add->InputAt(1)));
2912 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2913 break;
2914 }
2915
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002916 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002917 locations->SetInAt(0, Location::RequiresRegister());
2918 locations->SetInAt(1, Location::Any());
2919 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002920 break;
2921 }
2922
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002923 case Primitive::kPrimFloat:
2924 case Primitive::kPrimDouble: {
2925 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002926 if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2927 DCHECK(add->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00002928 } else if (add->InputAt(1)->IsConstant()) {
2929 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00002930 } else {
2931 locations->SetInAt(1, Location::Any());
2932 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002933 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002934 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002935 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002936
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002937 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002938 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
2939 break;
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002940 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002941}
2942
2943void InstructionCodeGeneratorX86::VisitAdd(HAdd* add) {
2944 LocationSummary* locations = add->GetLocations();
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002945 Location first = locations->InAt(0);
2946 Location second = locations->InAt(1);
Mark Mendell09b84632015-02-13 17:48:38 -05002947 Location out = locations->Out();
2948
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002949 switch (add->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002950 case Primitive::kPrimInt: {
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002951 if (second.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002952 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2953 __ addl(out.AsRegister<Register>(), second.AsRegister<Register>());
Mark Mendell33bf2452015-05-27 10:08:24 -04002954 } else if (out.AsRegister<Register>() == second.AsRegister<Register>()) {
2955 __ addl(out.AsRegister<Register>(), first.AsRegister<Register>());
Mark Mendell09b84632015-02-13 17:48:38 -05002956 } else {
2957 __ leal(out.AsRegister<Register>(), Address(
2958 first.AsRegister<Register>(), second.AsRegister<Register>(), TIMES_1, 0));
2959 }
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002960 } else if (second.IsConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05002961 int32_t value = second.GetConstant()->AsIntConstant()->GetValue();
2962 if (out.AsRegister<Register>() == first.AsRegister<Register>()) {
2963 __ addl(out.AsRegister<Register>(), Immediate(value));
2964 } else {
2965 __ leal(out.AsRegister<Register>(), Address(first.AsRegister<Register>(), value));
2966 }
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002967 } else {
Mark Mendell09b84632015-02-13 17:48:38 -05002968 DCHECK(first.Equals(locations->Out()));
Roland Levillain271ab9c2014-11-27 15:23:57 +00002969 __ addl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002970 }
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00002971 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002972 }
2973
2974 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00002975 if (second.IsRegisterPair()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002976 __ addl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
2977 __ adcl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002978 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01002979 __ addl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
2980 __ adcl(first.AsRegisterPairHigh<Register>(),
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002981 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00002982 } else {
2983 DCHECK(second.IsConstant()) << second;
2984 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
2985 __ addl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
2986 __ adcl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01002987 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01002988 break;
2989 }
2990
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01002991 case Primitive::kPrimFloat: {
2992 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00002993 __ addss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04002994 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
2995 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00002996 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04002997 __ addss(first.AsFpuRegister<XmmRegister>(),
2998 codegen_->LiteralFloatAddress(
2999 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3000 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3001 } else {
3002 DCHECK(second.IsStackSlot());
3003 __ addss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003004 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003005 break;
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003006 }
3007
3008 case Primitive::kPrimDouble: {
3009 if (second.IsFpuRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003010 __ addsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
Mark Mendell0616ae02015-04-17 12:49:27 -04003011 } else if (add->InputAt(1)->IsX86LoadFromConstantTable()) {
3012 HX86LoadFromConstantTable* const_area = add->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003013 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003014 __ addsd(first.AsFpuRegister<XmmRegister>(),
3015 codegen_->LiteralDoubleAddress(
3016 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3017 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3018 } else {
3019 DCHECK(second.IsDoubleStackSlot());
3020 __ addsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003021 }
3022 break;
3023 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003024
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003025 default:
Nicolas Geoffray7fb49da2014-10-06 09:12:41 +01003026 LOG(FATAL) << "Unexpected add type " << add->GetResultType();
Nicolas Geoffrayd8ee7372014-03-28 15:43:40 +00003027 }
3028}
3029
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003030void LocationsBuilderX86::VisitSub(HSub* sub) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01003031 LocationSummary* locations =
3032 new (GetGraph()->GetArena()) LocationSummary(sub, LocationSummary::kNoCall);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003033 switch (sub->GetResultType()) {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003034 case Primitive::kPrimInt:
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003035 case Primitive::kPrimLong: {
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003036 locations->SetInAt(0, Location::RequiresRegister());
3037 locations->SetInAt(1, Location::Any());
3038 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003039 break;
3040 }
Calin Juravle11351682014-10-23 15:38:15 +01003041 case Primitive::kPrimFloat:
3042 case Primitive::kPrimDouble: {
3043 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003044 if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3045 DCHECK(sub->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003046 } else if (sub->InputAt(1)->IsConstant()) {
3047 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003048 } else {
3049 locations->SetInAt(1, Location::Any());
3050 }
Calin Juravle11351682014-10-23 15:38:15 +01003051 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003052 break;
Calin Juravle11351682014-10-23 15:38:15 +01003053 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003054
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003055 default:
Calin Juravle11351682014-10-23 15:38:15 +01003056 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003057 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003058}
3059
3060void InstructionCodeGeneratorX86::VisitSub(HSub* sub) {
3061 LocationSummary* locations = sub->GetLocations();
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003062 Location first = locations->InAt(0);
3063 Location second = locations->InAt(1);
Calin Juravle11351682014-10-23 15:38:15 +01003064 DCHECK(first.Equals(locations->Out()));
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003065 switch (sub->GetResultType()) {
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003066 case Primitive::kPrimInt: {
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003067 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003068 __ subl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003069 } else if (second.IsConstant()) {
Roland Levillain199f3362014-11-27 17:15:16 +00003070 __ subl(first.AsRegister<Register>(),
3071 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003072 } else {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003073 __ subl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003074 }
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003075 break;
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003076 }
3077
3078 case Primitive::kPrimLong: {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00003079 if (second.IsRegisterPair()) {
Calin Juravle11351682014-10-23 15:38:15 +01003080 __ subl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
3081 __ sbbl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003082 } else if (second.IsDoubleStackSlot()) {
Calin Juravle11351682014-10-23 15:38:15 +01003083 __ subl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01003084 __ sbbl(first.AsRegisterPairHigh<Register>(),
3085 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003086 } else {
3087 DCHECK(second.IsConstant()) << second;
3088 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3089 __ subl(first.AsRegisterPairLow<Register>(), Immediate(Low32Bits(value)));
3090 __ sbbl(first.AsRegisterPairHigh<Register>(), Immediate(High32Bits(value)));
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01003091 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003092 break;
3093 }
3094
Calin Juravle11351682014-10-23 15:38:15 +01003095 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003096 if (second.IsFpuRegister()) {
3097 __ subss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3098 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3099 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003100 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003101 __ subss(first.AsFpuRegister<XmmRegister>(),
3102 codegen_->LiteralFloatAddress(
3103 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3104 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3105 } else {
3106 DCHECK(second.IsStackSlot());
3107 __ subss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3108 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003109 break;
Calin Juravle11351682014-10-23 15:38:15 +01003110 }
3111
3112 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003113 if (second.IsFpuRegister()) {
3114 __ subsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3115 } else if (sub->InputAt(1)->IsX86LoadFromConstantTable()) {
3116 HX86LoadFromConstantTable* const_area = sub->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003117 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003118 __ subsd(first.AsFpuRegister<XmmRegister>(),
3119 codegen_->LiteralDoubleAddress(
3120 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3121 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3122 } else {
3123 DCHECK(second.IsDoubleStackSlot());
3124 __ subsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3125 }
Calin Juravle11351682014-10-23 15:38:15 +01003126 break;
3127 }
Nicolas Geoffray01bc96d2014-04-11 17:43:50 +01003128
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003129 default:
Calin Juravle11351682014-10-23 15:38:15 +01003130 LOG(FATAL) << "Unexpected sub type " << sub->GetResultType();
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01003131 }
3132}
3133
Calin Juravle34bacdf2014-10-07 20:23:36 +01003134void LocationsBuilderX86::VisitMul(HMul* mul) {
3135 LocationSummary* locations =
3136 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
3137 switch (mul->GetResultType()) {
3138 case Primitive::kPrimInt:
3139 locations->SetInAt(0, Location::RequiresRegister());
3140 locations->SetInAt(1, Location::Any());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003141 if (mul->InputAt(1)->IsIntConstant()) {
3142 // Can use 3 operand multiply.
3143 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3144 } else {
3145 locations->SetOut(Location::SameAsFirstInput());
3146 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003147 break;
3148 case Primitive::kPrimLong: {
3149 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003150 locations->SetInAt(1, Location::Any());
3151 locations->SetOut(Location::SameAsFirstInput());
3152 // Needed for imul on 32bits with 64bits output.
3153 locations->AddTemp(Location::RegisterLocation(EAX));
3154 locations->AddTemp(Location::RegisterLocation(EDX));
3155 break;
3156 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003157 case Primitive::kPrimFloat:
3158 case Primitive::kPrimDouble: {
3159 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003160 if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3161 DCHECK(mul->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003162 } else if (mul->InputAt(1)->IsConstant()) {
3163 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003164 } else {
3165 locations->SetInAt(1, Location::Any());
3166 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003167 locations->SetOut(Location::SameAsFirstInput());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003168 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003169 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003170
3171 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003172 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003173 }
3174}
3175
3176void InstructionCodeGeneratorX86::VisitMul(HMul* mul) {
3177 LocationSummary* locations = mul->GetLocations();
3178 Location first = locations->InAt(0);
3179 Location second = locations->InAt(1);
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003180 Location out = locations->Out();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003181
3182 switch (mul->GetResultType()) {
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003183 case Primitive::kPrimInt:
3184 // The constant may have ended up in a register, so test explicitly to avoid
3185 // problems where the output may not be the same as the first operand.
3186 if (mul->InputAt(1)->IsIntConstant()) {
3187 Immediate imm(mul->InputAt(1)->AsIntConstant()->GetValue());
3188 __ imull(out.AsRegister<Register>(), first.AsRegister<Register>(), imm);
3189 } else if (second.IsRegister()) {
3190 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003191 __ imull(first.AsRegister<Register>(), second.AsRegister<Register>());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003192 } else {
3193 DCHECK(second.IsStackSlot());
Mark Mendell4a2aa4a2015-07-27 16:13:10 -04003194 DCHECK(first.Equals(out));
Roland Levillain271ab9c2014-11-27 15:23:57 +00003195 __ imull(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Calin Juravle34bacdf2014-10-07 20:23:36 +01003196 }
3197 break;
Calin Juravle34bacdf2014-10-07 20:23:36 +01003198
3199 case Primitive::kPrimLong: {
Calin Juravle34bacdf2014-10-07 20:23:36 +01003200 Register in1_hi = first.AsRegisterPairHigh<Register>();
3201 Register in1_lo = first.AsRegisterPairLow<Register>();
Roland Levillain271ab9c2014-11-27 15:23:57 +00003202 Register eax = locations->GetTemp(0).AsRegister<Register>();
3203 Register edx = locations->GetTemp(1).AsRegister<Register>();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003204
3205 DCHECK_EQ(EAX, eax);
3206 DCHECK_EQ(EDX, edx);
3207
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003208 // input: in1 - 64 bits, in2 - 64 bits.
Calin Juravle34bacdf2014-10-07 20:23:36 +01003209 // output: in1
3210 // formula: in1.hi : in1.lo = (in1.lo * in2.hi + in1.hi * in2.lo)* 2^32 + in1.lo * in2.lo
3211 // parts: in1.hi = in1.lo * in2.hi + in1.hi * in2.lo + (in1.lo * in2.lo)[63:32]
3212 // parts: in1.lo = (in1.lo * in2.lo)[31:0]
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003213 if (second.IsConstant()) {
3214 DCHECK(second.GetConstant()->IsLongConstant());
Calin Juravle34bacdf2014-10-07 20:23:36 +01003215
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003216 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
3217 int32_t low_value = Low32Bits(value);
3218 int32_t high_value = High32Bits(value);
3219 Immediate low(low_value);
3220 Immediate high(high_value);
3221
3222 __ movl(eax, high);
3223 // eax <- in1.lo * in2.hi
3224 __ imull(eax, in1_lo);
3225 // in1.hi <- in1.hi * in2.lo
3226 __ imull(in1_hi, low);
3227 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3228 __ addl(in1_hi, eax);
3229 // move in2_lo to eax to prepare for double precision
3230 __ movl(eax, low);
3231 // edx:eax <- in1.lo * in2.lo
3232 __ mull(in1_lo);
3233 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3234 __ addl(in1_hi, edx);
3235 // in1.lo <- (in1.lo * in2.lo)[31:0];
3236 __ movl(in1_lo, eax);
3237 } else if (second.IsRegisterPair()) {
3238 Register in2_hi = second.AsRegisterPairHigh<Register>();
3239 Register in2_lo = second.AsRegisterPairLow<Register>();
3240
3241 __ movl(eax, in2_hi);
3242 // eax <- in1.lo * in2.hi
3243 __ imull(eax, in1_lo);
3244 // in1.hi <- in1.hi * in2.lo
3245 __ imull(in1_hi, in2_lo);
3246 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3247 __ addl(in1_hi, eax);
3248 // move in1_lo to eax to prepare for double precision
3249 __ movl(eax, in1_lo);
3250 // edx:eax <- in1.lo * in2.lo
3251 __ mull(in2_lo);
3252 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3253 __ addl(in1_hi, edx);
3254 // in1.lo <- (in1.lo * in2.lo)[31:0];
3255 __ movl(in1_lo, eax);
3256 } else {
3257 DCHECK(second.IsDoubleStackSlot()) << second;
3258 Address in2_hi(ESP, second.GetHighStackIndex(kX86WordSize));
3259 Address in2_lo(ESP, second.GetStackIndex());
3260
3261 __ movl(eax, in2_hi);
3262 // eax <- in1.lo * in2.hi
3263 __ imull(eax, in1_lo);
3264 // in1.hi <- in1.hi * in2.lo
3265 __ imull(in1_hi, in2_lo);
3266 // in1.hi <- in1.lo * in2.hi + in1.hi * in2.lo
3267 __ addl(in1_hi, eax);
3268 // move in1_lo to eax to prepare for double precision
3269 __ movl(eax, in1_lo);
3270 // edx:eax <- in1.lo * in2.lo
3271 __ mull(in2_lo);
3272 // in1.hi <- in2.hi * in1.lo + in2.lo * in1.hi + (in1.lo * in2.lo)[63:32]
3273 __ addl(in1_hi, edx);
3274 // in1.lo <- (in1.lo * in2.lo)[31:0];
3275 __ movl(in1_lo, eax);
3276 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003277
3278 break;
3279 }
3280
Calin Juravleb5bfa962014-10-21 18:02:24 +01003281 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003282 DCHECK(first.Equals(locations->Out()));
3283 if (second.IsFpuRegister()) {
3284 __ mulss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3285 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3286 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003287 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003288 __ mulss(first.AsFpuRegister<XmmRegister>(),
3289 codegen_->LiteralFloatAddress(
3290 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3291 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3292 } else {
3293 DCHECK(second.IsStackSlot());
3294 __ mulss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3295 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003296 break;
Calin Juravleb5bfa962014-10-21 18:02:24 +01003297 }
3298
3299 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003300 DCHECK(first.Equals(locations->Out()));
3301 if (second.IsFpuRegister()) {
3302 __ mulsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3303 } else if (mul->InputAt(1)->IsX86LoadFromConstantTable()) {
3304 HX86LoadFromConstantTable* const_area = mul->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003305 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003306 __ mulsd(first.AsFpuRegister<XmmRegister>(),
3307 codegen_->LiteralDoubleAddress(
3308 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3309 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3310 } else {
3311 DCHECK(second.IsDoubleStackSlot());
3312 __ mulsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3313 }
Calin Juravleb5bfa962014-10-21 18:02:24 +01003314 break;
3315 }
Calin Juravle34bacdf2014-10-07 20:23:36 +01003316
3317 default:
Calin Juravleb5bfa962014-10-21 18:02:24 +01003318 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
Calin Juravle34bacdf2014-10-07 20:23:36 +01003319 }
3320}
3321
Roland Levillain232ade02015-04-20 15:14:36 +01003322void InstructionCodeGeneratorX86::PushOntoFPStack(Location source,
3323 uint32_t temp_offset,
3324 uint32_t stack_adjustment,
3325 bool is_fp,
3326 bool is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003327 if (source.IsStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003328 DCHECK(!is_wide);
3329 if (is_fp) {
3330 __ flds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3331 } else {
3332 __ filds(Address(ESP, source.GetStackIndex() + stack_adjustment));
3333 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003334 } else if (source.IsDoubleStackSlot()) {
Roland Levillain232ade02015-04-20 15:14:36 +01003335 DCHECK(is_wide);
3336 if (is_fp) {
3337 __ fldl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3338 } else {
3339 __ fildl(Address(ESP, source.GetStackIndex() + stack_adjustment));
3340 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003341 } else {
3342 // Write the value to the temporary location on the stack and load to FP stack.
Roland Levillain232ade02015-04-20 15:14:36 +01003343 if (!is_wide) {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003344 Location stack_temp = Location::StackSlot(temp_offset);
3345 codegen_->Move32(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003346 if (is_fp) {
3347 __ flds(Address(ESP, temp_offset));
3348 } else {
3349 __ filds(Address(ESP, temp_offset));
3350 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003351 } else {
3352 Location stack_temp = Location::DoubleStackSlot(temp_offset);
3353 codegen_->Move64(stack_temp, source);
Roland Levillain232ade02015-04-20 15:14:36 +01003354 if (is_fp) {
3355 __ fldl(Address(ESP, temp_offset));
3356 } else {
3357 __ fildl(Address(ESP, temp_offset));
3358 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003359 }
3360 }
3361}
3362
3363void InstructionCodeGeneratorX86::GenerateRemFP(HRem *rem) {
3364 Primitive::Type type = rem->GetResultType();
3365 bool is_float = type == Primitive::kPrimFloat;
3366 size_t elem_size = Primitive::ComponentSize(type);
3367 LocationSummary* locations = rem->GetLocations();
3368 Location first = locations->InAt(0);
3369 Location second = locations->InAt(1);
3370 Location out = locations->Out();
3371
3372 // Create stack space for 2 elements.
3373 // TODO: enhance register allocator to ask for stack temporaries.
3374 __ subl(ESP, Immediate(2 * elem_size));
3375
3376 // Load the values to the FP stack in reverse order, using temporaries if needed.
Roland Levillain232ade02015-04-20 15:14:36 +01003377 const bool is_wide = !is_float;
3378 PushOntoFPStack(second, elem_size, 2 * elem_size, /* is_fp */ true, is_wide);
3379 PushOntoFPStack(first, 0, 2 * elem_size, /* is_fp */ true, is_wide);
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003380
3381 // Loop doing FPREM until we stabilize.
Mark Mendell0c9497d2015-08-21 09:30:05 -04003382 NearLabel retry;
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003383 __ Bind(&retry);
3384 __ fprem();
3385
3386 // Move FP status to AX.
3387 __ fstsw();
3388
3389 // And see if the argument reduction is complete. This is signaled by the
3390 // C2 FPU flag bit set to 0.
3391 __ andl(EAX, Immediate(kC2ConditionMask));
3392 __ j(kNotEqual, &retry);
3393
3394 // We have settled on the final value. Retrieve it into an XMM register.
3395 // Store FP top of stack to real stack.
3396 if (is_float) {
3397 __ fsts(Address(ESP, 0));
3398 } else {
3399 __ fstl(Address(ESP, 0));
3400 }
3401
3402 // Pop the 2 items from the FP stack.
3403 __ fucompp();
3404
3405 // Load the value from the stack into an XMM register.
3406 DCHECK(out.IsFpuRegister()) << out;
3407 if (is_float) {
3408 __ movss(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3409 } else {
3410 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
3411 }
3412
3413 // And remove the temporary stack space we allocated.
3414 __ addl(ESP, Immediate(2 * elem_size));
3415}
3416
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003417
3418void InstructionCodeGeneratorX86::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3419 DCHECK(instruction->IsDiv() || instruction->IsRem());
3420
3421 LocationSummary* locations = instruction->GetLocations();
3422 DCHECK(locations->InAt(1).IsConstant());
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003423 DCHECK(locations->InAt(1).GetConstant()->IsIntConstant());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003424
3425 Register out_register = locations->Out().AsRegister<Register>();
3426 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003427 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003428
3429 DCHECK(imm == 1 || imm == -1);
3430
3431 if (instruction->IsRem()) {
3432 __ xorl(out_register, out_register);
3433 } else {
3434 __ movl(out_register, input_register);
3435 if (imm == -1) {
3436 __ negl(out_register);
3437 }
3438 }
3439}
3440
3441
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003442void InstructionCodeGeneratorX86::DivByPowerOfTwo(HDiv* instruction) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003443 LocationSummary* locations = instruction->GetLocations();
3444
3445 Register out_register = locations->Out().AsRegister<Register>();
3446 Register input_register = locations->InAt(0).AsRegister<Register>();
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003447 int32_t imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003448 DCHECK(IsPowerOfTwo(AbsOrMin(imm)));
3449 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003450
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003451 Register num = locations->GetTemp(0).AsRegister<Register>();
3452
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003453 __ leal(num, Address(input_register, abs_imm - 1));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003454 __ testl(input_register, input_register);
3455 __ cmovl(kGreaterEqual, num, input_register);
3456 int shift = CTZ(imm);
3457 __ sarl(num, Immediate(shift));
3458
3459 if (imm < 0) {
3460 __ negl(num);
3461 }
3462
3463 __ movl(out_register, num);
3464}
3465
3466void InstructionCodeGeneratorX86::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3467 DCHECK(instruction->IsDiv() || instruction->IsRem());
3468
3469 LocationSummary* locations = instruction->GetLocations();
3470 int imm = locations->InAt(1).GetConstant()->AsIntConstant()->GetValue();
3471
3472 Register eax = locations->InAt(0).AsRegister<Register>();
3473 Register out = locations->Out().AsRegister<Register>();
3474 Register num;
3475 Register edx;
3476
3477 if (instruction->IsDiv()) {
3478 edx = locations->GetTemp(0).AsRegister<Register>();
3479 num = locations->GetTemp(1).AsRegister<Register>();
3480 } else {
3481 edx = locations->Out().AsRegister<Register>();
3482 num = locations->GetTemp(0).AsRegister<Register>();
3483 }
3484
3485 DCHECK_EQ(EAX, eax);
3486 DCHECK_EQ(EDX, edx);
3487 if (instruction->IsDiv()) {
3488 DCHECK_EQ(EAX, out);
3489 } else {
3490 DCHECK_EQ(EDX, out);
3491 }
3492
3493 int64_t magic;
3494 int shift;
3495 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3496
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003497 // Save the numerator.
3498 __ movl(num, eax);
3499
3500 // EAX = magic
3501 __ movl(eax, Immediate(magic));
3502
3503 // EDX:EAX = magic * numerator
3504 __ imull(num);
3505
3506 if (imm > 0 && magic < 0) {
3507 // EDX += num
3508 __ addl(edx, num);
3509 } else if (imm < 0 && magic > 0) {
3510 __ subl(edx, num);
3511 }
3512
3513 // Shift if needed.
3514 if (shift != 0) {
3515 __ sarl(edx, Immediate(shift));
3516 }
3517
3518 // EDX += 1 if EDX < 0
3519 __ movl(eax, edx);
3520 __ shrl(edx, Immediate(31));
3521 __ addl(edx, eax);
3522
3523 if (instruction->IsRem()) {
3524 __ movl(eax, num);
3525 __ imull(edx, Immediate(imm));
3526 __ subl(eax, edx);
3527 __ movl(edx, eax);
3528 } else {
3529 __ movl(eax, edx);
3530 }
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003531}
3532
Calin Juravlebacfec32014-11-14 15:54:36 +00003533void InstructionCodeGeneratorX86::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3534 DCHECK(instruction->IsDiv() || instruction->IsRem());
3535
3536 LocationSummary* locations = instruction->GetLocations();
3537 Location out = locations->Out();
3538 Location first = locations->InAt(0);
3539 Location second = locations->InAt(1);
3540 bool is_div = instruction->IsDiv();
3541
3542 switch (instruction->GetResultType()) {
3543 case Primitive::kPrimInt: {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003544 DCHECK_EQ(EAX, first.AsRegister<Register>());
3545 DCHECK_EQ(is_div ? EAX : EDX, out.AsRegister<Register>());
Calin Juravlebacfec32014-11-14 15:54:36 +00003546
Vladimir Marko13c86fd2015-11-11 12:37:46 +00003547 if (second.IsConstant()) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003548 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003549
3550 if (imm == 0) {
3551 // Do not generate anything for 0. DivZeroCheck would forbid any generated code.
3552 } else if (imm == 1 || imm == -1) {
3553 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003554 } else if (is_div && IsPowerOfTwo(AbsOrMin(imm))) {
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003555 DivByPowerOfTwo(instruction->AsDiv());
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003556 } else {
3557 DCHECK(imm <= -2 || imm >= 2);
3558 GenerateDivRemWithAnyConstant(instruction);
3559 }
3560 } else {
David Srbecky9cd6d372016-02-09 15:24:47 +00003561 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivRemMinusOneSlowPathX86(
3562 instruction, out.AsRegister<Register>(), is_div);
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003563 codegen_->AddSlowPath(slow_path);
Calin Juravlebacfec32014-11-14 15:54:36 +00003564
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003565 Register second_reg = second.AsRegister<Register>();
3566 // 0x80000000/-1 triggers an arithmetic exception!
3567 // Dividing by -1 is actually negation and -0x800000000 = 0x80000000 so
3568 // it's safe to just use negl instead of more complex comparisons.
Calin Juravlebacfec32014-11-14 15:54:36 +00003569
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003570 __ cmpl(second_reg, Immediate(-1));
3571 __ j(kEqual, slow_path->GetEntryLabel());
Calin Juravlebacfec32014-11-14 15:54:36 +00003572
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003573 // edx:eax <- sign-extended of eax
3574 __ cdq();
3575 // eax = quotient, edx = remainder
3576 __ idivl(second_reg);
3577 __ Bind(slow_path->GetExitLabel());
3578 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003579 break;
3580 }
3581
3582 case Primitive::kPrimLong: {
3583 InvokeRuntimeCallingConvention calling_convention;
3584 DCHECK_EQ(calling_convention.GetRegisterAt(0), first.AsRegisterPairLow<Register>());
3585 DCHECK_EQ(calling_convention.GetRegisterAt(1), first.AsRegisterPairHigh<Register>());
3586 DCHECK_EQ(calling_convention.GetRegisterAt(2), second.AsRegisterPairLow<Register>());
3587 DCHECK_EQ(calling_convention.GetRegisterAt(3), second.AsRegisterPairHigh<Register>());
3588 DCHECK_EQ(EAX, out.AsRegisterPairLow<Register>());
3589 DCHECK_EQ(EDX, out.AsRegisterPairHigh<Register>());
3590
3591 if (is_div) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003592 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003593 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003594 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01003595 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00003596 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
Calin Juravlebacfec32014-11-14 15:54:36 +00003597 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003598 break;
3599 }
3600
3601 default:
3602 LOG(FATAL) << "Unexpected type for GenerateDivRemIntegral " << instruction->GetResultType();
3603 }
3604}
3605
Calin Juravle7c4954d2014-10-28 16:57:40 +00003606void LocationsBuilderX86::VisitDiv(HDiv* div) {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003607 LocationSummary::CallKind call_kind = (div->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003608 ? LocationSummary::kCallOnMainOnly
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003609 : LocationSummary::kNoCall;
3610 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3611
Calin Juravle7c4954d2014-10-28 16:57:40 +00003612 switch (div->GetResultType()) {
Calin Juravled0d48522014-11-04 16:40:20 +00003613 case Primitive::kPrimInt: {
3614 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003615 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Calin Juravled0d48522014-11-04 16:40:20 +00003616 locations->SetOut(Location::SameAsFirstInput());
3617 // Intel uses edx:eax as the dividend.
3618 locations->AddTemp(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003619 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3620 // which enforces results to be in EAX and EDX, things are simpler if we use EAX also as
3621 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003622 if (div->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003623 locations->AddTemp(Location::RequiresRegister());
3624 }
Calin Juravled0d48522014-11-04 16:40:20 +00003625 break;
3626 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003627 case Primitive::kPrimLong: {
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003628 InvokeRuntimeCallingConvention calling_convention;
3629 locations->SetInAt(0, Location::RegisterPairLocation(
3630 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3631 locations->SetInAt(1, Location::RegisterPairLocation(
3632 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3633 // Runtime helper puts the result in EAX, EDX.
3634 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
Calin Juravle7c4954d2014-10-28 16:57:40 +00003635 break;
3636 }
3637 case Primitive::kPrimFloat:
3638 case Primitive::kPrimDouble: {
3639 locations->SetInAt(0, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003640 if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3641 DCHECK(div->InputAt(1)->IsEmittedAtUseSite());
Nicolas Geoffray7770a3e2016-02-03 10:13:41 +00003642 } else if (div->InputAt(1)->IsConstant()) {
3643 locations->SetInAt(1, Location::RequiresFpuRegister());
David Brazdilb3e773e2016-01-26 11:28:37 +00003644 } else {
3645 locations->SetInAt(1, Location::Any());
3646 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003647 locations->SetOut(Location::SameAsFirstInput());
3648 break;
3649 }
3650
3651 default:
3652 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3653 }
3654}
3655
3656void InstructionCodeGeneratorX86::VisitDiv(HDiv* div) {
3657 LocationSummary* locations = div->GetLocations();
3658 Location first = locations->InAt(0);
3659 Location second = locations->InAt(1);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003660
3661 switch (div->GetResultType()) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003662 case Primitive::kPrimInt:
Calin Juravle7c4954d2014-10-28 16:57:40 +00003663 case Primitive::kPrimLong: {
Calin Juravlebacfec32014-11-14 15:54:36 +00003664 GenerateDivRemIntegral(div);
Calin Juravle7c4954d2014-10-28 16:57:40 +00003665 break;
3666 }
3667
3668 case Primitive::kPrimFloat: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003669 if (second.IsFpuRegister()) {
3670 __ divss(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3671 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3672 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003673 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003674 __ divss(first.AsFpuRegister<XmmRegister>(),
3675 codegen_->LiteralFloatAddress(
3676 const_area->GetConstant()->AsFloatConstant()->GetValue(),
3677 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3678 } else {
3679 DCHECK(second.IsStackSlot());
3680 __ divss(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3681 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003682 break;
3683 }
3684
3685 case Primitive::kPrimDouble: {
Mark Mendell0616ae02015-04-17 12:49:27 -04003686 if (second.IsFpuRegister()) {
3687 __ divsd(first.AsFpuRegister<XmmRegister>(), second.AsFpuRegister<XmmRegister>());
3688 } else if (div->InputAt(1)->IsX86LoadFromConstantTable()) {
3689 HX86LoadFromConstantTable* const_area = div->InputAt(1)->AsX86LoadFromConstantTable();
David Brazdilb3e773e2016-01-26 11:28:37 +00003690 DCHECK(const_area->IsEmittedAtUseSite());
Mark Mendell0616ae02015-04-17 12:49:27 -04003691 __ divsd(first.AsFpuRegister<XmmRegister>(),
3692 codegen_->LiteralDoubleAddress(
3693 const_area->GetConstant()->AsDoubleConstant()->GetValue(),
3694 const_area->GetLocations()->InAt(0).AsRegister<Register>()));
3695 } else {
3696 DCHECK(second.IsDoubleStackSlot());
3697 __ divsd(first.AsFpuRegister<XmmRegister>(), Address(ESP, second.GetStackIndex()));
3698 }
Calin Juravle7c4954d2014-10-28 16:57:40 +00003699 break;
3700 }
3701
3702 default:
3703 LOG(FATAL) << "Unexpected div type " << div->GetResultType();
3704 }
3705}
3706
Calin Juravlebacfec32014-11-14 15:54:36 +00003707void LocationsBuilderX86::VisitRem(HRem* rem) {
Calin Juravled2ec87d2014-12-08 14:24:46 +00003708 Primitive::Type type = rem->GetResultType();
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003709
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003710 LocationSummary::CallKind call_kind = (rem->GetResultType() == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003711 ? LocationSummary::kCallOnMainOnly
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00003712 : LocationSummary::kNoCall;
3713 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
Calin Juravlebacfec32014-11-14 15:54:36 +00003714
Calin Juravled2ec87d2014-12-08 14:24:46 +00003715 switch (type) {
Calin Juravlebacfec32014-11-14 15:54:36 +00003716 case Primitive::kPrimInt: {
3717 locations->SetInAt(0, Location::RegisterLocation(EAX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003718 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Calin Juravlebacfec32014-11-14 15:54:36 +00003719 locations->SetOut(Location::RegisterLocation(EDX));
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003720 // We need to save the numerator while we tweak eax and edx. As we are using imul in a way
3721 // which enforces results to be in EAX and EDX, things are simpler if we use EDX also as
3722 // output and request another temp.
Guillaume Sanchezb19930c2015-04-09 21:12:15 +01003723 if (rem->InputAt(1)->IsIntConstant()) {
Guillaume Sanchez0f88e872015-03-30 17:55:45 +01003724 locations->AddTemp(Location::RequiresRegister());
3725 }
Calin Juravlebacfec32014-11-14 15:54:36 +00003726 break;
3727 }
3728 case Primitive::kPrimLong: {
3729 InvokeRuntimeCallingConvention calling_convention;
3730 locations->SetInAt(0, Location::RegisterPairLocation(
3731 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3732 locations->SetInAt(1, Location::RegisterPairLocation(
3733 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3734 // Runtime helper puts the result in EAX, EDX.
3735 locations->SetOut(Location::RegisterPairLocation(EAX, EDX));
3736 break;
3737 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003738 case Primitive::kPrimDouble:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003739 case Primitive::kPrimFloat: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003740 locations->SetInAt(0, Location::Any());
3741 locations->SetInAt(1, Location::Any());
3742 locations->SetOut(Location::RequiresFpuRegister());
3743 locations->AddTemp(Location::RegisterLocation(EAX));
Calin Juravlebacfec32014-11-14 15:54:36 +00003744 break;
3745 }
3746
3747 default:
Calin Juravled2ec87d2014-12-08 14:24:46 +00003748 LOG(FATAL) << "Unexpected rem type " << type;
Calin Juravlebacfec32014-11-14 15:54:36 +00003749 }
3750}
3751
3752void InstructionCodeGeneratorX86::VisitRem(HRem* rem) {
3753 Primitive::Type type = rem->GetResultType();
3754 switch (type) {
3755 case Primitive::kPrimInt:
3756 case Primitive::kPrimLong: {
3757 GenerateDivRemIntegral(rem);
3758 break;
3759 }
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003760 case Primitive::kPrimFloat:
Calin Juravlebacfec32014-11-14 15:54:36 +00003761 case Primitive::kPrimDouble: {
Mark Mendell24f2dfa2015-01-14 19:51:45 -05003762 GenerateRemFP(rem);
Calin Juravlebacfec32014-11-14 15:54:36 +00003763 break;
3764 }
3765 default:
3766 LOG(FATAL) << "Unexpected rem type " << type;
3767 }
3768}
3769
Calin Juravled0d48522014-11-04 16:40:20 +00003770void LocationsBuilderX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003771 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003772 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003773 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003774 case Primitive::kPrimByte:
3775 case Primitive::kPrimChar:
3776 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003777 case Primitive::kPrimInt: {
3778 locations->SetInAt(0, Location::Any());
3779 break;
3780 }
3781 case Primitive::kPrimLong: {
3782 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
3783 if (!instruction->IsConstant()) {
3784 locations->AddTemp(Location::RequiresRegister());
3785 }
3786 break;
3787 }
3788 default:
3789 LOG(FATAL) << "Unexpected type for HDivZeroCheck " << instruction->GetType();
3790 }
Calin Juravled0d48522014-11-04 16:40:20 +00003791}
3792
3793void InstructionCodeGeneratorX86::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07003794 SlowPathCode* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathX86(instruction);
Calin Juravled0d48522014-11-04 16:40:20 +00003795 codegen_->AddSlowPath(slow_path);
3796
3797 LocationSummary* locations = instruction->GetLocations();
3798 Location value = locations->InAt(0);
3799
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003800 switch (instruction->GetType()) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003801 case Primitive::kPrimBoolean:
Serguei Katkov8c0676c2015-08-03 13:55:33 +06003802 case Primitive::kPrimByte:
3803 case Primitive::kPrimChar:
3804 case Primitive::kPrimShort:
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003805 case Primitive::kPrimInt: {
3806 if (value.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003807 __ testl(value.AsRegister<Register>(), value.AsRegister<Register>());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003808 __ j(kEqual, slow_path->GetEntryLabel());
3809 } else if (value.IsStackSlot()) {
3810 __ cmpl(Address(ESP, value.GetStackIndex()), Immediate(0));
3811 __ j(kEqual, slow_path->GetEntryLabel());
3812 } else {
3813 DCHECK(value.IsConstant()) << value;
3814 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01003815 __ jmp(slow_path->GetEntryLabel());
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003816 }
3817 }
3818 break;
Calin Juravled0d48522014-11-04 16:40:20 +00003819 }
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003820 case Primitive::kPrimLong: {
3821 if (value.IsRegisterPair()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003822 Register temp = locations->GetTemp(0).AsRegister<Register>();
Calin Juravled6fb6cf2014-11-11 19:07:44 +00003823 __ movl(temp, value.AsRegisterPairLow<Register>());
3824 __ orl(temp, value.AsRegisterPairHigh<Register>());
3825 __ j(kEqual, slow_path->GetEntryLabel());
3826 } else {
3827 DCHECK(value.IsConstant()) << value;
3828 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3829 __ jmp(slow_path->GetEntryLabel());
3830 }
3831 }
3832 break;
3833 }
3834 default:
3835 LOG(FATAL) << "Unexpected type for HDivZeroCheck" << instruction->GetType();
Calin Juravled0d48522014-11-04 16:40:20 +00003836 }
Calin Juravled0d48522014-11-04 16:40:20 +00003837}
3838
Calin Juravle9aec02f2014-11-18 23:06:35 +00003839void LocationsBuilderX86::HandleShift(HBinaryOperation* op) {
3840 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3841
3842 LocationSummary* locations =
3843 new (GetGraph()->GetArena()) LocationSummary(op, LocationSummary::kNoCall);
3844
3845 switch (op->GetResultType()) {
Mark P Mendell73945692015-04-29 14:56:17 +00003846 case Primitive::kPrimInt:
Calin Juravle9aec02f2014-11-18 23:06:35 +00003847 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003848 // Can't have Location::Any() and output SameAsFirstInput()
Calin Juravle9aec02f2014-11-18 23:06:35 +00003849 locations->SetInAt(0, Location::RequiresRegister());
Mark P Mendell73945692015-04-29 14:56:17 +00003850 // The shift count needs to be in CL or a constant.
3851 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, op->InputAt(1)));
Calin Juravle9aec02f2014-11-18 23:06:35 +00003852 locations->SetOut(Location::SameAsFirstInput());
3853 break;
3854 }
3855 default:
3856 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3857 }
3858}
3859
3860void InstructionCodeGeneratorX86::HandleShift(HBinaryOperation* op) {
3861 DCHECK(op->IsShl() || op->IsShr() || op->IsUShr());
3862
3863 LocationSummary* locations = op->GetLocations();
3864 Location first = locations->InAt(0);
3865 Location second = locations->InAt(1);
3866 DCHECK(first.Equals(locations->Out()));
3867
3868 switch (op->GetResultType()) {
3869 case Primitive::kPrimInt: {
Mark P Mendell73945692015-04-29 14:56:17 +00003870 DCHECK(first.IsRegister());
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003871 Register first_reg = first.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003872 if (second.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00003873 Register second_reg = second.AsRegister<Register>();
Calin Juravle9aec02f2014-11-18 23:06:35 +00003874 DCHECK_EQ(ECX, second_reg);
3875 if (op->IsShl()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003876 __ shll(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003877 } else if (op->IsShr()) {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003878 __ sarl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003879 } else {
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003880 __ shrl(first_reg, second_reg);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003881 }
3882 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00003883 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003884 if (shift == 0) {
3885 return;
3886 }
3887 Immediate imm(shift);
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003888 if (op->IsShl()) {
3889 __ shll(first_reg, imm);
3890 } else if (op->IsShr()) {
3891 __ sarl(first_reg, imm);
3892 } else {
3893 __ shrl(first_reg, imm);
Calin Juravle9aec02f2014-11-18 23:06:35 +00003894 }
3895 }
3896 break;
3897 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003898 case Primitive::kPrimLong: {
Mark P Mendell73945692015-04-29 14:56:17 +00003899 if (second.IsRegister()) {
3900 Register second_reg = second.AsRegister<Register>();
3901 DCHECK_EQ(ECX, second_reg);
3902 if (op->IsShl()) {
3903 GenerateShlLong(first, second_reg);
3904 } else if (op->IsShr()) {
3905 GenerateShrLong(first, second_reg);
3906 } else {
3907 GenerateUShrLong(first, second_reg);
3908 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003909 } else {
Mark P Mendell73945692015-04-29 14:56:17 +00003910 // Shift by a constant.
Roland Levillain5b5b9312016-03-22 14:57:31 +00003911 int32_t shift = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Mark P Mendell73945692015-04-29 14:56:17 +00003912 // Nothing to do if the shift is 0, as the input is already the output.
3913 if (shift != 0) {
3914 if (op->IsShl()) {
3915 GenerateShlLong(first, shift);
3916 } else if (op->IsShr()) {
3917 GenerateShrLong(first, shift);
3918 } else {
3919 GenerateUShrLong(first, shift);
3920 }
3921 }
Roland Levillainf9aac1e2015-04-10 18:12:48 +00003922 }
3923 break;
3924 }
Calin Juravle9aec02f2014-11-18 23:06:35 +00003925 default:
3926 LOG(FATAL) << "Unexpected op type " << op->GetResultType();
3927 }
3928}
3929
Mark P Mendell73945692015-04-29 14:56:17 +00003930void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, int shift) {
3931 Register low = loc.AsRegisterPairLow<Register>();
3932 Register high = loc.AsRegisterPairHigh<Register>();
Mark Mendellba56d062015-05-05 21:34:03 -04003933 if (shift == 1) {
3934 // This is just an addition.
3935 __ addl(low, low);
3936 __ adcl(high, high);
3937 } else if (shift == 32) {
Mark P Mendell73945692015-04-29 14:56:17 +00003938 // Shift by 32 is easy. High gets low, and low gets 0.
3939 codegen_->EmitParallelMoves(
3940 loc.ToLow(),
3941 loc.ToHigh(),
3942 Primitive::kPrimInt,
3943 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
3944 loc.ToLow(),
3945 Primitive::kPrimInt);
3946 } else if (shift > 32) {
3947 // Low part becomes 0. High part is low part << (shift-32).
3948 __ movl(high, low);
3949 __ shll(high, Immediate(shift - 32));
3950 __ xorl(low, low);
3951 } else {
3952 // Between 1 and 31.
3953 __ shld(high, low, Immediate(shift));
3954 __ shll(low, Immediate(shift));
3955 }
3956}
3957
Calin Juravle9aec02f2014-11-18 23:06:35 +00003958void InstructionCodeGeneratorX86::GenerateShlLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003959 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003960 __ shld(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>(), shifter);
3961 __ shll(loc.AsRegisterPairLow<Register>(), shifter);
3962 __ testl(shifter, Immediate(32));
3963 __ j(kEqual, &done);
3964 __ movl(loc.AsRegisterPairHigh<Register>(), loc.AsRegisterPairLow<Register>());
3965 __ movl(loc.AsRegisterPairLow<Register>(), Immediate(0));
3966 __ Bind(&done);
3967}
3968
Mark P Mendell73945692015-04-29 14:56:17 +00003969void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, int shift) {
3970 Register low = loc.AsRegisterPairLow<Register>();
3971 Register high = loc.AsRegisterPairHigh<Register>();
3972 if (shift == 32) {
3973 // Need to copy the sign.
3974 DCHECK_NE(low, high);
3975 __ movl(low, high);
3976 __ sarl(high, Immediate(31));
3977 } else if (shift > 32) {
3978 DCHECK_NE(low, high);
3979 // High part becomes sign. Low part is shifted by shift - 32.
3980 __ movl(low, high);
3981 __ sarl(high, Immediate(31));
3982 __ sarl(low, Immediate(shift - 32));
3983 } else {
3984 // Between 1 and 31.
3985 __ shrd(low, high, Immediate(shift));
3986 __ sarl(high, Immediate(shift));
3987 }
3988}
3989
Calin Juravle9aec02f2014-11-18 23:06:35 +00003990void InstructionCodeGeneratorX86::GenerateShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04003991 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00003992 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
3993 __ sarl(loc.AsRegisterPairHigh<Register>(), shifter);
3994 __ testl(shifter, Immediate(32));
3995 __ j(kEqual, &done);
3996 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
3997 __ sarl(loc.AsRegisterPairHigh<Register>(), Immediate(31));
3998 __ Bind(&done);
3999}
4000
Mark P Mendell73945692015-04-29 14:56:17 +00004001void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, int shift) {
4002 Register low = loc.AsRegisterPairLow<Register>();
4003 Register high = loc.AsRegisterPairHigh<Register>();
4004 if (shift == 32) {
4005 // Shift by 32 is easy. Low gets high, and high gets 0.
4006 codegen_->EmitParallelMoves(
4007 loc.ToHigh(),
4008 loc.ToLow(),
4009 Primitive::kPrimInt,
4010 Location::ConstantLocation(GetGraph()->GetIntConstant(0)),
4011 loc.ToHigh(),
4012 Primitive::kPrimInt);
4013 } else if (shift > 32) {
4014 // Low part is high >> (shift - 32). High part becomes 0.
4015 __ movl(low, high);
4016 __ shrl(low, Immediate(shift - 32));
4017 __ xorl(high, high);
4018 } else {
4019 // Between 1 and 31.
4020 __ shrd(low, high, Immediate(shift));
4021 __ shrl(high, Immediate(shift));
4022 }
4023}
4024
Calin Juravle9aec02f2014-11-18 23:06:35 +00004025void InstructionCodeGeneratorX86::GenerateUShrLong(const Location& loc, Register shifter) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004026 NearLabel done;
Calin Juravle9aec02f2014-11-18 23:06:35 +00004027 __ shrd(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>(), shifter);
4028 __ shrl(loc.AsRegisterPairHigh<Register>(), shifter);
4029 __ testl(shifter, Immediate(32));
4030 __ j(kEqual, &done);
4031 __ movl(loc.AsRegisterPairLow<Register>(), loc.AsRegisterPairHigh<Register>());
4032 __ movl(loc.AsRegisterPairHigh<Register>(), Immediate(0));
4033 __ Bind(&done);
4034}
4035
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004036void LocationsBuilderX86::VisitRor(HRor* ror) {
4037 LocationSummary* locations =
4038 new (GetGraph()->GetArena()) LocationSummary(ror, LocationSummary::kNoCall);
4039
4040 switch (ror->GetResultType()) {
4041 case Primitive::kPrimLong:
4042 // Add the temporary needed.
4043 locations->AddTemp(Location::RequiresRegister());
4044 FALLTHROUGH_INTENDED;
4045 case Primitive::kPrimInt:
4046 locations->SetInAt(0, Location::RequiresRegister());
4047 // The shift count needs to be in CL (unless it is a constant).
4048 locations->SetInAt(1, Location::ByteRegisterOrConstant(ECX, ror->InputAt(1)));
4049 locations->SetOut(Location::SameAsFirstInput());
4050 break;
4051 default:
4052 LOG(FATAL) << "Unexpected operation type " << ror->GetResultType();
4053 UNREACHABLE();
4054 }
4055}
4056
4057void InstructionCodeGeneratorX86::VisitRor(HRor* ror) {
4058 LocationSummary* locations = ror->GetLocations();
4059 Location first = locations->InAt(0);
4060 Location second = locations->InAt(1);
4061
4062 if (ror->GetResultType() == Primitive::kPrimInt) {
4063 Register first_reg = first.AsRegister<Register>();
4064 if (second.IsRegister()) {
4065 Register second_reg = second.AsRegister<Register>();
4066 __ rorl(first_reg, second_reg);
4067 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004068 Immediate imm(second.GetConstant()->AsIntConstant()->GetValue() & kMaxIntShiftDistance);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004069 __ rorl(first_reg, imm);
4070 }
4071 return;
4072 }
4073
4074 DCHECK_EQ(ror->GetResultType(), Primitive::kPrimLong);
4075 Register first_reg_lo = first.AsRegisterPairLow<Register>();
4076 Register first_reg_hi = first.AsRegisterPairHigh<Register>();
4077 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
4078 if (second.IsRegister()) {
4079 Register second_reg = second.AsRegister<Register>();
4080 DCHECK_EQ(second_reg, ECX);
4081 __ movl(temp_reg, first_reg_hi);
4082 __ shrd(first_reg_hi, first_reg_lo, second_reg);
4083 __ shrd(first_reg_lo, temp_reg, second_reg);
4084 __ movl(temp_reg, first_reg_hi);
4085 __ testl(second_reg, Immediate(32));
4086 __ cmovl(kNotEqual, first_reg_hi, first_reg_lo);
4087 __ cmovl(kNotEqual, first_reg_lo, temp_reg);
4088 } else {
Roland Levillain5b5b9312016-03-22 14:57:31 +00004089 int32_t shift_amt = second.GetConstant()->AsIntConstant()->GetValue() & kMaxLongShiftDistance;
Scott Wakeling40a04bf2015-12-11 09:50:36 +00004090 if (shift_amt == 0) {
4091 // Already fine.
4092 return;
4093 }
4094 if (shift_amt == 32) {
4095 // Just swap.
4096 __ movl(temp_reg, first_reg_lo);
4097 __ movl(first_reg_lo, first_reg_hi);
4098 __ movl(first_reg_hi, temp_reg);
4099 return;
4100 }
4101
4102 Immediate imm(shift_amt);
4103 // Save the constents of the low value.
4104 __ movl(temp_reg, first_reg_lo);
4105
4106 // Shift right into low, feeding bits from high.
4107 __ shrd(first_reg_lo, first_reg_hi, imm);
4108
4109 // Shift right into high, feeding bits from the original low.
4110 __ shrd(first_reg_hi, temp_reg, imm);
4111
4112 // Swap if needed.
4113 if (shift_amt > 32) {
4114 __ movl(temp_reg, first_reg_lo);
4115 __ movl(first_reg_lo, first_reg_hi);
4116 __ movl(first_reg_hi, temp_reg);
4117 }
4118 }
4119}
4120
Calin Juravle9aec02f2014-11-18 23:06:35 +00004121void LocationsBuilderX86::VisitShl(HShl* shl) {
4122 HandleShift(shl);
4123}
4124
4125void InstructionCodeGeneratorX86::VisitShl(HShl* shl) {
4126 HandleShift(shl);
4127}
4128
4129void LocationsBuilderX86::VisitShr(HShr* shr) {
4130 HandleShift(shr);
4131}
4132
4133void InstructionCodeGeneratorX86::VisitShr(HShr* shr) {
4134 HandleShift(shr);
4135}
4136
4137void LocationsBuilderX86::VisitUShr(HUShr* ushr) {
4138 HandleShift(ushr);
4139}
4140
4141void InstructionCodeGeneratorX86::VisitUShr(HUShr* ushr) {
4142 HandleShift(ushr);
4143}
4144
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004145void LocationsBuilderX86::VisitNewInstance(HNewInstance* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004146 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004147 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004148 locations->SetOut(Location::RegisterLocation(EAX));
David Brazdil6de19382016-01-08 17:37:10 +00004149 if (instruction->IsStringAlloc()) {
4150 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
4151 } else {
4152 InvokeRuntimeCallingConvention calling_convention;
4153 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
4154 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
4155 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004156}
4157
4158void InstructionCodeGeneratorX86::VisitNewInstance(HNewInstance* instruction) {
Roland Levillain4d027112015-07-01 15:41:14 +01004159 // Note: if heap poisoning is enabled, the entry point takes cares
4160 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00004161 if (instruction->IsStringAlloc()) {
4162 // String is allocated through StringFactory. Call NewEmptyString entry point.
4163 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07004164 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00004165 __ fs()->movl(temp, Address::Absolute(QUICK_ENTRY_POINT(pNewEmptyString)));
4166 __ call(Address(temp, code_offset.Int32Value()));
4167 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
4168 } else {
Serban Constantinescuba45db02016-07-12 22:53:02 +01004169 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
David Brazdil6de19382016-01-08 17:37:10 +00004170 CheckEntrypointTypes<kQuickAllocObjectWithAccessCheck, void*, uint32_t, ArtMethod*>();
4171 DCHECK(!codegen_->IsLeafMethod());
4172 }
Nicolas Geoffray2e7038a2014-04-03 18:49:58 +01004173}
4174
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004175void LocationsBuilderX86::VisitNewArray(HNewArray* instruction) {
4176 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004177 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004178 locations->SetOut(Location::RegisterLocation(EAX));
4179 InvokeRuntimeCallingConvention calling_convention;
4180 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Andreas Gampe1cc7dba2014-12-17 18:43:01 -08004181 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Nicolas Geoffray69aa6012015-06-09 10:34:25 +01004182 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004183}
4184
4185void InstructionCodeGeneratorX86::VisitNewArray(HNewArray* instruction) {
4186 InvokeRuntimeCallingConvention calling_convention;
Andreas Gampea5b09a62016-11-17 15:21:22 -08004187 __ movl(calling_convention.GetRegisterAt(0), Immediate(instruction->GetTypeIndex().index_));
Roland Levillain4d027112015-07-01 15:41:14 +01004188 // Note: if heap poisoning is enabled, the entry point takes cares
4189 // of poisoning the reference.
Serban Constantinescuba45db02016-07-12 22:53:02 +01004190 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00004191 CheckEntrypointTypes<kQuickAllocArrayWithAccessCheck, void*, uint32_t, int32_t, ArtMethod*>();
Nicolas Geoffraya3d05a42014-10-20 17:41:32 +01004192 DCHECK(!codegen_->IsLeafMethod());
4193}
4194
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004195void LocationsBuilderX86::VisitParameterValue(HParameterValue* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004196 LocationSummary* locations =
4197 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004198 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
4199 if (location.IsStackSlot()) {
4200 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
4201 } else if (location.IsDoubleStackSlot()) {
4202 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004203 }
Nicolas Geoffraya747a392014-04-17 14:56:23 +01004204 locations->SetOut(location);
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004205}
4206
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01004207void InstructionCodeGeneratorX86::VisitParameterValue(
4208 HParameterValue* instruction ATTRIBUTE_UNUSED) {
4209}
4210
4211void LocationsBuilderX86::VisitCurrentMethod(HCurrentMethod* instruction) {
4212 LocationSummary* locations =
4213 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4214 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
4215}
4216
4217void InstructionCodeGeneratorX86::VisitCurrentMethod(HCurrentMethod* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffrayf583e592014-04-07 13:20:42 +01004218}
4219
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004220void LocationsBuilderX86::VisitClassTableGet(HClassTableGet* instruction) {
4221 LocationSummary* locations =
4222 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4223 locations->SetInAt(0, Location::RequiresRegister());
4224 locations->SetOut(Location::RequiresRegister());
4225}
4226
4227void InstructionCodeGeneratorX86::VisitClassTableGet(HClassTableGet* instruction) {
4228 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00004229 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004230 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004231 instruction->GetIndex(), kX86PointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004232 __ movl(locations->Out().AsRegister<Register>(),
4233 Address(locations->InAt(0).AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004234 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004235 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00004236 instruction->GetIndex(), kX86PointerSize));
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01004237 __ movl(locations->Out().AsRegister<Register>(),
4238 Address(locations->InAt(0).AsRegister<Register>(),
4239 mirror::Class::ImtPtrOffset(kX86PointerSize).Uint32Value()));
4240 // temp = temp->GetImtEntryAt(method_offset);
4241 __ movl(locations->Out().AsRegister<Register>(),
4242 Address(locations->Out().AsRegister<Register>(), method_offset));
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004243 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00004244}
4245
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004246void LocationsBuilderX86::VisitNot(HNot* not_) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004247 LocationSummary* locations =
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004248 new (GetGraph()->GetArena()) LocationSummary(not_, LocationSummary::kNoCall);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004249 locations->SetInAt(0, Location::RequiresRegister());
4250 locations->SetOut(Location::SameAsFirstInput());
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004251}
4252
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004253void InstructionCodeGeneratorX86::VisitNot(HNot* not_) {
4254 LocationSummary* locations = not_->GetLocations();
Roland Levillain70566432014-10-24 16:20:17 +01004255 Location in = locations->InAt(0);
Nicolas Geoffraya7aca372014-04-28 17:47:12 +01004256 Location out = locations->Out();
Roland Levillain70566432014-10-24 16:20:17 +01004257 DCHECK(in.Equals(out));
Nicolas Geoffrayd8ef2e92015-02-24 16:02:06 +00004258 switch (not_->GetResultType()) {
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004259 case Primitive::kPrimInt:
Roland Levillain271ab9c2014-11-27 15:23:57 +00004260 __ notl(out.AsRegister<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004261 break;
4262
4263 case Primitive::kPrimLong:
Roland Levillain70566432014-10-24 16:20:17 +01004264 __ notl(out.AsRegisterPairLow<Register>());
4265 __ notl(out.AsRegisterPairHigh<Register>());
Roland Levillain1cc5f2512014-10-22 18:06:21 +01004266 break;
4267
4268 default:
4269 LOG(FATAL) << "Unimplemented type for not operation " << not_->GetResultType();
4270 }
Nicolas Geoffrayb55f8352014-04-07 15:26:35 +01004271}
4272
David Brazdil66d126e2015-04-03 16:02:44 +01004273void LocationsBuilderX86::VisitBooleanNot(HBooleanNot* bool_not) {
4274 LocationSummary* locations =
4275 new (GetGraph()->GetArena()) LocationSummary(bool_not, LocationSummary::kNoCall);
4276 locations->SetInAt(0, Location::RequiresRegister());
4277 locations->SetOut(Location::SameAsFirstInput());
4278}
4279
4280void InstructionCodeGeneratorX86::VisitBooleanNot(HBooleanNot* bool_not) {
David Brazdil66d126e2015-04-03 16:02:44 +01004281 LocationSummary* locations = bool_not->GetLocations();
4282 Location in = locations->InAt(0);
4283 Location out = locations->Out();
4284 DCHECK(in.Equals(out));
4285 __ xorl(out.AsRegister<Register>(), Immediate(1));
4286}
4287
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004288void LocationsBuilderX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004289 LocationSummary* locations =
4290 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Calin Juravleddb7df22014-11-25 20:56:51 +00004291 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004292 case Primitive::kPrimBoolean:
4293 case Primitive::kPrimByte:
4294 case Primitive::kPrimShort:
4295 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004296 case Primitive::kPrimInt:
Calin Juravleddb7df22014-11-25 20:56:51 +00004297 case Primitive::kPrimLong: {
4298 locations->SetInAt(0, Location::RequiresRegister());
Calin Juravleddb7df22014-11-25 20:56:51 +00004299 locations->SetInAt(1, Location::Any());
4300 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4301 break;
4302 }
4303 case Primitive::kPrimFloat:
4304 case Primitive::kPrimDouble: {
4305 locations->SetInAt(0, Location::RequiresFpuRegister());
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004306 if (compare->InputAt(1)->IsX86LoadFromConstantTable()) {
4307 DCHECK(compare->InputAt(1)->IsEmittedAtUseSite());
4308 } else if (compare->InputAt(1)->IsConstant()) {
4309 locations->SetInAt(1, Location::RequiresFpuRegister());
4310 } else {
4311 locations->SetInAt(1, Location::Any());
4312 }
Calin Juravleddb7df22014-11-25 20:56:51 +00004313 locations->SetOut(Location::RequiresRegister());
4314 break;
4315 }
4316 default:
4317 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
4318 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004319}
4320
4321void InstructionCodeGeneratorX86::VisitCompare(HCompare* compare) {
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004322 LocationSummary* locations = compare->GetLocations();
Roland Levillain271ab9c2014-11-27 15:23:57 +00004323 Register out = locations->Out().AsRegister<Register>();
Calin Juravleddb7df22014-11-25 20:56:51 +00004324 Location left = locations->InAt(0);
4325 Location right = locations->InAt(1);
4326
Mark Mendell0c9497d2015-08-21 09:30:05 -04004327 NearLabel less, greater, done;
Aart Bika19616e2016-02-01 18:57:58 -08004328 Condition less_cond = kLess;
4329
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004330 switch (compare->InputAt(0)->GetType()) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00004331 case Primitive::kPrimBoolean:
4332 case Primitive::kPrimByte:
4333 case Primitive::kPrimShort:
4334 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08004335 case Primitive::kPrimInt: {
Roland Levillain0b671c02016-08-19 12:02:34 +01004336 codegen_->GenerateIntCompare(left, right);
Aart Bika19616e2016-02-01 18:57:58 -08004337 break;
4338 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004339 case Primitive::kPrimLong: {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004340 Register left_low = left.AsRegisterPairLow<Register>();
4341 Register left_high = left.AsRegisterPairHigh<Register>();
4342 int32_t val_low = 0;
4343 int32_t val_high = 0;
4344 bool right_is_const = false;
4345
4346 if (right.IsConstant()) {
4347 DCHECK(right.GetConstant()->IsLongConstant());
4348 right_is_const = true;
4349 int64_t val = right.GetConstant()->AsLongConstant()->GetValue();
4350 val_low = Low32Bits(val);
4351 val_high = High32Bits(val);
4352 }
4353
Calin Juravleddb7df22014-11-25 20:56:51 +00004354 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004355 __ cmpl(left_high, right.AsRegisterPairHigh<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004356 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004357 __ cmpl(left_high, Address(ESP, right.GetHighStackIndex(kX86WordSize)));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004358 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004359 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004360 codegen_->Compare32BitValue(left_high, val_high);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004361 }
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004362 __ j(kLess, &less); // Signed compare.
4363 __ j(kGreater, &greater); // Signed compare.
Nicolas Geoffray56b9ee62014-10-09 11:47:51 +01004364 if (right.IsRegisterPair()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004365 __ cmpl(left_low, right.AsRegisterPairLow<Register>());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004366 } else if (right.IsDoubleStackSlot()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004367 __ cmpl(left_low, Address(ESP, right.GetStackIndex()));
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004368 } else {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04004369 DCHECK(right_is_const) << right;
Aart Bika19616e2016-02-01 18:57:58 -08004370 codegen_->Compare32BitValue(left_low, val_low);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004371 }
Aart Bika19616e2016-02-01 18:57:58 -08004372 less_cond = kBelow; // for CF (unsigned).
Calin Juravleddb7df22014-11-25 20:56:51 +00004373 break;
4374 }
4375 case Primitive::kPrimFloat: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004376 GenerateFPCompare(left, right, compare, false);
Calin Juravleddb7df22014-11-25 20:56:51 +00004377 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004378 less_cond = kBelow; // for CF (floats).
Calin Juravleddb7df22014-11-25 20:56:51 +00004379 break;
4380 }
4381 case Primitive::kPrimDouble: {
Mark P Mendell2f10a5f2016-01-25 14:47:50 +00004382 GenerateFPCompare(left, right, compare, true);
Calin Juravleddb7df22014-11-25 20:56:51 +00004383 __ j(kUnordered, compare->IsGtBias() ? &greater : &less);
Aart Bika19616e2016-02-01 18:57:58 -08004384 less_cond = kBelow; // for CF (floats).
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004385 break;
4386 }
4387 default:
Calin Juravleddb7df22014-11-25 20:56:51 +00004388 LOG(FATAL) << "Unexpected type for compare operation " << compare->InputAt(0)->GetType();
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004389 }
Aart Bika19616e2016-02-01 18:57:58 -08004390
Calin Juravleddb7df22014-11-25 20:56:51 +00004391 __ movl(out, Immediate(0));
4392 __ j(kEqual, &done);
Aart Bika19616e2016-02-01 18:57:58 -08004393 __ j(less_cond, &less);
Calin Juravleddb7df22014-11-25 20:56:51 +00004394
4395 __ Bind(&greater);
4396 __ movl(out, Immediate(1));
4397 __ jmp(&done);
4398
4399 __ Bind(&less);
4400 __ movl(out, Immediate(-1));
4401
4402 __ Bind(&done);
Nicolas Geoffray412f10c2014-06-19 10:00:34 +01004403}
4404
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004405void LocationsBuilderX86::VisitPhi(HPhi* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01004406 LocationSummary* locations =
4407 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
Vladimir Marko372f10e2016-05-17 16:30:10 +01004408 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Nicolas Geoffray31d76b42014-06-09 15:02:22 +01004409 locations->SetInAt(i, Location::Any());
4410 }
4411 locations->SetOut(Location::Any());
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004412}
4413
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01004414void InstructionCodeGeneratorX86::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01004415 LOG(FATAL) << "Unreachable";
Nicolas Geoffrayc32e7702014-04-24 12:43:16 +01004416}
4417
Roland Levillain7c1559a2015-12-15 10:55:36 +00004418void CodeGeneratorX86::GenerateMemoryBarrier(MemBarrierKind kind) {
Calin Juravle52c48962014-12-16 17:02:57 +00004419 /*
4420 * According to the JSR-133 Cookbook, for x86 only StoreLoad/AnyAny barriers need memory fence.
4421 * All other barriers (LoadAny, AnyStore, StoreStore) are nops due to the x86 memory model.
4422 * For those cases, all we need to ensure is that there is a scheduling barrier in place.
4423 */
4424 switch (kind) {
4425 case MemBarrierKind::kAnyAny: {
Mark P Mendell17077d82015-12-16 19:15:59 +00004426 MemoryFence();
Calin Juravle52c48962014-12-16 17:02:57 +00004427 break;
4428 }
4429 case MemBarrierKind::kAnyStore:
4430 case MemBarrierKind::kLoadAny:
4431 case MemBarrierKind::kStoreStore: {
4432 // nop
4433 break;
4434 }
Mark Mendell7aa04a12016-01-27 22:39:07 -05004435 case MemBarrierKind::kNTStoreStore:
4436 // Non-Temporal Store/Store needs an explicit fence.
4437 MemoryFence(/* non-temporal */ true);
4438 break;
Nicolas Geoffray1a43dd72014-07-17 15:15:34 +01004439 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004440}
4441
Vladimir Markodc151b22015-10-15 18:02:30 +01004442HInvokeStaticOrDirect::DispatchInfo CodeGeneratorX86::GetSupportedInvokeStaticOrDirectDispatch(
4443 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004444 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004445 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
4446
4447 // We disable pc-relative load when there is an irreducible loop, as the optimization
4448 // is incompatible with it.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004449 // TODO: Create as many X86ComputeBaseMethodAddress instructions
4450 // as needed for methods with irreducible loops.
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004451 if (GetGraph()->HasIrreducibleLoops() &&
4452 (dispatch_info.method_load_kind ==
4453 HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative)) {
4454 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
4455 }
4456 switch (dispatch_info.code_ptr_location) {
Vladimir Markodc151b22015-10-15 18:02:30 +01004457 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4458 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
4459 // For direct code, we actually prefer to call via the code pointer from ArtMethod*.
4460 // (Though the direct CALL ptr16:32 is available for consideration).
4461 return HInvokeStaticOrDirect::DispatchInfo {
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004462 dispatch_info.method_load_kind,
Vladimir Markodc151b22015-10-15 18:02:30 +01004463 HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod,
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004464 dispatch_info.method_load_data,
Vladimir Markodc151b22015-10-15 18:02:30 +01004465 0u
4466 };
4467 default:
Nicolas Geoffray15bd2282016-01-05 15:55:41 +00004468 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01004469 }
4470}
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004471
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004472Register CodeGeneratorX86::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
4473 Register temp) {
4474 DCHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
Vladimir Markoc53c0792015-11-19 15:48:33 +00004475 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004476 if (!invoke->GetLocations()->Intrinsified()) {
4477 return location.AsRegister<Register>();
4478 }
4479 // For intrinsics we allow any location, so it may be on the stack.
4480 if (!location.IsRegister()) {
4481 __ movl(temp, Address(ESP, location.GetStackIndex()));
4482 return temp;
4483 }
4484 // For register locations, check if the register was saved. If so, get it from the stack.
4485 // Note: There is a chance that the register was saved but not overwritten, so we could
4486 // save one load. However, since this is just an intrinsic slow path we prefer this
4487 // simple and more robust approach rather that trying to determine if that's the case.
4488 SlowPathCode* slow_path = GetCurrentSlowPath();
Serguei Katkov288c7a82016-05-16 11:53:15 +06004489 if (slow_path != nullptr) {
4490 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
4491 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
4492 __ movl(temp, Address(ESP, stack_offset));
4493 return temp;
4494 }
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004495 }
4496 return location.AsRegister<Register>();
4497}
4498
Serguei Katkov288c7a82016-05-16 11:53:15 +06004499Location CodeGeneratorX86::GenerateCalleeMethodStaticOrDirectCall(HInvokeStaticOrDirect* invoke,
4500 Location temp) {
Vladimir Marko58155012015-08-19 12:49:41 +00004501 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
4502 switch (invoke->GetMethodLoadKind()) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004503 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Vladimir Marko58155012015-08-19 12:49:41 +00004504 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004505 uint32_t offset =
4506 GetThreadOffset<kX86PointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
4507 __ fs()->movl(temp.AsRegister<Register>(), Address::Absolute(offset));
Vladimir Marko58155012015-08-19 12:49:41 +00004508 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01004509 }
Vladimir Marko58155012015-08-19 12:49:41 +00004510 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00004511 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004512 break;
4513 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
4514 __ movl(temp.AsRegister<Register>(), Immediate(invoke->GetMethodAddress()));
4515 break;
4516 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup:
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004517 __ movl(temp.AsRegister<Register>(), Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00004518 method_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4519 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004520 __ Bind(&method_patches_.back().label); // Bind the label at the end of the "movl" insn.
4521 break;
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004522 case HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative: {
4523 Register base_reg = GetInvokeStaticOrDirectExtraParameter(invoke,
4524 temp.AsRegister<Register>());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004525 __ movl(temp.AsRegister<Register>(), Address(base_reg, kDummy32BitOffset));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004526 // Bind a new fixup label at the end of the "movl" insn.
4527 uint32_t offset = invoke->GetDexCacheArrayOffset();
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01004528 __ Bind(NewPcRelativeDexCacheArrayPatch(invoke->GetDexFile(), offset));
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004529 break;
4530 }
Vladimir Marko58155012015-08-19 12:49:41 +00004531 case HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod: {
Vladimir Markoc53c0792015-11-19 15:48:33 +00004532 Location current_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Vladimir Marko58155012015-08-19 12:49:41 +00004533 Register method_reg;
4534 Register reg = temp.AsRegister<Register>();
4535 if (current_method.IsRegister()) {
4536 method_reg = current_method.AsRegister<Register>();
4537 } else {
David Brazdil58282f42016-01-14 12:45:10 +00004538 DCHECK(invoke->GetLocations()->Intrinsified());
Vladimir Marko58155012015-08-19 12:49:41 +00004539 DCHECK(!current_method.IsValid());
4540 method_reg = reg;
4541 __ movl(reg, Address(ESP, kCurrentMethodStackOffset));
4542 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00004543 // /* ArtMethod*[] */ temp = temp.ptr_sized_fields_->dex_cache_resolved_methods_;
Vladimir Marko05792b92015-08-03 11:56:49 +01004544 __ movl(reg, Address(method_reg,
4545 ArtMethod::DexCacheResolvedMethodsOffset(kX86PointerSize).Int32Value()));
Vladimir Marko40ecb122016-04-06 17:33:41 +01004546 // temp = temp[index_in_cache];
4547 // Note: Don't use invoke->GetTargetMethod() as it may point to a different dex file.
4548 uint32_t index_in_cache = invoke->GetDexMethodIndex();
Vladimir Marko58155012015-08-19 12:49:41 +00004549 __ movl(reg, Address(reg, CodeGenerator::GetCachePointerOffset(index_in_cache)));
4550 break;
Vladimir Marko9b688a02015-05-06 14:12:42 +01004551 }
Vladimir Marko58155012015-08-19 12:49:41 +00004552 }
Serguei Katkov288c7a82016-05-16 11:53:15 +06004553 return callee_method;
4554}
4555
4556void CodeGeneratorX86::GenerateStaticOrDirectCall(HInvokeStaticOrDirect* invoke, Location temp) {
4557 Location callee_method = GenerateCalleeMethodStaticOrDirectCall(invoke, temp);
Vladimir Marko58155012015-08-19 12:49:41 +00004558
4559 switch (invoke->GetCodePtrLocation()) {
4560 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
4561 __ call(GetFrameEntryLabel());
4562 break;
4563 case HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004564 relative_call_patches_.emplace_back(*invoke->GetTargetMethod().dex_file,
4565 invoke->GetTargetMethod().dex_method_index);
Vladimir Marko58155012015-08-19 12:49:41 +00004566 Label* label = &relative_call_patches_.back().label;
4567 __ call(label); // Bind to the patch label, override at link time.
4568 __ Bind(label); // Bind the label at the end of the "call" insn.
4569 break;
4570 }
4571 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup:
4572 case HInvokeStaticOrDirect::CodePtrLocation::kCallDirect:
Vladimir Markodc151b22015-10-15 18:02:30 +01004573 // Filtered out by GetSupportedInvokeStaticOrDirectDispatch().
4574 LOG(FATAL) << "Unsupported";
4575 UNREACHABLE();
Vladimir Marko58155012015-08-19 12:49:41 +00004576 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
4577 // (callee_method + offset_of_quick_compiled_code)()
4578 __ call(Address(callee_method.AsRegister<Register>(),
4579 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07004580 kX86PointerSize).Int32Value()));
Vladimir Marko58155012015-08-19 12:49:41 +00004581 break;
Mark Mendell09ed1a32015-03-25 08:30:06 -04004582 }
4583
4584 DCHECK(!IsLeafMethod());
Mark Mendell09ed1a32015-03-25 08:30:06 -04004585}
4586
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004587void CodeGeneratorX86::GenerateVirtualCall(HInvokeVirtual* invoke, Location temp_in) {
4588 Register temp = temp_in.AsRegister<Register>();
4589 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
4590 invoke->GetVTableIndex(), kX86PointerSize).Uint32Value();
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004591
4592 // Use the calling convention instead of the location of the receiver, as
4593 // intrinsics may have put the receiver in a different register. In the intrinsics
4594 // slow path, the arguments have been moved to the right place, so here we are
4595 // guaranteed that the receiver is the first register of the calling convention.
4596 InvokeDexCallingConvention calling_convention;
4597 Register receiver = calling_convention.GetRegisterAt(0);
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004598 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004599 // /* HeapReference<Class> */ temp = receiver->klass_
Nicolas Geoffraye5234232015-12-02 09:06:11 +00004600 __ movl(temp, Address(receiver, class_offset));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004601 MaybeRecordImplicitNullCheck(invoke);
Roland Levillain0d5a2812015-11-13 10:07:31 +00004602 // Instead of simply (possibly) unpoisoning `temp` here, we should
4603 // emit a read barrier for the previous class reference load.
4604 // However this is not required in practice, as this is an
4605 // intermediate/temporary reference and because the current
4606 // concurrent copying collector keeps the from-space memory
4607 // intact/accessible until the end of the marking phase (the
4608 // concurrent copying collector may not in the future).
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004609 __ MaybeUnpoisonHeapReference(temp);
4610 // temp = temp->GetMethodAt(method_offset);
4611 __ movl(temp, Address(temp, method_offset));
4612 // call temp->GetEntryPoint();
4613 __ call(Address(
Andreas Gampe542451c2016-07-26 09:02:02 -07004614 temp, ArtMethod::EntryPointFromQuickCompiledCodeOffset(kX86PointerSize).Int32Value()));
Andreas Gampebfb5ba92015-09-01 15:45:02 +00004615}
4616
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004617void CodeGeneratorX86::RecordSimplePatch() {
4618 if (GetCompilerOptions().GetIncludePatchInformation()) {
4619 simple_patches_.emplace_back();
4620 __ Bind(&simple_patches_.back());
4621 }
4622}
4623
Vladimir Markoaad75c62016-10-03 08:46:48 +00004624void CodeGeneratorX86::RecordBootStringPatch(HLoadString* load_string) {
4625 DCHECK(GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004626 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004627 __ Bind(&string_patches_.back().label);
4628}
4629
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004630void CodeGeneratorX86::RecordTypePatch(HLoadClass* load_class) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08004631 type_patches_.emplace_back(load_class->GetDexFile(), load_class->GetTypeIndex().index_);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004632 __ Bind(&type_patches_.back().label);
4633}
4634
Vladimir Markoaad75c62016-10-03 08:46:48 +00004635Label* CodeGeneratorX86::NewStringBssEntryPatch(HLoadString* load_string) {
4636 DCHECK(!GetCompilerOptions().IsBootImage());
Andreas Gampe8a0128a2016-11-28 07:38:35 -08004637 string_patches_.emplace_back(load_string->GetDexFile(), load_string->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004638 return &string_patches_.back().label;
4639}
4640
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004641Label* CodeGeneratorX86::NewPcRelativeDexCacheArrayPatch(const DexFile& dex_file,
4642 uint32_t element_offset) {
4643 // Add the patch entry and bind its label at the end of the instruction.
4644 pc_relative_dex_cache_patches_.emplace_back(dex_file, element_offset);
4645 return &pc_relative_dex_cache_patches_.back().label;
4646}
4647
Vladimir Markoaad75c62016-10-03 08:46:48 +00004648// The label points to the end of the "movl" or another instruction but the literal offset
4649// for method patch needs to point to the embedded constant which occupies the last 4 bytes.
4650constexpr uint32_t kLabelPositionToLiteralOffsetAdjustment = 4u;
4651
4652template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
4653inline void CodeGeneratorX86::EmitPcRelativeLinkerPatches(
4654 const ArenaDeque<PatchInfo<Label>>& infos,
4655 ArenaVector<LinkerPatch>* linker_patches) {
4656 for (const PatchInfo<Label>& info : infos) {
4657 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4658 linker_patches->push_back(
4659 Factory(literal_offset, &info.dex_file, GetMethodAddressOffset(), info.index));
4660 }
4661}
4662
Vladimir Marko58155012015-08-19 12:49:41 +00004663void CodeGeneratorX86::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
4664 DCHECK(linker_patches->empty());
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004665 size_t size =
4666 method_patches_.size() +
4667 relative_call_patches_.size() +
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004668 pc_relative_dex_cache_patches_.size() +
4669 simple_patches_.size() +
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004670 string_patches_.size() +
4671 type_patches_.size();
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004672 linker_patches->reserve(size);
Vladimir Markoaad75c62016-10-03 08:46:48 +00004673 for (const PatchInfo<Label>& info : method_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004674 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004675 linker_patches->push_back(LinkerPatch::MethodPatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004676 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004677 for (const PatchInfo<Label>& info : relative_call_patches_) {
Vladimir Marko0f7dca42015-11-02 14:36:43 +00004678 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004679 linker_patches->push_back(
4680 LinkerPatch::RelativeCodePatch(literal_offset, &info.dex_file, info.index));
Vladimir Marko58155012015-08-19 12:49:41 +00004681 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004682 EmitPcRelativeLinkerPatches<LinkerPatch::DexCacheArrayPatch>(pc_relative_dex_cache_patches_,
4683 linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004684 for (const Label& label : simple_patches_) {
4685 uint32_t literal_offset = label.Position() - kLabelPositionToLiteralOffsetAdjustment;
4686 linker_patches->push_back(LinkerPatch::RecordPosition(literal_offset));
4687 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004688 if (!GetCompilerOptions().IsBootImage()) {
4689 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_patches_, linker_patches);
4690 } else if (GetCompilerOptions().GetCompilePic()) {
4691 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(string_patches_, linker_patches);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004692 } else {
Vladimir Markoaad75c62016-10-03 08:46:48 +00004693 for (const PatchInfo<Label>& info : string_patches_) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004694 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004695 linker_patches->push_back(
4696 LinkerPatch::StringPatch(literal_offset, &info.dex_file, info.index));
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004697 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00004698 }
4699 if (GetCompilerOptions().GetCompilePic()) {
4700 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(type_patches_, linker_patches);
4701 } else {
4702 for (const PatchInfo<Label>& info : type_patches_) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004703 uint32_t literal_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
Vladimir Markoaad75c62016-10-03 08:46:48 +00004704 linker_patches->push_back(LinkerPatch::TypePatch(literal_offset, &info.dex_file, info.index));
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01004705 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00004706 }
Vladimir Marko58155012015-08-19 12:49:41 +00004707}
4708
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004709void CodeGeneratorX86::MarkGCCard(Register temp,
4710 Register card,
4711 Register object,
4712 Register value,
4713 bool value_can_be_null) {
Mark Mendell0c9497d2015-08-21 09:30:05 -04004714 NearLabel is_null;
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004715 if (value_can_be_null) {
4716 __ testl(value, value);
4717 __ j(kEqual, &is_null);
4718 }
Andreas Gampe542451c2016-07-26 09:02:02 -07004719 __ fs()->movl(card, Address::Absolute(Thread::CardTableOffset<kX86PointerSize>().Int32Value()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004720 __ movl(temp, object);
4721 __ shrl(temp, Immediate(gc::accounting::CardTable::kCardShift));
Nicolas Geoffray5b4b8982014-12-18 17:45:56 +00004722 __ movb(Address(temp, card, TIMES_1, 0),
4723 X86ManagedRegister::FromCpuRegister(card).AsByteRegister());
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004724 if (value_can_be_null) {
4725 __ Bind(&is_null);
4726 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01004727}
4728
Calin Juravle52c48962014-12-16 17:02:57 +00004729void LocationsBuilderX86::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
4730 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Roland Levillain0d5a2812015-11-13 10:07:31 +00004731
4732 bool object_field_get_with_read_barrier =
4733 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01004734 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00004735 new (GetGraph()->GetArena()) LocationSummary(instruction,
4736 kEmitCompilerReadBarrier ?
4737 LocationSummary::kCallOnSlowPath :
4738 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01004739 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004740 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01004741 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01004742 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004743
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004744 if (Primitive::IsFloatingPointType(instruction->GetType())) {
4745 locations->SetOut(Location::RequiresFpuRegister());
4746 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00004747 // The output overlaps in case of long: we don't want the low move
4748 // to overwrite the object's location. Likewise, in the case of
4749 // an object field get with read barriers enabled, we do not want
4750 // the move to overwrite the object's location, as we need it to emit
4751 // the read barrier.
4752 locations->SetOut(
4753 Location::RequiresRegister(),
4754 (object_field_get_with_read_barrier || instruction->GetType() == Primitive::kPrimLong) ?
4755 Location::kOutputOverlap :
4756 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004757 }
Calin Juravle52c48962014-12-16 17:02:57 +00004758
4759 if (field_info.IsVolatile() && (field_info.GetFieldType() == Primitive::kPrimLong)) {
4760 // Long values can be loaded atomically into an XMM using movsd.
Roland Levillain7c1559a2015-12-15 10:55:36 +00004761 // So we use an XMM register as a temp to achieve atomicity (first
4762 // load the temp into the XMM and then copy the XMM into the
4763 // output, 32 bits at a time).
Calin Juravle52c48962014-12-16 17:02:57 +00004764 locations->AddTemp(Location::RequiresFpuRegister());
4765 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004766}
4767
Calin Juravle52c48962014-12-16 17:02:57 +00004768void InstructionCodeGeneratorX86::HandleFieldGet(HInstruction* instruction,
4769 const FieldInfo& field_info) {
4770 DCHECK(instruction->IsInstanceFieldGet() || instruction->IsStaticFieldGet());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004771
Calin Juravle52c48962014-12-16 17:02:57 +00004772 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00004773 Location base_loc = locations->InAt(0);
4774 Register base = base_loc.AsRegister<Register>();
Calin Juravle52c48962014-12-16 17:02:57 +00004775 Location out = locations->Out();
4776 bool is_volatile = field_info.IsVolatile();
4777 Primitive::Type field_type = field_info.GetFieldType();
4778 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
4779
4780 switch (field_type) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004781 case Primitive::kPrimBoolean: {
Calin Juravle52c48962014-12-16 17:02:57 +00004782 __ movzxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004783 break;
4784 }
4785
4786 case Primitive::kPrimByte: {
Calin Juravle52c48962014-12-16 17:02:57 +00004787 __ movsxb(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004788 break;
4789 }
4790
4791 case Primitive::kPrimShort: {
Calin Juravle52c48962014-12-16 17:02:57 +00004792 __ movsxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004793 break;
4794 }
4795
4796 case Primitive::kPrimChar: {
Calin Juravle52c48962014-12-16 17:02:57 +00004797 __ movzxw(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004798 break;
4799 }
4800
4801 case Primitive::kPrimInt:
Calin Juravle52c48962014-12-16 17:02:57 +00004802 __ movl(out.AsRegister<Register>(), Address(base, offset));
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004803 break;
Roland Levillain7c1559a2015-12-15 10:55:36 +00004804
4805 case Primitive::kPrimNot: {
4806 // /* HeapReference<Object> */ out = *(base + offset)
4807 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004808 // Note that a potential implicit null check is handled in this
4809 // CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier call.
4810 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00004811 instruction, out, base, offset, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00004812 if (is_volatile) {
4813 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4814 }
4815 } else {
4816 __ movl(out.AsRegister<Register>(), Address(base, offset));
4817 codegen_->MaybeRecordImplicitNullCheck(instruction);
4818 if (is_volatile) {
4819 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4820 }
4821 // If read barriers are enabled, emit read barriers other than
4822 // Baker's using a slow path (and also unpoison the loaded
4823 // reference, if heap poisoning is enabled).
4824 codegen_->MaybeGenerateReadBarrierSlow(instruction, out, out, base_loc, offset);
4825 }
4826 break;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004827 }
4828
4829 case Primitive::kPrimLong: {
Calin Juravle52c48962014-12-16 17:02:57 +00004830 if (is_volatile) {
4831 XmmRegister temp = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4832 __ movsd(temp, Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004833 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004834 __ movd(out.AsRegisterPairLow<Register>(), temp);
4835 __ psrlq(temp, Immediate(32));
4836 __ movd(out.AsRegisterPairHigh<Register>(), temp);
4837 } else {
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00004838 DCHECK_NE(base, out.AsRegisterPairLow<Register>());
Calin Juravle52c48962014-12-16 17:02:57 +00004839 __ movl(out.AsRegisterPairLow<Register>(), Address(base, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00004840 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00004841 __ movl(out.AsRegisterPairHigh<Register>(), Address(base, kX86WordSize + offset));
4842 }
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004843 break;
4844 }
4845
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004846 case Primitive::kPrimFloat: {
Calin Juravle52c48962014-12-16 17:02:57 +00004847 __ movss(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004848 break;
4849 }
4850
4851 case Primitive::kPrimDouble: {
Calin Juravle52c48962014-12-16 17:02:57 +00004852 __ movsd(out.AsFpuRegister<XmmRegister>(), Address(base, offset));
Nicolas Geoffray52e832b2014-11-06 15:15:31 +00004853 break;
4854 }
4855
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004856 case Primitive::kPrimVoid:
Calin Juravle52c48962014-12-16 17:02:57 +00004857 LOG(FATAL) << "Unreachable type " << field_type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07004858 UNREACHABLE();
Nicolas Geoffraye5038322014-07-04 09:41:32 +01004859 }
Calin Juravle52c48962014-12-16 17:02:57 +00004860
Roland Levillain7c1559a2015-12-15 10:55:36 +00004861 if (field_type == Primitive::kPrimNot || field_type == Primitive::kPrimLong) {
4862 // Potential implicit null checks, in the case of reference or
4863 // long fields, are handled in the previous switch statement.
4864 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00004865 codegen_->MaybeRecordImplicitNullCheck(instruction);
4866 }
4867
Calin Juravle52c48962014-12-16 17:02:57 +00004868 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004869 if (field_type == Primitive::kPrimNot) {
4870 // Memory barriers, in the case of references, are also handled
4871 // in the previous switch statement.
4872 } else {
4873 codegen_->GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
4874 }
Roland Levillain4d027112015-07-01 15:41:14 +01004875 }
Calin Juravle52c48962014-12-16 17:02:57 +00004876}
4877
4878void LocationsBuilderX86::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
4879 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4880
4881 LocationSummary* locations =
4882 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
4883 locations->SetInAt(0, Location::RequiresRegister());
4884 bool is_volatile = field_info.IsVolatile();
4885 Primitive::Type field_type = field_info.GetFieldType();
4886 bool is_byte_type = (field_type == Primitive::kPrimBoolean)
4887 || (field_type == Primitive::kPrimByte);
4888
4889 // The register allocator does not support multiple
4890 // inputs that die at entry with one in a specific register.
4891 if (is_byte_type) {
4892 // Ensure the value is in a byte register.
4893 locations->SetInAt(1, Location::RegisterLocation(EAX));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01004894 } else if (Primitive::IsFloatingPointType(field_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05004895 if (is_volatile && field_type == Primitive::kPrimDouble) {
4896 // In order to satisfy the semantics of volatile, this must be a single instruction store.
4897 locations->SetInAt(1, Location::RequiresFpuRegister());
4898 } else {
4899 locations->SetInAt(1, Location::FpuRegisterOrConstant(instruction->InputAt(1)));
4900 }
4901 } else if (is_volatile && field_type == Primitive::kPrimLong) {
4902 // In order to satisfy the semantics of volatile, this must be a single instruction store.
Calin Juravle52c48962014-12-16 17:02:57 +00004903 locations->SetInAt(1, Location::RequiresRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004904
Calin Juravle52c48962014-12-16 17:02:57 +00004905 // 64bits value can be atomically written to an address with movsd and an XMM register.
4906 // We need two XMM registers because there's no easier way to (bit) copy a register pair
4907 // into a single XMM register (we copy each pair part into the XMMs and then interleave them).
4908 // NB: We could make the register allocator understand fp_reg <-> core_reg moves but given the
4909 // isolated cases when we need this it isn't worth adding the extra complexity.
4910 locations->AddTemp(Location::RequiresFpuRegister());
4911 locations->AddTemp(Location::RequiresFpuRegister());
Mark Mendell81489372015-11-04 11:30:41 -05004912 } else {
4913 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
4914
4915 if (CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1))) {
4916 // Temporary registers for the write barrier.
4917 locations->AddTemp(Location::RequiresRegister()); // May be used for reference poisoning too.
4918 // Ensure the card is in a byte register.
4919 locations->AddTemp(Location::RegisterLocation(ECX));
4920 }
Calin Juravle52c48962014-12-16 17:02:57 +00004921 }
4922}
4923
4924void InstructionCodeGeneratorX86::HandleFieldSet(HInstruction* instruction,
Nicolas Geoffray07276db2015-05-18 14:22:09 +01004925 const FieldInfo& field_info,
4926 bool value_can_be_null) {
Calin Juravle52c48962014-12-16 17:02:57 +00004927 DCHECK(instruction->IsInstanceFieldSet() || instruction->IsStaticFieldSet());
4928
4929 LocationSummary* locations = instruction->GetLocations();
4930 Register base = locations->InAt(0).AsRegister<Register>();
4931 Location value = locations->InAt(1);
4932 bool is_volatile = field_info.IsVolatile();
4933 Primitive::Type field_type = field_info.GetFieldType();
4934 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Roland Levillain4d027112015-07-01 15:41:14 +01004935 bool needs_write_barrier =
4936 CodeGenerator::StoreNeedsWriteBarrier(field_type, instruction->InputAt(1));
Calin Juravle52c48962014-12-16 17:02:57 +00004937
4938 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00004939 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
Calin Juravle52c48962014-12-16 17:02:57 +00004940 }
4941
Mark Mendell81489372015-11-04 11:30:41 -05004942 bool maybe_record_implicit_null_check_done = false;
4943
Calin Juravle52c48962014-12-16 17:02:57 +00004944 switch (field_type) {
4945 case Primitive::kPrimBoolean:
4946 case Primitive::kPrimByte: {
4947 __ movb(Address(base, offset), value.AsRegister<ByteRegister>());
4948 break;
4949 }
4950
4951 case Primitive::kPrimShort:
4952 case Primitive::kPrimChar: {
Mark Mendell81489372015-11-04 11:30:41 -05004953 if (value.IsConstant()) {
4954 int16_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4955 __ movw(Address(base, offset), Immediate(v));
4956 } else {
4957 __ movw(Address(base, offset), value.AsRegister<Register>());
4958 }
Calin Juravle52c48962014-12-16 17:02:57 +00004959 break;
4960 }
4961
4962 case Primitive::kPrimInt:
4963 case Primitive::kPrimNot: {
Roland Levillain4d027112015-07-01 15:41:14 +01004964 if (kPoisonHeapReferences && needs_write_barrier) {
4965 // Note that in the case where `value` is a null reference,
4966 // we do not enter this block, as the reference does not
4967 // need poisoning.
4968 DCHECK_EQ(field_type, Primitive::kPrimNot);
4969 Register temp = locations->GetTemp(0).AsRegister<Register>();
4970 __ movl(temp, value.AsRegister<Register>());
4971 __ PoisonHeapReference(temp);
4972 __ movl(Address(base, offset), temp);
Mark Mendell81489372015-11-04 11:30:41 -05004973 } else if (value.IsConstant()) {
4974 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
4975 __ movl(Address(base, offset), Immediate(v));
Roland Levillain4d027112015-07-01 15:41:14 +01004976 } else {
Nicolas Geoffray03971632016-03-17 10:44:24 +00004977 DCHECK(value.IsRegister()) << value;
Roland Levillain4d027112015-07-01 15:41:14 +01004978 __ movl(Address(base, offset), value.AsRegister<Register>());
4979 }
Calin Juravle52c48962014-12-16 17:02:57 +00004980 break;
4981 }
4982
4983 case Primitive::kPrimLong: {
4984 if (is_volatile) {
4985 XmmRegister temp1 = locations->GetTemp(0).AsFpuRegister<XmmRegister>();
4986 XmmRegister temp2 = locations->GetTemp(1).AsFpuRegister<XmmRegister>();
4987 __ movd(temp1, value.AsRegisterPairLow<Register>());
4988 __ movd(temp2, value.AsRegisterPairHigh<Register>());
4989 __ punpckldq(temp1, temp2);
4990 __ movsd(Address(base, offset), temp1);
Calin Juravle77520bc2015-01-12 18:45:46 +00004991 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell81489372015-11-04 11:30:41 -05004992 } else if (value.IsConstant()) {
4993 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
4994 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
4995 codegen_->MaybeRecordImplicitNullCheck(instruction);
4996 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
Calin Juravle52c48962014-12-16 17:02:57 +00004997 } else {
4998 __ movl(Address(base, offset), value.AsRegisterPairLow<Register>());
Calin Juravle77520bc2015-01-12 18:45:46 +00004999 codegen_->MaybeRecordImplicitNullCheck(instruction);
Calin Juravle52c48962014-12-16 17:02:57 +00005000 __ movl(Address(base, kX86WordSize + offset), value.AsRegisterPairHigh<Register>());
5001 }
Mark Mendell81489372015-11-04 11:30:41 -05005002 maybe_record_implicit_null_check_done = true;
Calin Juravle52c48962014-12-16 17:02:57 +00005003 break;
5004 }
5005
5006 case Primitive::kPrimFloat: {
Mark Mendell81489372015-11-04 11:30:41 -05005007 if (value.IsConstant()) {
5008 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5009 __ movl(Address(base, offset), Immediate(v));
5010 } else {
5011 __ movss(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5012 }
Calin Juravle52c48962014-12-16 17:02:57 +00005013 break;
5014 }
5015
5016 case Primitive::kPrimDouble: {
Mark Mendell81489372015-11-04 11:30:41 -05005017 if (value.IsConstant()) {
5018 int64_t v = CodeGenerator::GetInt64ValueOf(value.GetConstant());
5019 __ movl(Address(base, offset), Immediate(Low32Bits(v)));
5020 codegen_->MaybeRecordImplicitNullCheck(instruction);
5021 __ movl(Address(base, kX86WordSize + offset), Immediate(High32Bits(v)));
5022 maybe_record_implicit_null_check_done = true;
5023 } else {
5024 __ movsd(Address(base, offset), value.AsFpuRegister<XmmRegister>());
5025 }
Calin Juravle52c48962014-12-16 17:02:57 +00005026 break;
5027 }
5028
5029 case Primitive::kPrimVoid:
5030 LOG(FATAL) << "Unreachable type " << field_type;
5031 UNREACHABLE();
5032 }
5033
Mark Mendell81489372015-11-04 11:30:41 -05005034 if (!maybe_record_implicit_null_check_done) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005035 codegen_->MaybeRecordImplicitNullCheck(instruction);
5036 }
5037
Roland Levillain4d027112015-07-01 15:41:14 +01005038 if (needs_write_barrier) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005039 Register temp = locations->GetTemp(0).AsRegister<Register>();
5040 Register card = locations->GetTemp(1).AsRegister<Register>();
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005041 codegen_->MarkGCCard(temp, card, base, value.AsRegister<Register>(), value_can_be_null);
Calin Juravle77520bc2015-01-12 18:45:46 +00005042 }
5043
Calin Juravle52c48962014-12-16 17:02:57 +00005044 if (is_volatile) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005045 codegen_->GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
Calin Juravle52c48962014-12-16 17:02:57 +00005046 }
5047}
5048
5049void LocationsBuilderX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5050 HandleFieldGet(instruction, instruction->GetFieldInfo());
5051}
5052
5053void InstructionCodeGeneratorX86::VisitStaticFieldGet(HStaticFieldGet* instruction) {
5054 HandleFieldGet(instruction, instruction->GetFieldInfo());
5055}
5056
5057void LocationsBuilderX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
5058 HandleFieldSet(instruction, instruction->GetFieldInfo());
5059}
5060
5061void InstructionCodeGeneratorX86::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005062 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005063}
5064
5065void LocationsBuilderX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
5066 HandleFieldSet(instruction, instruction->GetFieldInfo());
5067}
5068
5069void InstructionCodeGeneratorX86::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Nicolas Geoffray07276db2015-05-18 14:22:09 +01005070 HandleFieldSet(instruction, instruction->GetFieldInfo(), instruction->GetValueCanBeNull());
Calin Juravle52c48962014-12-16 17:02:57 +00005071}
5072
5073void LocationsBuilderX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5074 HandleFieldGet(instruction, instruction->GetFieldInfo());
5075}
5076
5077void InstructionCodeGeneratorX86::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
5078 HandleFieldGet(instruction, instruction->GetFieldInfo());
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005079}
5080
Calin Juravlee460d1d2015-09-29 04:52:17 +01005081void LocationsBuilderX86::VisitUnresolvedInstanceFieldGet(
5082 HUnresolvedInstanceFieldGet* instruction) {
5083 FieldAccessCallingConventionX86 calling_convention;
5084 codegen_->CreateUnresolvedFieldLocationSummary(
5085 instruction, instruction->GetFieldType(), calling_convention);
5086}
5087
5088void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldGet(
5089 HUnresolvedInstanceFieldGet* instruction) {
5090 FieldAccessCallingConventionX86 calling_convention;
5091 codegen_->GenerateUnresolvedFieldAccess(instruction,
5092 instruction->GetFieldType(),
5093 instruction->GetFieldIndex(),
5094 instruction->GetDexPc(),
5095 calling_convention);
5096}
5097
5098void LocationsBuilderX86::VisitUnresolvedInstanceFieldSet(
5099 HUnresolvedInstanceFieldSet* instruction) {
5100 FieldAccessCallingConventionX86 calling_convention;
5101 codegen_->CreateUnresolvedFieldLocationSummary(
5102 instruction, instruction->GetFieldType(), calling_convention);
5103}
5104
5105void InstructionCodeGeneratorX86::VisitUnresolvedInstanceFieldSet(
5106 HUnresolvedInstanceFieldSet* instruction) {
5107 FieldAccessCallingConventionX86 calling_convention;
5108 codegen_->GenerateUnresolvedFieldAccess(instruction,
5109 instruction->GetFieldType(),
5110 instruction->GetFieldIndex(),
5111 instruction->GetDexPc(),
5112 calling_convention);
5113}
5114
5115void LocationsBuilderX86::VisitUnresolvedStaticFieldGet(
5116 HUnresolvedStaticFieldGet* instruction) {
5117 FieldAccessCallingConventionX86 calling_convention;
5118 codegen_->CreateUnresolvedFieldLocationSummary(
5119 instruction, instruction->GetFieldType(), calling_convention);
5120}
5121
5122void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldGet(
5123 HUnresolvedStaticFieldGet* instruction) {
5124 FieldAccessCallingConventionX86 calling_convention;
5125 codegen_->GenerateUnresolvedFieldAccess(instruction,
5126 instruction->GetFieldType(),
5127 instruction->GetFieldIndex(),
5128 instruction->GetDexPc(),
5129 calling_convention);
5130}
5131
5132void LocationsBuilderX86::VisitUnresolvedStaticFieldSet(
5133 HUnresolvedStaticFieldSet* instruction) {
5134 FieldAccessCallingConventionX86 calling_convention;
5135 codegen_->CreateUnresolvedFieldLocationSummary(
5136 instruction, instruction->GetFieldType(), calling_convention);
5137}
5138
5139void InstructionCodeGeneratorX86::VisitUnresolvedStaticFieldSet(
5140 HUnresolvedStaticFieldSet* instruction) {
5141 FieldAccessCallingConventionX86 calling_convention;
5142 codegen_->GenerateUnresolvedFieldAccess(instruction,
5143 instruction->GetFieldType(),
5144 instruction->GetFieldIndex(),
5145 instruction->GetDexPc(),
5146 calling_convention);
5147}
5148
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005149void LocationsBuilderX86::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005150 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
5151 Location loc = codegen_->GetCompilerOptions().GetImplicitNullChecks()
5152 ? Location::RequiresRegister()
5153 : Location::Any();
5154 locations->SetInAt(0, loc);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005155}
5156
Calin Juravle2ae48182016-03-16 14:05:09 +00005157void CodeGeneratorX86::GenerateImplicitNullCheck(HNullCheck* instruction) {
5158 if (CanMoveNullCheckToUser(instruction)) {
Calin Juravle77520bc2015-01-12 18:45:46 +00005159 return;
5160 }
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005161 LocationSummary* locations = instruction->GetLocations();
5162 Location obj = locations->InAt(0);
Calin Juravle77520bc2015-01-12 18:45:46 +00005163
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005164 __ testl(EAX, Address(obj.AsRegister<Register>(), 0));
Calin Juravle2ae48182016-03-16 14:05:09 +00005165 RecordPcInfo(instruction, instruction->GetDexPc());
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005166}
5167
Calin Juravle2ae48182016-03-16 14:05:09 +00005168void CodeGeneratorX86::GenerateExplicitNullCheck(HNullCheck* instruction) {
Andreas Gampe85b62f22015-09-09 13:15:38 -07005169 SlowPathCode* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathX86(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00005170 AddSlowPath(slow_path);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005171
5172 LocationSummary* locations = instruction->GetLocations();
5173 Location obj = locations->InAt(0);
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005174
5175 if (obj.IsRegister()) {
Mark Mendell42514f62015-03-31 11:34:22 -04005176 __ testl(obj.AsRegister<Register>(), obj.AsRegister<Register>());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005177 } else if (obj.IsStackSlot()) {
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005178 __ cmpl(Address(ESP, obj.GetStackIndex()), Immediate(0));
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005179 } else {
5180 DCHECK(obj.IsConstant()) << obj;
David Brazdil77a48ae2015-09-15 12:34:04 +00005181 DCHECK(obj.GetConstant()->IsNullConstant());
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005182 __ jmp(slow_path->GetEntryLabel());
5183 return;
Nicolas Geoffraye5038322014-07-04 09:41:32 +01005184 }
5185 __ j(kEqual, slow_path->GetEntryLabel());
5186}
5187
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005188void InstructionCodeGeneratorX86::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00005189 codegen_->GenerateNullCheck(instruction);
Calin Juravlecd6dffe2015-01-08 17:35:35 +00005190}
5191
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005192void LocationsBuilderX86::VisitArrayGet(HArrayGet* instruction) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005193 bool object_array_get_with_read_barrier =
5194 kEmitCompilerReadBarrier && (instruction->GetType() == Primitive::kPrimNot);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005195 LocationSummary* locations =
Roland Levillain0d5a2812015-11-13 10:07:31 +00005196 new (GetGraph()->GetArena()) LocationSummary(instruction,
5197 object_array_get_with_read_barrier ?
5198 LocationSummary::kCallOnSlowPath :
5199 LocationSummary::kNoCall);
Vladimir Marko70e97462016-08-09 11:04:26 +01005200 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005201 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01005202 }
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005203 locations->SetInAt(0, Location::RequiresRegister());
5204 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005205 if (Primitive::IsFloatingPointType(instruction->GetType())) {
5206 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
5207 } else {
Roland Levillain0d5a2812015-11-13 10:07:31 +00005208 // The output overlaps in case of long: we don't want the low move
5209 // to overwrite the array's location. Likewise, in the case of an
5210 // object array get with read barriers enabled, we do not want the
5211 // move to overwrite the array's location, as we need it to emit
5212 // the read barrier.
5213 locations->SetOut(
5214 Location::RequiresRegister(),
5215 (instruction->GetType() == Primitive::kPrimLong || object_array_get_with_read_barrier) ?
5216 Location::kOutputOverlap :
5217 Location::kNoOutputOverlap);
Alexandre Rames88c13cd2015-04-14 17:35:39 +01005218 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005219}
5220
5221void InstructionCodeGeneratorX86::VisitArrayGet(HArrayGet* instruction) {
5222 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005223 Location obj_loc = locations->InAt(0);
5224 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005225 Location index = locations->InAt(1);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005226 Location out_loc = locations->Out();
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01005227 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005228
Calin Juravle77520bc2015-01-12 18:45:46 +00005229 Primitive::Type type = instruction->GetType();
5230 switch (type) {
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005231 case Primitive::kPrimBoolean: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005232 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005233 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005234 break;
5235 }
5236
5237 case Primitive::kPrimByte: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005238 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005239 __ movsxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005240 break;
5241 }
5242
5243 case Primitive::kPrimShort: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005244 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005245 __ movsxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005246 break;
5247 }
5248
5249 case Primitive::kPrimChar: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005250 Register out = out_loc.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07005251 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5252 // Branch cases into compressed and uncompressed for each index's type.
5253 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
5254 NearLabel done, not_compressed;
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005255 __ testl(Address(obj, count_offset), Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005256 codegen_->MaybeRecordImplicitNullCheck(instruction);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005257 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
5258 "Expecting 0=compressed, 1=uncompressed");
5259 __ j(kNotZero, &not_compressed);
jessicahandojo4877b792016-09-08 19:49:13 -07005260 __ movzxb(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_1, data_offset));
5261 __ jmp(&done);
5262 __ Bind(&not_compressed);
5263 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5264 __ Bind(&done);
5265 } else {
5266 // Common case for charAt of array of char or when string compression's
5267 // feature is turned off.
5268 __ movzxw(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_2, data_offset));
5269 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005270 break;
5271 }
5272
Roland Levillain7c1559a2015-12-15 10:55:36 +00005273 case Primitive::kPrimInt: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005274 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005275 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005276 break;
5277 }
5278
Roland Levillain7c1559a2015-12-15 10:55:36 +00005279 case Primitive::kPrimNot: {
5280 static_assert(
5281 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
5282 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00005283 // /* HeapReference<Object> */ out =
5284 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
5285 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005286 // Note that a potential implicit null check is handled in this
5287 // CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier call.
5288 codegen_->GenerateArrayLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00005289 instruction, out_loc, obj, data_offset, index, /* needs_null_check */ true);
Roland Levillain7c1559a2015-12-15 10:55:36 +00005290 } else {
5291 Register out = out_loc.AsRegister<Register>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005292 __ movl(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
5293 codegen_->MaybeRecordImplicitNullCheck(instruction);
5294 // If read barriers are enabled, emit read barriers other than
5295 // Baker's using a slow path (and also unpoison the loaded
5296 // reference, if heap poisoning is enabled).
Roland Levillain7c1559a2015-12-15 10:55:36 +00005297 if (index.IsConstant()) {
5298 uint32_t offset =
5299 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Roland Levillain7c1559a2015-12-15 10:55:36 +00005300 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
5301 } else {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005302 codegen_->MaybeGenerateReadBarrierSlow(
5303 instruction, out_loc, out_loc, obj_loc, data_offset, index);
5304 }
5305 }
5306 break;
5307 }
5308
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005309 case Primitive::kPrimLong: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005310 DCHECK_NE(obj, out_loc.AsRegisterPairLow<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005311 __ movl(out_loc.AsRegisterPairLow<Register>(),
5312 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
5313 codegen_->MaybeRecordImplicitNullCheck(instruction);
5314 __ movl(out_loc.AsRegisterPairHigh<Register>(),
5315 CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset + kX86WordSize));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005316 break;
5317 }
5318
Mark Mendell7c8d0092015-01-26 11:21:33 -05005319 case Primitive::kPrimFloat: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005320 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005321 __ movss(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005322 break;
5323 }
5324
5325 case Primitive::kPrimDouble: {
Roland Levillain7c1559a2015-12-15 10:55:36 +00005326 XmmRegister out = out_loc.AsFpuRegister<XmmRegister>();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005327 __ movsd(out, CodeGeneratorX86::ArrayAddress(obj, index, TIMES_8, data_offset));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005328 break;
5329 }
5330
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005331 case Primitive::kPrimVoid:
Calin Juravle77520bc2015-01-12 18:45:46 +00005332 LOG(FATAL) << "Unreachable type " << type;
Ian Rogersfc787ec2014-10-09 21:56:44 -07005333 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005334 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005335
Roland Levillain7c1559a2015-12-15 10:55:36 +00005336 if (type == Primitive::kPrimNot || type == Primitive::kPrimLong) {
5337 // Potential implicit null checks, in the case of reference or
5338 // long arrays, are handled in the previous switch statement.
5339 } else {
Calin Juravle77520bc2015-01-12 18:45:46 +00005340 codegen_->MaybeRecordImplicitNullCheck(instruction);
5341 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005342}
5343
5344void LocationsBuilderX86::VisitArraySet(HArraySet* instruction) {
Nicolas Geoffray39468442014-09-02 15:17:15 +01005345 Primitive::Type value_type = instruction->GetComponentType();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005346
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005347 bool needs_write_barrier =
5348 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Roland Levillain0d5a2812015-11-13 10:07:31 +00005349 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005350
Nicolas Geoffray39468442014-09-02 15:17:15 +01005351 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
5352 instruction,
Vladimir Marko8d49fd72016-08-25 15:20:47 +01005353 may_need_runtime_call_for_type_check ?
Roland Levillain0d5a2812015-11-13 10:07:31 +00005354 LocationSummary::kCallOnSlowPath :
5355 LocationSummary::kNoCall);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005356
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005357 bool is_byte_type = (value_type == Primitive::kPrimBoolean)
5358 || (value_type == Primitive::kPrimByte);
5359 // We need the inputs to be different than the output in case of long operation.
5360 // In case of a byte operation, the register allocator does not support multiple
5361 // inputs that die at entry with one in a specific register.
5362 locations->SetInAt(0, Location::RequiresRegister());
5363 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5364 if (is_byte_type) {
5365 // Ensure the value is in a byte register.
5366 locations->SetInAt(2, Location::ByteRegisterOrConstant(EAX, instruction->InputAt(2)));
5367 } else if (Primitive::IsFloatingPointType(value_type)) {
Mark Mendell81489372015-11-04 11:30:41 -05005368 locations->SetInAt(2, Location::FpuRegisterOrConstant(instruction->InputAt(2)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005369 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005370 locations->SetInAt(2, Location::RegisterOrConstant(instruction->InputAt(2)));
5371 }
5372 if (needs_write_barrier) {
5373 // Temporary registers for the write barrier.
5374 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
5375 // Ensure the card is in a byte register.
Roland Levillain4f6b0b52015-11-23 19:29:22 +00005376 locations->AddTemp(Location::RegisterLocation(ECX));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005377 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005378}
5379
5380void InstructionCodeGeneratorX86::VisitArraySet(HArraySet* instruction) {
5381 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005382 Location array_loc = locations->InAt(0);
5383 Register array = array_loc.AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005384 Location index = locations->InAt(1);
Nicolas Geoffray26a25ef2014-09-30 13:54:09 +01005385 Location value = locations->InAt(2);
Nicolas Geoffray39468442014-09-02 15:17:15 +01005386 Primitive::Type value_type = instruction->GetComponentType();
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005387 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
5388 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
5389 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005390 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005391 bool needs_write_barrier =
5392 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005393
5394 switch (value_type) {
5395 case Primitive::kPrimBoolean:
5396 case Primitive::kPrimByte: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005397 uint32_t offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005398 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_1, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005399 if (value.IsRegister()) {
5400 __ movb(address, value.AsRegister<ByteRegister>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005401 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005402 __ movb(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005403 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005404 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005405 break;
5406 }
5407
5408 case Primitive::kPrimShort:
5409 case Primitive::kPrimChar: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005410 uint32_t offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005411 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_2, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005412 if (value.IsRegister()) {
5413 __ movw(address, value.AsRegister<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005414 } else {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005415 __ movw(address, Immediate(value.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005416 }
Calin Juravle77520bc2015-01-12 18:45:46 +00005417 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005418 break;
5419 }
5420
Nicolas Geoffrayaf07bc12014-11-12 18:08:09 +00005421 case Primitive::kPrimNot: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005422 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005423 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005424
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005425 if (!value.IsRegister()) {
5426 // Just setting null.
5427 DCHECK(instruction->InputAt(2)->IsNullConstant());
5428 DCHECK(value.IsConstant()) << value;
5429 __ movl(address, Immediate(0));
Calin Juravle77520bc2015-01-12 18:45:46 +00005430 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005431 DCHECK(!needs_write_barrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00005432 DCHECK(!may_need_runtime_call_for_type_check);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005433 break;
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005434 }
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005435
5436 DCHECK(needs_write_barrier);
5437 Register register_value = value.AsRegister<Register>();
Roland Levillain16d9f942016-08-25 17:27:56 +01005438 // We cannot use a NearLabel for `done`, as its range may be too
5439 // short when Baker read barriers are enabled.
5440 Label done;
5441 NearLabel not_null, do_put;
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005442 SlowPathCode* slow_path = nullptr;
Roland Levillain16d9f942016-08-25 17:27:56 +01005443 Location temp_loc = locations->GetTemp(0);
5444 Register temp = temp_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00005445 if (may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005446 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathX86(instruction);
5447 codegen_->AddSlowPath(slow_path);
5448 if (instruction->GetValueCanBeNull()) {
5449 __ testl(register_value, register_value);
5450 __ j(kNotEqual, &not_null);
5451 __ movl(address, Immediate(0));
5452 codegen_->MaybeRecordImplicitNullCheck(instruction);
5453 __ jmp(&done);
5454 __ Bind(&not_null);
5455 }
5456
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005457 // Note that when Baker read barriers are enabled, the type
5458 // checks are performed without read barriers. This is fine,
5459 // even in the case where a class object is in the from-space
5460 // after the flip, as a comparison involving such a type would
5461 // not produce a false positive; it may of course produce a
5462 // false negative, in which case we would take the ArraySet
5463 // slow path.
Roland Levillain16d9f942016-08-25 17:27:56 +01005464
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005465 // /* HeapReference<Class> */ temp = array->klass_
5466 __ movl(temp, Address(array, class_offset));
5467 codegen_->MaybeRecordImplicitNullCheck(instruction);
5468 __ MaybeUnpoisonHeapReference(temp);
Roland Levillain16d9f942016-08-25 17:27:56 +01005469
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005470 // /* HeapReference<Class> */ temp = temp->component_type_
5471 __ movl(temp, Address(temp, component_offset));
5472 // If heap poisoning is enabled, no need to unpoison `temp`
5473 // nor the object reference in `register_value->klass`, as
5474 // we are comparing two poisoned references.
5475 __ cmpl(temp, Address(register_value, class_offset));
Roland Levillain16d9f942016-08-25 17:27:56 +01005476
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005477 if (instruction->StaticTypeOfArrayIsObjectArray()) {
5478 __ j(kEqual, &do_put);
5479 // If heap poisoning is enabled, the `temp` reference has
5480 // not been unpoisoned yet; unpoison it now.
Roland Levillain0d5a2812015-11-13 10:07:31 +00005481 __ MaybeUnpoisonHeapReference(temp);
5482
Roland Levillain9d6e1f82016-09-05 15:57:33 +01005483 // If heap poisoning is enabled, no need to unpoison the
5484 // heap reference loaded below, as it is only used for a
5485 // comparison with null.
5486 __ cmpl(Address(temp, super_offset), Immediate(0));
5487 __ j(kNotEqual, slow_path->GetEntryLabel());
5488 __ Bind(&do_put);
5489 } else {
5490 __ j(kNotEqual, slow_path->GetEntryLabel());
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005491 }
5492 }
5493
5494 if (kPoisonHeapReferences) {
5495 __ movl(temp, register_value);
5496 __ PoisonHeapReference(temp);
5497 __ movl(address, temp);
5498 } else {
5499 __ movl(address, register_value);
5500 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005501 if (!may_need_runtime_call_for_type_check) {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005502 codegen_->MaybeRecordImplicitNullCheck(instruction);
5503 }
5504
5505 Register card = locations->GetTemp(1).AsRegister<Register>();
5506 codegen_->MarkGCCard(
5507 temp, card, array, value.AsRegister<Register>(), instruction->GetValueCanBeNull());
5508 __ Bind(&done);
5509
5510 if (slow_path != nullptr) {
5511 __ Bind(slow_path->GetExitLabel());
5512 }
5513
5514 break;
5515 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00005516
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005517 case Primitive::kPrimInt: {
5518 uint32_t offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005519 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005520 if (value.IsRegister()) {
5521 __ movl(address, value.AsRegister<Register>());
5522 } else {
5523 DCHECK(value.IsConstant()) << value;
5524 int32_t v = CodeGenerator::GetInt32ValueOf(value.GetConstant());
5525 __ movl(address, Immediate(v));
5526 }
5527 codegen_->MaybeRecordImplicitNullCheck(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005528 break;
5529 }
5530
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005531 case Primitive::kPrimLong: {
5532 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005533 if (value.IsRegisterPair()) {
5534 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5535 value.AsRegisterPairLow<Register>());
5536 codegen_->MaybeRecordImplicitNullCheck(instruction);
5537 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5538 value.AsRegisterPairHigh<Register>());
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005539 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005540 DCHECK(value.IsConstant());
5541 int64_t val = value.GetConstant()->AsLongConstant()->GetValue();
5542 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset),
5543 Immediate(Low32Bits(val)));
5544 codegen_->MaybeRecordImplicitNullCheck(instruction);
5545 __ movl(CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, data_offset + kX86WordSize),
5546 Immediate(High32Bits(val)));
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005547 }
5548 break;
5549 }
5550
Mark Mendell7c8d0092015-01-26 11:21:33 -05005551 case Primitive::kPrimFloat: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005552 uint32_t offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005553 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_4, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005554 if (value.IsFpuRegister()) {
5555 __ movss(address, value.AsFpuRegister<XmmRegister>());
5556 } else {
5557 DCHECK(value.IsConstant());
5558 int32_t v = bit_cast<int32_t, float>(value.GetConstant()->AsFloatConstant()->GetValue());
5559 __ movl(address, Immediate(v));
5560 }
5561 codegen_->MaybeRecordImplicitNullCheck(instruction);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005562 break;
5563 }
5564
5565 case Primitive::kPrimDouble: {
Nicolas Geoffraye0395dd2015-09-25 11:04:45 +01005566 uint32_t offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005567 Address address = CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset);
Mark Mendell81489372015-11-04 11:30:41 -05005568 if (value.IsFpuRegister()) {
5569 __ movsd(address, value.AsFpuRegister<XmmRegister>());
5570 } else {
5571 DCHECK(value.IsConstant());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005572 Address address_hi =
5573 CodeGeneratorX86::ArrayAddress(array, index, TIMES_8, offset + kX86WordSize);
Mark Mendell81489372015-11-04 11:30:41 -05005574 int64_t v = bit_cast<int64_t, double>(value.GetConstant()->AsDoubleConstant()->GetValue());
5575 __ movl(address, Immediate(Low32Bits(v)));
5576 codegen_->MaybeRecordImplicitNullCheck(instruction);
5577 __ movl(address_hi, Immediate(High32Bits(v)));
5578 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005579 break;
5580 }
5581
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005582 case Primitive::kPrimVoid:
5583 LOG(FATAL) << "Unreachable type " << instruction->GetType();
Ian Rogersfc787ec2014-10-09 21:56:44 -07005584 UNREACHABLE();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005585 }
5586}
5587
5588void LocationsBuilderX86::VisitArrayLength(HArrayLength* instruction) {
5589 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Nicolas Geoffray8e3964b2014-10-17 11:06:38 +01005590 locations->SetInAt(0, Location::RequiresRegister());
Mark Mendellee8d9712016-07-12 11:13:15 -04005591 if (!instruction->IsEmittedAtUseSite()) {
5592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
5593 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005594}
5595
5596void InstructionCodeGeneratorX86::VisitArrayLength(HArrayLength* instruction) {
Mark Mendellee8d9712016-07-12 11:13:15 -04005597 if (instruction->IsEmittedAtUseSite()) {
5598 return;
5599 }
5600
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005601 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01005602 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Roland Levillain271ab9c2014-11-27 15:23:57 +00005603 Register obj = locations->InAt(0).AsRegister<Register>();
5604 Register out = locations->Out().AsRegister<Register>();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005605 __ movl(out, Address(obj, offset));
Calin Juravle77520bc2015-01-12 18:45:46 +00005606 codegen_->MaybeRecordImplicitNullCheck(instruction);
jessicahandojo4877b792016-09-08 19:49:13 -07005607 // Mask out most significant bit in case the array is String's array of char.
5608 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005609 __ shrl(out, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005610 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005611}
5612
5613void LocationsBuilderX86::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01005614 RegisterSet caller_saves = RegisterSet::Empty();
5615 InvokeRuntimeCallingConvention calling_convention;
5616 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5617 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
5618 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Mark Mendellf60c90b2015-03-04 15:12:59 -05005619 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Mark Mendellee8d9712016-07-12 11:13:15 -04005620 HInstruction* length = instruction->InputAt(1);
5621 if (!length->IsEmittedAtUseSite()) {
5622 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
5623 }
jessicahandojo4877b792016-09-08 19:49:13 -07005624 // Need register to see array's length.
5625 if (mirror::kUseStringCompression && instruction->IsStringCharAt()) {
5626 locations->AddTemp(Location::RequiresRegister());
5627 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005628}
5629
5630void InstructionCodeGeneratorX86::VisitBoundsCheck(HBoundsCheck* instruction) {
jessicahandojo4877b792016-09-08 19:49:13 -07005631 const bool is_string_compressed_char_at =
5632 mirror::kUseStringCompression && instruction->IsStringCharAt();
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005633 LocationSummary* locations = instruction->GetLocations();
Mark Mendellf60c90b2015-03-04 15:12:59 -05005634 Location index_loc = locations->InAt(0);
5635 Location length_loc = locations->InAt(1);
Andreas Gampe85b62f22015-09-09 13:15:38 -07005636 SlowPathCode* slow_path =
Serban Constantinescu5a6cc492015-08-13 15:20:25 +01005637 new (GetGraph()->GetArena()) BoundsCheckSlowPathX86(instruction);
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005638
Mark Mendell99dbd682015-04-22 16:18:52 -04005639 if (length_loc.IsConstant()) {
5640 int32_t length = CodeGenerator::GetInt32ValueOf(length_loc.GetConstant());
5641 if (index_loc.IsConstant()) {
5642 // BCE will remove the bounds check if we are guarenteed to pass.
5643 int32_t index = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5644 if (index < 0 || index >= length) {
5645 codegen_->AddSlowPath(slow_path);
5646 __ jmp(slow_path->GetEntryLabel());
5647 } else {
5648 // Some optimization after BCE may have generated this, and we should not
5649 // generate a bounds check if it is a valid range.
5650 }
5651 return;
5652 }
5653
5654 // We have to reverse the jump condition because the length is the constant.
5655 Register index_reg = index_loc.AsRegister<Register>();
5656 __ cmpl(index_reg, Immediate(length));
5657 codegen_->AddSlowPath(slow_path);
5658 __ j(kAboveEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005659 } else {
Mark Mendellee8d9712016-07-12 11:13:15 -04005660 HInstruction* array_length = instruction->InputAt(1);
5661 if (array_length->IsEmittedAtUseSite()) {
5662 // Address the length field in the array.
5663 DCHECK(array_length->IsArrayLength());
5664 uint32_t len_offset = CodeGenerator::GetArrayLengthOffset(array_length->AsArrayLength());
5665 Location array_loc = array_length->GetLocations()->InAt(0);
5666 Address array_len(array_loc.AsRegister<Register>(), len_offset);
jessicahandojo4877b792016-09-08 19:49:13 -07005667 if (is_string_compressed_char_at) {
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005668 // TODO: if index_loc.IsConstant(), compare twice the index (to compensate for
5669 // the string compression flag) with the in-memory length and avoid the temporary.
jessicahandojo4877b792016-09-08 19:49:13 -07005670 Register length_reg = locations->GetTemp(0).AsRegister<Register>();
5671 __ movl(length_reg, array_len);
5672 codegen_->MaybeRecordImplicitNullCheck(array_length);
Vladimir Markofdaf0f42016-10-13 19:29:53 +01005673 __ shrl(length_reg, Immediate(1));
jessicahandojo4877b792016-09-08 19:49:13 -07005674 codegen_->GenerateIntCompare(length_reg, index_loc);
Mark Mendellee8d9712016-07-12 11:13:15 -04005675 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07005676 // Checking bounds for general case:
5677 // Array of char or string's array with feature compression off.
5678 if (index_loc.IsConstant()) {
5679 int32_t value = CodeGenerator::GetInt32ValueOf(index_loc.GetConstant());
5680 __ cmpl(array_len, Immediate(value));
5681 } else {
5682 __ cmpl(array_len, index_loc.AsRegister<Register>());
5683 }
5684 codegen_->MaybeRecordImplicitNullCheck(array_length);
Mark Mendellee8d9712016-07-12 11:13:15 -04005685 }
Mark Mendell99dbd682015-04-22 16:18:52 -04005686 } else {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01005687 codegen_->GenerateIntCompare(length_loc, index_loc);
Mark Mendell99dbd682015-04-22 16:18:52 -04005688 }
5689 codegen_->AddSlowPath(slow_path);
5690 __ j(kBelowEqual, slow_path->GetEntryLabel());
Mark Mendellf60c90b2015-03-04 15:12:59 -05005691 }
Nicolas Geoffray3c7bb982014-07-23 16:04:16 +01005692}
5693
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01005694void LocationsBuilderX86::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005695 LOG(FATAL) << "Unreachable";
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01005696}
5697
5698void InstructionCodeGeneratorX86::VisitParallelMove(HParallelMove* instruction) {
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005699 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
5700}
5701
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005702void LocationsBuilderX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01005703 LocationSummary* locations =
5704 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01005705 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005706}
5707
5708void InstructionCodeGeneratorX86::VisitSuspendCheck(HSuspendCheck* instruction) {
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005709 HBasicBlock* block = instruction->GetBlock();
5710 if (block->GetLoopInformation() != nullptr) {
5711 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
5712 // The back edge will generate the suspend check.
5713 return;
5714 }
5715 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
5716 // The goto will generate the suspend check.
5717 return;
5718 }
5719 GenerateSuspendCheck(instruction, nullptr);
5720}
5721
5722void InstructionCodeGeneratorX86::GenerateSuspendCheck(HSuspendCheck* instruction,
5723 HBasicBlock* successor) {
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005724 SuspendCheckSlowPathX86* slow_path =
Nicolas Geoffraydb216f42015-05-05 17:02:20 +01005725 down_cast<SuspendCheckSlowPathX86*>(instruction->GetSlowPath());
5726 if (slow_path == nullptr) {
5727 slow_path = new (GetGraph()->GetArena()) SuspendCheckSlowPathX86(instruction, successor);
5728 instruction->SetSlowPath(slow_path);
5729 codegen_->AddSlowPath(slow_path);
5730 if (successor != nullptr) {
5731 DCHECK(successor->IsLoopHeader());
5732 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(instruction);
5733 }
5734 } else {
5735 DCHECK_EQ(slow_path->GetSuccessor(), successor);
5736 }
5737
Andreas Gampe542451c2016-07-26 09:02:02 -07005738 __ fs()->cmpw(Address::Absolute(Thread::ThreadFlagsOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00005739 Immediate(0));
Nicolas Geoffray3c049742014-09-24 18:10:46 +01005740 if (successor == nullptr) {
5741 __ j(kNotEqual, slow_path->GetEntryLabel());
5742 __ Bind(slow_path->GetReturnLabel());
5743 } else {
5744 __ j(kEqual, codegen_->GetLabelOf(successor));
5745 __ jmp(slow_path->GetEntryLabel());
5746 }
Nicolas Geoffrayfbc695f2014-09-15 15:33:30 +00005747}
5748
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005749X86Assembler* ParallelMoveResolverX86::GetAssembler() const {
5750 return codegen_->GetAssembler();
5751}
5752
Mark Mendell7c8d0092015-01-26 11:21:33 -05005753void ParallelMoveResolverX86::MoveMemoryToMemory32(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005754 ScratchRegisterScope ensure_scratch(
5755 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5756 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5757 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5758 __ movl(temp_reg, Address(ESP, src + stack_offset));
5759 __ movl(Address(ESP, dst + stack_offset), temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005760}
5761
5762void ParallelMoveResolverX86::MoveMemoryToMemory64(int dst, int src) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005763 ScratchRegisterScope ensure_scratch(
5764 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5765 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5766 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5767 __ movl(temp_reg, Address(ESP, src + stack_offset));
5768 __ movl(Address(ESP, dst + stack_offset), temp_reg);
5769 __ movl(temp_reg, Address(ESP, src + stack_offset + kX86WordSize));
5770 __ movl(Address(ESP, dst + stack_offset + kX86WordSize), temp_reg);
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005771}
5772
5773void ParallelMoveResolverX86::EmitMove(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005774 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005775 Location source = move->GetSource();
5776 Location destination = move->GetDestination();
5777
5778 if (source.IsRegister()) {
5779 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005780 __ movl(destination.AsRegister<Register>(), source.AsRegister<Register>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005781 } else if (destination.IsFpuRegister()) {
5782 __ movd(destination.AsFpuRegister<XmmRegister>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005783 } else {
5784 DCHECK(destination.IsStackSlot());
Roland Levillain271ab9c2014-11-27 15:23:57 +00005785 __ movl(Address(ESP, destination.GetStackIndex()), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005786 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005787 } else if (source.IsRegisterPair()) {
5788 size_t elem_size = Primitive::ComponentSize(Primitive::kPrimInt);
5789 // Create stack space for 2 elements.
5790 __ subl(ESP, Immediate(2 * elem_size));
5791 __ movl(Address(ESP, 0), source.AsRegisterPairLow<Register>());
5792 __ movl(Address(ESP, elem_size), source.AsRegisterPairHigh<Register>());
5793 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, 0));
5794 // And remove the temporary stack space we allocated.
5795 __ addl(ESP, Immediate(2 * elem_size));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005796 } else if (source.IsFpuRegister()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005797 if (destination.IsRegister()) {
5798 __ movd(destination.AsRegister<Register>(), source.AsFpuRegister<XmmRegister>());
5799 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005800 __ movaps(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
David Brazdil74eb1b22015-12-14 11:44:01 +00005801 } else if (destination.IsRegisterPair()) {
5802 XmmRegister src_reg = source.AsFpuRegister<XmmRegister>();
5803 __ movd(destination.AsRegisterPairLow<Register>(), src_reg);
5804 __ psrlq(src_reg, Immediate(32));
5805 __ movd(destination.AsRegisterPairHigh<Register>(), src_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005806 } else if (destination.IsStackSlot()) {
5807 __ movss(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5808 } else {
5809 DCHECK(destination.IsDoubleStackSlot());
5810 __ movsd(Address(ESP, destination.GetStackIndex()), source.AsFpuRegister<XmmRegister>());
5811 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005812 } else if (source.IsStackSlot()) {
5813 if (destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005814 __ movl(destination.AsRegister<Register>(), Address(ESP, source.GetStackIndex()));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005815 } else if (destination.IsFpuRegister()) {
5816 __ movss(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005817 } else {
5818 DCHECK(destination.IsStackSlot());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005819 MoveMemoryToMemory32(destination.GetStackIndex(), source.GetStackIndex());
5820 }
5821 } else if (source.IsDoubleStackSlot()) {
David Brazdil74eb1b22015-12-14 11:44:01 +00005822 if (destination.IsRegisterPair()) {
5823 __ movl(destination.AsRegisterPairLow<Register>(), Address(ESP, source.GetStackIndex()));
5824 __ movl(destination.AsRegisterPairHigh<Register>(),
5825 Address(ESP, source.GetHighStackIndex(kX86WordSize)));
5826 } else if (destination.IsFpuRegister()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005827 __ movsd(destination.AsFpuRegister<XmmRegister>(), Address(ESP, source.GetStackIndex()));
5828 } else {
5829 DCHECK(destination.IsDoubleStackSlot()) << destination;
5830 MoveMemoryToMemory64(destination.GetStackIndex(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005831 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005832 } else if (source.IsConstant()) {
Mark Mendell7c8d0092015-01-26 11:21:33 -05005833 HConstant* constant = source.GetConstant();
Nicolas Geoffrayd6138ef2015-02-18 14:48:53 +00005834 if (constant->IsIntConstant() || constant->IsNullConstant()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005835 int32_t value = CodeGenerator::GetInt32ValueOf(constant);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005836 if (destination.IsRegister()) {
Mark Mendell09b84632015-02-13 17:48:38 -05005837 if (value == 0) {
5838 __ xorl(destination.AsRegister<Register>(), destination.AsRegister<Register>());
5839 } else {
5840 __ movl(destination.AsRegister<Register>(), Immediate(value));
5841 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005842 } else {
5843 DCHECK(destination.IsStackSlot()) << destination;
Mark Mendell09b84632015-02-13 17:48:38 -05005844 __ movl(Address(ESP, destination.GetStackIndex()), Immediate(value));
Mark Mendell7c8d0092015-01-26 11:21:33 -05005845 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005846 } else if (constant->IsFloatConstant()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005847 float fp_value = constant->AsFloatConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005848 int32_t value = bit_cast<int32_t, float>(fp_value);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005849 Immediate imm(value);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005850 if (destination.IsFpuRegister()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005851 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5852 if (value == 0) {
5853 // Easy handling of 0.0.
5854 __ xorps(dest, dest);
5855 } else {
5856 ScratchRegisterScope ensure_scratch(
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005857 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5858 Register temp = static_cast<Register>(ensure_scratch.GetRegister());
5859 __ movl(temp, Immediate(value));
5860 __ movd(dest, temp);
Mark Mendell3f6c7f62015-03-13 13:47:53 -04005861 }
Mark Mendell7c8d0092015-01-26 11:21:33 -05005862 } else {
5863 DCHECK(destination.IsStackSlot()) << destination;
5864 __ movl(Address(ESP, destination.GetStackIndex()), imm);
5865 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005866 } else if (constant->IsLongConstant()) {
5867 int64_t value = constant->AsLongConstant()->GetValue();
5868 int32_t low_value = Low32Bits(value);
5869 int32_t high_value = High32Bits(value);
5870 Immediate low(low_value);
5871 Immediate high(high_value);
5872 if (destination.IsDoubleStackSlot()) {
5873 __ movl(Address(ESP, destination.GetStackIndex()), low);
5874 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5875 } else {
5876 __ movl(destination.AsRegisterPairLow<Register>(), low);
5877 __ movl(destination.AsRegisterPairHigh<Register>(), high);
5878 }
5879 } else {
5880 DCHECK(constant->IsDoubleConstant());
5881 double dbl_value = constant->AsDoubleConstant()->GetValue();
Roland Levillainda4d79b2015-03-24 14:36:11 +00005882 int64_t value = bit_cast<int64_t, double>(dbl_value);
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005883 int32_t low_value = Low32Bits(value);
5884 int32_t high_value = High32Bits(value);
5885 Immediate low(low_value);
5886 Immediate high(high_value);
5887 if (destination.IsFpuRegister()) {
5888 XmmRegister dest = destination.AsFpuRegister<XmmRegister>();
5889 if (value == 0) {
5890 // Easy handling of 0.0.
5891 __ xorpd(dest, dest);
5892 } else {
5893 __ pushl(high);
5894 __ pushl(low);
5895 __ movsd(dest, Address(ESP, 0));
5896 __ addl(ESP, Immediate(8));
5897 }
5898 } else {
5899 DCHECK(destination.IsDoubleStackSlot()) << destination;
5900 __ movl(Address(ESP, destination.GetStackIndex()), low);
5901 __ movl(Address(ESP, destination.GetHighStackIndex(kX86WordSize)), high);
5902 }
Nicolas Geoffray96f89a22014-07-11 10:57:49 +01005903 }
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005904 } else {
Nicolas Geoffray42d1f5f2015-01-16 09:14:18 +00005905 LOG(FATAL) << "Unimplemented move: " << destination << " <- " << source;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005906 }
5907}
5908
Mark Mendella5c19ce2015-04-01 12:51:05 -04005909void ParallelMoveResolverX86::Exchange(Register reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005910 Register suggested_scratch = reg == EAX ? EBX : EAX;
5911 ScratchRegisterScope ensure_scratch(
5912 this, reg, suggested_scratch, codegen_->GetNumberOfCoreRegisters());
5913
5914 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5915 __ movl(static_cast<Register>(ensure_scratch.GetRegister()), Address(ESP, mem + stack_offset));
5916 __ movl(Address(ESP, mem + stack_offset), reg);
5917 __ movl(reg, static_cast<Register>(ensure_scratch.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005918}
5919
Mark Mendell7c8d0092015-01-26 11:21:33 -05005920void ParallelMoveResolverX86::Exchange32(XmmRegister reg, int mem) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005921 ScratchRegisterScope ensure_scratch(
5922 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
5923
5924 Register temp_reg = static_cast<Register>(ensure_scratch.GetRegister());
5925 int stack_offset = ensure_scratch.IsSpilled() ? kX86WordSize : 0;
5926 __ movl(temp_reg, Address(ESP, mem + stack_offset));
5927 __ movss(Address(ESP, mem + stack_offset), reg);
5928 __ movd(reg, temp_reg);
Mark Mendell7c8d0092015-01-26 11:21:33 -05005929}
5930
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005931void ParallelMoveResolverX86::Exchange(int mem1, int mem2) {
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005932 ScratchRegisterScope ensure_scratch1(
5933 this, kNoRegister, EAX, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005934
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005935 Register suggested_scratch = ensure_scratch1.GetRegister() == EAX ? EBX : EAX;
5936 ScratchRegisterScope ensure_scratch2(
5937 this, ensure_scratch1.GetRegister(), suggested_scratch, codegen_->GetNumberOfCoreRegisters());
Nicolas Geoffraye27f31a2014-06-12 17:53:14 +01005938
Guillaume Sancheze14590b2015-04-15 18:57:27 +00005939 int stack_offset = ensure_scratch1.IsSpilled() ? kX86WordSize : 0;
5940 stack_offset += ensure_scratch2.IsSpilled() ? kX86WordSize : 0;
5941 __ movl(static_cast<Register>(ensure_scratch1.GetRegister()), Address(ESP, mem1 + stack_offset));
5942 __ movl(static_cast<Register>(ensure_scratch2.GetRegister()), Address(ESP, mem2 + stack_offset));
5943 __ movl(Address(ESP, mem2 + stack_offset), static_cast<Register>(ensure_scratch1.GetRegister()));
5944 __ movl(Address(ESP, mem1 + stack_offset), static_cast<Register>(ensure_scratch2.GetRegister()));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005945}
5946
5947void ParallelMoveResolverX86::EmitSwap(size_t index) {
Vladimir Marko225b6462015-09-28 12:17:40 +01005948 MoveOperands* move = moves_[index];
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005949 Location source = move->GetSource();
5950 Location destination = move->GetDestination();
5951
5952 if (source.IsRegister() && destination.IsRegister()) {
Mark Mendell90979812015-07-28 16:41:21 -04005953 // Use XOR swap algorithm to avoid serializing XCHG instruction or using a temporary.
5954 DCHECK_NE(destination.AsRegister<Register>(), source.AsRegister<Register>());
5955 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
5956 __ xorl(source.AsRegister<Register>(), destination.AsRegister<Register>());
5957 __ xorl(destination.AsRegister<Register>(), source.AsRegister<Register>());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005958 } else if (source.IsRegister() && destination.IsStackSlot()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005959 Exchange(source.AsRegister<Register>(), destination.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005960 } else if (source.IsStackSlot() && destination.IsRegister()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00005961 Exchange(destination.AsRegister<Register>(), source.GetStackIndex());
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01005962 } else if (source.IsStackSlot() && destination.IsStackSlot()) {
5963 Exchange(destination.GetStackIndex(), source.GetStackIndex());
Mark Mendell7c8d0092015-01-26 11:21:33 -05005964 } else if (source.IsFpuRegister() && destination.IsFpuRegister()) {
5965 // Use XOR Swap algorithm to avoid a temporary.
5966 DCHECK_NE(source.reg(), destination.reg());
5967 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5968 __ xorpd(source.AsFpuRegister<XmmRegister>(), destination.AsFpuRegister<XmmRegister>());
5969 __ xorpd(destination.AsFpuRegister<XmmRegister>(), source.AsFpuRegister<XmmRegister>());
5970 } else if (source.IsFpuRegister() && destination.IsStackSlot()) {
5971 Exchange32(source.AsFpuRegister<XmmRegister>(), destination.GetStackIndex());
5972 } else if (destination.IsFpuRegister() && source.IsStackSlot()) {
5973 Exchange32(destination.AsFpuRegister<XmmRegister>(), source.GetStackIndex());
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00005974 } else if (source.IsFpuRegister() && destination.IsDoubleStackSlot()) {
5975 // Take advantage of the 16 bytes in the XMM register.
5976 XmmRegister reg = source.AsFpuRegister<XmmRegister>();
5977 Address stack(ESP, destination.GetStackIndex());
5978 // Load the double into the high doubleword.
5979 __ movhpd(reg, stack);
5980
5981 // Store the low double into the destination.
5982 __ movsd(stack, reg);
5983
5984 // Move the high double to the low double.
5985 __ psrldq(reg, Immediate(8));
5986 } else if (destination.IsFpuRegister() && source.IsDoubleStackSlot()) {
5987 // Take advantage of the 16 bytes in the XMM register.
5988 XmmRegister reg = destination.AsFpuRegister<XmmRegister>();
5989 Address stack(ESP, source.GetStackIndex());
5990 // Load the double into the high doubleword.
5991 __ movhpd(reg, stack);
5992
5993 // Store the low double into the destination.
5994 __ movsd(stack, reg);
5995
5996 // Move the high double to the low double.
5997 __ psrldq(reg, Immediate(8));
5998 } else if (destination.IsDoubleStackSlot() && source.IsDoubleStackSlot()) {
5999 Exchange(destination.GetStackIndex(), source.GetStackIndex());
6000 Exchange(destination.GetHighStackIndex(kX86WordSize), source.GetHighStackIndex(kX86WordSize));
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006001 } else {
Mark Mendell7c8d0092015-01-26 11:21:33 -05006002 LOG(FATAL) << "Unimplemented: source: " << source << ", destination: " << destination;
Nicolas Geoffray86dbb9a2014-06-04 11:12:39 +01006003 }
6004}
6005
6006void ParallelMoveResolverX86::SpillScratch(int reg) {
6007 __ pushl(static_cast<Register>(reg));
6008}
6009
6010void ParallelMoveResolverX86::RestoreScratch(int reg) {
6011 __ popl(static_cast<Register>(reg));
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01006012}
6013
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006014HLoadClass::LoadKind CodeGeneratorX86::GetSupportedLoadClassKind(
6015 HLoadClass::LoadKind desired_class_load_kind) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006016 switch (desired_class_load_kind) {
6017 case HLoadClass::LoadKind::kReferrersClass:
6018 break;
6019 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
6020 DCHECK(!GetCompilerOptions().GetCompilePic());
6021 break;
6022 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
6023 DCHECK(GetCompilerOptions().GetCompilePic());
6024 FALLTHROUGH_INTENDED;
6025 case HLoadClass::LoadKind::kDexCachePcRelative:
6026 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
6027 // We disable pc-relative load when there is an irreducible loop, as the optimization
6028 // is incompatible with it.
6029 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6030 // with irreducible loops.
6031 if (GetGraph()->HasIrreducibleLoops()) {
6032 return HLoadClass::LoadKind::kDexCacheViaMethod;
6033 }
6034 break;
6035 case HLoadClass::LoadKind::kBootImageAddress:
6036 break;
6037 case HLoadClass::LoadKind::kDexCacheAddress:
6038 DCHECK(Runtime::Current()->UseJitCompilation());
6039 break;
6040 case HLoadClass::LoadKind::kDexCacheViaMethod:
6041 break;
6042 }
6043 return desired_class_load_kind;
6044}
6045
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006046void LocationsBuilderX86::VisitLoadClass(HLoadClass* cls) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006047 if (cls->NeedsAccessCheck()) {
6048 InvokeRuntimeCallingConvention calling_convention;
6049 CodeGenerator::CreateLoadClassLocationSummary(
6050 cls,
6051 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
6052 Location::RegisterLocation(EAX),
6053 /* code_generator_supports_read_barrier */ true);
6054 return;
6055 }
6056
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006057 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
6058 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006059 ? LocationSummary::kCallOnSlowPath
6060 : LocationSummary::kNoCall;
6061 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006062 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006063 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006064 }
6065
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006066 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
6067 if (load_kind == HLoadClass::LoadKind::kReferrersClass ||
6068 load_kind == HLoadClass::LoadKind::kDexCacheViaMethod ||
6069 load_kind == HLoadClass::LoadKind::kBootImageLinkTimePcRelative ||
6070 load_kind == HLoadClass::LoadKind::kDexCachePcRelative) {
6071 locations->SetInAt(0, Location::RequiresRegister());
6072 }
6073 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006074}
6075
6076void InstructionCodeGeneratorX86::VisitLoadClass(HLoadClass* cls) {
Nicolas Geoffray76b1e172015-05-27 17:18:33 +01006077 LocationSummary* locations = cls->GetLocations();
Calin Juravle98893e12015-10-02 21:05:03 +01006078 if (cls->NeedsAccessCheck()) {
Andreas Gampea5b09a62016-11-17 15:21:22 -08006079 codegen_->MoveConstant(locations->GetTemp(0), cls->GetTypeIndex().index_);
Serban Constantinescuba45db02016-07-12 22:53:02 +01006080 codegen_->InvokeRuntime(kQuickInitializeTypeAndVerifyAccess, cls, cls->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006081 CheckEntrypointTypes<kQuickInitializeTypeAndVerifyAccess, void*, uint32_t>();
Calin Juravle580b6092015-10-06 17:35:58 +01006082 return;
6083 }
6084
Roland Levillain0d5a2812015-11-13 10:07:31 +00006085 Location out_loc = locations->Out();
6086 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006087
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006088 bool generate_null_check = false;
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006089 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
6090 ? kWithoutReadBarrier
6091 : kCompilerReadBarrierOption;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006092 switch (cls->GetLoadKind()) {
6093 case HLoadClass::LoadKind::kReferrersClass: {
6094 DCHECK(!cls->CanCallRuntime());
6095 DCHECK(!cls->MustGenerateClinitCheck());
6096 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
6097 Register current_method = locations->InAt(0).AsRegister<Register>();
6098 GenerateGcRootFieldLoad(
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006099 cls,
6100 out_loc,
6101 Address(current_method, ArtMethod::DeclaringClassOffset().Int32Value()),
Roland Levillain00468f32016-10-27 18:02:48 +01006102 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006103 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006104 break;
6105 }
6106 case HLoadClass::LoadKind::kBootImageLinkTimeAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006107 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006108 __ movl(out, Immediate(/* placeholder */ 0));
6109 codegen_->RecordTypePatch(cls);
6110 break;
6111 }
6112 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006113 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006114 Register method_address = locations->InAt(0).AsRegister<Register>();
6115 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
6116 codegen_->RecordTypePatch(cls);
6117 break;
6118 }
6119 case HLoadClass::LoadKind::kBootImageAddress: {
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006120 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006121 DCHECK_NE(cls->GetAddress(), 0u);
6122 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6123 __ movl(out, Immediate(address));
6124 codegen_->RecordSimplePatch();
6125 break;
6126 }
6127 case HLoadClass::LoadKind::kDexCacheAddress: {
6128 DCHECK_NE(cls->GetAddress(), 0u);
6129 uint32_t address = dchecked_integral_cast<uint32_t>(cls->GetAddress());
6130 // /* GcRoot<mirror::Class> */ out = *address
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006131 GenerateGcRootFieldLoad(cls,
6132 out_loc,
6133 Address::Absolute(address),
Roland Levillain00468f32016-10-27 18:02:48 +01006134 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006135 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006136 generate_null_check = !cls->IsInDexCache();
6137 break;
6138 }
6139 case HLoadClass::LoadKind::kDexCachePcRelative: {
6140 Register base_reg = locations->InAt(0).AsRegister<Register>();
6141 uint32_t offset = cls->GetDexCacheElementOffset();
6142 Label* fixup_label = codegen_->NewPcRelativeDexCacheArrayPatch(cls->GetDexFile(), offset);
6143 // /* GcRoot<mirror::Class> */ out = *(base + offset) /* PC-relative */
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006144 GenerateGcRootFieldLoad(cls,
6145 out_loc,
6146 Address(base_reg, CodeGeneratorX86::kDummy32BitOffset),
6147 fixup_label,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006148 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006149 generate_null_check = !cls->IsInDexCache();
6150 break;
6151 }
6152 case HLoadClass::LoadKind::kDexCacheViaMethod: {
6153 // /* GcRoot<mirror::Class>[] */ out =
6154 // current_method.ptr_sized_fields_->dex_cache_resolved_types_
6155 Register current_method = locations->InAt(0).AsRegister<Register>();
6156 __ movl(out, Address(current_method,
6157 ArtMethod::DexCacheResolvedTypesOffset(kX86PointerSize).Int32Value()));
6158 // /* GcRoot<mirror::Class> */ out = out[type_index]
Mathieu Chartier31b12e32016-09-02 17:11:57 -07006159 GenerateGcRootFieldLoad(cls,
6160 out_loc,
Andreas Gampea5b09a62016-11-17 15:21:22 -08006161 Address(out,
6162 CodeGenerator::GetCacheOffset(cls->GetTypeIndex().index_)),
Roland Levillain00468f32016-10-27 18:02:48 +01006163 /* fixup_label */ nullptr,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006164 read_barrier_option);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006165 generate_null_check = !cls->IsInDexCache();
6166 break;
6167 }
6168 }
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006169
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006170 if (generate_null_check || cls->MustGenerateClinitCheck()) {
6171 DCHECK(cls->CanCallRuntime());
6172 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
6173 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
6174 codegen_->AddSlowPath(slow_path);
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006175
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006176 if (generate_null_check) {
6177 __ testl(out, out);
6178 __ j(kEqual, slow_path->GetEntryLabel());
6179 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00006180
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01006181 if (cls->MustGenerateClinitCheck()) {
6182 GenerateClassInitializationCheck(slow_path, out);
6183 } else {
6184 __ Bind(slow_path->GetExitLabel());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006185 }
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006186 }
6187}
6188
6189void LocationsBuilderX86::VisitClinitCheck(HClinitCheck* check) {
6190 LocationSummary* locations =
6191 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
6192 locations->SetInAt(0, Location::RequiresRegister());
6193 if (check->HasUses()) {
6194 locations->SetOut(Location::SameAsFirstInput());
6195 }
6196}
6197
6198void InstructionCodeGeneratorX86::VisitClinitCheck(HClinitCheck* check) {
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006199 // We assume the class to not be null.
Andreas Gampe85b62f22015-09-09 13:15:38 -07006200 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathX86(
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006201 check->GetLoadClass(), check, check->GetDexPc(), true);
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006202 codegen_->AddSlowPath(slow_path);
Roland Levillain199f3362014-11-27 17:15:16 +00006203 GenerateClassInitializationCheck(slow_path,
6204 check->GetLocations()->InAt(0).AsRegister<Register>());
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006205}
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006206
Nicolas Geoffray424f6762014-11-03 14:51:25 +00006207void InstructionCodeGeneratorX86::GenerateClassInitializationCheck(
Andreas Gampe85b62f22015-09-09 13:15:38 -07006208 SlowPathCode* slow_path, Register class_reg) {
Nicolas Geoffray19a19cf2014-10-22 16:07:05 +01006209 __ cmpl(Address(class_reg, mirror::Class::StatusOffset().Int32Value()),
6210 Immediate(mirror::Class::kStatusInitialized));
6211 __ j(kLess, slow_path->GetEntryLabel());
6212 __ Bind(slow_path->GetExitLabel());
6213 // No need for memory fence, thanks to the X86 memory model.
6214}
6215
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006216HLoadString::LoadKind CodeGeneratorX86::GetSupportedLoadStringKind(
6217 HLoadString::LoadKind desired_string_load_kind) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006218 switch (desired_string_load_kind) {
6219 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
6220 DCHECK(!GetCompilerOptions().GetCompilePic());
6221 break;
6222 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
6223 DCHECK(GetCompilerOptions().GetCompilePic());
6224 FALLTHROUGH_INTENDED;
Vladimir Markoaad75c62016-10-03 08:46:48 +00006225 case HLoadString::LoadKind::kBssEntry:
Calin Juravleffc87072016-04-20 14:22:09 +01006226 DCHECK(!Runtime::Current()->UseJitCompilation()); // Note: boot image is also non-JIT.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006227 // We disable pc-relative load when there is an irreducible loop, as the optimization
6228 // is incompatible with it.
6229 // TODO: Create as many X86ComputeBaseMethodAddress instructions as needed for methods
6230 // with irreducible loops.
6231 if (GetGraph()->HasIrreducibleLoops()) {
6232 return HLoadString::LoadKind::kDexCacheViaMethod;
6233 }
6234 break;
6235 case HLoadString::LoadKind::kBootImageAddress:
6236 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006237 case HLoadString::LoadKind::kDexCacheViaMethod:
6238 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006239 case HLoadString::LoadKind::kJitTableAddress:
6240 DCHECK(Runtime::Current()->UseJitCompilation());
6241 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006242 }
6243 return desired_string_load_kind;
6244}
6245
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006246void LocationsBuilderX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006247 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00006248 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006249 HLoadString::LoadKind load_kind = load->GetLoadKind();
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006250 if (load_kind == HLoadString::LoadKind::kBootImageLinkTimePcRelative ||
Vladimir Markoaad75c62016-10-03 08:46:48 +00006251 load_kind == HLoadString::LoadKind::kBssEntry) {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006252 locations->SetInAt(0, Location::RequiresRegister());
6253 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006254 if (load_kind == HLoadString::LoadKind::kDexCacheViaMethod) {
6255 locations->SetOut(Location::RegisterLocation(EAX));
6256 } else {
6257 locations->SetOut(Location::RequiresRegister());
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006258 if (load_kind == HLoadString::LoadKind::kBssEntry) {
6259 if (!kUseReadBarrier || kUseBakerReadBarrier) {
6260 // Rely on the pResolveString and/or marking to save everything.
6261 RegisterSet caller_saves = RegisterSet::Empty();
6262 InvokeRuntimeCallingConvention calling_convention;
6263 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6264 locations->SetCustomSlowPathCallerSaves(caller_saves);
6265 } else {
6266 // For non-Baker read barrier we have a temp-clobbering call.
6267 }
6268 }
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006269 }
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006270}
6271
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006272Label* CodeGeneratorX86::NewJitRootStringPatch(const DexFile& dex_file,
6273 dex::StringIndex dex_index) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006274 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index), /* placeholder */ 0u);
6275 // Add a patch entry and return the label.
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006276 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006277 PatchInfo<Label>* info = &jit_string_patches_.back();
6278 return &info->label;
6279}
6280
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006281void InstructionCodeGeneratorX86::VisitLoadString(HLoadString* load) {
Nicolas Geoffrayfbdaa302015-05-29 12:06:56 +01006282 LocationSummary* locations = load->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006283 Location out_loc = locations->Out();
6284 Register out = out_loc.AsRegister<Register>();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006285
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006286 switch (load->GetLoadKind()) {
6287 case HLoadString::LoadKind::kBootImageLinkTimeAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006288 __ movl(out, Immediate(/* placeholder */ 0));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006289 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006290 return; // No dex cache slow path.
6291 }
6292 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006293 Register method_address = locations->InAt(0).AsRegister<Register>();
6294 __ leal(out, Address(method_address, CodeGeneratorX86::kDummy32BitOffset));
Vladimir Markoaad75c62016-10-03 08:46:48 +00006295 codegen_->RecordBootStringPatch(load);
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006296 return; // No dex cache slow path.
6297 }
6298 case HLoadString::LoadKind::kBootImageAddress: {
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006299 DCHECK_NE(load->GetAddress(), 0u);
6300 uint32_t address = dchecked_integral_cast<uint32_t>(load->GetAddress());
6301 __ movl(out, Immediate(address));
6302 codegen_->RecordSimplePatch();
6303 return; // No dex cache slow path.
6304 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00006305 case HLoadString::LoadKind::kBssEntry: {
6306 Register method_address = locations->InAt(0).AsRegister<Register>();
6307 Address address = Address(method_address, CodeGeneratorX86::kDummy32BitOffset);
6308 Label* fixup_label = codegen_->NewStringBssEntryPatch(load);
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006309 // /* GcRoot<mirror::String> */ out = *address /* PC-relative */
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006310 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
Vladimir Markoaad75c62016-10-03 08:46:48 +00006311 SlowPathCode* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathX86(load);
6312 codegen_->AddSlowPath(slow_path);
6313 __ testl(out, out);
6314 __ j(kEqual, slow_path->GetEntryLabel());
6315 __ Bind(slow_path->GetExitLabel());
6316 return;
6317 }
Nicolas Geoffray132d8362016-11-16 09:19:42 +00006318 case HLoadString::LoadKind::kJitTableAddress: {
6319 Address address = Address::Absolute(CodeGeneratorX86::kDummy32BitOffset);
6320 Label* fixup_label = codegen_->NewJitRootStringPatch(
6321 load->GetDexFile(), load->GetStringIndex());
6322 // /* GcRoot<mirror::String> */ out = *address
6323 GenerateGcRootFieldLoad(load, out_loc, address, fixup_label, kCompilerReadBarrierOption);
6324 return;
6325 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006326 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006327 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006328 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006329
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07006330 // TODO: Re-add the compiler code to do string dex cache lookup again.
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006331 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko94ce9c22016-09-30 14:50:51 +01006332 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08006333 __ movl(calling_convention.GetRegisterAt(0), Immediate(load->GetStringIndex().index_));
Christina Wadsworth175d09b2016-08-31 16:26:01 -07006334 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
6335 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Nicolas Geoffrayb5f62b32014-10-30 10:58:41 +00006336}
6337
David Brazdilcb1c0552015-08-04 16:22:25 +01006338static Address GetExceptionTlsAddress() {
Andreas Gampe542451c2016-07-26 09:02:02 -07006339 return Address::Absolute(Thread::ExceptionOffset<kX86PointerSize>().Int32Value());
David Brazdilcb1c0552015-08-04 16:22:25 +01006340}
6341
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006342void LocationsBuilderX86::VisitLoadException(HLoadException* load) {
6343 LocationSummary* locations =
6344 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
6345 locations->SetOut(Location::RequiresRegister());
6346}
6347
6348void InstructionCodeGeneratorX86::VisitLoadException(HLoadException* load) {
David Brazdilcb1c0552015-08-04 16:22:25 +01006349 __ fs()->movl(load->GetLocations()->Out().AsRegister<Register>(), GetExceptionTlsAddress());
6350}
6351
6352void LocationsBuilderX86::VisitClearException(HClearException* clear) {
6353 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
6354}
6355
6356void InstructionCodeGeneratorX86::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
6357 __ fs()->movl(GetExceptionTlsAddress(), Immediate(0));
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006358}
6359
6360void LocationsBuilderX86::VisitThrow(HThrow* instruction) {
6361 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006362 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006363 InvokeRuntimeCallingConvention calling_convention;
6364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6365}
6366
6367void InstructionCodeGeneratorX86::VisitThrow(HThrow* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006368 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006369 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
Nicolas Geoffrayde58ab22014-11-05 12:46:03 +00006370}
6371
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006372// Temp is used for read barrier.
6373static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
6374 if (kEmitCompilerReadBarrier &&
Vladimir Marko953437b2016-08-24 08:30:46 +00006375 !kUseBakerReadBarrier &&
6376 (type_check_kind == TypeCheckKind::kAbstractClassCheck ||
Roland Levillain7c1559a2015-12-15 10:55:36 +00006377 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006378 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
6379 return 1;
6380 }
6381 return 0;
6382}
6383
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006384// Interface case has 3 temps, one for holding the number of interfaces, one for the current
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006385// interface pointer, one for loading the current interface.
6386// The other checks have one temp for loading the object's class.
6387static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
6388 if (type_check_kind == TypeCheckKind::kInterfaceCheck && !kPoisonHeapReferences) {
6389 return 2;
6390 }
6391 return 1 + NumberOfInstanceOfTemps(type_check_kind);
Roland Levillain7c1559a2015-12-15 10:55:36 +00006392}
6393
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006394void LocationsBuilderX86::VisitInstanceOf(HInstanceOf* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006395 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006396 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Vladimir Marko70e97462016-08-09 11:04:26 +01006397 bool baker_read_barrier_slow_path = false;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006398 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006399 case TypeCheckKind::kExactCheck:
6400 case TypeCheckKind::kAbstractClassCheck:
6401 case TypeCheckKind::kClassHierarchyCheck:
6402 case TypeCheckKind::kArrayObjectCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006403 call_kind =
6404 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Vladimir Marko70e97462016-08-09 11:04:26 +01006405 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006406 break;
6407 case TypeCheckKind::kArrayCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006408 case TypeCheckKind::kUnresolvedCheck:
6409 case TypeCheckKind::kInterfaceCheck:
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006410 call_kind = LocationSummary::kCallOnSlowPath;
6411 break;
6412 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006413
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006414 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Vladimir Marko70e97462016-08-09 11:04:26 +01006415 if (baker_read_barrier_slow_path) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01006416 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Vladimir Marko70e97462016-08-09 11:04:26 +01006417 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006418 locations->SetInAt(0, Location::RequiresRegister());
6419 locations->SetInAt(1, Location::Any());
6420 // Note that TypeCheckSlowPathX86 uses this "out" register too.
6421 locations->SetOut(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006422 // When read barriers are enabled, we need a temporary register for some cases.
6423 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006424}
6425
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006426void InstructionCodeGeneratorX86::VisitInstanceOf(HInstanceOf* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006427 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006428 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006429 Location obj_loc = locations->InAt(0);
6430 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006431 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006432 Location out_loc = locations->Out();
6433 Register out = out_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006434 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6435 DCHECK_LE(num_temps, 1u);
6436 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006437 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006438 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6439 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6440 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Andreas Gampe85b62f22015-09-09 13:15:38 -07006441 SlowPathCode* slow_path = nullptr;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006442 NearLabel done, zero;
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006443
6444 // Return 0 if `obj` is null.
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006445 // Avoid null check if we know obj is not null.
6446 if (instruction->MustDoNullCheck()) {
6447 __ testl(obj, obj);
6448 __ j(kEqual, &zero);
6449 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006450
Roland Levillain7c1559a2015-12-15 10:55:36 +00006451 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006452 case TypeCheckKind::kExactCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006453 // /* HeapReference<Class> */ out = obj->klass_
6454 GenerateReferenceLoadTwoRegisters(instruction,
6455 out_loc,
6456 obj_loc,
6457 class_offset,
6458 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006459 if (cls.IsRegister()) {
6460 __ cmpl(out, cls.AsRegister<Register>());
6461 } else {
6462 DCHECK(cls.IsStackSlot()) << cls;
6463 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6464 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006465
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006466 // Classes must be equal for the instanceof to succeed.
6467 __ j(kNotEqual, &zero);
6468 __ movl(out, Immediate(1));
6469 __ jmp(&done);
6470 break;
6471 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006472
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006473 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006474 // /* HeapReference<Class> */ out = obj->klass_
6475 GenerateReferenceLoadTwoRegisters(instruction,
6476 out_loc,
6477 obj_loc,
6478 class_offset,
6479 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006480 // If the class is abstract, we eagerly fetch the super class of the
6481 // object to avoid doing a comparison we know will fail.
6482 NearLabel loop;
6483 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006484 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006485 GenerateReferenceLoadOneRegister(instruction,
6486 out_loc,
6487 super_offset,
6488 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006489 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006490 __ testl(out, out);
6491 // If `out` is null, we use it for the result, and jump to `done`.
6492 __ j(kEqual, &done);
6493 if (cls.IsRegister()) {
6494 __ cmpl(out, cls.AsRegister<Register>());
6495 } else {
6496 DCHECK(cls.IsStackSlot()) << cls;
6497 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6498 }
6499 __ j(kNotEqual, &loop);
6500 __ movl(out, Immediate(1));
6501 if (zero.IsLinked()) {
6502 __ jmp(&done);
6503 }
6504 break;
6505 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006506
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006507 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006508 // /* HeapReference<Class> */ out = obj->klass_
6509 GenerateReferenceLoadTwoRegisters(instruction,
6510 out_loc,
6511 obj_loc,
6512 class_offset,
6513 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006514 // Walk over the class hierarchy to find a match.
6515 NearLabel loop, success;
6516 __ Bind(&loop);
6517 if (cls.IsRegister()) {
6518 __ cmpl(out, cls.AsRegister<Register>());
6519 } else {
6520 DCHECK(cls.IsStackSlot()) << cls;
6521 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6522 }
6523 __ j(kEqual, &success);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006524 // /* HeapReference<Class> */ out = out->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006525 GenerateReferenceLoadOneRegister(instruction,
6526 out_loc,
6527 super_offset,
6528 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006529 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006530 __ testl(out, out);
6531 __ j(kNotEqual, &loop);
6532 // If `out` is null, we use it for the result, and jump to `done`.
6533 __ jmp(&done);
6534 __ Bind(&success);
6535 __ movl(out, Immediate(1));
6536 if (zero.IsLinked()) {
6537 __ jmp(&done);
6538 }
6539 break;
6540 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006541
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006542 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006543 // /* HeapReference<Class> */ out = obj->klass_
6544 GenerateReferenceLoadTwoRegisters(instruction,
6545 out_loc,
6546 obj_loc,
6547 class_offset,
6548 kCompilerReadBarrierOption);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006549 // Do an exact check.
6550 NearLabel exact_check;
6551 if (cls.IsRegister()) {
6552 __ cmpl(out, cls.AsRegister<Register>());
6553 } else {
6554 DCHECK(cls.IsStackSlot()) << cls;
6555 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6556 }
6557 __ j(kEqual, &exact_check);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006558 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006559 // /* HeapReference<Class> */ out = out->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006560 GenerateReferenceLoadOneRegister(instruction,
6561 out_loc,
6562 component_offset,
6563 maybe_temp_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006564 kCompilerReadBarrierOption);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006565 __ testl(out, out);
6566 // If `out` is null, we use it for the result, and jump to `done`.
6567 __ j(kEqual, &done);
6568 __ cmpw(Address(out, primitive_offset), Immediate(Primitive::kPrimNot));
6569 __ j(kNotEqual, &zero);
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006570 __ Bind(&exact_check);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006571 __ movl(out, Immediate(1));
6572 __ jmp(&done);
6573 break;
6574 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006575
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006576 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier9fd8c602016-11-14 14:38:53 -08006577 // No read barrier since the slow path will retry upon failure.
6578 // /* HeapReference<Class> */ out = obj->klass_
6579 GenerateReferenceLoadTwoRegisters(instruction,
6580 out_loc,
6581 obj_loc,
6582 class_offset,
6583 kWithoutReadBarrier);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006584 if (cls.IsRegister()) {
6585 __ cmpl(out, cls.AsRegister<Register>());
6586 } else {
6587 DCHECK(cls.IsStackSlot()) << cls;
6588 __ cmpl(out, Address(ESP, cls.GetStackIndex()));
6589 }
6590 DCHECK(locations->OnlyCallsOnSlowPath());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006591 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6592 /* is_fatal */ false);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006593 codegen_->AddSlowPath(slow_path);
6594 __ j(kNotEqual, slow_path->GetEntryLabel());
6595 __ movl(out, Immediate(1));
6596 if (zero.IsLinked()) {
6597 __ jmp(&done);
6598 }
6599 break;
6600 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006601
Calin Juravle98893e12015-10-02 21:05:03 +01006602 case TypeCheckKind::kUnresolvedCheck:
Roland Levillain0d5a2812015-11-13 10:07:31 +00006603 case TypeCheckKind::kInterfaceCheck: {
6604 // Note that we indeed only call on slow path, but we always go
Roland Levillaine3f43ac2016-01-19 15:07:47 +00006605 // into the slow path for the unresolved and interface check
Roland Levillain0d5a2812015-11-13 10:07:31 +00006606 // cases.
6607 //
6608 // We cannot directly call the InstanceofNonTrivial runtime
6609 // entry point without resorting to a type checking slow path
6610 // here (i.e. by calling InvokeRuntime directly), as it would
6611 // require to assign fixed registers for the inputs of this
6612 // HInstanceOf instruction (following the runtime calling
6613 // convention), which might be cluttered by the potential first
6614 // read barrier emission at the beginning of this method.
Roland Levillain7c1559a2015-12-15 10:55:36 +00006615 //
6616 // TODO: Introduce a new runtime entry point taking the object
6617 // to test (instead of its class) as argument, and let it deal
6618 // with the read barrier issues. This will let us refactor this
6619 // case of the `switch` code as it was previously (with a direct
6620 // call to the runtime not using a type checking slow path).
6621 // This should also be beneficial for the other cases above.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006622 DCHECK(locations->OnlyCallsOnSlowPath());
6623 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6624 /* is_fatal */ false);
6625 codegen_->AddSlowPath(slow_path);
6626 __ jmp(slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006627 if (zero.IsLinked()) {
6628 __ jmp(&done);
6629 }
6630 break;
6631 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006632 }
6633
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006634 if (zero.IsLinked()) {
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006635 __ Bind(&zero);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006636 __ xorl(out, out);
6637 }
6638
6639 if (done.IsLinked()) {
6640 __ Bind(&done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006641 }
6642
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006643 if (slow_path != nullptr) {
6644 __ Bind(slow_path->GetExitLabel());
6645 }
Nicolas Geoffray6f5c41f2014-11-06 08:59:20 +00006646}
6647
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006648static bool IsTypeCheckSlowPathFatal(TypeCheckKind type_check_kind, bool throws_into_catch) {
6649 switch (type_check_kind) {
6650 case TypeCheckKind::kExactCheck:
6651 case TypeCheckKind::kAbstractClassCheck:
6652 case TypeCheckKind::kClassHierarchyCheck:
6653 case TypeCheckKind::kArrayObjectCheck:
6654 return !throws_into_catch && !kEmitCompilerReadBarrier;
6655 case TypeCheckKind::kInterfaceCheck:
6656 return !throws_into_catch && !kEmitCompilerReadBarrier && !kPoisonHeapReferences;
6657 case TypeCheckKind::kArrayCheck:
6658 case TypeCheckKind::kUnresolvedCheck:
6659 return false;
6660 }
6661 LOG(FATAL) << "Unreachable";
6662 UNREACHABLE();
6663}
6664
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006665void LocationsBuilderX86::VisitCheckCast(HCheckCast* instruction) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006666 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006667 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Mathieu Chartier5ac321b2016-11-09 16:33:54 -08006668 LocationSummary::CallKind call_kind =
6669 IsTypeCheckSlowPathFatal(type_check_kind, throws_into_catch)
6670 ? LocationSummary::kNoCall
6671 : LocationSummary::kCallOnSlowPath;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006672 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
6673 locations->SetInAt(0, Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006674 if (type_check_kind == TypeCheckKind::kInterfaceCheck) {
6675 // Require a register for the interface check since there is a loop that compares the class to
6676 // a memory address.
6677 locations->SetInAt(1, Location::RequiresRegister());
6678 } else {
6679 locations->SetInAt(1, Location::Any());
6680 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006681 // Note that TypeCheckSlowPathX86 uses this "temp" register too.
6682 locations->AddTemp(Location::RequiresRegister());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006683 // When read barriers are enabled, we need an additional temporary register for some cases.
6684 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
6685}
6686
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006687void InstructionCodeGeneratorX86::VisitCheckCast(HCheckCast* instruction) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00006688 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006689 LocationSummary* locations = instruction->GetLocations();
Roland Levillain0d5a2812015-11-13 10:07:31 +00006690 Location obj_loc = locations->InAt(0);
6691 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006692 Location cls = locations->InAt(1);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006693 Location temp_loc = locations->GetTemp(0);
6694 Register temp = temp_loc.AsRegister<Register>();
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006695 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
6696 DCHECK_GE(num_temps, 1u);
6697 DCHECK_LE(num_temps, 2u);
6698 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
6699 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6700 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6701 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6702 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
6703 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
6704 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
6705 const uint32_t object_array_data_offset =
6706 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006707
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006708 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
6709 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
6710 // read barriers is done for performance and code size reasons.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006711 bool is_type_check_slow_path_fatal =
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006712 IsTypeCheckSlowPathFatal(type_check_kind, instruction->CanThrowIntoCatchBlock());
6713
Roland Levillain0d5a2812015-11-13 10:07:31 +00006714 SlowPathCode* type_check_slow_path =
6715 new (GetGraph()->GetArena()) TypeCheckSlowPathX86(instruction,
6716 is_type_check_slow_path_fatal);
6717 codegen_->AddSlowPath(type_check_slow_path);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006718
Roland Levillain0d5a2812015-11-13 10:07:31 +00006719 NearLabel done;
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006720 // Avoid null check if we know obj is not null.
6721 if (instruction->MustDoNullCheck()) {
6722 __ testl(obj, obj);
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006723 __ j(kEqual, &done);
Guillaume "Vermeille" Sanchezaf888352015-04-20 14:41:30 +01006724 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006725
Roland Levillain0d5a2812015-11-13 10:07:31 +00006726 switch (type_check_kind) {
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006727 case TypeCheckKind::kExactCheck:
6728 case TypeCheckKind::kArrayCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006729 // /* HeapReference<Class> */ temp = obj->klass_
6730 GenerateReferenceLoadTwoRegisters(instruction,
6731 temp_loc,
6732 obj_loc,
6733 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006734 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006735
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006736 if (cls.IsRegister()) {
6737 __ cmpl(temp, cls.AsRegister<Register>());
6738 } else {
6739 DCHECK(cls.IsStackSlot()) << cls;
6740 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6741 }
6742 // Jump to slow path for throwing the exception or doing a
6743 // more involved array check.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006744 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006745 break;
6746 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006747
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006748 case TypeCheckKind::kAbstractClassCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006749 // /* HeapReference<Class> */ temp = obj->klass_
6750 GenerateReferenceLoadTwoRegisters(instruction,
6751 temp_loc,
6752 obj_loc,
6753 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006754 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006755
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006756 // If the class is abstract, we eagerly fetch the super class of the
6757 // object to avoid doing a comparison we know will fail.
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006758 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006759 __ Bind(&loop);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006760 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006761 GenerateReferenceLoadOneRegister(instruction,
6762 temp_loc,
6763 super_offset,
6764 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006765 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006766
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006767 // If the class reference currently in `temp` is null, jump to the slow path to throw the
6768 // exception.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006769 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006770 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006771
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006772 // Otherwise, compare the classes
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006773 if (cls.IsRegister()) {
6774 __ cmpl(temp, cls.AsRegister<Register>());
6775 } else {
6776 DCHECK(cls.IsStackSlot()) << cls;
6777 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6778 }
6779 __ j(kNotEqual, &loop);
6780 break;
6781 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006782
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006783 case TypeCheckKind::kClassHierarchyCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006784 // /* HeapReference<Class> */ temp = obj->klass_
6785 GenerateReferenceLoadTwoRegisters(instruction,
6786 temp_loc,
6787 obj_loc,
6788 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006789 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006790
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006791 // Walk over the class hierarchy to find a match.
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006792 NearLabel loop;
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006793 __ Bind(&loop);
6794 if (cls.IsRegister()) {
6795 __ cmpl(temp, cls.AsRegister<Register>());
6796 } else {
6797 DCHECK(cls.IsStackSlot()) << cls;
6798 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6799 }
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006800 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006801
Roland Levillain0d5a2812015-11-13 10:07:31 +00006802 // /* HeapReference<Class> */ temp = temp->super_class_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006803 GenerateReferenceLoadOneRegister(instruction,
6804 temp_loc,
6805 super_offset,
6806 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006807 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006808
6809 // If the class reference currently in `temp` is not null, jump
6810 // back at the beginning of the loop.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006811 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006812 __ j(kNotZero, &loop);
6813 // Otherwise, jump to the slow path to throw the exception.;
Roland Levillain0d5a2812015-11-13 10:07:31 +00006814 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006815 break;
6816 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006817
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006818 case TypeCheckKind::kArrayObjectCheck: {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006819 // /* HeapReference<Class> */ temp = obj->klass_
6820 GenerateReferenceLoadTwoRegisters(instruction,
6821 temp_loc,
6822 obj_loc,
6823 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006824 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006825
Nicolas Geoffrayabfcf182015-09-21 18:41:21 +01006826 // Do an exact check.
6827 if (cls.IsRegister()) {
6828 __ cmpl(temp, cls.AsRegister<Register>());
6829 } else {
6830 DCHECK(cls.IsStackSlot()) << cls;
6831 __ cmpl(temp, Address(ESP, cls.GetStackIndex()));
6832 }
6833 __ j(kEqual, &done);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006834
6835 // Otherwise, we need to check that the object's class is a non-primitive array.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006836 // /* HeapReference<Class> */ temp = temp->component_type_
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006837 GenerateReferenceLoadOneRegister(instruction,
6838 temp_loc,
6839 component_offset,
6840 maybe_temp2_loc,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006841 kWithoutReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00006842
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006843 // If the component type is null (i.e. the object not an array), jump to the slow path to
6844 // throw the exception. Otherwise proceed with the check.
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006845 __ testl(temp, temp);
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006846 __ j(kZero, type_check_slow_path->GetEntryLabel());
Roland Levillain0d5a2812015-11-13 10:07:31 +00006847
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006848 __ cmpw(Address(temp, primitive_offset), Immediate(Primitive::kPrimNot));
Mathieu Chartierb99f4d62016-11-07 16:17:26 -08006849 __ j(kNotEqual, type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006850 break;
6851 }
Roland Levillain0d5a2812015-11-13 10:07:31 +00006852
Calin Juravle98893e12015-10-02 21:05:03 +01006853 case TypeCheckKind::kUnresolvedCheck:
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006854 // We always go into the type check slow path for the unresolved check case.
Roland Levillain0d5a2812015-11-13 10:07:31 +00006855 // We cannot directly call the CheckCast runtime entry point
6856 // without resorting to a type checking slow path here (i.e. by
6857 // calling InvokeRuntime directly), as it would require to
6858 // assign fixed registers for the inputs of this HInstanceOf
6859 // instruction (following the runtime calling convention), which
6860 // might be cluttered by the potential first read barrier
6861 // emission at the beginning of this method.
6862 __ jmp(type_check_slow_path->GetEntryLabel());
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006863 break;
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006864
6865 case TypeCheckKind::kInterfaceCheck: {
6866 // Fast path for the interface check. Since we compare with a memory location in the inner
6867 // loop we would need to have cls poisoned. However unpoisoning cls would reset the
6868 // conditional flags and cause the conditional jump to be incorrect. Therefore we just jump
Mathieu Chartieraa474eb2016-11-09 15:18:27 -08006869 // to the slow path if we are running under poisoning.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006870 if (!kPoisonHeapReferences) {
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006871 // Try to avoid read barriers to improve the fast path. We can not get false positives by
6872 // doing this.
6873 // /* HeapReference<Class> */ temp = obj->klass_
6874 GenerateReferenceLoadTwoRegisters(instruction,
6875 temp_loc,
6876 obj_loc,
6877 class_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006878 kWithoutReadBarrier);
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006879
6880 // /* HeapReference<Class> */ temp = temp->iftable_
6881 GenerateReferenceLoadTwoRegisters(instruction,
6882 temp_loc,
6883 temp_loc,
6884 iftable_offset,
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08006885 kWithoutReadBarrier);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006886 // Iftable is never null.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006887 __ movl(maybe_temp2_loc.AsRegister<Register>(), Address(temp, array_length_offset));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006888 // Loop through the iftable and check if any class matches.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006889 NearLabel start_loop;
6890 __ Bind(&start_loop);
Mathieu Chartier6beced42016-11-15 15:51:31 -08006891 // Need to subtract first to handle the empty array case.
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006892 __ subl(maybe_temp2_loc.AsRegister<Register>(), Immediate(2));
Mathieu Chartier6beced42016-11-15 15:51:31 -08006893 __ j(kNegative, type_check_slow_path->GetEntryLabel());
6894 // Go to next interface if the classes do not match.
6895 __ cmpl(cls.AsRegister<Register>(),
6896 CodeGeneratorX86::ArrayAddress(temp,
6897 maybe_temp2_loc,
6898 TIMES_4,
6899 object_array_data_offset));
6900 __ j(kNotEqual, &start_loop);
6901 } else {
6902 __ jmp(type_check_slow_path->GetEntryLabel());
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006903 }
Mathieu Chartier5c44c1b2016-11-04 18:13:04 -07006904 break;
6905 }
Nicolas Geoffray85c7bab2015-09-18 13:40:46 +00006906 }
6907 __ Bind(&done);
6908
Roland Levillain0d5a2812015-11-13 10:07:31 +00006909 __ Bind(type_check_slow_path->GetExitLabel());
Nicolas Geoffray57a88d42014-11-10 15:09:21 +00006910}
6911
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006912void LocationsBuilderX86::VisitMonitorOperation(HMonitorOperation* instruction) {
6913 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006914 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006915 InvokeRuntimeCallingConvention calling_convention;
6916 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6917}
6918
6919void InstructionCodeGeneratorX86::VisitMonitorOperation(HMonitorOperation* instruction) {
Serban Constantinescuba45db02016-07-12 22:53:02 +01006920 codegen_->InvokeRuntime(instruction->IsEnter() ? kQuickLockObject
6921 : kQuickUnlockObject,
Alexandre Rames8158f282015-08-07 10:26:17 +01006922 instruction,
Serban Constantinescuba45db02016-07-12 22:53:02 +01006923 instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00006924 if (instruction->IsEnter()) {
6925 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
6926 } else {
6927 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
6928 }
Nicolas Geoffrayb7baf5c2014-11-11 16:29:44 +00006929}
6930
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006931void LocationsBuilderX86::VisitAnd(HAnd* instruction) { HandleBitwiseOperation(instruction); }
6932void LocationsBuilderX86::VisitOr(HOr* instruction) { HandleBitwiseOperation(instruction); }
6933void LocationsBuilderX86::VisitXor(HXor* instruction) { HandleBitwiseOperation(instruction); }
6934
6935void LocationsBuilderX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6936 LocationSummary* locations =
6937 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
6938 DCHECK(instruction->GetResultType() == Primitive::kPrimInt
6939 || instruction->GetResultType() == Primitive::kPrimLong);
6940 locations->SetInAt(0, Location::RequiresRegister());
6941 locations->SetInAt(1, Location::Any());
6942 locations->SetOut(Location::SameAsFirstInput());
6943}
6944
6945void InstructionCodeGeneratorX86::VisitAnd(HAnd* instruction) {
6946 HandleBitwiseOperation(instruction);
6947}
6948
6949void InstructionCodeGeneratorX86::VisitOr(HOr* instruction) {
6950 HandleBitwiseOperation(instruction);
6951}
6952
6953void InstructionCodeGeneratorX86::VisitXor(HXor* instruction) {
6954 HandleBitwiseOperation(instruction);
6955}
6956
6957void InstructionCodeGeneratorX86::HandleBitwiseOperation(HBinaryOperation* instruction) {
6958 LocationSummary* locations = instruction->GetLocations();
6959 Location first = locations->InAt(0);
6960 Location second = locations->InAt(1);
6961 DCHECK(first.Equals(locations->Out()));
6962
6963 if (instruction->GetResultType() == Primitive::kPrimInt) {
6964 if (second.IsRegister()) {
6965 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006966 __ andl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006967 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006968 __ orl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006969 } else {
6970 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006971 __ xorl(first.AsRegister<Register>(), second.AsRegister<Register>());
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006972 }
6973 } else if (second.IsConstant()) {
6974 if (instruction->IsAnd()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006975 __ andl(first.AsRegister<Register>(),
6976 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006977 } else if (instruction->IsOr()) {
Roland Levillain199f3362014-11-27 17:15:16 +00006978 __ orl(first.AsRegister<Register>(),
6979 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006980 } else {
6981 DCHECK(instruction->IsXor());
Roland Levillain199f3362014-11-27 17:15:16 +00006982 __ xorl(first.AsRegister<Register>(),
6983 Immediate(second.GetConstant()->AsIntConstant()->GetValue()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006984 }
6985 } else {
6986 if (instruction->IsAnd()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006987 __ andl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006988 } else if (instruction->IsOr()) {
Roland Levillain271ab9c2014-11-27 15:23:57 +00006989 __ orl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006990 } else {
6991 DCHECK(instruction->IsXor());
Roland Levillain271ab9c2014-11-27 15:23:57 +00006992 __ xorl(first.AsRegister<Register>(), Address(ESP, second.GetStackIndex()));
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00006993 }
6994 }
6995 } else {
6996 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimLong);
6997 if (second.IsRegisterPair()) {
6998 if (instruction->IsAnd()) {
6999 __ andl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7000 __ andl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7001 } else if (instruction->IsOr()) {
7002 __ orl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7003 __ orl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7004 } else {
7005 DCHECK(instruction->IsXor());
7006 __ xorl(first.AsRegisterPairLow<Register>(), second.AsRegisterPairLow<Register>());
7007 __ xorl(first.AsRegisterPairHigh<Register>(), second.AsRegisterPairHigh<Register>());
7008 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007009 } else if (second.IsDoubleStackSlot()) {
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007010 if (instruction->IsAnd()) {
7011 __ andl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7012 __ andl(first.AsRegisterPairHigh<Register>(),
7013 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7014 } else if (instruction->IsOr()) {
7015 __ orl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7016 __ orl(first.AsRegisterPairHigh<Register>(),
7017 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7018 } else {
7019 DCHECK(instruction->IsXor());
7020 __ xorl(first.AsRegisterPairLow<Register>(), Address(ESP, second.GetStackIndex()));
7021 __ xorl(first.AsRegisterPairHigh<Register>(),
7022 Address(ESP, second.GetHighStackIndex(kX86WordSize)));
7023 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007024 } else {
7025 DCHECK(second.IsConstant()) << second;
7026 int64_t value = second.GetConstant()->AsLongConstant()->GetValue();
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007027 int32_t low_value = Low32Bits(value);
7028 int32_t high_value = High32Bits(value);
7029 Immediate low(low_value);
7030 Immediate high(high_value);
7031 Register first_low = first.AsRegisterPairLow<Register>();
7032 Register first_high = first.AsRegisterPairHigh<Register>();
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007033 if (instruction->IsAnd()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007034 if (low_value == 0) {
7035 __ xorl(first_low, first_low);
7036 } else if (low_value != -1) {
7037 __ andl(first_low, low);
7038 }
7039 if (high_value == 0) {
7040 __ xorl(first_high, first_high);
7041 } else if (high_value != -1) {
7042 __ andl(first_high, high);
7043 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007044 } else if (instruction->IsOr()) {
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007045 if (low_value != 0) {
7046 __ orl(first_low, low);
7047 }
7048 if (high_value != 0) {
7049 __ orl(first_high, high);
7050 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007051 } else {
7052 DCHECK(instruction->IsXor());
Mark Mendell3f6c7f62015-03-13 13:47:53 -04007053 if (low_value != 0) {
7054 __ xorl(first_low, low);
7055 }
7056 if (high_value != 0) {
7057 __ xorl(first_high, high);
7058 }
Nicolas Geoffray234d69d2015-03-09 10:28:50 +00007059 }
Nicolas Geoffray9574c4b2014-11-12 13:19:37 +00007060 }
7061 }
7062}
7063
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007064void InstructionCodeGeneratorX86::GenerateReferenceLoadOneRegister(
7065 HInstruction* instruction,
7066 Location out,
7067 uint32_t offset,
7068 Location maybe_temp,
7069 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007070 Register out_reg = out.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007071 if (read_barrier_option == kWithReadBarrier) {
7072 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007073 if (kUseBakerReadBarrier) {
7074 // Load with fast path based Baker's read barrier.
7075 // /* HeapReference<Object> */ out = *(out + offset)
7076 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007077 instruction, out, out_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007078 } else {
7079 // Load with slow path based read barrier.
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007080 // Save the value of `out` into `maybe_temp` before overwriting it
Roland Levillain7c1559a2015-12-15 10:55:36 +00007081 // in the following move operation, as we will need it for the
7082 // read barrier below.
Vladimir Marko953437b2016-08-24 08:30:46 +00007083 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007084 __ movl(maybe_temp.AsRegister<Register>(), out_reg);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007085 // /* HeapReference<Object> */ out = *(out + offset)
7086 __ movl(out_reg, Address(out_reg, offset));
Roland Levillain95e7ffc2016-01-22 11:57:25 +00007087 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007088 }
7089 } else {
7090 // Plain load with no read barrier.
7091 // /* HeapReference<Object> */ out = *(out + offset)
7092 __ movl(out_reg, Address(out_reg, offset));
7093 __ MaybeUnpoisonHeapReference(out_reg);
7094 }
7095}
7096
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007097void InstructionCodeGeneratorX86::GenerateReferenceLoadTwoRegisters(
7098 HInstruction* instruction,
7099 Location out,
7100 Location obj,
7101 uint32_t offset,
7102 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007103 Register out_reg = out.AsRegister<Register>();
7104 Register obj_reg = obj.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007105 if (read_barrier_option == kWithReadBarrier) {
7106 CHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007107 if (kUseBakerReadBarrier) {
7108 // Load with fast path based Baker's read barrier.
7109 // /* HeapReference<Object> */ out = *(obj + offset)
7110 codegen_->GenerateFieldLoadWithBakerReadBarrier(
Vladimir Marko953437b2016-08-24 08:30:46 +00007111 instruction, out, obj_reg, offset, /* needs_null_check */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007112 } else {
7113 // Load with slow path based read barrier.
7114 // /* HeapReference<Object> */ out = *(obj + offset)
7115 __ movl(out_reg, Address(obj_reg, offset));
7116 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
7117 }
7118 } else {
7119 // Plain load with no read barrier.
7120 // /* HeapReference<Object> */ out = *(obj + offset)
7121 __ movl(out_reg, Address(obj_reg, offset));
7122 __ MaybeUnpoisonHeapReference(out_reg);
7123 }
7124}
7125
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007126void InstructionCodeGeneratorX86::GenerateGcRootFieldLoad(
7127 HInstruction* instruction,
7128 Location root,
7129 const Address& address,
7130 Label* fixup_label,
7131 ReadBarrierOption read_barrier_option) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007132 Register root_reg = root.AsRegister<Register>();
Mathieu Chartier3af00dc2016-11-10 11:25:57 -08007133 if (read_barrier_option == kWithReadBarrier) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -07007134 DCHECK(kEmitCompilerReadBarrier);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007135 if (kUseBakerReadBarrier) {
7136 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7137 // Baker's read barrier are used:
7138 //
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007139 // root = *address;
Roland Levillain7c1559a2015-12-15 10:55:36 +00007140 // if (Thread::Current()->GetIsGcMarking()) {
7141 // root = ReadBarrier::Mark(root)
7142 // }
7143
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007144 // /* GcRoot<mirror::Object> */ root = *address
7145 __ movl(root_reg, address);
7146 if (fixup_label != nullptr) {
7147 __ Bind(fixup_label);
7148 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007149 static_assert(
7150 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7151 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7152 "have different sizes.");
7153 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7154 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7155 "have different sizes.");
7156
Vladimir Marko953437b2016-08-24 08:30:46 +00007157 // Slow path marking the GC root `root`.
7158 SlowPathCode* slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007159 instruction, root, /* unpoison_ref_before_marking */ false);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007160 codegen_->AddSlowPath(slow_path);
7161
Andreas Gampe542451c2016-07-26 09:02:02 -07007162 __ fs()->cmpl(Address::Absolute(Thread::IsGcMarkingOffset<kX86PointerSize>().Int32Value()),
Roland Levillain7c1559a2015-12-15 10:55:36 +00007163 Immediate(0));
7164 __ j(kNotEqual, slow_path->GetEntryLabel());
7165 __ Bind(slow_path->GetExitLabel());
7166 } else {
7167 // GC root loaded through a slow path for read barriers other
7168 // than Baker's.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007169 // /* GcRoot<mirror::Object>* */ root = address
7170 __ leal(root_reg, address);
7171 if (fixup_label != nullptr) {
7172 __ Bind(fixup_label);
7173 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007174 // /* mirror::Object* */ root = root->Read()
7175 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7176 }
7177 } else {
7178 // Plain GC root load with no read barrier.
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007179 // /* GcRoot<mirror::Object> */ root = *address
7180 __ movl(root_reg, address);
7181 if (fixup_label != nullptr) {
7182 __ Bind(fixup_label);
7183 }
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007184 // Note that GC roots are not affected by heap poisoning, thus we
7185 // do not have to unpoison `root_reg` here.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007186 }
7187}
7188
7189void CodeGeneratorX86::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7190 Location ref,
7191 Register obj,
7192 uint32_t offset,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007193 bool needs_null_check) {
7194 DCHECK(kEmitCompilerReadBarrier);
7195 DCHECK(kUseBakerReadBarrier);
7196
7197 // /* HeapReference<Object> */ ref = *(obj + offset)
7198 Address src(obj, offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007199 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007200}
7201
7202void CodeGeneratorX86::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7203 Location ref,
7204 Register obj,
7205 uint32_t data_offset,
7206 Location index,
Roland Levillain7c1559a2015-12-15 10:55:36 +00007207 bool needs_null_check) {
7208 DCHECK(kEmitCompilerReadBarrier);
7209 DCHECK(kUseBakerReadBarrier);
7210
Roland Levillain3d312422016-06-23 13:53:42 +01007211 static_assert(
7212 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7213 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Roland Levillain7c1559a2015-12-15 10:55:36 +00007214 // /* HeapReference<Object> */ ref =
7215 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007216 Address src = CodeGeneratorX86::ArrayAddress(obj, index, TIMES_4, data_offset);
Vladimir Marko953437b2016-08-24 08:30:46 +00007217 GenerateReferenceLoadWithBakerReadBarrier(instruction, ref, obj, src, needs_null_check);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007218}
7219
7220void CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7221 Location ref,
7222 Register obj,
7223 const Address& src,
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007224 bool needs_null_check,
7225 bool always_update_field,
7226 Register* temp) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007227 DCHECK(kEmitCompilerReadBarrier);
7228 DCHECK(kUseBakerReadBarrier);
7229
7230 // In slow path based read barriers, the read barrier call is
7231 // inserted after the original load. However, in fast path based
7232 // Baker's read barriers, we need to perform the load of
7233 // mirror::Object::monitor_ *before* the original reference load.
7234 // This load-load ordering is required by the read barrier.
7235 // The fast path/slow path (for Baker's algorithm) should look like:
7236 //
7237 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7238 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7239 // HeapReference<Object> ref = *src; // Original reference load.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007240 // bool is_gray = (rb_state == ReadBarrier::GrayState());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007241 // if (is_gray) {
7242 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7243 // }
7244 //
7245 // Note: the original implementation in ReadBarrier::Barrier is
7246 // slightly more complex as:
7247 // - it implements the load-load fence using a data dependency on
Roland Levillaine3f43ac2016-01-19 15:07:47 +00007248 // the high-bits of rb_state, which are expected to be all zeroes
7249 // (we use CodeGeneratorX86::GenerateMemoryBarrier instead here,
7250 // which is a no-op thanks to the x86 memory model);
Roland Levillain7c1559a2015-12-15 10:55:36 +00007251 // - it performs additional checks that we do not do here for
7252 // performance reasons.
7253
7254 Register ref_reg = ref.AsRegister<Register>();
Roland Levillain7c1559a2015-12-15 10:55:36 +00007255 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7256
Vladimir Marko953437b2016-08-24 08:30:46 +00007257 // Given the numeric representation, it's enough to check the low bit of the rb_state.
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007258 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7259 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
Vladimir Marko953437b2016-08-24 08:30:46 +00007260 constexpr uint32_t gray_byte_position = LockWord::kReadBarrierStateShift / kBitsPerByte;
7261 constexpr uint32_t gray_bit_position = LockWord::kReadBarrierStateShift % kBitsPerByte;
7262 constexpr int32_t test_value = static_cast<int8_t>(1 << gray_bit_position);
7263
Hiroshi Yamauchi12b58b22016-11-01 11:55:29 -07007264 // if (rb_state == ReadBarrier::GrayState())
Vladimir Marko953437b2016-08-24 08:30:46 +00007265 // ref = ReadBarrier::Mark(ref);
7266 // At this point, just do the "if" and make sure that flags are preserved until the branch.
7267 __ testb(Address(obj, monitor_offset + gray_byte_position), Immediate(test_value));
Roland Levillain7c1559a2015-12-15 10:55:36 +00007268 if (needs_null_check) {
7269 MaybeRecordImplicitNullCheck(instruction);
7270 }
Roland Levillain7c1559a2015-12-15 10:55:36 +00007271
7272 // Load fence to prevent load-load reordering.
7273 // Note that this is a no-op, thanks to the x86 memory model.
7274 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
7275
7276 // The actual reference load.
7277 // /* HeapReference<Object> */ ref = *src
Vladimir Marko953437b2016-08-24 08:30:46 +00007278 __ movl(ref_reg, src); // Flags are unaffected.
7279
7280 // Note: Reference unpoisoning modifies the flags, so we need to delay it after the branch.
7281 // Slow path marking the object `ref` when it is gray.
Roland Levillaina1aa3b12016-10-26 13:03:38 +01007282 SlowPathCode* slow_path;
7283 if (always_update_field) {
7284 DCHECK(temp != nullptr);
7285 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkAndUpdateFieldSlowPathX86(
7286 instruction, ref, obj, src, /* unpoison_ref_before_marking */ true, *temp);
7287 } else {
7288 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathX86(
7289 instruction, ref, /* unpoison_ref_before_marking */ true);
7290 }
Vladimir Marko953437b2016-08-24 08:30:46 +00007291 AddSlowPath(slow_path);
7292
7293 // We have done the "if" of the gray bit check above, now branch based on the flags.
7294 __ j(kNotZero, slow_path->GetEntryLabel());
Roland Levillain7c1559a2015-12-15 10:55:36 +00007295
7296 // Object* ref = ref_addr->AsMirrorPtr()
7297 __ MaybeUnpoisonHeapReference(ref_reg);
7298
Roland Levillain7c1559a2015-12-15 10:55:36 +00007299 __ Bind(slow_path->GetExitLabel());
7300}
7301
7302void CodeGeneratorX86::GenerateReadBarrierSlow(HInstruction* instruction,
7303 Location out,
7304 Location ref,
7305 Location obj,
7306 uint32_t offset,
7307 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007308 DCHECK(kEmitCompilerReadBarrier);
7309
Roland Levillain7c1559a2015-12-15 10:55:36 +00007310 // Insert a slow path based read barrier *after* the reference load.
7311 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007312 // If heap poisoning is enabled, the unpoisoning of the loaded
7313 // reference will be carried out by the runtime within the slow
7314 // path.
7315 //
7316 // Note that `ref` currently does not get unpoisoned (when heap
7317 // poisoning is enabled), which is alright as the `ref` argument is
7318 // not used by the artReadBarrierSlow entry point.
7319 //
7320 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7321 SlowPathCode* slow_path = new (GetGraph()->GetArena())
7322 ReadBarrierForHeapReferenceSlowPathX86(instruction, out, ref, obj, offset, index);
7323 AddSlowPath(slow_path);
7324
Roland Levillain0d5a2812015-11-13 10:07:31 +00007325 __ jmp(slow_path->GetEntryLabel());
7326 __ Bind(slow_path->GetExitLabel());
7327}
7328
Roland Levillain7c1559a2015-12-15 10:55:36 +00007329void CodeGeneratorX86::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7330 Location out,
7331 Location ref,
7332 Location obj,
7333 uint32_t offset,
7334 Location index) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007335 if (kEmitCompilerReadBarrier) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007336 // Baker's read barriers shall be handled by the fast path
7337 // (CodeGeneratorX86::GenerateReferenceLoadWithBakerReadBarrier).
7338 DCHECK(!kUseBakerReadBarrier);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007339 // If heap poisoning is enabled, unpoisoning will be taken care of
7340 // by the runtime within the slow path.
Roland Levillain7c1559a2015-12-15 10:55:36 +00007341 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
Roland Levillain0d5a2812015-11-13 10:07:31 +00007342 } else if (kPoisonHeapReferences) {
7343 __ UnpoisonHeapReference(out.AsRegister<Register>());
7344 }
7345}
7346
Roland Levillain7c1559a2015-12-15 10:55:36 +00007347void CodeGeneratorX86::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7348 Location out,
7349 Location root) {
Roland Levillain0d5a2812015-11-13 10:07:31 +00007350 DCHECK(kEmitCompilerReadBarrier);
7351
Roland Levillain7c1559a2015-12-15 10:55:36 +00007352 // Insert a slow path based read barrier *after* the GC root load.
7353 //
Roland Levillain0d5a2812015-11-13 10:07:31 +00007354 // Note that GC roots are not affected by heap poisoning, so we do
7355 // not need to do anything special for this here.
7356 SlowPathCode* slow_path =
7357 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathX86(instruction, out, root);
7358 AddSlowPath(slow_path);
7359
Roland Levillain0d5a2812015-11-13 10:07:31 +00007360 __ jmp(slow_path->GetEntryLabel());
7361 __ Bind(slow_path->GetExitLabel());
7362}
7363
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007364void LocationsBuilderX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007365 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007366 LOG(FATAL) << "Unreachable";
7367}
7368
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01007369void InstructionCodeGeneratorX86::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
Calin Juravleb1498f62015-02-16 13:13:29 +00007370 // Nothing to do, this should be removed during prepare for register allocator.
Calin Juravleb1498f62015-02-16 13:13:29 +00007371 LOG(FATAL) << "Unreachable";
7372}
7373
Mark Mendellfe57faa2015-09-18 09:26:15 -04007374// Simple implementation of packed switch - generate cascaded compare/jumps.
7375void LocationsBuilderX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7376 LocationSummary* locations =
7377 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7378 locations->SetInAt(0, Location::RequiresRegister());
7379}
7380
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007381void InstructionCodeGeneratorX86::GenPackedSwitchWithCompares(Register value_reg,
7382 int32_t lower_bound,
7383 uint32_t num_entries,
7384 HBasicBlock* switch_block,
7385 HBasicBlock* default_block) {
7386 // Figure out the correct compare values and jump conditions.
7387 // Handle the first compare/branch as a special case because it might
7388 // jump to the default case.
7389 DCHECK_GT(num_entries, 2u);
7390 Condition first_condition;
7391 uint32_t index;
7392 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
7393 if (lower_bound != 0) {
7394 first_condition = kLess;
7395 __ cmpl(value_reg, Immediate(lower_bound));
7396 __ j(first_condition, codegen_->GetLabelOf(default_block));
7397 __ j(kEqual, codegen_->GetLabelOf(successors[0]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007398
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007399 index = 1;
7400 } else {
7401 // Handle all the compare/jumps below.
7402 first_condition = kBelow;
7403 index = 0;
7404 }
7405
7406 // Handle the rest of the compare/jumps.
7407 for (; index + 1 < num_entries; index += 2) {
7408 int32_t compare_to_value = lower_bound + index + 1;
7409 __ cmpl(value_reg, Immediate(compare_to_value));
7410 // Jump to successors[index] if value < case_value[index].
7411 __ j(first_condition, codegen_->GetLabelOf(successors[index]));
7412 // Jump to successors[index + 1] if value == case_value[index + 1].
7413 __ j(kEqual, codegen_->GetLabelOf(successors[index + 1]));
7414 }
7415
7416 if (index != num_entries) {
7417 // There are an odd number of entries. Handle the last one.
7418 DCHECK_EQ(index + 1, num_entries);
7419 __ cmpl(value_reg, Immediate(lower_bound + index));
7420 __ j(kEqual, codegen_->GetLabelOf(successors[index]));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007421 }
7422
7423 // And the default for any other value.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007424 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
7425 __ jmp(codegen_->GetLabelOf(default_block));
Mark Mendellfe57faa2015-09-18 09:26:15 -04007426 }
7427}
7428
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007429void InstructionCodeGeneratorX86::VisitPackedSwitch(HPackedSwitch* switch_instr) {
7430 int32_t lower_bound = switch_instr->GetStartValue();
7431 uint32_t num_entries = switch_instr->GetNumEntries();
7432 LocationSummary* locations = switch_instr->GetLocations();
7433 Register value_reg = locations->InAt(0).AsRegister<Register>();
7434
7435 GenPackedSwitchWithCompares(value_reg,
7436 lower_bound,
7437 num_entries,
7438 switch_instr->GetBlock(),
7439 switch_instr->GetDefaultBlock());
7440}
7441
Mark Mendell805b3b52015-09-18 14:10:29 -04007442void LocationsBuilderX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7443 LocationSummary* locations =
7444 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
7445 locations->SetInAt(0, Location::RequiresRegister());
7446
7447 // Constant area pointer.
7448 locations->SetInAt(1, Location::RequiresRegister());
7449
7450 // And the temporary we need.
7451 locations->AddTemp(Location::RequiresRegister());
7452}
7453
7454void InstructionCodeGeneratorX86::VisitX86PackedSwitch(HX86PackedSwitch* switch_instr) {
7455 int32_t lower_bound = switch_instr->GetStartValue();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007456 uint32_t num_entries = switch_instr->GetNumEntries();
Mark Mendell805b3b52015-09-18 14:10:29 -04007457 LocationSummary* locations = switch_instr->GetLocations();
7458 Register value_reg = locations->InAt(0).AsRegister<Register>();
7459 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
7460
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007461 if (num_entries <= kPackedSwitchJumpTableThreshold) {
7462 GenPackedSwitchWithCompares(value_reg,
7463 lower_bound,
7464 num_entries,
7465 switch_instr->GetBlock(),
7466 default_block);
7467 return;
7468 }
7469
Mark Mendell805b3b52015-09-18 14:10:29 -04007470 // Optimizing has a jump area.
7471 Register temp_reg = locations->GetTemp(0).AsRegister<Register>();
7472 Register constant_area = locations->InAt(1).AsRegister<Register>();
7473
7474 // Remove the bias, if needed.
7475 if (lower_bound != 0) {
7476 __ leal(temp_reg, Address(value_reg, -lower_bound));
7477 value_reg = temp_reg;
7478 }
7479
7480 // Is the value in range?
Vladimir Markof3e0ee22015-12-17 15:23:13 +00007481 DCHECK_GE(num_entries, 1u);
Mark Mendell805b3b52015-09-18 14:10:29 -04007482 __ cmpl(value_reg, Immediate(num_entries - 1));
7483 __ j(kAbove, codegen_->GetLabelOf(default_block));
7484
7485 // We are in the range of the table.
7486 // Load (target-constant_area) from the jump table, indexing by the value.
7487 __ movl(temp_reg, codegen_->LiteralCaseTable(switch_instr, constant_area, value_reg));
7488
7489 // Compute the actual target address by adding in constant_area.
7490 __ addl(temp_reg, constant_area);
7491
7492 // And jump.
7493 __ jmp(temp_reg);
7494}
7495
Mark Mendell0616ae02015-04-17 12:49:27 -04007496void LocationsBuilderX86::VisitX86ComputeBaseMethodAddress(
7497 HX86ComputeBaseMethodAddress* insn) {
7498 LocationSummary* locations =
7499 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7500 locations->SetOut(Location::RequiresRegister());
7501}
7502
7503void InstructionCodeGeneratorX86::VisitX86ComputeBaseMethodAddress(
7504 HX86ComputeBaseMethodAddress* insn) {
7505 LocationSummary* locations = insn->GetLocations();
7506 Register reg = locations->Out().AsRegister<Register>();
7507
7508 // Generate call to next instruction.
7509 Label next_instruction;
7510 __ call(&next_instruction);
7511 __ Bind(&next_instruction);
7512
7513 // Remember this offset for later use with constant area.
7514 codegen_->SetMethodAddressOffset(GetAssembler()->CodeSize());
7515
7516 // Grab the return address off the stack.
7517 __ popl(reg);
7518}
7519
7520void LocationsBuilderX86::VisitX86LoadFromConstantTable(
7521 HX86LoadFromConstantTable* insn) {
7522 LocationSummary* locations =
7523 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
7524
7525 locations->SetInAt(0, Location::RequiresRegister());
7526 locations->SetInAt(1, Location::ConstantLocation(insn->GetConstant()));
7527
7528 // If we don't need to be materialized, we only need the inputs to be set.
David Brazdilb3e773e2016-01-26 11:28:37 +00007529 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007530 return;
7531 }
7532
7533 switch (insn->GetType()) {
7534 case Primitive::kPrimFloat:
7535 case Primitive::kPrimDouble:
7536 locations->SetOut(Location::RequiresFpuRegister());
7537 break;
7538
7539 case Primitive::kPrimInt:
7540 locations->SetOut(Location::RequiresRegister());
7541 break;
7542
7543 default:
7544 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7545 }
7546}
7547
7548void InstructionCodeGeneratorX86::VisitX86LoadFromConstantTable(HX86LoadFromConstantTable* insn) {
David Brazdilb3e773e2016-01-26 11:28:37 +00007549 if (insn->IsEmittedAtUseSite()) {
Mark Mendell0616ae02015-04-17 12:49:27 -04007550 return;
7551 }
7552
7553 LocationSummary* locations = insn->GetLocations();
7554 Location out = locations->Out();
7555 Register const_area = locations->InAt(0).AsRegister<Register>();
7556 HConstant *value = insn->GetConstant();
7557
7558 switch (insn->GetType()) {
7559 case Primitive::kPrimFloat:
7560 __ movss(out.AsFpuRegister<XmmRegister>(),
7561 codegen_->LiteralFloatAddress(value->AsFloatConstant()->GetValue(), const_area));
7562 break;
7563
7564 case Primitive::kPrimDouble:
7565 __ movsd(out.AsFpuRegister<XmmRegister>(),
7566 codegen_->LiteralDoubleAddress(value->AsDoubleConstant()->GetValue(), const_area));
7567 break;
7568
7569 case Primitive::kPrimInt:
7570 __ movl(out.AsRegister<Register>(),
7571 codegen_->LiteralInt32Address(value->AsIntConstant()->GetValue(), const_area));
7572 break;
7573
7574 default:
7575 LOG(FATAL) << "Unsupported x86 constant area type " << insn->GetType();
7576 }
7577}
7578
Mark Mendell0616ae02015-04-17 12:49:27 -04007579/**
7580 * Class to handle late fixup of offsets into constant area.
7581 */
Vladimir Marko5233f932015-09-29 19:01:15 +01007582class RIPFixup : public AssemblerFixup, public ArenaObject<kArenaAllocCodeGenerator> {
Mark Mendell0616ae02015-04-17 12:49:27 -04007583 public:
Mark Mendell805b3b52015-09-18 14:10:29 -04007584 RIPFixup(CodeGeneratorX86& codegen, size_t offset)
7585 : codegen_(&codegen), offset_into_constant_area_(offset) {}
7586
7587 protected:
7588 void SetOffset(size_t offset) { offset_into_constant_area_ = offset; }
7589
7590 CodeGeneratorX86* codegen_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007591
7592 private:
7593 void Process(const MemoryRegion& region, int pos) OVERRIDE {
7594 // Patch the correct offset for the instruction. The place to patch is the
7595 // last 4 bytes of the instruction.
7596 // The value to patch is the distance from the offset in the constant area
7597 // from the address computed by the HX86ComputeBaseMethodAddress instruction.
Mark Mendell805b3b52015-09-18 14:10:29 -04007598 int32_t constant_offset = codegen_->ConstantAreaStart() + offset_into_constant_area_;
Mathieu Chartier6beced42016-11-15 15:51:31 -08007599 int32_t relative_position = constant_offset - codegen_->GetMethodAddressOffset();
Mark Mendell0616ae02015-04-17 12:49:27 -04007600
7601 // Patch in the right value.
7602 region.StoreUnaligned<int32_t>(pos - 4, relative_position);
7603 }
7604
Mark Mendell0616ae02015-04-17 12:49:27 -04007605 // Location in constant area that the fixup refers to.
Mark Mendell805b3b52015-09-18 14:10:29 -04007606 int32_t offset_into_constant_area_;
Mark Mendell0616ae02015-04-17 12:49:27 -04007607};
7608
Mark Mendell805b3b52015-09-18 14:10:29 -04007609/**
7610 * Class to handle late fixup of offsets to a jump table that will be created in the
7611 * constant area.
7612 */
7613class JumpTableRIPFixup : public RIPFixup {
7614 public:
7615 JumpTableRIPFixup(CodeGeneratorX86& codegen, HX86PackedSwitch* switch_instr)
7616 : RIPFixup(codegen, static_cast<size_t>(-1)), switch_instr_(switch_instr) {}
7617
7618 void CreateJumpTable() {
7619 X86Assembler* assembler = codegen_->GetAssembler();
7620
7621 // Ensure that the reference to the jump table has the correct offset.
7622 const int32_t offset_in_constant_table = assembler->ConstantAreaSize();
7623 SetOffset(offset_in_constant_table);
7624
7625 // The label values in the jump table are computed relative to the
7626 // instruction addressing the constant area.
7627 const int32_t relative_offset = codegen_->GetMethodAddressOffset();
7628
7629 // Populate the jump table with the correct values for the jump table.
7630 int32_t num_entries = switch_instr_->GetNumEntries();
7631 HBasicBlock* block = switch_instr_->GetBlock();
7632 const ArenaVector<HBasicBlock*>& successors = block->GetSuccessors();
7633 // The value that we want is the target offset - the position of the table.
7634 for (int32_t i = 0; i < num_entries; i++) {
7635 HBasicBlock* b = successors[i];
7636 Label* l = codegen_->GetLabelOf(b);
7637 DCHECK(l->IsBound());
7638 int32_t offset_to_block = l->Position() - relative_offset;
7639 assembler->AppendInt32(offset_to_block);
7640 }
7641 }
7642
7643 private:
7644 const HX86PackedSwitch* switch_instr_;
7645};
7646
7647void CodeGeneratorX86::Finalize(CodeAllocator* allocator) {
7648 // Generate the constant area if needed.
7649 X86Assembler* assembler = GetAssembler();
7650 if (!assembler->IsConstantAreaEmpty() || !fixups_to_jump_tables_.empty()) {
7651 // Align to 4 byte boundary to reduce cache misses, as the data is 4 and 8
7652 // byte values.
7653 assembler->Align(4, 0);
7654 constant_area_start_ = assembler->CodeSize();
7655
7656 // Populate any jump tables.
7657 for (auto jump_table : fixups_to_jump_tables_) {
7658 jump_table->CreateJumpTable();
7659 }
7660
7661 // And now add the constant area to the generated code.
7662 assembler->AddConstantArea();
7663 }
7664
7665 // And finish up.
7666 CodeGenerator::Finalize(allocator);
7667}
7668
Mark Mendell0616ae02015-04-17 12:49:27 -04007669Address CodeGeneratorX86::LiteralDoubleAddress(double v, Register reg) {
7670 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddDouble(v));
7671 return Address(reg, kDummy32BitOffset, fixup);
7672}
7673
7674Address CodeGeneratorX86::LiteralFloatAddress(float v, Register reg) {
7675 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddFloat(v));
7676 return Address(reg, kDummy32BitOffset, fixup);
7677}
7678
7679Address CodeGeneratorX86::LiteralInt32Address(int32_t v, Register reg) {
7680 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt32(v));
7681 return Address(reg, kDummy32BitOffset, fixup);
7682}
7683
7684Address CodeGeneratorX86::LiteralInt64Address(int64_t v, Register reg) {
7685 AssemblerFixup* fixup = new (GetGraph()->GetArena()) RIPFixup(*this, __ AddInt64(v));
7686 return Address(reg, kDummy32BitOffset, fixup);
7687}
7688
Aart Bika19616e2016-02-01 18:57:58 -08007689void CodeGeneratorX86::Load32BitValue(Register dest, int32_t value) {
7690 if (value == 0) {
7691 __ xorl(dest, dest);
7692 } else {
7693 __ movl(dest, Immediate(value));
7694 }
7695}
7696
7697void CodeGeneratorX86::Compare32BitValue(Register dest, int32_t value) {
7698 if (value == 0) {
7699 __ testl(dest, dest);
7700 } else {
7701 __ cmpl(dest, Immediate(value));
7702 }
7703}
7704
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007705void CodeGeneratorX86::GenerateIntCompare(Location lhs, Location rhs) {
7706 Register lhs_reg = lhs.AsRegister<Register>();
jessicahandojo4877b792016-09-08 19:49:13 -07007707 GenerateIntCompare(lhs_reg, rhs);
7708}
7709
7710void CodeGeneratorX86::GenerateIntCompare(Register lhs, Location rhs) {
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007711 if (rhs.IsConstant()) {
7712 int32_t value = CodeGenerator::GetInt32ValueOf(rhs.GetConstant());
jessicahandojo4877b792016-09-08 19:49:13 -07007713 Compare32BitValue(lhs, value);
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007714 } else if (rhs.IsStackSlot()) {
jessicahandojo4877b792016-09-08 19:49:13 -07007715 __ cmpl(lhs, Address(ESP, rhs.GetStackIndex()));
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007716 } else {
jessicahandojo4877b792016-09-08 19:49:13 -07007717 __ cmpl(lhs, rhs.AsRegister<Register>());
Vladimir Marko56f4bdd2016-09-16 11:32:36 +01007718 }
7719}
7720
7721Address CodeGeneratorX86::ArrayAddress(Register obj,
7722 Location index,
7723 ScaleFactor scale,
7724 uint32_t data_offset) {
7725 return index.IsConstant() ?
7726 Address(obj, (index.GetConstant()->AsIntConstant()->GetValue() << scale) + data_offset) :
7727 Address(obj, index.AsRegister<Register>(), scale, data_offset);
7728}
7729
Mark Mendell805b3b52015-09-18 14:10:29 -04007730Address CodeGeneratorX86::LiteralCaseTable(HX86PackedSwitch* switch_instr,
7731 Register reg,
7732 Register value) {
7733 // Create a fixup to be used to create and address the jump table.
7734 JumpTableRIPFixup* table_fixup =
7735 new (GetGraph()->GetArena()) JumpTableRIPFixup(*this, switch_instr);
7736
7737 // We have to populate the jump tables.
7738 fixups_to_jump_tables_.push_back(table_fixup);
7739
7740 // We want a scaled address, as we are extracting the correct offset from the table.
7741 return Address(reg, value, TIMES_4, kDummy32BitOffset, table_fixup);
7742}
7743
Andreas Gampe85b62f22015-09-09 13:15:38 -07007744// TODO: target as memory.
7745void CodeGeneratorX86::MoveFromReturnRegister(Location target, Primitive::Type type) {
7746 if (!target.IsValid()) {
Roland Levillain7c1559a2015-12-15 10:55:36 +00007747 DCHECK_EQ(type, Primitive::kPrimVoid);
Andreas Gampe85b62f22015-09-09 13:15:38 -07007748 return;
7749 }
7750
7751 DCHECK_NE(type, Primitive::kPrimVoid);
7752
7753 Location return_loc = InvokeDexCallingConventionVisitorX86().GetReturnLocation(type);
7754 if (target.Equals(return_loc)) {
7755 return;
7756 }
7757
7758 // TODO: Consider pairs in the parallel move resolver, then this could be nicely merged
7759 // with the else branch.
7760 if (type == Primitive::kPrimLong) {
7761 HParallelMove parallel_move(GetGraph()->GetArena());
7762 parallel_move.AddMove(return_loc.ToLow(), target.ToLow(), Primitive::kPrimInt, nullptr);
7763 parallel_move.AddMove(return_loc.ToHigh(), target.ToHigh(), Primitive::kPrimInt, nullptr);
7764 GetMoveResolver()->EmitNativeCode(&parallel_move);
7765 } else {
7766 // Let the parallel move resolver take care of all of this.
7767 HParallelMove parallel_move(GetGraph()->GetArena());
7768 parallel_move.AddMove(return_loc, target, type, nullptr);
7769 GetMoveResolver()->EmitNativeCode(&parallel_move);
7770 }
7771}
7772
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007773void CodeGeneratorX86::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
7774 for (const PatchInfo<Label>& info : jit_string_patches_) {
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007775 const auto& it = jit_string_roots_.find(StringReference(&info.dex_file,
7776 dex::StringIndex(info.index)));
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007777 DCHECK(it != jit_string_roots_.end());
7778 size_t index_in_table = it->second;
7779 uint32_t code_offset = info.label.Position() - kLabelPositionToLiteralOffsetAdjustment;
7780 uintptr_t address =
7781 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
7782 typedef __attribute__((__aligned__(1))) uint32_t unaligned_uint32_t;
7783 reinterpret_cast<unaligned_uint32_t*>(code + code_offset)[0] =
7784 dchecked_integral_cast<uint32_t>(address);
7785 }
7786}
7787
Roland Levillain4d027112015-07-01 15:41:14 +01007788#undef __
7789
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00007790} // namespace x86
7791} // namespace art